DateTimeAxis.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* *
  2. *
  3. * (c) 2010-2020 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 Axis from './Axis.js';
  12. import U from '../Utilities.js';
  13. var addEvent = U.addEvent, getMagnitude = U.getMagnitude, normalizeTickInterval = U.normalizeTickInterval, timeUnits = U.timeUnits;
  14. /* eslint-disable valid-jsdoc */
  15. var DateTimeAxisAdditions = /** @class */ (function () {
  16. /* *
  17. *
  18. * Constructors
  19. *
  20. * */
  21. function DateTimeAxisAdditions(axis) {
  22. this.axis = axis;
  23. }
  24. /* *
  25. *
  26. * Functions
  27. *
  28. * */
  29. /**
  30. * Get a normalized tick interval for dates. Returns a configuration object
  31. * with unit range (interval), count and name. Used to prepare data for
  32. * `getTimeTicks`. Previously this logic was part of getTimeTicks, but as
  33. * `getTimeTicks` now runs of segments in stock charts, the normalizing
  34. * logic was extracted in order to prevent it for running over again for
  35. * each segment having the same interval. #662, #697.
  36. * @private
  37. */
  38. /**
  39. * Get a normalized tick interval for dates. Returns a configuration object
  40. * with unit range (interval), count and name. Used to prepare data for
  41. * `getTimeTicks`. Previously this logic was part of getTimeTicks, but as
  42. * `getTimeTicks` now runs of segments in stock charts, the normalizing
  43. * logic was extracted in order to prevent it for running over again for
  44. * each segment having the same interval. #662, #697.
  45. * @private
  46. */
  47. DateTimeAxisAdditions.prototype.normalizeTimeTickInterval = function (tickInterval, unitsOption) {
  48. var units = unitsOption || [[
  49. 'millisecond',
  50. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  51. ], [
  52. 'second',
  53. [1, 2, 5, 10, 15, 30]
  54. ], [
  55. 'minute',
  56. [1, 2, 5, 10, 15, 30]
  57. ], [
  58. 'hour',
  59. [1, 2, 3, 4, 6, 8, 12]
  60. ], [
  61. 'day',
  62. [1, 2]
  63. ], [
  64. 'week',
  65. [1, 2]
  66. ], [
  67. 'month',
  68. [1, 2, 3, 4, 6]
  69. ], [
  70. 'year',
  71. null
  72. ]], unit = units[units.length - 1], // default unit is years
  73. interval = timeUnits[unit[0]], multiples = unit[1], count, i;
  74. // loop through the units to find the one that best fits the
  75. // tickInterval
  76. for (i = 0; i < units.length; i++) {
  77. unit = units[i];
  78. interval = timeUnits[unit[0]];
  79. multiples = unit[1];
  80. if (units[i + 1]) {
  81. // lessThan is in the middle between the highest multiple and
  82. // the next unit.
  83. var lessThan = (interval *
  84. multiples[multiples.length - 1] +
  85. timeUnits[units[i + 1][0]]) / 2;
  86. // break and keep the current unit
  87. if (tickInterval <= lessThan) {
  88. break;
  89. }
  90. }
  91. }
  92. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  93. if (interval === timeUnits.year && tickInterval < 5 * interval) {
  94. multiples = [1, 2, 5];
  95. }
  96. // get the count
  97. count = normalizeTickInterval(tickInterval / interval, multiples, unit[0] === 'year' ? // #1913, #2360
  98. Math.max(getMagnitude(tickInterval / interval), 1) :
  99. 1);
  100. return {
  101. unitRange: interval,
  102. count: count,
  103. unitName: unit[0]
  104. };
  105. };
  106. return DateTimeAxisAdditions;
  107. }());
  108. /**
  109. * Date and time support for axes.
  110. *
  111. * @private
  112. * @class
  113. */
  114. var DateTimeAxis = /** @class */ (function () {
  115. function DateTimeAxis() {
  116. }
  117. /* *
  118. *
  119. * Static Functions
  120. *
  121. * */
  122. /**
  123. * Extends axis class with date and time support.
  124. * @private
  125. */
  126. DateTimeAxis.compose = function (AxisClass) {
  127. AxisClass.keepProps.push('dateTime');
  128. var axisProto = AxisClass.prototype;
  129. /**
  130. * Set the tick positions to a time unit that makes sense, for example
  131. * on the first of each month or on every Monday. Return an array with
  132. * the time positions. Used in datetime axes as well as for grouping
  133. * data on a datetime axis.
  134. *
  135. * @private
  136. * @function Highcharts.Axis#getTimeTicks
  137. *
  138. * @param {Highcharts.TimeNormalizeObject} normalizedInterval
  139. * The interval in axis values (ms) and thecount.
  140. *
  141. * @param {number} min
  142. * The minimum in axis values.
  143. *
  144. * @param {number} max
  145. * The maximum in axis values.
  146. *
  147. * @param {number} startOfWeek
  148. *
  149. * @return {Highcharts.AxisTickPositionsArray}
  150. */
  151. axisProto.getTimeTicks = function () {
  152. return this.chart.time.getTimeTicks.apply(this.chart.time, arguments);
  153. };
  154. /* eslint-disable no-invalid-this */
  155. addEvent(AxisClass, 'init', function (e) {
  156. var axis = this;
  157. var options = e.userOptions;
  158. if (options.type !== 'datetime') {
  159. axis.dateTime = void 0;
  160. return;
  161. }
  162. if (!axis.dateTime) {
  163. axis.dateTime = new DateTimeAxisAdditions(axis);
  164. }
  165. });
  166. /* eslint-enable no-invalid-this */
  167. };
  168. /* *
  169. *
  170. * Static Properties
  171. *
  172. * */
  173. DateTimeAxis.AdditionsClass = DateTimeAxisAdditions;
  174. return DateTimeAxis;
  175. }());
  176. DateTimeAxis.compose(Axis);
  177. export default DateTimeAxis;