ScrollbarAxis.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. import H from '../Globals.js';
  11. import U from '../Utilities.js';
  12. var addEvent = U.addEvent, defined = U.defined, pick = U.pick;
  13. /* eslint-disable no-invalid-this, valid-jsdoc */
  14. /**
  15. * Creates scrollbars if enabled.
  16. *
  17. * @private
  18. */
  19. var ScrollbarAxis = /** @class */ (function () {
  20. function ScrollbarAxis() {
  21. }
  22. /**
  23. * Attaches to axis events to create scrollbars if enabled.
  24. *
  25. * @private
  26. *
  27. * @param AxisClass
  28. * Axis class to extend.
  29. *
  30. * @param ScrollbarClass
  31. * Scrollbar class to use.
  32. */
  33. ScrollbarAxis.compose = function (AxisClass, ScrollbarClass) {
  34. // Wrap axis initialization and create scrollbar if enabled:
  35. addEvent(AxisClass, 'afterInit', function () {
  36. var axis = this;
  37. if (axis.options &&
  38. axis.options.scrollbar &&
  39. axis.options.scrollbar.enabled) {
  40. // Predefined options:
  41. axis.options.scrollbar.vertical = !axis.horiz;
  42. axis.options.startOnTick = axis.options.endOnTick = false;
  43. axis.scrollbar = new ScrollbarClass(axis.chart.renderer, axis.options.scrollbar, axis.chart);
  44. addEvent(axis.scrollbar, 'changed', function (e) {
  45. var axisMin = pick(axis.options && axis.options.min, axis.min), axisMax = pick(axis.options && axis.options.max, axis.max), unitedMin = defined(axis.dataMin) ?
  46. Math.min(axisMin, axis.min, axis.dataMin) : axisMin, unitedMax = defined(axis.dataMax) ?
  47. Math.max(axisMax, axis.max, axis.dataMax) : axisMax, range = unitedMax - unitedMin, to, from;
  48. // #12834, scroll when show/hide series, wrong extremes
  49. if (!defined(axisMin) || !defined(axisMax)) {
  50. return;
  51. }
  52. if ((axis.horiz && !axis.reversed) ||
  53. (!axis.horiz && axis.reversed)) {
  54. to = unitedMin + range * this.to;
  55. from = unitedMin + range * this.from;
  56. }
  57. else {
  58. // y-values in browser are reversed, but this also
  59. // applies for reversed horizontal axis:
  60. to = unitedMin + range * (1 - this.from);
  61. from = unitedMin + range * (1 - this.to);
  62. }
  63. if (pick(this.options.liveRedraw, H.svg && !H.isTouchDevice && !this.chart.isBoosting) ||
  64. // Mouseup always should change extremes
  65. e.DOMType === 'mouseup' ||
  66. // Internal events
  67. !defined(e.DOMType)) {
  68. axis.setExtremes(from, to, true, e.DOMType !== 'mousemove', e);
  69. }
  70. else {
  71. // When live redraw is disabled, don't change extremes
  72. // Only change the position of the scollbar thumb
  73. this.setRange(this.from, this.to);
  74. }
  75. });
  76. }
  77. });
  78. // Wrap rendering axis, and update scrollbar if one is created:
  79. addEvent(AxisClass, 'afterRender', function () {
  80. var axis = this, scrollMin = Math.min(pick(axis.options.min, axis.min), axis.min, pick(axis.dataMin, axis.min) // #6930
  81. ), scrollMax = Math.max(pick(axis.options.max, axis.max), axis.max, pick(axis.dataMax, axis.max) // #6930
  82. ), scrollbar = axis.scrollbar, offset = axis.axisTitleMargin + (axis.titleOffset || 0), scrollbarsOffsets = axis.chart.scrollbarsOffsets, axisMargin = axis.options.margin || 0, offsetsIndex, from, to;
  83. if (scrollbar) {
  84. if (axis.horiz) {
  85. // Reserve space for labels/title
  86. if (!axis.opposite) {
  87. scrollbarsOffsets[1] += offset;
  88. }
  89. scrollbar.position(axis.left, axis.top + axis.height + 2 + scrollbarsOffsets[1] -
  90. (axis.opposite ? axisMargin : 0), axis.width, axis.height);
  91. // Next scrollbar should reserve space for margin (if set)
  92. if (!axis.opposite) {
  93. scrollbarsOffsets[1] += axisMargin;
  94. }
  95. offsetsIndex = 1;
  96. }
  97. else {
  98. // Reserve space for labels/title
  99. if (axis.opposite) {
  100. scrollbarsOffsets[0] += offset;
  101. }
  102. scrollbar.position(axis.left + axis.width + 2 + scrollbarsOffsets[0] -
  103. (axis.opposite ? 0 : axisMargin), axis.top, axis.width, axis.height);
  104. // Next scrollbar should reserve space for margin (if set)
  105. if (axis.opposite) {
  106. scrollbarsOffsets[0] += axisMargin;
  107. }
  108. offsetsIndex = 0;
  109. }
  110. scrollbarsOffsets[offsetsIndex] += scrollbar.size +
  111. scrollbar.options.margin;
  112. if (isNaN(scrollMin) ||
  113. isNaN(scrollMax) ||
  114. !defined(axis.min) ||
  115. !defined(axis.max) ||
  116. axis.min === axis.max // #10733
  117. ) {
  118. // default action: when extremes are the same or there is
  119. // not extremes on the axis, but scrollbar exists, make it
  120. // full size
  121. scrollbar.setRange(0, 1);
  122. }
  123. else {
  124. from =
  125. (axis.min - scrollMin) / (scrollMax - scrollMin);
  126. to =
  127. (axis.max - scrollMin) / (scrollMax - scrollMin);
  128. if ((axis.horiz && !axis.reversed) ||
  129. (!axis.horiz && axis.reversed)) {
  130. scrollbar.setRange(from, to);
  131. }
  132. else {
  133. // inverse vertical axis
  134. scrollbar.setRange(1 - to, 1 - from);
  135. }
  136. }
  137. }
  138. });
  139. // Make space for a scrollbar:
  140. addEvent(AxisClass, 'afterGetOffset', function () {
  141. var axis = this, index = axis.horiz ? 2 : 1, scrollbar = axis.scrollbar;
  142. if (scrollbar) {
  143. axis.chart.scrollbarsOffsets = [0, 0]; // reset scrollbars offsets
  144. axis.chart.axisOffset[index] +=
  145. scrollbar.size + scrollbar.options.margin;
  146. }
  147. });
  148. };
  149. return ScrollbarAxis;
  150. }());
  151. export default ScrollbarAxis;