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.

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