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.

454 lines
19 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /**
  2. * @constructor TimeStep
  3. * The class TimeStep is an iterator for dates. You provide a start date and an
  4. * end date. 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 TimeStep 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. TimeStep = function(start, end, minimumStep) {
  28. // variables
  29. this.current = new Date();
  30. this._start = new Date();
  31. this._end = new Date();
  32. this.autoScale = true;
  33. this.scale = TimeStep.SCALE.DAY;
  34. this.step = 1;
  35. // initialize the range
  36. this.setRange(start, end, minimumStep);
  37. };
  38. /// enum scale
  39. TimeStep.SCALE = {
  40. MILLISECOND: 1,
  41. SECOND: 2,
  42. MINUTE: 3,
  43. HOUR: 4,
  44. DAY: 5,
  45. WEEKDAY: 6,
  46. MONTH: 7,
  47. YEAR: 8
  48. };
  49. /**
  50. * Set a new range
  51. * If minimumStep is provided, the step size is chosen as close as possible
  52. * to the minimumStep but larger than minimumStep. If minimumStep is not
  53. * provided, the scale is set to 1 DAY.
  54. * The minimumStep should correspond with the onscreen size of about 6 characters
  55. * @param {Date} [start] The start date and time.
  56. * @param {Date} [end] The end date and time.
  57. * @param {int} [minimumStep] Optional. Minimum step size in milliseconds
  58. */
  59. TimeStep.prototype.setRange = function(start, end, minimumStep) {
  60. if (!(start instanceof Date) || !(end instanceof Date)) {
  61. throw "No legal start or end date in method setRange";
  62. }
  63. this._start = (start != undefined) ? new Date(start.valueOf()) : new Date();
  64. this._end = (end != undefined) ? new Date(end.valueOf()) : new Date();
  65. if (this.autoScale) {
  66. this.setMinimumStep(minimumStep);
  67. }
  68. };
  69. /**
  70. * Set the range iterator to the start date.
  71. */
  72. TimeStep.prototype.first = function() {
  73. this.current = new Date(this._start.valueOf());
  74. this.roundToMinor();
  75. };
  76. /**
  77. * Round the current date to the first minor date value
  78. * This must be executed once when the current date is set to start Date
  79. */
  80. TimeStep.prototype.roundToMinor = function() {
  81. // round to floor
  82. // IMPORTANT: we have no breaks in this switch! (this is no bug)
  83. //noinspection FallthroughInSwitchStatementJS
  84. switch (this.scale) {
  85. case TimeStep.SCALE.YEAR:
  86. this.current.setFullYear(this.step * Math.floor(this.current.getFullYear() / this.step));
  87. this.current.setMonth(0);
  88. case TimeStep.SCALE.MONTH: this.current.setDate(1);
  89. case TimeStep.SCALE.DAY: // intentional fall through
  90. case TimeStep.SCALE.WEEKDAY: this.current.setHours(0);
  91. case TimeStep.SCALE.HOUR: this.current.setMinutes(0);
  92. case TimeStep.SCALE.MINUTE: this.current.setSeconds(0);
  93. case TimeStep.SCALE.SECOND: this.current.setMilliseconds(0);
  94. //case TimeStep.SCALE.MILLISECOND: // nothing to do for milliseconds
  95. }
  96. if (this.step != 1) {
  97. // round down to the first minor value that is a multiple of the current step size
  98. switch (this.scale) {
  99. case TimeStep.SCALE.MILLISECOND: this.current.setMilliseconds(this.current.getMilliseconds() - this.current.getMilliseconds() % this.step); break;
  100. case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() - this.current.getSeconds() % this.step); break;
  101. case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() - this.current.getMinutes() % this.step); break;
  102. case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() - this.current.getHours() % this.step); break;
  103. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  104. case TimeStep.SCALE.DAY: this.current.setDate((this.current.getDate()-1) - (this.current.getDate()-1) % this.step + 1); break;
  105. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() - this.current.getMonth() % this.step); break;
  106. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() - this.current.getFullYear() % this.step); break;
  107. default: break;
  108. }
  109. }
  110. };
  111. /**
  112. * Check if the there is a next step
  113. * @return {boolean} true if the current date has not passed the end date
  114. */
  115. TimeStep.prototype.hasNext = function () {
  116. return (this.current.valueOf() <= this._end.valueOf());
  117. };
  118. /**
  119. * Do the next step
  120. */
  121. TimeStep.prototype.next = function() {
  122. var prev = this.current.valueOf();
  123. // Two cases, needed to prevent issues with switching daylight savings
  124. // (end of March and end of October)
  125. if (this.current.getMonth() < 6) {
  126. switch (this.scale) {
  127. case TimeStep.SCALE.MILLISECOND:
  128. this.current = new Date(this.current.valueOf() + this.step); break;
  129. case TimeStep.SCALE.SECOND: this.current = new Date(this.current.valueOf() + this.step * 1000); break;
  130. case TimeStep.SCALE.MINUTE: this.current = new Date(this.current.valueOf() + this.step * 1000 * 60); break;
  131. case TimeStep.SCALE.HOUR:
  132. this.current = new Date(this.current.valueOf() + this.step * 1000 * 60 * 60);
  133. // in case of skipping an hour for daylight savings, adjust the hour again (else you get: 0h 5h 9h ... instead of 0h 4h 8h ...)
  134. var h = this.current.getHours();
  135. this.current.setHours(h - (h % this.step));
  136. break;
  137. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  138. case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break;
  139. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break;
  140. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break;
  141. default: break;
  142. }
  143. }
  144. else {
  145. switch (this.scale) {
  146. case TimeStep.SCALE.MILLISECOND: this.current = new Date(this.current.valueOf() + this.step); break;
  147. case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() + this.step); break;
  148. case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() + this.step); break;
  149. case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() + this.step); break;
  150. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  151. case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break;
  152. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break;
  153. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break;
  154. default: break;
  155. }
  156. }
  157. if (this.step != 1) {
  158. // round down to the correct major value
  159. switch (this.scale) {
  160. case TimeStep.SCALE.MILLISECOND: if(this.current.getMilliseconds() < this.step) this.current.setMilliseconds(0); break;
  161. case TimeStep.SCALE.SECOND: if(this.current.getSeconds() < this.step) this.current.setSeconds(0); break;
  162. case TimeStep.SCALE.MINUTE: if(this.current.getMinutes() < this.step) this.current.setMinutes(0); break;
  163. case TimeStep.SCALE.HOUR: if(this.current.getHours() < this.step) this.current.setHours(0); break;
  164. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  165. case TimeStep.SCALE.DAY: if(this.current.getDate() < this.step+1) this.current.setDate(1); break;
  166. case TimeStep.SCALE.MONTH: if(this.current.getMonth() < this.step) this.current.setMonth(0); break;
  167. case TimeStep.SCALE.YEAR: break; // nothing to do for year
  168. default: break;
  169. }
  170. }
  171. // safety mechanism: if current time is still unchanged, move to the end
  172. if (this.current.valueOf() == prev) {
  173. this.current = new Date(this._end.valueOf());
  174. }
  175. };
  176. /**
  177. * Get the current datetime
  178. * @return {Date} current The current date
  179. */
  180. TimeStep.prototype.getCurrent = function() {
  181. return this.current;
  182. };
  183. /**
  184. * Set a custom scale. Autoscaling will be disabled.
  185. * For example setScale(SCALE.MINUTES, 5) will result
  186. * in minor steps of 5 minutes, and major steps of an hour.
  187. *
  188. * @param {TimeStep.SCALE} newScale
  189. * A scale. Choose from SCALE.MILLISECOND,
  190. * SCALE.SECOND, SCALE.MINUTE, SCALE.HOUR,
  191. * SCALE.WEEKDAY, SCALE.DAY, SCALE.MONTH,
  192. * SCALE.YEAR.
  193. * @param {Number} newStep A step size, by default 1. Choose for
  194. * example 1, 2, 5, or 10.
  195. */
  196. TimeStep.prototype.setScale = function(newScale, newStep) {
  197. this.scale = newScale;
  198. if (newStep > 0) {
  199. this.step = newStep;
  200. }
  201. this.autoScale = false;
  202. };
  203. /**
  204. * Enable or disable autoscaling
  205. * @param {boolean} enable If true, autoascaling is set true
  206. */
  207. TimeStep.prototype.setAutoScale = function (enable) {
  208. this.autoScale = enable;
  209. };
  210. /**
  211. * Automatically determine the scale that bests fits the provided minimum step
  212. * @param {Number} [minimumStep] The minimum step size in milliseconds
  213. */
  214. TimeStep.prototype.setMinimumStep = function(minimumStep) {
  215. if (minimumStep == undefined) {
  216. return;
  217. }
  218. var stepYear = (1000 * 60 * 60 * 24 * 30 * 12);
  219. var stepMonth = (1000 * 60 * 60 * 24 * 30);
  220. var stepDay = (1000 * 60 * 60 * 24);
  221. var stepHour = (1000 * 60 * 60);
  222. var stepMinute = (1000 * 60);
  223. var stepSecond = (1000);
  224. var stepMillisecond= (1);
  225. // find the smallest step that is larger than the provided minimumStep
  226. if (stepYear*1000 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1000;}
  227. if (stepYear*500 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 500;}
  228. if (stepYear*100 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 100;}
  229. if (stepYear*50 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 50;}
  230. if (stepYear*10 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 10;}
  231. if (stepYear*5 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 5;}
  232. if (stepYear > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1;}
  233. if (stepMonth*3 > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 3;}
  234. if (stepMonth > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 1;}
  235. if (stepDay*5 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 5;}
  236. if (stepDay*2 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 2;}
  237. if (stepDay > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 1;}
  238. if (stepDay/2 > minimumStep) {this.scale = TimeStep.SCALE.WEEKDAY; this.step = 1;}
  239. if (stepHour*4 > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 4;}
  240. if (stepHour > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 1;}
  241. if (stepMinute*15 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 15;}
  242. if (stepMinute*10 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 10;}
  243. if (stepMinute*5 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 5;}
  244. if (stepMinute > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 1;}
  245. if (stepSecond*15 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 15;}
  246. if (stepSecond*10 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 10;}
  247. if (stepSecond*5 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 5;}
  248. if (stepSecond > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 1;}
  249. if (stepMillisecond*200 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 200;}
  250. if (stepMillisecond*100 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 100;}
  251. if (stepMillisecond*50 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 50;}
  252. if (stepMillisecond*10 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 10;}
  253. if (stepMillisecond*5 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 5;}
  254. if (stepMillisecond > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 1;}
  255. };
  256. /**
  257. * Snap a date to a rounded value.
  258. * The snap intervals are dependent on the current scale and step.
  259. * @param {Date} date the date to be snapped.
  260. * @return {Date} snappedDate
  261. */
  262. TimeStep.prototype.snap = function(date) {
  263. var clone = new Date(date.valueOf());
  264. if (this.scale == TimeStep.SCALE.YEAR) {
  265. var year = clone.getFullYear() + Math.round(clone.getMonth() / 12);
  266. clone.setFullYear(Math.round(year / this.step) * this.step);
  267. clone.setMonth(0);
  268. clone.setDate(0);
  269. clone.setHours(0);
  270. clone.setMinutes(0);
  271. clone.setSeconds(0);
  272. clone.setMilliseconds(0);
  273. }
  274. else if (this.scale == TimeStep.SCALE.MONTH) {
  275. if (clone.getDate() > 15) {
  276. clone.setDate(1);
  277. clone.setMonth(clone.getMonth() + 1);
  278. // important: first set Date to 1, after that change the month.
  279. }
  280. else {
  281. clone.setDate(1);
  282. }
  283. clone.setHours(0);
  284. clone.setMinutes(0);
  285. clone.setSeconds(0);
  286. clone.setMilliseconds(0);
  287. }
  288. else if (this.scale == TimeStep.SCALE.DAY ||
  289. this.scale == TimeStep.SCALE.WEEKDAY) {
  290. //noinspection FallthroughInSwitchStatementJS
  291. switch (this.step) {
  292. case 5:
  293. case 2:
  294. clone.setHours(Math.round(clone.getHours() / 24) * 24); break;
  295. default:
  296. clone.setHours(Math.round(clone.getHours() / 12) * 12); break;
  297. }
  298. clone.setMinutes(0);
  299. clone.setSeconds(0);
  300. clone.setMilliseconds(0);
  301. }
  302. else if (this.scale == TimeStep.SCALE.HOUR) {
  303. switch (this.step) {
  304. case 4:
  305. clone.setMinutes(Math.round(clone.getMinutes() / 60) * 60); break;
  306. default:
  307. clone.setMinutes(Math.round(clone.getMinutes() / 30) * 30); break;
  308. }
  309. clone.setSeconds(0);
  310. clone.setMilliseconds(0);
  311. } else if (this.scale == TimeStep.SCALE.MINUTE) {
  312. //noinspection FallthroughInSwitchStatementJS
  313. switch (this.step) {
  314. case 15:
  315. case 10:
  316. clone.setMinutes(Math.round(clone.getMinutes() / 5) * 5);
  317. clone.setSeconds(0);
  318. break;
  319. case 5:
  320. clone.setSeconds(Math.round(clone.getSeconds() / 60) * 60); break;
  321. default:
  322. clone.setSeconds(Math.round(clone.getSeconds() / 30) * 30); break;
  323. }
  324. clone.setMilliseconds(0);
  325. }
  326. else if (this.scale == TimeStep.SCALE.SECOND) {
  327. //noinspection FallthroughInSwitchStatementJS
  328. switch (this.step) {
  329. case 15:
  330. case 10:
  331. clone.setSeconds(Math.round(clone.getSeconds() / 5) * 5);
  332. clone.setMilliseconds(0);
  333. break;
  334. case 5:
  335. clone.setMilliseconds(Math.round(clone.getMilliseconds() / 1000) * 1000); break;
  336. default:
  337. clone.setMilliseconds(Math.round(clone.getMilliseconds() / 500) * 500); break;
  338. }
  339. }
  340. else if (this.scale == TimeStep.SCALE.MILLISECOND) {
  341. var step = this.step > 5 ? this.step / 2 : 1;
  342. clone.setMilliseconds(Math.round(clone.getMilliseconds() / step) * step);
  343. }
  344. return clone;
  345. };
  346. /**
  347. * Check if the current value is a major value (for example when the step
  348. * is DAY, a major value is each first day of the MONTH)
  349. * @return {boolean} true if current date is major, else false.
  350. */
  351. TimeStep.prototype.isMajor = function() {
  352. switch (this.scale) {
  353. case TimeStep.SCALE.MILLISECOND:
  354. return (this.current.getMilliseconds() == 0);
  355. case TimeStep.SCALE.SECOND:
  356. return (this.current.getSeconds() == 0);
  357. case TimeStep.SCALE.MINUTE:
  358. return (this.current.getHours() == 0) && (this.current.getMinutes() == 0);
  359. // Note: this is no bug. Major label is equal for both minute and hour scale
  360. case TimeStep.SCALE.HOUR:
  361. return (this.current.getHours() == 0);
  362. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  363. case TimeStep.SCALE.DAY:
  364. return (this.current.getDate() == 1);
  365. case TimeStep.SCALE.MONTH:
  366. return (this.current.getMonth() == 0);
  367. case TimeStep.SCALE.YEAR:
  368. return false;
  369. default:
  370. return false;
  371. }
  372. };
  373. /**
  374. * Returns formatted text for the minor axislabel, depending on the current
  375. * date and the scale. For example when scale is MINUTE, the current time is
  376. * formatted as "hh:mm".
  377. * @param {Date} [date] custom date. if not provided, current date is taken
  378. */
  379. TimeStep.prototype.getLabelMinor = function(date) {
  380. if (date == undefined) {
  381. date = this.current;
  382. }
  383. switch (this.scale) {
  384. case TimeStep.SCALE.MILLISECOND: return moment(date).format('SSS');
  385. case TimeStep.SCALE.SECOND: return moment(date).format('s');
  386. case TimeStep.SCALE.MINUTE: return moment(date).format('HH:mm');
  387. case TimeStep.SCALE.HOUR: return moment(date).format('HH:mm');
  388. case TimeStep.SCALE.WEEKDAY: return moment(date).format('ddd D');
  389. case TimeStep.SCALE.DAY: return moment(date).format('D');
  390. case TimeStep.SCALE.MONTH: return moment(date).format('MMM');
  391. case TimeStep.SCALE.YEAR: return moment(date).format('YYYY');
  392. default: return '';
  393. }
  394. };
  395. /**
  396. * Returns formatted text for the major axis label, depending on the current
  397. * date and the scale. For example when scale is MINUTE, the major scale is
  398. * hours, and the hour will be formatted as "hh".
  399. * @param {Date} [date] custom date. if not provided, current date is taken
  400. */
  401. TimeStep.prototype.getLabelMajor = function(date) {
  402. if (date == undefined) {
  403. date = this.current;
  404. }
  405. //noinspection FallthroughInSwitchStatementJS
  406. switch (this.scale) {
  407. case TimeStep.SCALE.MILLISECOND:return moment(date).format('HH:mm:ss');
  408. case TimeStep.SCALE.SECOND: return moment(date).format('D MMMM HH:mm');
  409. case TimeStep.SCALE.MINUTE:
  410. case TimeStep.SCALE.HOUR: return moment(date).format('ddd D MMMM');
  411. case TimeStep.SCALE.WEEKDAY:
  412. case TimeStep.SCALE.DAY: return moment(date).format('MMMM YYYY');
  413. case TimeStep.SCALE.MONTH: return moment(date).format('YYYY');
  414. case TimeStep.SCALE.YEAR: return '';
  415. default: return '';
  416. }
  417. };