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.

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