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.

487 lines
20 KiB

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