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
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(SCALE.MINUTES, 5) will result
  221. * in minor steps of 5 minutes, and major steps of an hour.
  222. *
  223. * @param {string} newScale
  224. * A scale. Choose from 'millisecond, 'second,
  225. * 'minute', 'hour', 'weekday, 'day, 'month, 'year'.
  226. * @param {Number} newStep A step size, by default 1. Choose for
  227. * example 1, 2, 5, or 10.
  228. */
  229. TimeStep.prototype.setScale = function(newScale, newStep) {
  230. this.scale = newScale;
  231. if (newStep > 0) {
  232. this.step = newStep;
  233. }
  234. this.autoScale = false;
  235. };
  236. /**
  237. * Enable or disable autoscaling
  238. * @param {boolean} enable If true, autoascaling is set true
  239. */
  240. TimeStep.prototype.setAutoScale = function (enable) {
  241. this.autoScale = enable;
  242. };
  243. /**
  244. * Automatically determine the scale that bests fits the provided minimum step
  245. * @param {Number} [minimumStep] The minimum step size in milliseconds
  246. */
  247. TimeStep.prototype.setMinimumStep = function(minimumStep) {
  248. if (minimumStep == undefined) {
  249. return;
  250. }
  251. //var b = asc + ds;
  252. var stepYear = (1000 * 60 * 60 * 24 * 30 * 12);
  253. var stepMonth = (1000 * 60 * 60 * 24 * 30);
  254. var stepDay = (1000 * 60 * 60 * 24);
  255. var stepHour = (1000 * 60 * 60);
  256. var stepMinute = (1000 * 60);
  257. var stepSecond = (1000);
  258. var stepMillisecond= (1);
  259. // find the smallest step that is larger than the provided minimumStep
  260. if (stepYear*1000 > minimumStep) {this.scale = 'year'; this.step = 1000;}
  261. if (stepYear*500 > minimumStep) {this.scale = 'year'; this.step = 500;}
  262. if (stepYear*100 > minimumStep) {this.scale = 'year'; this.step = 100;}
  263. if (stepYear*50 > minimumStep) {this.scale = 'year'; this.step = 50;}
  264. if (stepYear*10 > minimumStep) {this.scale = 'year'; this.step = 10;}
  265. if (stepYear*5 > minimumStep) {this.scale = 'year'; this.step = 5;}
  266. if (stepYear > minimumStep) {this.scale = 'year'; this.step = 1;}
  267. if (stepMonth*3 > minimumStep) {this.scale = 'month'; this.step = 3;}
  268. if (stepMonth > minimumStep) {this.scale = 'month'; this.step = 1;}
  269. if (stepDay*5 > minimumStep) {this.scale = 'day'; this.step = 5;}
  270. if (stepDay*2 > minimumStep) {this.scale = 'day'; this.step = 2;}
  271. if (stepDay > minimumStep) {this.scale = 'day'; this.step = 1;}
  272. if (stepDay/2 > minimumStep) {this.scale = 'weekday'; this.step = 1;}
  273. if (stepHour*4 > minimumStep) {this.scale = 'hour'; this.step = 4;}
  274. if (stepHour > minimumStep) {this.scale = 'hour'; this.step = 1;}
  275. if (stepMinute*15 > minimumStep) {this.scale = 'minute'; this.step = 15;}
  276. if (stepMinute*10 > minimumStep) {this.scale = 'minute'; this.step = 10;}
  277. if (stepMinute*5 > minimumStep) {this.scale = 'minute'; this.step = 5;}
  278. if (stepMinute > minimumStep) {this.scale = 'minute'; this.step = 1;}
  279. if (stepSecond*15 > minimumStep) {this.scale = 'second'; this.step = 15;}
  280. if (stepSecond*10 > minimumStep) {this.scale = 'second'; this.step = 10;}
  281. if (stepSecond*5 > minimumStep) {this.scale = 'second'; this.step = 5;}
  282. if (stepSecond > minimumStep) {this.scale = 'second'; this.step = 1;}
  283. if (stepMillisecond*200 > minimumStep) {this.scale = 'millisecond'; this.step = 200;}
  284. if (stepMillisecond*100 > minimumStep) {this.scale = 'millisecond'; this.step = 100;}
  285. if (stepMillisecond*50 > minimumStep) {this.scale = 'millisecond'; this.step = 50;}
  286. if (stepMillisecond*10 > minimumStep) {this.scale = 'millisecond'; this.step = 10;}
  287. if (stepMillisecond*5 > minimumStep) {this.scale = 'millisecond'; this.step = 5;}
  288. if (stepMillisecond > minimumStep) {this.scale = 'millisecond'; this.step = 1;}
  289. };
  290. /**
  291. * Snap a date to a rounded value.
  292. * The snap intervals are dependent on the current scale and step.
  293. * @param {Date} date the date to be snapped.
  294. * @return {Date} snappedDate
  295. */
  296. TimeStep.prototype.snap = function(date) {
  297. var clone = new Date(date.valueOf());
  298. if (this.scale == 'year') {
  299. var year = clone.getFullYear() + Math.round(clone.getMonth() / 12);
  300. clone.setFullYear(Math.round(year / this.step) * this.step);
  301. clone.setMonth(0);
  302. clone.setDate(0);
  303. clone.setHours(0);
  304. clone.setMinutes(0);
  305. clone.setSeconds(0);
  306. clone.setMilliseconds(0);
  307. }
  308. else if (this.scale == 'month') {
  309. if (clone.getDate() > 15) {
  310. clone.setDate(1);
  311. clone.setMonth(clone.getMonth() + 1);
  312. // important: first set Date to 1, after that change the month.
  313. }
  314. else {
  315. clone.setDate(1);
  316. }
  317. clone.setHours(0);
  318. clone.setMinutes(0);
  319. clone.setSeconds(0);
  320. clone.setMilliseconds(0);
  321. }
  322. else if (this.scale == 'day') {
  323. //noinspection FallthroughInSwitchStatementJS
  324. switch (this.step) {
  325. case 5:
  326. case 2:
  327. clone.setHours(Math.round(clone.getHours() / 24) * 24); break;
  328. default:
  329. clone.setHours(Math.round(clone.getHours() / 12) * 12); break;
  330. }
  331. clone.setMinutes(0);
  332. clone.setSeconds(0);
  333. clone.setMilliseconds(0);
  334. }
  335. else if (this.scale == 'weekday') {
  336. //noinspection FallthroughInSwitchStatementJS
  337. switch (this.step) {
  338. case 5:
  339. case 2:
  340. clone.setHours(Math.round(clone.getHours() / 12) * 12); break;
  341. default:
  342. clone.setHours(Math.round(clone.getHours() / 6) * 6); break;
  343. }
  344. clone.setMinutes(0);
  345. clone.setSeconds(0);
  346. clone.setMilliseconds(0);
  347. }
  348. else if (this.scale == 'hour') {
  349. switch (this.step) {
  350. case 4:
  351. clone.setMinutes(Math.round(clone.getMinutes() / 60) * 60); break;
  352. default:
  353. clone.setMinutes(Math.round(clone.getMinutes() / 30) * 30); break;
  354. }
  355. clone.setSeconds(0);
  356. clone.setMilliseconds(0);
  357. } else if (this.scale == 'minute') {
  358. //noinspection FallthroughInSwitchStatementJS
  359. switch (this.step) {
  360. case 15:
  361. case 10:
  362. clone.setMinutes(Math.round(clone.getMinutes() / 5) * 5);
  363. clone.setSeconds(0);
  364. break;
  365. case 5:
  366. clone.setSeconds(Math.round(clone.getSeconds() / 60) * 60); break;
  367. default:
  368. clone.setSeconds(Math.round(clone.getSeconds() / 30) * 30); break;
  369. }
  370. clone.setMilliseconds(0);
  371. }
  372. else if (this.scale == 'second') {
  373. //noinspection FallthroughInSwitchStatementJS
  374. switch (this.step) {
  375. case 15:
  376. case 10:
  377. clone.setSeconds(Math.round(clone.getSeconds() / 5) * 5);
  378. clone.setMilliseconds(0);
  379. break;
  380. case 5:
  381. clone.setMilliseconds(Math.round(clone.getMilliseconds() / 1000) * 1000); break;
  382. default:
  383. clone.setMilliseconds(Math.round(clone.getMilliseconds() / 500) * 500); break;
  384. }
  385. }
  386. else if (this.scale == 'millisecond') {
  387. var step = this.step > 5 ? this.step / 2 : 1;
  388. clone.setMilliseconds(Math.round(clone.getMilliseconds() / step) * step);
  389. }
  390. return clone;
  391. };
  392. /**
  393. * Check if the current value is a major value (for example when the step
  394. * is DAY, a major value is each first day of the MONTH)
  395. * @return {boolean} true if current date is major, else false.
  396. */
  397. TimeStep.prototype.isMajor = function() {
  398. if (this.switchedYear == true) {
  399. this.switchedYear = false;
  400. switch (this.scale) {
  401. case 'year':
  402. case 'month':
  403. case 'weekday':
  404. case 'day':
  405. case 'hour':
  406. case 'minute':
  407. case 'second':
  408. case 'millisecond':
  409. return true;
  410. default:
  411. return false;
  412. }
  413. }
  414. else if (this.switchedMonth == true) {
  415. this.switchedMonth = false;
  416. switch (this.scale) {
  417. case 'weekday':
  418. case 'day':
  419. case 'hour':
  420. case 'minute':
  421. case 'second':
  422. case 'millisecond':
  423. return true;
  424. default:
  425. return false;
  426. }
  427. }
  428. else if (this.switchedDay == true) {
  429. this.switchedDay = false;
  430. switch (this.scale) {
  431. case 'millisecond':
  432. case 'second':
  433. case 'minute':
  434. case 'hour':
  435. return true;
  436. default:
  437. return false;
  438. }
  439. }
  440. switch (this.scale) {
  441. case 'millisecond':
  442. return (this.current.getMilliseconds() == 0);
  443. case 'second':
  444. return (this.current.getSeconds() == 0);
  445. case 'minute':
  446. return (this.current.getHours() == 0) && (this.current.getMinutes() == 0);
  447. case 'hour':
  448. return (this.current.getHours() == 0);
  449. case 'weekday': // intentional fall through
  450. case 'day':
  451. return (this.current.getDate() == 1);
  452. case 'month':
  453. return (this.current.getMonth() == 0);
  454. case 'year':
  455. return false;
  456. default:
  457. return false;
  458. }
  459. };
  460. /**
  461. * Returns formatted text for the minor axislabel, depending on the current
  462. * date and the scale. For example when scale is MINUTE, the current time is
  463. * formatted as "hh:mm".
  464. * @param {Date} [date] custom date. if not provided, current date is taken
  465. */
  466. TimeStep.prototype.getLabelMinor = function(date) {
  467. if (date == undefined) {
  468. date = this.current;
  469. }
  470. var format = this.format.minorLabels[this.scale];
  471. return (format && format.length > 0) ? moment(date).format(format) : '';
  472. };
  473. /**
  474. * Returns formatted text for the major axis label, depending on the current
  475. * date and the scale. For example when scale is MINUTE, the major scale is
  476. * hours, and the hour will be formatted as "hh".
  477. * @param {Date} [date] custom date. if not provided, current date is taken
  478. */
  479. TimeStep.prototype.getLabelMajor = function(date) {
  480. if (date == undefined) {
  481. date = this.current;
  482. }
  483. var format = this.format.majorLabels[this.scale];
  484. return (format && format.length > 0) ? moment(date).format(format) : '';
  485. };
  486. TimeStep.prototype.getClassName = function() {
  487. var date = moment(this.current).locale('en');
  488. var step = this.step;
  489. function even(value) {
  490. return (value / step % 2 == 0) ? ' even' : ' odd';
  491. }
  492. function today(date) {
  493. if (date.isSame(new Date(), 'day')) {
  494. return ' today';
  495. }
  496. if (date.isSame(moment().add(1, 'day'), 'day')) {
  497. return ' tomorrow';
  498. }
  499. if (date.isSame(moment().add(-1, 'day'), 'day')) {
  500. return ' yesterday';
  501. }
  502. return '';
  503. }
  504. function currentWeek(date) {
  505. return date.isSame(new Date(), 'week') ? ' current-week' : '';
  506. }
  507. function currentMonth(date) {
  508. return date.isSame(new Date(), 'month') ? ' current-month' : '';
  509. }
  510. function currentYear(date) {
  511. return date.isSame(new Date(), 'year') ? ' current-year' : '';
  512. }
  513. switch (this.scale) {
  514. case 'millisecond':
  515. return even(date.milliseconds()).trim();
  516. case 'second':
  517. return even(date.seconds()).trim();
  518. case 'minute':
  519. return even(date.minutes()).trim();
  520. case 'hour':
  521. var hours = date.hours();
  522. if (this.step == 4) {
  523. hours = hours + '-' + (hours + 4);
  524. }
  525. return hours + 'h' + today(date) + even(date.hours());
  526. case 'weekday':
  527. return date.format('dddd').toLowerCase() +
  528. today(date) + currentWeek(date) + even(date.date());
  529. case 'day':
  530. var day = date.date();
  531. var month = date.format('MMMM').toLowerCase();
  532. return 'day' + day + ' ' + month + currentMonth(date) + even(day - 1);
  533. case 'month':
  534. return date.format('MMMM').toLowerCase() +
  535. currentMonth(date) + even(date.month());
  536. case 'year':
  537. var year = date.year();
  538. return 'year' + year + currentYear(date)+ even(year);
  539. default:
  540. return '';
  541. }
  542. };
  543. module.exports = TimeStep;