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.

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