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.

220 lines
6.7 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() {
  154. var toPrecision = '' + Number(this.current).toPrecision(5);
  155. for (var i = toPrecision.length-1; i > 0; i--) {
  156. if (toPrecision[i] == "0") {
  157. toPrecision = toPrecision.slice(0,i);
  158. }
  159. else if (toPrecision[i] == "." || toPrecision[i] == ",") {
  160. toPrecision = toPrecision.slice(0,i);
  161. break;
  162. }
  163. else{
  164. break;
  165. }
  166. }
  167. return toPrecision;
  168. };
  169. /**
  170. * Snap a date to a rounded value.
  171. * The snap intervals are dependent on the current scale and step.
  172. * @param {Date} date the date to be snapped.
  173. * @return {Date} snappedDate
  174. */
  175. DataStep.prototype.snap = function(date) {
  176. };
  177. /**
  178. * Check if the current value is a major value (for example when the step
  179. * is DAY, a major value is each first day of the MONTH)
  180. * @return {boolean} true if current date is major, else false.
  181. */
  182. DataStep.prototype.isMajor = function() {
  183. return (this.current % (this.scale * this.majorSteps[this.stepIndex]) == 0);
  184. };
  185. module.exports = DataStep;