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.

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