DateTimeAxis.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* *
  2. *
  3. * (c) 2010-2019 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import H from './Globals.js';
  12. import './Utilities.js';
  13. var Axis = H.Axis, getMagnitude = H.getMagnitude, normalizeTickInterval = H.normalizeTickInterval, timeUnits = H.timeUnits;
  14. /* eslint-disable valid-jsdoc */
  15. /**
  16. * Set the tick positions to a time unit that makes sense, for example
  17. * on the first of each month or on every Monday. Return an array
  18. * with the time positions. Used in datetime axes as well as for grouping
  19. * data on a datetime axis.
  20. *
  21. * @private
  22. * @function Highcharts.Axis#getTimeTicks
  23. *
  24. * @param {Highcharts.DateTimeAxisNormalizedObject} normalizedInterval
  25. * The interval in axis values (ms) and thecount
  26. *
  27. * @param {number} min
  28. * The minimum in axis values
  29. *
  30. * @param {number} max
  31. * The maximum in axis values
  32. *
  33. * @param {number} startOfWeek
  34. *
  35. * @return {Highcharts.AxisTickPositionsArray}
  36. */
  37. Axis.prototype.getTimeTicks = function () {
  38. return this.chart.time.getTimeTicks.apply(this.chart.time, arguments);
  39. };
  40. /**
  41. * Get a normalized tick interval for dates. Returns a configuration object with
  42. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  43. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  44. * of segments in stock charts, the normalizing logic was extracted in order to
  45. * prevent it for running over again for each segment having the same interval.
  46. * #662, #697.
  47. *
  48. * @private
  49. * @function Highcharts.Axis#normalizeTimeTickInterval
  50. * @param {number} tickInterval
  51. * @param {Array<Array<string,(Array<number>|null)>>} [unitsOption]
  52. * @return {Highcharts.DateTimeAxisNormalizedObject}
  53. */
  54. Axis.prototype.normalizeTimeTickInterval = function (tickInterval, unitsOption) {
  55. var units = unitsOption || [[
  56. 'millisecond',
  57. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  58. ], [
  59. 'second',
  60. [1, 2, 5, 10, 15, 30]
  61. ], [
  62. 'minute',
  63. [1, 2, 5, 10, 15, 30]
  64. ], [
  65. 'hour',
  66. [1, 2, 3, 4, 6, 8, 12]
  67. ], [
  68. 'day',
  69. [1, 2]
  70. ], [
  71. 'week',
  72. [1, 2]
  73. ], [
  74. 'month',
  75. [1, 2, 3, 4, 6]
  76. ], [
  77. 'year',
  78. null
  79. ]], unit = units[units.length - 1], // default unit is years
  80. interval = timeUnits[unit[0]], multiples = unit[1], count, i;
  81. // loop through the units to find the one that best fits the tickInterval
  82. for (i = 0; i < units.length; i++) {
  83. unit = units[i];
  84. interval = timeUnits[unit[0]];
  85. multiples = unit[1];
  86. if (units[i + 1]) {
  87. // lessThan is in the middle between the highest multiple and the
  88. // next unit.
  89. var lessThan = (interval *
  90. multiples[multiples.length - 1] +
  91. timeUnits[units[i + 1][0]]) / 2;
  92. // break and keep the current unit
  93. if (tickInterval <= lessThan) {
  94. break;
  95. }
  96. }
  97. }
  98. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  99. if (interval === timeUnits.year && tickInterval < 5 * interval) {
  100. multiples = [1, 2, 5];
  101. }
  102. // get the count
  103. count = normalizeTickInterval(tickInterval / interval, multiples, unit[0] === 'year' ?
  104. Math.max(getMagnitude(tickInterval / interval), 1) : // #1913, #2360
  105. 1);
  106. return {
  107. unitRange: interval,
  108. count: count,
  109. unitName: unit[0]
  110. };
  111. };