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.

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