OnSeries.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* *
  2. *
  3. * (c) 2010-2021 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 ColumnSeries from '../Series/Column/ColumnSeries.js';
  12. var columnProto = ColumnSeries.prototype;
  13. import Series from '../Core/Series/Series.js';
  14. var seriesProto = Series.prototype;
  15. import U from '../Core/Utilities.js';
  16. var defined = U.defined, stableSort = U.stableSort;
  17. /**
  18. * @private
  19. * @mixin onSeriesMixin
  20. */
  21. var onSeriesMixin = {
  22. /* eslint-disable valid-jsdoc */
  23. /**
  24. * Override getPlotBox. If the onSeries option is valid, return the plot box
  25. * of the onSeries, otherwise proceed as usual.
  26. *
  27. * @private
  28. * @function onSeriesMixin.getPlotBox
  29. * @return {Highcharts.SeriesPlotBoxObject}
  30. */
  31. getPlotBox: function () {
  32. return seriesProto.getPlotBox.call((this.options.onSeries &&
  33. this.chart.get(this.options.onSeries)) || this);
  34. },
  35. /**
  36. * Extend the translate method by placing the point on the related series
  37. *
  38. * @private
  39. * @function onSeriesMixin.translate
  40. * @return {void}
  41. */
  42. translate: function () {
  43. columnProto.translate.apply(this);
  44. var series = this, options = series.options, chart = series.chart, points = series.points, cursor = points.length - 1, point, lastPoint, optionsOnSeries = options.onSeries, onSeries = (optionsOnSeries &&
  45. chart.get(optionsOnSeries)), onKey = options.onKey || 'y', step = onSeries && onSeries.options.step, onData = (onSeries && onSeries.points), i = onData && onData.length, inverted = chart.inverted, xAxis = series.xAxis, yAxis = series.yAxis, xOffset = 0, leftPoint, lastX, rightPoint, currentDataGrouping, distanceRatio;
  46. // relate to a master series
  47. if (onSeries && onSeries.visible && i) {
  48. xOffset = (onSeries.pointXOffset || 0) + (onSeries.barW || 0) / 2;
  49. currentDataGrouping = onSeries.currentDataGrouping;
  50. lastX = (onData[i - 1].x +
  51. (currentDataGrouping ? currentDataGrouping.totalRange : 0)); // #2374
  52. // sort the data points
  53. stableSort(points, function (a, b) {
  54. return (a.x - b.x);
  55. });
  56. onKey = 'plot' + onKey[0].toUpperCase() + onKey.substr(1);
  57. while (i-- && points[cursor]) {
  58. leftPoint = onData[i];
  59. point = points[cursor];
  60. point.y = leftPoint.y;
  61. if (leftPoint.x <= point.x &&
  62. typeof leftPoint[onKey] !== 'undefined') {
  63. if (point.x <= lastX) { // #803
  64. point.plotY = leftPoint[onKey];
  65. // interpolate between points, #666
  66. if (leftPoint.x < point.x &&
  67. !step) {
  68. rightPoint = onData[i + 1];
  69. if (rightPoint &&
  70. typeof rightPoint[onKey] !== 'undefined') {
  71. // the distance ratio, between 0 and 1
  72. distanceRatio =
  73. (point.x - leftPoint.x) /
  74. (rightPoint.x - leftPoint.x);
  75. point.plotY +=
  76. distanceRatio *
  77. // the plotY distance
  78. (rightPoint[onKey] - leftPoint[onKey]);
  79. point.y +=
  80. distanceRatio *
  81. (rightPoint.y - leftPoint.y);
  82. }
  83. }
  84. }
  85. cursor--;
  86. i++; // check again for points in the same x position
  87. if (cursor < 0) {
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. // Add plotY position and handle stacking
  94. points.forEach(function (point, i) {
  95. var stackIndex;
  96. point.plotX += xOffset; // #2049
  97. // Undefined plotY means the point is either on axis, outside series
  98. // range or hidden series. If the series is outside the range of the
  99. // x axis it should fall through with an undefined plotY, but then
  100. // we must remove the shapeArgs (#847). For inverted charts, we need
  101. // to calculate position anyway, because series.invertGroups is not
  102. // defined
  103. if (typeof point.plotY === 'undefined' || inverted) {
  104. if (point.plotX >= 0 &&
  105. point.plotX <= xAxis.len) {
  106. // We're inside xAxis range
  107. if (inverted) {
  108. point.plotY = xAxis.translate(point.x, 0, 1, 0, 1);
  109. point.plotX = defined(point.y) ?
  110. yAxis.translate(point.y, 0, 0, 0, 1) :
  111. 0;
  112. }
  113. else {
  114. point.plotY = (xAxis.opposite ? 0 : series.yAxis.len) +
  115. xAxis.offset; // For the windbarb demo
  116. }
  117. }
  118. else {
  119. point.shapeArgs = {}; // 847
  120. }
  121. }
  122. // if multiple flags appear at the same x, order them into a stack
  123. lastPoint = points[i - 1];
  124. if (lastPoint && lastPoint.plotX === point.plotX) {
  125. if (typeof lastPoint.stackIndex === 'undefined') {
  126. lastPoint.stackIndex = 0;
  127. }
  128. stackIndex = lastPoint.stackIndex + 1;
  129. }
  130. point.stackIndex = stackIndex; // #3639
  131. });
  132. this.onSeries = onSeries;
  133. }
  134. /* eslint-enable valid-jsdoc */
  135. };
  136. export default onSeriesMixin;