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.

219 lines
7.0 KiB

8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 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, formattingFunction, alignZeros) {
  28. // variables
  29. this.current = -1;
  30. this.stepIndex = 0;
  31. this.step = 1;
  32. this.scale = 1;
  33. this.formattingFunction = formattingFunction;
  34. this.marginStart;
  35. this.marginEnd;
  36. this.majorSteps = [1, 2, 5, 10];
  37. this.minorSteps = [0.25, 0.5, 1, 2];
  38. this.alignZeros = alignZeros;
  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. if (customRange === undefined) {
  53. customRange = {};
  54. }
  55. this._start = customRange.min === undefined ? start : customRange.min;
  56. this._end = customRange.max === undefined ? end : customRange.max;
  57. if (this._start === this._end) {
  58. this._start = customRange.min === undefined ? this._start - 0.75 : this._start;
  59. this._end = customRange.max === undefined ? this._end + 1 : this._end;
  60. }
  61. this.setMinimumStep(minimumStep, containerHeight);
  62. this.setFirst(customRange);
  63. };
  64. /**
  65. * Automatically determine the scale that bests fits the provided minimum step
  66. * @param {Number} [minimumStep] The minimum step size in pixels
  67. */
  68. DataStep.prototype.setMinimumStep = function(minimumStep, containerHeight) {
  69. // round to floor
  70. var range = this._end - this._start;
  71. var safeRange = range * 1.2;
  72. var minimumStepValue = minimumStep * (safeRange / containerHeight);
  73. var orderOfMagnitude = Math.round(Math.log(safeRange)/Math.LN10);
  74. var minorStepIdx = -1;
  75. var magnitudefactor = Math.pow(10,orderOfMagnitude);
  76. var start = 0;
  77. if (orderOfMagnitude < 0) {
  78. start = orderOfMagnitude;
  79. }
  80. var solutionFound = false;
  81. for (var i = start; Math.abs(i) <= Math.abs(orderOfMagnitude); i++) {
  82. magnitudefactor = Math.pow(10,i);
  83. for (var j = 0; j < this.minorSteps.length; j++) {
  84. var stepSize = magnitudefactor * this.minorSteps[j];
  85. if (stepSize >= minimumStepValue) {
  86. solutionFound = true;
  87. minorStepIdx = j;
  88. break;
  89. }
  90. }
  91. if (solutionFound === true) {
  92. break;
  93. }
  94. }
  95. this.stepIndex = minorStepIdx;
  96. this.scale = magnitudefactor;
  97. this.step = magnitudefactor * this.minorSteps[minorStepIdx];
  98. };
  99. /**
  100. * Round the current date to the first minor date value
  101. * This must be executed once when the current date is set to start Date
  102. */
  103. DataStep.prototype.setFirst = function(customRange) {
  104. if (customRange === undefined) {
  105. customRange = {};
  106. }
  107. var niceStart = customRange.min === undefined ? this._start - (this.scale * 2 * this.minorSteps[this.stepIndex]) : customRange.min;
  108. var niceEnd = customRange.max === undefined ? this._end + (this.scale * this.minorSteps[this.stepIndex]) : customRange.max;
  109. this.marginEnd = customRange.max === undefined ? this.roundToMinor(niceEnd) : customRange.max;
  110. this.marginStart = customRange.min === undefined ? this.roundToMinor(niceStart) : customRange.min;
  111. // if we need to align the zero's we need to make sure that there is a zero to use.
  112. if (this.alignZeros === true && (this.marginEnd - this.marginStart) % this.step != 0) {
  113. this.marginEnd += this.marginEnd % this.step;
  114. }
  115. this.marginRange = this.marginEnd - this.marginStart;
  116. this.current = this.marginEnd;
  117. };
  118. DataStep.prototype.roundToMinor = function(value) {
  119. var rounded = value - (value % (this.scale * this.minorSteps[this.stepIndex]));
  120. if (value % (this.scale * this.minorSteps[this.stepIndex]) > 0.5 * (this.scale * this.minorSteps[this.stepIndex])) {
  121. return rounded + (this.scale * this.minorSteps[this.stepIndex]);
  122. }
  123. else {
  124. return rounded;
  125. }
  126. }
  127. /**
  128. * Check if the there is a next step
  129. * @return {boolean} true if the current date has not passed the end date
  130. */
  131. DataStep.prototype.hasNext = function () {
  132. return (this.current >= this.marginStart);
  133. };
  134. /**
  135. * Do the next step
  136. */
  137. DataStep.prototype.next = function() {
  138. var prev = this.current;
  139. this.current -= this.step;
  140. // safety mechanism: if current time is still unchanged, move to the end
  141. if (this.current === prev) {
  142. this.current = this._end;
  143. }
  144. };
  145. /**
  146. * Do the next step
  147. */
  148. DataStep.prototype.previous = function() {
  149. this.current += this.step;
  150. this.marginEnd += this.step;
  151. this.marginRange = this.marginEnd - this.marginStart;
  152. };
  153. /**
  154. * Get the current datetime
  155. * @return {String} current The current date
  156. */
  157. DataStep.prototype.getCurrent = function() {
  158. // prevent round-off errors when close to zero
  159. var current = (Math.abs(this.current) < this.step / 2) ? 0 : this.current;
  160. var returnValue = current;
  161. if (typeof this.formattingFunction === 'function') {
  162. return this.formattingFunction(current);
  163. }
  164. return '' + returnValue.toPrecision(3);
  165. };
  166. /**
  167. * Check if the current value is a major value (for example when the step
  168. * is DAY, a major value is each first day of the MONTH)
  169. * @return {boolean} true if current date is major, else false.
  170. */
  171. DataStep.prototype.isMajor = function() {
  172. return (this.current % (this.scale * this.majorSteps[this.stepIndex]) === 0);
  173. };
  174. DataStep.prototype.shift = function(steps) {
  175. if (steps < 0) {
  176. for (let i = 0; i < -steps; i++) {
  177. this.previous();
  178. }
  179. }
  180. else if (steps > 0) {
  181. for (let i = 0; i < steps; i++) {
  182. this.next();
  183. }
  184. }
  185. }
  186. module.exports = DataStep;