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.

223 lines
7.1 KiB

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