LogarithmicAxis.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 U from './Utilities.js';
  13. var pick = U.pick;
  14. var Axis = H.Axis, getMagnitude = H.getMagnitude, normalizeTickInterval = H.normalizeTickInterval;
  15. /* ************************************************************************** *
  16. * Methods defined on the Axis prototype
  17. * ************************************************************************** */
  18. /* eslint-disable valid-jsdoc */
  19. /**
  20. * Set the tick positions of a logarithmic axis.
  21. *
  22. * @private
  23. * @function Highcharts.Axis#getLogTickPositions
  24. * @param {number} interval
  25. * @param {number} min
  26. * @param {number} max
  27. * @param {boolean} [minor]
  28. * @return {Array<number>}
  29. */
  30. Axis.prototype.getLogTickPositions = function (interval, min, max, minor) {
  31. var axis = this, options = axis.options, axisLength = axis.len,
  32. // Since we use this method for both major and minor ticks,
  33. // use a local variable and return the result
  34. positions = [];
  35. // Reset
  36. if (!minor) {
  37. axis._minorAutoInterval = null;
  38. }
  39. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  40. if (interval >= 0.5) {
  41. interval = Math.round(interval);
  42. positions = axis.getLinearTickPositions(interval, min, max);
  43. // Second case: We need intermediary ticks. For example
  44. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  45. }
  46. else if (interval >= 0.08) {
  47. var roundedMin = Math.floor(min), intermediate, i, j, len, pos, lastPos, break2;
  48. if (interval > 0.3) {
  49. intermediate = [1, 2, 4];
  50. // 0.2 equals five minor ticks per 1, 10, 100 etc
  51. }
  52. else if (interval > 0.15) {
  53. intermediate = [1, 2, 4, 6, 8];
  54. }
  55. else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  56. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  57. }
  58. for (i = roundedMin; i < max + 1 && !break2; i++) {
  59. len = intermediate.length;
  60. for (j = 0; j < len && !break2; j++) {
  61. pos = axis.log2lin(axis.lin2log(i) * intermediate[j]);
  62. // #1670, lastPos is #3113
  63. if (pos > min &&
  64. (!minor || lastPos <= max) &&
  65. typeof lastPos !== 'undefined') {
  66. positions.push(lastPos);
  67. }
  68. if (lastPos > max) {
  69. break2 = true;
  70. }
  71. lastPos = pos;
  72. }
  73. }
  74. // Third case: We are so deep in between whole logarithmic values that
  75. // we might as well handle the tick positions like a linear axis. For
  76. // example 1.01, 1.02, 1.03, 1.04.
  77. }
  78. else {
  79. var realMin = axis.lin2log(min), realMax = axis.lin2log(max), tickIntervalOption = minor ?
  80. this.getMinorTickInterval() :
  81. options.tickInterval, filteredTickIntervalOption = tickIntervalOption === 'auto' ?
  82. null :
  83. tickIntervalOption, tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1), totalPixelLength = minor ?
  84. axisLength / axis.tickPositions.length :
  85. axisLength;
  86. interval = pick(filteredTickIntervalOption, axis._minorAutoInterval, (realMax - realMin) *
  87. tickPixelIntervalOption / (totalPixelLength || 1));
  88. interval = normalizeTickInterval(interval, null, getMagnitude(interval));
  89. positions = axis.getLinearTickPositions(interval, realMin, realMax).map(axis.log2lin);
  90. if (!minor) {
  91. axis._minorAutoInterval = interval / 5;
  92. }
  93. }
  94. // Set the axis-level tickInterval variable
  95. if (!minor) {
  96. axis.tickInterval = interval;
  97. }
  98. return positions;
  99. };
  100. /**
  101. * @private
  102. * @function Highcharts.Axis#log2lin
  103. *
  104. * @param {number} num
  105. *
  106. * @return {number}
  107. */
  108. Axis.prototype.log2lin = function (num) {
  109. return Math.log(num) / Math.LN10;
  110. };
  111. /**
  112. * @private
  113. * @function Highcharts.Axis#lin2log
  114. *
  115. * @param {number} num
  116. *
  117. * @return {number}
  118. */
  119. Axis.prototype.lin2log = function (num) {
  120. return Math.pow(10, num);
  121. };