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.

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