NavigatorAxis.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 H from './Globals.js';
  12. var isTouchDevice = H.isTouchDevice;
  13. import U from './Utilities.js';
  14. var addEvent = U.addEvent, correctFloat = U.correctFloat, defined = U.defined, isNumber = U.isNumber, pick = U.pick;
  15. /* eslint-disable valid-jsdoc */
  16. /**
  17. * @private
  18. * @class
  19. */
  20. var NavigatorAxisAdditions = /** @class */ (function () {
  21. /* *
  22. *
  23. * Constructors
  24. *
  25. * */
  26. function NavigatorAxisAdditions(axis) {
  27. this.axis = axis;
  28. }
  29. /* *
  30. *
  31. * Functions
  32. *
  33. * */
  34. /**
  35. * @private
  36. */
  37. NavigatorAxisAdditions.prototype.destroy = function () {
  38. this.axis = void 0;
  39. };
  40. /**
  41. * Add logic to normalize the zoomed range in order to preserve the pressed
  42. * state of range selector buttons
  43. *
  44. * @private
  45. * @function Highcharts.Axis#toFixedRange
  46. * @param {number} [pxMin]
  47. * @param {number} [pxMax]
  48. * @param {number} [fixedMin]
  49. * @param {number} [fixedMax]
  50. * @return {*}
  51. */
  52. NavigatorAxisAdditions.prototype.toFixedRange = function (pxMin, pxMax, fixedMin, fixedMax) {
  53. var navigator = this;
  54. var axis = navigator.axis;
  55. var chart = axis.chart;
  56. var fixedRange = chart && chart.fixedRange, halfPointRange = (axis.pointRange || 0) / 2, newMin = pick(fixedMin, axis.translate(pxMin, true, !axis.horiz)), newMax = pick(fixedMax, axis.translate(pxMax, true, !axis.horiz)), changeRatio = fixedRange && (newMax - newMin) / fixedRange;
  57. // Add/remove half point range to/from the extremes (#1172)
  58. if (!defined(fixedMin)) {
  59. newMin = correctFloat(newMin + halfPointRange);
  60. }
  61. if (!defined(fixedMax)) {
  62. newMax = correctFloat(newMax - halfPointRange);
  63. }
  64. // If the difference between the fixed range and the actual requested
  65. // range is too great, the user is dragging across an ordinal gap, and
  66. // we need to release the range selector button.
  67. if (changeRatio > 0.7 && changeRatio < 1.3) {
  68. if (fixedMax) {
  69. newMin = newMax - fixedRange;
  70. }
  71. else {
  72. newMax = newMin + fixedRange;
  73. }
  74. }
  75. if (!isNumber(newMin) || !isNumber(newMax)) { // #1195, #7411
  76. newMin = newMax = void 0;
  77. }
  78. return {
  79. min: newMin,
  80. max: newMax
  81. };
  82. };
  83. return NavigatorAxisAdditions;
  84. }());
  85. /**
  86. * @private
  87. * @class
  88. */
  89. var NavigatorAxis = /** @class */ (function () {
  90. function NavigatorAxis() {
  91. }
  92. /* *
  93. *
  94. * Static Functions
  95. *
  96. * */
  97. /**
  98. * @private
  99. */
  100. NavigatorAxis.compose = function (AxisClass) {
  101. AxisClass.keepProps.push('navigatorAxis');
  102. /* eslint-disable no-invalid-this */
  103. addEvent(AxisClass, 'init', function () {
  104. var axis = this;
  105. if (!axis.navigatorAxis) {
  106. axis.navigatorAxis = new NavigatorAxisAdditions(axis);
  107. }
  108. });
  109. // For Stock charts, override selection zooming with some special
  110. // features because X axis zooming is already allowed by the Navigator
  111. // and Range selector.
  112. addEvent(AxisClass, 'zoom', function (e) {
  113. var axis = this;
  114. var chart = axis.chart;
  115. var chartOptions = chart.options;
  116. var navigator = chartOptions.navigator;
  117. var navigatorAxis = axis.navigatorAxis;
  118. var pinchType = chartOptions.chart.pinchType;
  119. var rangeSelector = chartOptions.rangeSelector;
  120. var zoomType = chartOptions.chart.zoomType;
  121. var previousZoom;
  122. if (axis.isXAxis && ((navigator && navigator.enabled) ||
  123. (rangeSelector && rangeSelector.enabled))) {
  124. // For y only zooming, ignore the X axis completely
  125. if (zoomType === 'y') {
  126. e.zoomed = false;
  127. // For xy zooming, record the state of the zoom before zoom
  128. // selection, then when the reset button is pressed, revert to
  129. // this state. This should apply only if the chart is
  130. // initialized with a range (#6612), otherwise zoom all the way
  131. // out.
  132. }
  133. else if (((!isTouchDevice && zoomType === 'xy') ||
  134. (isTouchDevice && pinchType === 'xy')) &&
  135. axis.options.range) {
  136. previousZoom = navigatorAxis.previousZoom;
  137. if (defined(e.newMin)) {
  138. navigatorAxis.previousZoom = [axis.min, axis.max];
  139. }
  140. else if (previousZoom) {
  141. e.newMin = previousZoom[0];
  142. e.newMax = previousZoom[1];
  143. navigatorAxis.previousZoom = void 0;
  144. }
  145. }
  146. }
  147. if (typeof e.zoomed !== 'undefined') {
  148. e.preventDefault();
  149. }
  150. });
  151. /* eslint-enable no-invalid-this */
  152. };
  153. /* *
  154. *
  155. * Static Properties
  156. *
  157. * */
  158. /**
  159. * @private
  160. */
  161. NavigatorAxis.AdditionsClass = NavigatorAxisAdditions;
  162. return NavigatorAxis;
  163. }());
  164. export default NavigatorAxis;