vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /**
  2. * @constructor DataStep
  3. * The class DataStep is an iterator for data for the lineGraph. You provide a start data point and an
  4. * end data point. The class itself determines the best scale (step size) based on the
  5. * provided start Date, end Date, and minimumStep.
  6. *
  7. * If minimumStep is provided, the step size is chosen as close as possible
  8. * to the minimumStep but larger than minimumStep. If minimumStep is not
  9. * provided, the scale is set to 1 DAY.
  10. * The minimumStep should correspond with the onscreen size of about 6 characters
  11. *
  12. * Alternatively, you can set a scale by hand.
  13. * After creation, you can initialize the class by executing first(). Then you
  14. * can iterate from the start date to the end date via next(). You can check if
  15. * the end date is reached with the function hasNext(). After each step, you can
  16. * retrieve the current date via getCurrent().
  17. * The DataStep has scales ranging from milliseconds, seconds, minutes, hours,
  18. * days, to years.
  19. *
  20. * Version: 1.2
  21. *
  22. * @param {Date} [start] The start date, for example new Date(2010, 9, 21)
  23. * or new Date(2010, 9, 21, 23, 45, 00)
  24. * @param {Date} [end] The end date
  25. * @param {Number} [minimumStep] Optional. Minimum step size in milliseconds
  26. */
  27. function DataStep(start, end, minimumStep, containerHeight, customRange) {
  28. // variables
  29. this.current = 0;
  30. this.autoScale = true;
  31. this.stepIndex = 0;
  32. this.step = 1;
  33. this.scale = 1;
  34. this.marginStart;
  35. this.marginEnd;
  36. this.deadSpace = 0;
  37. this.majorSteps = [1, 2, 5, 10];
  38. this.minorSteps = [0.25, 0.5, 1, 2];
  39. this.setRange(start, end, minimumStep, containerHeight, customRange);
  40. }
  41. /**
  42. * Set a new range
  43. * If minimumStep is provided, the step size is chosen as close as possible
  44. * to the minimumStep but larger than minimumStep. If minimumStep is not
  45. * provided, the scale is set to 1 DAY.
  46. * The minimumStep should correspond with the onscreen size of about 6 characters
  47. * @param {Number} [start] The start date and time.
  48. * @param {Number} [end] The end date and time.
  49. * @param {Number} [minimumStep] Optional. Minimum step size in milliseconds
  50. */
  51. DataStep.prototype.setRange = function(start, end, minimumStep, containerHeight, customRange) {
  52. this._start = customRange.min === undefined ? start : customRange.min;
  53. this._end = customRange.max === undefined ? end : customRange.max;
  54. if (this._start == this._end) {
  55. this._start -= 0.75;
  56. this._end += 1;
  57. }
  58. if (this.autoScale) {
  59. this.setMinimumStep(minimumStep, containerHeight);
  60. }
  61. this.setFirst(customRange);
  62. };
  63. /**
  64. * Automatically determine the scale that bests fits the provided minimum step
  65. * @param {Number} [minimumStep] The minimum step size in milliseconds
  66. */
  67. DataStep.prototype.setMinimumStep = function(minimumStep, containerHeight) {
  68. // round to floor
  69. var size = this._end - this._start;
  70. var safeSize = size * 1.2;
  71. var minimumStepValue = minimumStep * (safeSize / containerHeight);
  72. var orderOfMagnitude = Math.round(Math.log(safeSize)/Math.LN10);
  73. var minorStepIdx = -1;
  74. var magnitudefactor = Math.pow(10,orderOfMagnitude);
  75. var start = 0;
  76. if (orderOfMagnitude < 0) {
  77. start = orderOfMagnitude;
  78. }
  79. var solutionFound = false;
  80. for (var i = start; Math.abs(i) <= Math.abs(orderOfMagnitude); i++) {
  81. magnitudefactor = Math.pow(10,i);
  82. for (var j = 0; j < this.minorSteps.length; j++) {
  83. var stepSize = magnitudefactor * this.minorSteps[j];
  84. if (stepSize >= minimumStepValue) {
  85. solutionFound = true;
  86. minorStepIdx = j;
  87. break;
  88. }
  89. }
  90. if (solutionFound == true) {
  91. break;
  92. }
  93. }
  94. this.stepIndex = minorStepIdx;
  95. this.scale = magnitudefactor;
  96. this.step = magnitudefactor * this.minorSteps[minorStepIdx];
  97. };
  98. /**
  99. * Round the current date to the first minor date value
  100. * This must be executed once when the current date is set to start Date
  101. */
  102. DataStep.prototype.setFirst = function(customRange) {
  103. if (customRange === undefined) {
  104. customRange = {};
  105. }
  106. var niceStart = customRange.min === undefined ? this._start - (this.scale * 2 * this.minorSteps[this.stepIndex]) : customRange.min;
  107. var niceEnd = customRange.max === undefined ? this._end + (this.scale * this.minorSteps[this.stepIndex]) : customRange.max;
  108. this.marginEnd = customRange.max === undefined ? this.roundToMinor(niceEnd) : customRange.max;
  109. this.marginStart = customRange.min === undefined ? this.roundToMinor(niceStart) : customRange.min;
  110. this.deadSpace = this.roundToMinor(niceEnd) - niceEnd + this.roundToMinor(niceStart) - niceStart;
  111. this.marginRange = this.marginEnd - this.marginStart;
  112. this.current = this.marginEnd;
  113. };
  114. DataStep.prototype.roundToMinor = function(value) {
  115. var rounded = value - (value % (this.scale * this.minorSteps[this.stepIndex]));
  116. if (value % (this.scale * this.minorSteps[this.stepIndex]) > 0.5 * (this.scale * this.minorSteps[this.stepIndex])) {
  117. return rounded + (this.scale * this.minorSteps[this.stepIndex]);
  118. }
  119. else {
  120. return rounded;
  121. }
  122. }
  123. /**
  124. * Check if the there is a next step
  125. * @return {boolean} true if the current date has not passed the end date
  126. */
  127. DataStep.prototype.hasNext = function () {
  128. return (this.current >= this.marginStart);
  129. };
  130. /**
  131. * Do the next step
  132. */
  133. DataStep.prototype.next = function() {
  134. var prev = this.current;
  135. this.current -= this.step;
  136. // safety mechanism: if current time is still unchanged, move to the end
  137. if (this.current == prev) {
  138. this.current = this._end;
  139. }
  140. };
  141. /**
  142. * Do the next step
  143. */
  144. DataStep.prototype.previous = function() {
  145. this.current += this.step;
  146. this.marginEnd += this.step;
  147. this.marginRange = this.marginEnd - this.marginStart;
  148. };
  149. /**
  150. * Get the current datetime
  151. * @return {String} current The current date
  152. */
  153. DataStep.prototype.getCurrent = function(decimals) {
  154. var toPrecision = '' + Number(this.current).toPrecision(5);
  155. // If decimals is specified, then limit or extend the string as required
  156. if(decimals !== undefined && !isNaN(Number(decimals))) {
  157. // If string includes exponent, then we need to add it to the end
  158. var exp = "";
  159. var index = toPrecision.indexOf("e");
  160. if(index != -1) {
  161. // Get the exponent
  162. exp = toPrecision.slice(index);
  163. // Remove the exponent in case we need to zero-extend
  164. toPrecision = toPrecision.slice(0, index);
  165. }
  166. index = Math.max(toPrecision.indexOf(","), toPrecision.indexOf("."));
  167. if(index === -1) {
  168. // No decimal found - if we want decimals, then we need to add it
  169. if(decimals !== 0) {
  170. toPrecision += '.';
  171. }
  172. // Calculate how long the string should be
  173. index = toPrecision.length + decimals;
  174. }
  175. else if(decimals !== 0) {
  176. // Calculate how long the string should be - accounting for the decimal place
  177. index += decimals + 1;
  178. }
  179. if(index > toPrecision.length) {
  180. // We need to add zeros!
  181. for(var cnt = index - toPrecision.length; cnt > 0; cnt--) {
  182. toPrecision += '0';
  183. }
  184. }
  185. else {
  186. // we need to remove characters
  187. toPrecision = toPrecision.slice(0, index);
  188. }
  189. // Add the exponent if there is one
  190. toPrecision += exp;
  191. }
  192. else {
  193. if (toPrecision.indexOf(",") != -1 || toPrecision.indexOf(".") != -1) {
  194. // If no decimal is specified, and there are decimal places, remove trailing zeros
  195. for (var i = toPrecision.length - 1; i > 0; i--) {
  196. if (toPrecision[i] == "0") {
  197. toPrecision = toPrecision.slice(0, i);
  198. }
  199. else if (toPrecision[i] == "." || toPrecision[i] == ",") {
  200. toPrecision = toPrecision.slice(0, i);
  201. break;
  202. }
  203. else {
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. return toPrecision;
  210. };
  211. /**
  212. * Snap a date to a rounded value.
  213. * The snap intervals are dependent on the current scale and step.
  214. * @param {Date} date the date to be snapped.
  215. * @return {Date} snappedDate
  216. */
  217. DataStep.prototype.snap = function(date) {
  218. };
  219. /**
  220. * Check if the current value is a major value (for example when the step
  221. * is DAY, a major value is each first day of the MONTH)
  222. * @return {boolean} true if current date is major, else false.
  223. */
  224. DataStep.prototype.isMajor = function() {
  225. return (this.current % (this.scale * this.majorSteps[this.stepIndex]) == 0);
  226. };
  227. module.exports = DataStep;