LogarithmicAxis.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, pick = U.pick;
  14. /* eslint-disable valid-jsdoc */
  15. /**
  16. * Provides logarithmic support for axes.
  17. *
  18. * @private
  19. * @class
  20. */
  21. var LogarithmicAxisAdditions = /** @class */ (function () {
  22. /* *
  23. *
  24. * Constructors
  25. *
  26. * */
  27. function LogarithmicAxisAdditions(axis) {
  28. this.axis = axis;
  29. }
  30. /* *
  31. *
  32. * Functions
  33. *
  34. * */
  35. /**
  36. * Set the tick positions of a logarithmic axis.
  37. */
  38. LogarithmicAxisAdditions.prototype.getLogTickPositions = function (interval, min, max, minor) {
  39. var log = this;
  40. var axis = log.axis;
  41. var axisLength = axis.len;
  42. var options = axis.options;
  43. // Since we use this method for both major and minor ticks,
  44. // use a local variable and return the result
  45. var positions = [];
  46. // Reset
  47. if (!minor) {
  48. log.minorAutoInterval = void 0;
  49. }
  50. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  51. if (interval >= 0.5) {
  52. interval = Math.round(interval);
  53. positions = axis.getLinearTickPositions(interval, min, max);
  54. // Second case: We need intermediary ticks. For example
  55. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  56. }
  57. else if (interval >= 0.08) {
  58. var roundedMin = Math.floor(min), intermediate, i, j, len, pos, lastPos, break2;
  59. if (interval > 0.3) {
  60. intermediate = [1, 2, 4];
  61. // 0.2 equals five minor ticks per 1, 10, 100 etc
  62. }
  63. else if (interval > 0.15) {
  64. intermediate = [1, 2, 4, 6, 8];
  65. }
  66. else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  67. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  68. }
  69. for (i = roundedMin; i < max + 1 && !break2; i++) {
  70. len = intermediate.length;
  71. for (j = 0; j < len && !break2; j++) {
  72. pos = log.log2lin(log.lin2log(i) * intermediate[j]);
  73. // #1670, lastPos is #3113
  74. if (pos > min &&
  75. (!minor || lastPos <= max) &&
  76. typeof lastPos !== 'undefined') {
  77. positions.push(lastPos);
  78. }
  79. if (lastPos > max) {
  80. break2 = true;
  81. }
  82. lastPos = pos;
  83. }
  84. }
  85. // Third case: We are so deep in between whole logarithmic values that
  86. // we might as well handle the tick positions like a linear axis. For
  87. // example 1.01, 1.02, 1.03, 1.04.
  88. }
  89. else {
  90. var realMin = log.lin2log(min), realMax = log.lin2log(max), tickIntervalOption = minor ?
  91. axis.getMinorTickInterval() :
  92. options.tickInterval, filteredTickIntervalOption = tickIntervalOption === 'auto' ?
  93. null :
  94. tickIntervalOption, tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1), totalPixelLength = minor ?
  95. axisLength / axis.tickPositions.length :
  96. axisLength;
  97. interval = pick(filteredTickIntervalOption, log.minorAutoInterval, (realMax - realMin) *
  98. tickPixelIntervalOption / (totalPixelLength || 1));
  99. interval = normalizeTickInterval(interval, void 0, getMagnitude(interval));
  100. positions = axis.getLinearTickPositions(interval, realMin, realMax).map(log.log2lin);
  101. if (!minor) {
  102. log.minorAutoInterval = interval / 5;
  103. }
  104. }
  105. // Set the axis-level tickInterval variable
  106. if (!minor) {
  107. axis.tickInterval = interval;
  108. }
  109. return positions;
  110. };
  111. LogarithmicAxisAdditions.prototype.lin2log = function (num) {
  112. return Math.pow(10, num);
  113. };
  114. LogarithmicAxisAdditions.prototype.log2lin = function (num) {
  115. return Math.log(num) / Math.LN10;
  116. };
  117. return LogarithmicAxisAdditions;
  118. }());
  119. var LogarithmicAxis = /** @class */ (function () {
  120. function LogarithmicAxis() {
  121. }
  122. /**
  123. * Provides logarithmic support for axes.
  124. *
  125. * @private
  126. */
  127. LogarithmicAxis.compose = function (AxisClass) {
  128. AxisClass.keepProps.push('logarithmic');
  129. // HC <= 8 backwards compatibility, allow wrapping
  130. // Axis.prototype.lin2log and log2lin
  131. // @todo Remove this in next major
  132. var axisProto = AxisClass.prototype;
  133. var logAxisProto = LogarithmicAxisAdditions.prototype;
  134. axisProto.log2lin = logAxisProto.log2lin;
  135. axisProto.lin2log = logAxisProto.lin2log;
  136. /* eslint-disable no-invalid-this */
  137. addEvent(AxisClass, 'init', function (e) {
  138. var axis = this;
  139. var options = e.userOptions;
  140. var logarithmic = axis.logarithmic;
  141. if (options.type !== 'logarithmic') {
  142. axis.logarithmic = void 0;
  143. }
  144. else {
  145. if (!logarithmic) {
  146. logarithmic = axis.logarithmic = new LogarithmicAxisAdditions(axis);
  147. }
  148. // HC <= 8 backwards compatibility, allow wrapping
  149. // Axis.prototype.lin2log and log2lin
  150. // @todo Remove this in next major
  151. if (axis.log2lin !== logarithmic.log2lin) {
  152. logarithmic.log2lin = axis.log2lin.bind(axis);
  153. }
  154. if (axis.lin2log !== logarithmic.lin2log) {
  155. logarithmic.lin2log = axis.lin2log.bind(axis);
  156. }
  157. }
  158. });
  159. addEvent(AxisClass, 'afterInit', function () {
  160. var axis = this;
  161. var log = axis.logarithmic;
  162. // extend logarithmic axis
  163. if (log) {
  164. axis.lin2val = function (num) {
  165. return log.lin2log(num);
  166. };
  167. axis.val2lin = function (num) {
  168. return log.log2lin(num);
  169. };
  170. }
  171. });
  172. };
  173. return LogarithmicAxis;
  174. }());
  175. LogarithmicAxis.compose(Axis); // @todo move to factory functions
  176. export default LogarithmicAxis;