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.

621 lines
21 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
11 years ago
11 years ago
  1. var moment = require('../module/moment');
  2. var DateUtil = require('./DateUtil');
  3. var util = require('../util');
  4. /**
  5. * @constructor TimeStep
  6. * The class TimeStep is an iterator for dates. You provide a start date and an
  7. * end date. The class itself determines the best scale (step size) based on the
  8. * provided start Date, end Date, and minimumStep.
  9. *
  10. * If minimumStep is provided, the step size is chosen as close as possible
  11. * to the minimumStep but larger than minimumStep. If minimumStep is not
  12. * provided, the scale is set to 1 DAY.
  13. * The minimumStep should correspond with the onscreen size of about 6 characters
  14. *
  15. * Alternatively, you can set a scale by hand.
  16. * After creation, you can initialize the class by executing first(). Then you
  17. * can iterate from the start date to the end date via next(). You can check if
  18. * the end date is reached with the function hasNext(). After each step, you can
  19. * retrieve the current date via getCurrent().
  20. * The TimeStep has scales ranging from milliseconds, seconds, minutes, hours,
  21. * days, to years.
  22. *
  23. * Version: 1.2
  24. *
  25. * @param {Date} [start] The start date, for example new Date(2010, 9, 21)
  26. * or new Date(2010, 9, 21, 23, 45, 00)
  27. * @param {Date} [end] The end date
  28. * @param {Number} [minimumStep] Optional. Minimum step size in milliseconds
  29. */
  30. function TimeStep(start, end, minimumStep, hiddenDates) {
  31. this.moment = moment;
  32. // variables
  33. this.current = this.moment();
  34. this._start = this.moment();
  35. this._end = this.moment();
  36. this.autoScale = true;
  37. this.scale = 'day';
  38. this.step = 1;
  39. // initialize the range
  40. this.setRange(start, end, minimumStep);
  41. // hidden Dates options
  42. this.switchedDay = false;
  43. this.switchedMonth = false;
  44. this.switchedYear = false;
  45. this.hiddenDates = hiddenDates;
  46. if (hiddenDates === undefined) {
  47. this.hiddenDates = [];
  48. }
  49. this.format = TimeStep.FORMAT; // default formatting
  50. }
  51. // Time formatting
  52. TimeStep.FORMAT = {
  53. minorLabels: {
  54. millisecond:'SSS',
  55. second: 's',
  56. minute: 'HH:mm',
  57. hour: 'HH:mm',
  58. weekday: 'ddd D',
  59. day: 'D',
  60. month: 'MMM',
  61. year: 'YYYY'
  62. },
  63. majorLabels: {
  64. millisecond:'HH:mm:ss',
  65. second: 'D MMMM HH:mm',
  66. minute: 'ddd D MMMM',
  67. hour: 'ddd D MMMM',
  68. weekday: 'MMMM YYYY',
  69. day: 'MMMM YYYY',
  70. month: 'YYYY',
  71. year: ''
  72. }
  73. };
  74. /**
  75. * Set custom constructor function for moment. Can be used to set dates
  76. * to UTC or to set a utcOffset.
  77. * @param {function} moment
  78. */
  79. TimeStep.prototype.setMoment = function (moment) {
  80. this.moment = moment;
  81. // update the date properties, can have a new utcOffset
  82. this.current = this.moment(this.current);
  83. this._start = this.moment(this._start);
  84. this._end = this.moment(this._end);
  85. };
  86. /**
  87. * Set custom formatting for the minor an major labels of the TimeStep.
  88. * Both `minorLabels` and `majorLabels` are an Object with properties:
  89. * 'millisecond', 'second', 'minute', 'hour', 'weekday', 'day', 'month', 'year'.
  90. * @param {{minorLabels: Object, majorLabels: Object}} format
  91. */
  92. TimeStep.prototype.setFormat = function (format) {
  93. var defaultFormat = util.deepExtend({}, TimeStep.FORMAT);
  94. this.format = util.deepExtend(defaultFormat, format);
  95. };
  96. /**
  97. * Set a new range
  98. * If minimumStep is provided, the step size is chosen as close as possible
  99. * to the minimumStep but larger than minimumStep. If minimumStep is not
  100. * provided, the scale is set to 1 DAY.
  101. * The minimumStep should correspond with the onscreen size of about 6 characters
  102. * @param {Date} [start] The start date and time.
  103. * @param {Date} [end] The end date and time.
  104. * @param {int} [minimumStep] Optional. Minimum step size in milliseconds
  105. */
  106. TimeStep.prototype.setRange = function(start, end, minimumStep) {
  107. if (!(start instanceof Date) || !(end instanceof Date)) {
  108. throw "No legal start or end date in method setRange";
  109. }
  110. this._start = (start != undefined) ? this.moment(start.valueOf()) : new Date();
  111. this._end = (end != undefined) ? this.moment(end.valueOf()) : new Date();
  112. if (this.autoScale) {
  113. this.setMinimumStep(minimumStep);
  114. }
  115. };
  116. /**
  117. * Set the range iterator to the start date.
  118. */
  119. TimeStep.prototype.start = function() {
  120. this.current = this._start.clone();
  121. this.roundToMinor();
  122. };
  123. /**
  124. * Round the current date to the first minor date value
  125. * This must be executed once when the current date is set to start Date
  126. */
  127. TimeStep.prototype.roundToMinor = function() {
  128. // round to floor
  129. // IMPORTANT: we have no breaks in this switch! (this is no bug)
  130. // noinspection FallThroughInSwitchStatementJS
  131. switch (this.scale) {
  132. case 'year':
  133. this.current.year(this.step * Math.floor(this.current.year() / this.step));
  134. this.current.month(0);
  135. case 'month': this.current.date(1);
  136. case 'day': // intentional fall through
  137. case 'weekday': this.current.hours(0);
  138. case 'hour': this.current.minutes(0);
  139. case 'minute': this.current.seconds(0);
  140. case 'second': this.current.milliseconds(0);
  141. //case 'millisecond': // nothing to do for milliseconds
  142. }
  143. if (this.step != 1) {
  144. // round down to the first minor value that is a multiple of the current step size
  145. switch (this.scale) {
  146. case 'millisecond': this.current.subtract(this.current.milliseconds() % this.step, 'milliseconds'); break;
  147. case 'second': this.current.subtract(this.current.seconds() % this.step, 'seconds'); break;
  148. case 'minute': this.current.subtract(this.current.minutes() % this.step, 'minutes'); break;
  149. case 'hour': this.current.subtract(this.current.hours() % this.step, 'hours'); break;
  150. case 'weekday': // intentional fall through
  151. case 'day': this.current.subtract((this.current.date() - 1) % this.step, 'day'); break;
  152. case 'month': this.current.subtract(this.current.month() % this.step, 'month'); break;
  153. case 'year': this.current.subtract(this.current.year() % this.step, 'year'); break;
  154. default: break;
  155. }
  156. }
  157. };
  158. /**
  159. * Check if the there is a next step
  160. * @return {boolean} true if the current date has not passed the end date
  161. */
  162. TimeStep.prototype.hasNext = function () {
  163. return (this.current.valueOf() <= this._end.valueOf());
  164. };
  165. /**
  166. * Do the next step
  167. */
  168. TimeStep.prototype.next = function() {
  169. var prev = this.current.valueOf();
  170. // Two cases, needed to prevent issues with switching daylight savings
  171. // (end of March and end of October)
  172. if (this.current.month() < 6) {
  173. switch (this.scale) {
  174. case 'millisecond': this.current.add(this.step, 'millisecond'); break;
  175. case 'second': this.current.add(this.step, 'second'); break;
  176. case 'minute': this.current.add(this.step, 'minute'); break;
  177. case 'hour':
  178. this.current.add(this.step, 'hour');
  179. // in case of skipping an hour for daylight savings, adjust the hour again (else you get: 0h 5h 9h ... instead of 0h 4h 8h ...)
  180. // TODO: is this still needed now we use the function of moment.js?
  181. this.current.subtract(this.current.hours() % this.step, 'hour');
  182. break;
  183. case 'weekday': // intentional fall through
  184. case 'day': this.current.add(this.step, 'day'); break;
  185. case 'month': this.current.add(this.step, 'month'); break;
  186. case 'year': this.current.add(this.step, 'year'); break;
  187. default: break;
  188. }
  189. }
  190. else {
  191. switch (this.scale) {
  192. case 'millisecond': this.current.add(this.step, 'millisecond'); break;
  193. case 'second': this.current.add(this.step, 'second'); break;
  194. case 'minute': this.current.add(this.step, 'minute'); break;
  195. case 'hour': this.current.add(this.step, 'hour'); break;
  196. case 'weekday': // intentional fall through
  197. case 'day': this.current.add(this.step, 'day'); break;
  198. case 'month': this.current.add(this.step, 'month'); break;
  199. case 'year': this.current.add(this.step, 'year'); break;
  200. default: break;
  201. }
  202. }
  203. if (this.step != 1) {
  204. // round down to the correct major value
  205. switch (this.scale) {
  206. case 'millisecond': if(this.current.milliseconds() < this.step) this.current.milliseconds(0); break;
  207. case 'second': if(this.current.seconds() < this.step) this.current.seconds(0); break;
  208. case 'minute': if(this.current.minutes() < this.step) this.current.minutes(0); break;
  209. case 'hour': if(this.current.hours() < this.step) this.current.hours(0); break;
  210. case 'weekday': // intentional fall through
  211. case 'day': if(this.current.date() < this.step+1) this.current.date(1); break;
  212. case 'month': if(this.current.month() < this.step) this.current.month(0); break;
  213. case 'year': break; // nothing to do for year
  214. default: break;
  215. }
  216. }
  217. // safety mechanism: if current time is still unchanged, move to the end
  218. if (this.current.valueOf() == prev) {
  219. this.current = this._end.clone();
  220. }
  221. DateUtil.stepOverHiddenDates(this.moment, this, prev);
  222. };
  223. /**
  224. * Get the current datetime
  225. * @return {Moment} current The current date
  226. */
  227. TimeStep.prototype.getCurrent = function() {
  228. return this.current;
  229. };
  230. /**
  231. * Set a custom scale. Autoscaling will be disabled.
  232. * For example setScale('minute', 5) will result
  233. * in minor steps of 5 minutes, and major steps of an hour.
  234. *
  235. * @param {{scale: string, step: number}} params
  236. * An object containing two properties:
  237. * - A string 'scale'. Choose from 'millisecond', 'second',
  238. * 'minute', 'hour', 'weekday', 'day', 'month', 'year'.
  239. * - A number 'step'. A step size, by default 1.
  240. * Choose for example 1, 2, 5, or 10.
  241. */
  242. TimeStep.prototype.setScale = function(params) {
  243. if (params && typeof params.scale == 'string') {
  244. this.scale = params.scale;
  245. this.step = params.step > 0 ? params.step : 1;
  246. this.autoScale = false;
  247. }
  248. };
  249. /**
  250. * Enable or disable autoscaling
  251. * @param {boolean} enable If true, autoascaling is set true
  252. */
  253. TimeStep.prototype.setAutoScale = function (enable) {
  254. this.autoScale = enable;
  255. };
  256. /**
  257. * Automatically determine the scale that bests fits the provided minimum step
  258. * @param {Number} [minimumStep] The minimum step size in milliseconds
  259. */
  260. TimeStep.prototype.setMinimumStep = function(minimumStep) {
  261. if (minimumStep == undefined) {
  262. return;
  263. }
  264. //var b = asc + ds;
  265. var stepYear = (1000 * 60 * 60 * 24 * 30 * 12);
  266. var stepMonth = (1000 * 60 * 60 * 24 * 30);
  267. var stepDay = (1000 * 60 * 60 * 24);
  268. var stepHour = (1000 * 60 * 60);
  269. var stepMinute = (1000 * 60);
  270. var stepSecond = (1000);
  271. var stepMillisecond= (1);
  272. // find the smallest step that is larger than the provided minimumStep
  273. if (stepYear*1000 > minimumStep) {this.scale = 'year'; this.step = 1000;}
  274. if (stepYear*500 > minimumStep) {this.scale = 'year'; this.step = 500;}
  275. if (stepYear*100 > minimumStep) {this.scale = 'year'; this.step = 100;}
  276. if (stepYear*50 > minimumStep) {this.scale = 'year'; this.step = 50;}
  277. if (stepYear*10 > minimumStep) {this.scale = 'year'; this.step = 10;}
  278. if (stepYear*5 > minimumStep) {this.scale = 'year'; this.step = 5;}
  279. if (stepYear > minimumStep) {this.scale = 'year'; this.step = 1;}
  280. if (stepMonth*3 > minimumStep) {this.scale = 'month'; this.step = 3;}
  281. if (stepMonth > minimumStep) {this.scale = 'month'; this.step = 1;}
  282. if (stepDay*5 > minimumStep) {this.scale = 'day'; this.step = 5;}
  283. if (stepDay*2 > minimumStep) {this.scale = 'day'; this.step = 2;}
  284. if (stepDay > minimumStep) {this.scale = 'day'; this.step = 1;}
  285. if (stepDay/2 > minimumStep) {this.scale = 'weekday'; this.step = 1;}
  286. if (stepHour*4 > minimumStep) {this.scale = 'hour'; this.step = 4;}
  287. if (stepHour > minimumStep) {this.scale = 'hour'; this.step = 1;}
  288. if (stepMinute*15 > minimumStep) {this.scale = 'minute'; this.step = 15;}
  289. if (stepMinute*10 > minimumStep) {this.scale = 'minute'; this.step = 10;}
  290. if (stepMinute*5 > minimumStep) {this.scale = 'minute'; this.step = 5;}
  291. if (stepMinute > minimumStep) {this.scale = 'minute'; this.step = 1;}
  292. if (stepSecond*15 > minimumStep) {this.scale = 'second'; this.step = 15;}
  293. if (stepSecond*10 > minimumStep) {this.scale = 'second'; this.step = 10;}
  294. if (stepSecond*5 > minimumStep) {this.scale = 'second'; this.step = 5;}
  295. if (stepSecond > minimumStep) {this.scale = 'second'; this.step = 1;}
  296. if (stepMillisecond*200 > minimumStep) {this.scale = 'millisecond'; this.step = 200;}
  297. if (stepMillisecond*100 > minimumStep) {this.scale = 'millisecond'; this.step = 100;}
  298. if (stepMillisecond*50 > minimumStep) {this.scale = 'millisecond'; this.step = 50;}
  299. if (stepMillisecond*10 > minimumStep) {this.scale = 'millisecond'; this.step = 10;}
  300. if (stepMillisecond*5 > minimumStep) {this.scale = 'millisecond'; this.step = 5;}
  301. if (stepMillisecond > minimumStep) {this.scale = 'millisecond'; this.step = 1;}
  302. };
  303. /**
  304. * Snap a date to a rounded value.
  305. * The snap intervals are dependent on the current scale and step.
  306. * Static function
  307. * @param {Date} date the date to be snapped.
  308. * @param {string} scale Current scale, can be 'millisecond', 'second',
  309. * 'minute', 'hour', 'weekday, 'day', 'month', 'year'.
  310. * @param {number} step Current step (1, 2, 4, 5, ...
  311. * @return {Date} snappedDate
  312. */
  313. TimeStep.snap = function(date, scale, step) {
  314. var clone = moment(date);
  315. if (scale == 'year') {
  316. var year = clone.year() + Math.round(clone.month() / 12);
  317. clone.year(Math.round(year / step) * step);
  318. clone.month(0);
  319. clone.date(0);
  320. clone.hours(0);
  321. clone.minutes(0);
  322. clone.seconds(0);
  323. clone.milliseconds(0);
  324. }
  325. else if (scale == 'month') {
  326. if (clone.date() > 15) {
  327. clone.date(1);
  328. clone.add(1, 'month');
  329. // important: first set Date to 1, after that change the month.
  330. }
  331. else {
  332. clone.date(1);
  333. }
  334. clone.hours(0);
  335. clone.minutes(0);
  336. clone.seconds(0);
  337. clone.milliseconds(0);
  338. }
  339. else if (scale == 'day') {
  340. //noinspection FallthroughInSwitchStatementJS
  341. switch (step) {
  342. case 5:
  343. case 2:
  344. clone.hours(Math.round(clone.hours() / 24) * 24); break;
  345. default:
  346. clone.hours(Math.round(clone.hours() / 12) * 12); break;
  347. }
  348. clone.minutes(0);
  349. clone.seconds(0);
  350. clone.milliseconds(0);
  351. }
  352. else if (scale == 'weekday') {
  353. //noinspection FallthroughInSwitchStatementJS
  354. switch (step) {
  355. case 5:
  356. case 2:
  357. clone.hours(Math.round(clone.hours() / 12) * 12); break;
  358. default:
  359. clone.hours(Math.round(clone.hours() / 6) * 6); break;
  360. }
  361. clone.minutes(0);
  362. clone.seconds(0);
  363. clone.milliseconds(0);
  364. }
  365. else if (scale == 'hour') {
  366. switch (step) {
  367. case 4:
  368. clone.minutes(Math.round(clone.minutes() / 60) * 60); break;
  369. default:
  370. clone.minutes(Math.round(clone.minutes() / 30) * 30); break;
  371. }
  372. clone.seconds(0);
  373. clone.milliseconds(0);
  374. } else if (scale == 'minute') {
  375. //noinspection FallthroughInSwitchStatementJS
  376. switch (step) {
  377. case 15:
  378. case 10:
  379. clone.minutes(Math.round(clone.minutes() / 5) * 5);
  380. clone.seconds(0);
  381. break;
  382. case 5:
  383. clone.seconds(Math.round(clone.seconds() / 60) * 60); break;
  384. default:
  385. clone.seconds(Math.round(clone.seconds() / 30) * 30); break;
  386. }
  387. clone.milliseconds(0);
  388. }
  389. else if (scale == 'second') {
  390. //noinspection FallthroughInSwitchStatementJS
  391. switch (step) {
  392. case 15:
  393. case 10:
  394. clone.seconds(Math.round(clone.seconds() / 5) * 5);
  395. clone.milliseconds(0);
  396. break;
  397. case 5:
  398. clone.milliseconds(Math.round(clone.milliseconds() / 1000) * 1000); break;
  399. default:
  400. clone.milliseconds(Math.round(clone.milliseconds() / 500) * 500); break;
  401. }
  402. }
  403. else if (scale == 'millisecond') {
  404. var _step = step > 5 ? step / 2 : 1;
  405. clone.milliseconds(Math.round(clone.milliseconds() / _step) * _step);
  406. }
  407. return clone;
  408. };
  409. /**
  410. * Check if the current value is a major value (for example when the step
  411. * is DAY, a major value is each first day of the MONTH)
  412. * @return {boolean} true if current date is major, else false.
  413. */
  414. TimeStep.prototype.isMajor = function() {
  415. if (this.switchedYear == true) {
  416. this.switchedYear = false;
  417. switch (this.scale) {
  418. case 'year':
  419. case 'month':
  420. case 'weekday':
  421. case 'day':
  422. case 'hour':
  423. case 'minute':
  424. case 'second':
  425. case 'millisecond':
  426. return true;
  427. default:
  428. return false;
  429. }
  430. }
  431. else if (this.switchedMonth == true) {
  432. this.switchedMonth = false;
  433. switch (this.scale) {
  434. case 'weekday':
  435. case 'day':
  436. case 'hour':
  437. case 'minute':
  438. case 'second':
  439. case 'millisecond':
  440. return true;
  441. default:
  442. return false;
  443. }
  444. }
  445. else if (this.switchedDay == true) {
  446. this.switchedDay = false;
  447. switch (this.scale) {
  448. case 'millisecond':
  449. case 'second':
  450. case 'minute':
  451. case 'hour':
  452. return true;
  453. default:
  454. return false;
  455. }
  456. }
  457. var date = this.moment(this.current);
  458. switch (this.scale) {
  459. case 'millisecond':
  460. return (date.milliseconds() == 0);
  461. case 'second':
  462. return (date.seconds() == 0);
  463. case 'minute':
  464. return (date.hours() == 0) && (date.minutes() == 0);
  465. case 'hour':
  466. return (date.hours() == 0);
  467. case 'weekday': // intentional fall through
  468. case 'day':
  469. return (date.date() == 1);
  470. case 'month':
  471. return (date.month() == 0);
  472. case 'year':
  473. return false;
  474. default:
  475. return false;
  476. }
  477. };
  478. /**
  479. * Returns formatted text for the minor axislabel, depending on the current
  480. * date and the scale. For example when scale is MINUTE, the current time is
  481. * formatted as "hh:mm".
  482. * @param {Date} [date] custom date. if not provided, current date is taken
  483. */
  484. TimeStep.prototype.getLabelMinor = function(date) {
  485. if (date == undefined) {
  486. date = this.current;
  487. }
  488. var format = this.format.minorLabels[this.scale];
  489. return (format && format.length > 0) ? this.moment(date).format(format) : '';
  490. };
  491. /**
  492. * Returns formatted text for the major axis label, depending on the current
  493. * date and the scale. For example when scale is MINUTE, the major scale is
  494. * hours, and the hour will be formatted as "hh".
  495. * @param {Date} [date] custom date. if not provided, current date is taken
  496. */
  497. TimeStep.prototype.getLabelMajor = function(date) {
  498. if (date == undefined) {
  499. date = this.current;
  500. }
  501. var format = this.format.majorLabels[this.scale];
  502. return (format && format.length > 0) ? this.moment(date).format(format) : '';
  503. };
  504. TimeStep.prototype.getClassName = function() {
  505. var _moment = this.moment;
  506. var m = this.moment(this.current);
  507. var current = m.locale ? m.locale('en') : m.lang('en'); // old versions of moment have .lang() function
  508. var step = this.step;
  509. function even(value) {
  510. return (value / step % 2 == 0) ? ' vis-even' : ' vis-odd';
  511. }
  512. function today(date) {
  513. if (date.isSame(new Date(), 'day')) {
  514. return ' vis-today';
  515. }
  516. if (date.isSame(_moment().add(1, 'day'), 'day')) {
  517. return ' vis-tomorrow';
  518. }
  519. if (date.isSame(_moment().add(-1, 'day'), 'day')) {
  520. return ' vis-yesterday';
  521. }
  522. return '';
  523. }
  524. function currentWeek(date) {
  525. return date.isSame(new Date(), 'week') ? ' vis-current-week' : '';
  526. }
  527. function currentMonth(date) {
  528. return date.isSame(new Date(), 'month') ? ' vis-current-month' : '';
  529. }
  530. function currentYear(date) {
  531. return date.isSame(new Date(), 'year') ? ' vis-current-year' : '';
  532. }
  533. switch (this.scale) {
  534. case 'millisecond':
  535. return even(current.milliseconds()).trim();
  536. case 'second':
  537. return even(current.seconds()).trim();
  538. case 'minute':
  539. return even(current.minutes()).trim();
  540. case 'hour':
  541. var hours = current.hours();
  542. if (this.step == 4) {
  543. hours = hours + '-h' + (hours + 4);
  544. }
  545. return 'vis-h' + hours + today(current) + even(current.hours());
  546. case 'weekday':
  547. return 'vis-' + current.format('dddd').toLowerCase() +
  548. today(current) + currentWeek(current) + even(current.date());
  549. case 'day':
  550. var day = current.date();
  551. var month = current.format('MMMM').toLowerCase();
  552. return 'vis-day' + day + ' vis-' + month + currentMonth(current) + even(day - 1);
  553. case 'month':
  554. return 'vis-' + current.format('MMMM').toLowerCase() +
  555. currentMonth(current) + even(current.month());
  556. case 'year':
  557. var year = current.year();
  558. return 'vis-year' + year + currentYear(current)+ even(year);
  559. default:
  560. return '';
  561. }
  562. };
  563. module.exports = TimeStep;