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.

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