DerivedSeries.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. 'use strict';
  7. import H from '../Core/Globals.js';
  8. var noop = H.noop;
  9. import Series from '../Core/Series/Series.js';
  10. import U from '../Core/Utilities.js';
  11. var addEvent = U.addEvent, defined = U.defined;
  12. /* ************************************************************************** *
  13. *
  14. * DERIVED SERIES MIXIN
  15. *
  16. * ************************************************************************** */
  17. /**
  18. * Provides methods for auto setting/updating series data based on the based
  19. * series data.
  20. *
  21. * @private
  22. * @mixin derivedSeriesMixin
  23. */
  24. var derivedSeriesMixin = {
  25. hasDerivedData: true,
  26. /* eslint-disable valid-jsdoc */
  27. /**
  28. * Initialise series
  29. *
  30. * @private
  31. * @function derivedSeriesMixin.init
  32. * @return {void}
  33. */
  34. init: function () {
  35. Series.prototype.init.apply(this, arguments);
  36. this.initialised = false;
  37. this.baseSeries = null;
  38. this.eventRemovers = [];
  39. this.addEvents();
  40. },
  41. /**
  42. * Method to be implemented - inside the method the series has already
  43. * access to the base series via m `this.baseSeries` and the bases data is
  44. * initialised. It should return data in the format accepted by
  45. * `Series.setData()` method
  46. *
  47. * @private
  48. * @function derivedSeriesMixin.setDerivedData
  49. * @return {Array<Highcharts.PointOptionsType>}
  50. * An array of data
  51. */
  52. setDerivedData: noop,
  53. /**
  54. * Sets base series for the series
  55. *
  56. * @private
  57. * @function derivedSeriesMixin.setBaseSeries
  58. * @return {void}
  59. */
  60. setBaseSeries: function () {
  61. var chart = this.chart, baseSeriesOptions = this.options.baseSeries, baseSeries = (defined(baseSeriesOptions) &&
  62. (chart.series[baseSeriesOptions] ||
  63. chart.get(baseSeriesOptions)));
  64. this.baseSeries = baseSeries || null;
  65. },
  66. /**
  67. * Adds events for the series
  68. *
  69. * @private
  70. * @function derivedSeriesMixin.addEvents
  71. * @return {void}
  72. */
  73. addEvents: function () {
  74. var derivedSeries = this, chartSeriesLinked;
  75. chartSeriesLinked = addEvent(this.chart, 'afterLinkSeries', function () {
  76. derivedSeries.setBaseSeries();
  77. if (derivedSeries.baseSeries && !derivedSeries.initialised) {
  78. derivedSeries.setDerivedData();
  79. derivedSeries.addBaseSeriesEvents();
  80. derivedSeries.initialised = true;
  81. }
  82. });
  83. this.eventRemovers.push(chartSeriesLinked);
  84. },
  85. /**
  86. * Adds events to the base series - it required for recalculating the data
  87. * in the series if the base series is updated / removed / etc.
  88. *
  89. * @private
  90. * @function derivedSeriesMixin.addBaseSeriesEvents
  91. * @return {void}
  92. */
  93. addBaseSeriesEvents: function () {
  94. var derivedSeries = this, updatedDataRemover, destroyRemover;
  95. updatedDataRemover = addEvent(derivedSeries.baseSeries, 'updatedData', function () {
  96. derivedSeries.setDerivedData();
  97. });
  98. destroyRemover = addEvent(derivedSeries.baseSeries, 'destroy', function () {
  99. derivedSeries.baseSeries = null;
  100. derivedSeries.initialised = false;
  101. });
  102. derivedSeries.eventRemovers.push(updatedDataRemover, destroyRemover);
  103. },
  104. /**
  105. * Destroys the series
  106. *
  107. * @private
  108. * @function derivedSeriesMixin.destroy
  109. */
  110. destroy: function () {
  111. this.eventRemovers.forEach(function (remover) {
  112. remover();
  113. });
  114. Series.prototype.destroy.apply(this, arguments);
  115. }
  116. /* eslint-disable valid-jsdoc */
  117. };
  118. export default derivedSeriesMixin;