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