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.

601 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. * @param {Date} date the date to be snapped.
  295. * @return {Date} snappedDate
  296. */
  297. TimeStep.prototype.snap = function(date) {
  298. var clone = new Date(date.valueOf());
  299. if (this.scale == 'year') {
  300. var year = clone.getFullYear() + Math.round(clone.getMonth() / 12);
  301. clone.setFullYear(Math.round(year / this.step) * this.step);
  302. clone.setMonth(0);
  303. clone.setDate(0);
  304. clone.setHours(0);
  305. clone.setMinutes(0);
  306. clone.setSeconds(0);
  307. clone.setMilliseconds(0);
  308. }
  309. else if (this.scale == 'month') {
  310. if (clone.getDate() > 15) {
  311. clone.setDate(1);
  312. clone.setMonth(clone.getMonth() + 1);
  313. // important: first set Date to 1, after that change the month.
  314. }
  315. else {
  316. clone.setDate(1);
  317. }
  318. clone.setHours(0);
  319. clone.setMinutes(0);
  320. clone.setSeconds(0);
  321. clone.setMilliseconds(0);
  322. }
  323. else if (this.scale == 'day') {
  324. //noinspection FallthroughInSwitchStatementJS
  325. switch (this.step) {
  326. case 5:
  327. case 2:
  328. clone.setHours(Math.round(clone.getHours() / 24) * 24); break;
  329. default:
  330. clone.setHours(Math.round(clone.getHours() / 12) * 12); break;
  331. }
  332. clone.setMinutes(0);
  333. clone.setSeconds(0);
  334. clone.setMilliseconds(0);
  335. }
  336. else if (this.scale == 'weekday') {
  337. //noinspection FallthroughInSwitchStatementJS
  338. switch (this.step) {
  339. case 5:
  340. case 2:
  341. clone.setHours(Math.round(clone.getHours() / 12) * 12); break;
  342. default:
  343. clone.setHours(Math.round(clone.getHours() / 6) * 6); break;
  344. }
  345. clone.setMinutes(0);
  346. clone.setSeconds(0);
  347. clone.setMilliseconds(0);
  348. }
  349. else if (this.scale == 'hour') {
  350. switch (this.step) {
  351. case 4:
  352. clone.setMinutes(Math.round(clone.getMinutes() / 60) * 60); break;
  353. default:
  354. clone.setMinutes(Math.round(clone.getMinutes() / 30) * 30); break;
  355. }
  356. clone.setSeconds(0);
  357. clone.setMilliseconds(0);
  358. } else if (this.scale == 'minute') {
  359. //noinspection FallthroughInSwitchStatementJS
  360. switch (this.step) {
  361. case 15:
  362. case 10:
  363. clone.setMinutes(Math.round(clone.getMinutes() / 5) * 5);
  364. clone.setSeconds(0);
  365. break;
  366. case 5:
  367. clone.setSeconds(Math.round(clone.getSeconds() / 60) * 60); break;
  368. default:
  369. clone.setSeconds(Math.round(clone.getSeconds() / 30) * 30); break;
  370. }
  371. clone.setMilliseconds(0);
  372. }
  373. else if (this.scale == 'second') {
  374. //noinspection FallthroughInSwitchStatementJS
  375. switch (this.step) {
  376. case 15:
  377. case 10:
  378. clone.setSeconds(Math.round(clone.getSeconds() / 5) * 5);
  379. clone.setMilliseconds(0);
  380. break;
  381. case 5:
  382. clone.setMilliseconds(Math.round(clone.getMilliseconds() / 1000) * 1000); break;
  383. default:
  384. clone.setMilliseconds(Math.round(clone.getMilliseconds() / 500) * 500); break;
  385. }
  386. }
  387. else if (this.scale == 'millisecond') {
  388. var step = this.step > 5 ? this.step / 2 : 1;
  389. clone.setMilliseconds(Math.round(clone.getMilliseconds() / step) * step);
  390. }
  391. return clone;
  392. };
  393. /**
  394. * Check if the current value is a major value (for example when the step
  395. * is DAY, a major value is each first day of the MONTH)
  396. * @return {boolean} true if current date is major, else false.
  397. */
  398. TimeStep.prototype.isMajor = function() {
  399. if (this.switchedYear == true) {
  400. this.switchedYear = false;
  401. switch (this.scale) {
  402. case 'year':
  403. case 'month':
  404. case 'weekday':
  405. case 'day':
  406. case 'hour':
  407. case 'minute':
  408. case 'second':
  409. case 'millisecond':
  410. return true;
  411. default:
  412. return false;
  413. }
  414. }
  415. else if (this.switchedMonth == true) {
  416. this.switchedMonth = false;
  417. switch (this.scale) {
  418. case 'weekday':
  419. case 'day':
  420. case 'hour':
  421. case 'minute':
  422. case 'second':
  423. case 'millisecond':
  424. return true;
  425. default:
  426. return false;
  427. }
  428. }
  429. else if (this.switchedDay == true) {
  430. this.switchedDay = false;
  431. switch (this.scale) {
  432. case 'millisecond':
  433. case 'second':
  434. case 'minute':
  435. case 'hour':
  436. return true;
  437. default:
  438. return false;
  439. }
  440. }
  441. switch (this.scale) {
  442. case 'millisecond':
  443. return (this.current.getMilliseconds() == 0);
  444. case 'second':
  445. return (this.current.getSeconds() == 0);
  446. case 'minute':
  447. return (this.current.getHours() == 0) && (this.current.getMinutes() == 0);
  448. case 'hour':
  449. return (this.current.getHours() == 0);
  450. case 'weekday': // intentional fall through
  451. case 'day':
  452. return (this.current.getDate() == 1);
  453. case 'month':
  454. return (this.current.getMonth() == 0);
  455. case 'year':
  456. return false;
  457. default:
  458. return false;
  459. }
  460. };
  461. /**
  462. * Returns formatted text for the minor axislabel, depending on the current
  463. * date and the scale. For example when scale is MINUTE, the current time is
  464. * formatted as "hh:mm".
  465. * @param {Date} [date] custom date. if not provided, current date is taken
  466. */
  467. TimeStep.prototype.getLabelMinor = function(date) {
  468. if (date == undefined) {
  469. date = this.current;
  470. }
  471. var format = this.format.minorLabels[this.scale];
  472. return (format && format.length > 0) ? moment(date).format(format) : '';
  473. };
  474. /**
  475. * Returns formatted text for the major axis label, depending on the current
  476. * date and the scale. For example when scale is MINUTE, the major scale is
  477. * hours, and the hour will be formatted as "hh".
  478. * @param {Date} [date] custom date. if not provided, current date is taken
  479. */
  480. TimeStep.prototype.getLabelMajor = function(date) {
  481. if (date == undefined) {
  482. date = this.current;
  483. }
  484. var format = this.format.majorLabels[this.scale];
  485. return (format && format.length > 0) ? moment(date).format(format) : '';
  486. };
  487. TimeStep.prototype.getClassName = function() {
  488. var m = moment(this.current);
  489. var date = m.locale ? m.locale('en') : m.lang('en'); // old versions of moment have .lang() function
  490. var step = this.step;
  491. function even(value) {
  492. return (value / step % 2 == 0) ? ' even' : ' odd';
  493. }
  494. function today(date) {
  495. if (date.isSame(new Date(), 'day')) {
  496. return ' today';
  497. }
  498. if (date.isSame(moment().add(1, 'day'), 'day')) {
  499. return ' tomorrow';
  500. }
  501. if (date.isSame(moment().add(-1, 'day'), 'day')) {
  502. return ' yesterday';
  503. }
  504. return '';
  505. }
  506. function currentWeek(date) {
  507. return date.isSame(new Date(), 'week') ? ' current-week' : '';
  508. }
  509. function currentMonth(date) {
  510. return date.isSame(new Date(), 'month') ? ' current-month' : '';
  511. }
  512. function currentYear(date) {
  513. return date.isSame(new Date(), 'year') ? ' current-year' : '';
  514. }
  515. switch (this.scale) {
  516. case 'millisecond':
  517. return even(date.milliseconds()).trim();
  518. case 'second':
  519. return even(date.seconds()).trim();
  520. case 'minute':
  521. return even(date.minutes()).trim();
  522. case 'hour':
  523. var hours = date.hours();
  524. if (this.step == 4) {
  525. hours = hours + '-' + (hours + 4);
  526. }
  527. return hours + 'h' + today(date) + even(date.hours());
  528. case 'weekday':
  529. return date.format('dddd').toLowerCase() +
  530. today(date) + currentWeek(date) + even(date.date());
  531. case 'day':
  532. var day = date.date();
  533. var month = date.format('MMMM').toLowerCase();
  534. return 'day' + day + ' ' + month + currentMonth(date) + even(day - 1);
  535. case 'month':
  536. return date.format('MMMM').toLowerCase() +
  537. currentMonth(date) + even(date.month());
  538. case 'year':
  539. var year = date.year();
  540. return 'year' + year + currentYear(date)+ even(year);
  541. default:
  542. return '';
  543. }
  544. };
  545. module.exports = TimeStep;