OnSeries.js 5.8 KB

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