boost-utils.js
7.53 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* *
*
* Copyright (c) 2019-2019 Highsoft AS
*
* Boost module: stripped-down renderer for higher performance
*
* License: highcharts.com/license
*
* This files contains generic utility functions used by the boost module.
*
* */
'use strict';
import H from '../../parts/Globals.js';
import '../../parts/Series.js';
import boostableMap from './boostable-map.js';
import createAndAttachRenderer from './boost-attach.js';
var win = H.win,
doc = win.document,
pick = H.pick;
// This should be a const.
var CHUNK_SIZE = 3000;
/**
* Tolerant max() function.
*
* @private
* @function patientMax
*
* @return {number}
* max value
*/
function patientMax() {
var args = Array.prototype.slice.call(arguments),
r = -Number.MAX_VALUE;
args.forEach(function (t) {
if (
typeof t !== 'undefined' &&
t !== null &&
typeof t.length !== 'undefined'
) {
// r = r < t.length ? t.length : r;
if (t.length > 0) {
r = t.length;
return true;
}
}
});
return r;
}
/**
* Return true if ths boost.enabled option is true
*
* @private
* @function boostEnabled
*
* @param {Highcharts.Chart} chart
* The chart
*
* @return {boolean}
*/
function boostEnabled(chart) {
return pick(
(
chart &&
chart.options &&
chart.options.boost &&
chart.options.boost.enabled
),
true
);
}
/**
* Returns true if we should force boosting the chart
* @private
* @function shouldForceChartSeriesBoosting
*
* @param {Highcharts.Chart} chart
* The chart to check for forcing on
*
* @return {boolean}
*/
function shouldForceChartSeriesBoosting(chart) {
// If there are more than five series currently boosting,
// we should boost the whole chart to avoid running out of webgl contexts.
var sboostCount = 0,
canBoostCount = 0,
allowBoostForce = pick(
chart.options.boost && chart.options.boost.allowForce,
true
),
series;
if (typeof chart.boostForceChartBoost !== 'undefined') {
return chart.boostForceChartBoost;
}
if (chart.series.length > 1) {
for (var i = 0; i < chart.series.length; i++) {
series = chart.series[i];
// Don't count series with boostThreshold set to 0
// See #8950
// Also don't count if the series is hidden.
// See #9046
if (series.options.boostThreshold === 0 ||
series.visible === false) {
continue;
}
// Don't count heatmap series as they are handled differently.
// In the future we should make the heatmap/treemap path compatible
// with forcing. See #9636.
if (series.type === 'heatmap') {
continue;
}
if (boostableMap[series.type]) {
++canBoostCount;
}
if (patientMax(
series.processedXData,
series.options.data,
// series.xData,
series.points
) >= (series.options.boostThreshold || Number.MAX_VALUE)) {
++sboostCount;
}
}
}
chart.boostForceChartBoost = allowBoostForce && (
(
canBoostCount === chart.series.length &&
sboostCount > 0
) ||
sboostCount > 5
);
return chart.boostForceChartBoost;
}
/*
* Performs the actual render if the renderer is
* attached to the series.
* @param renderer {OGLRenderer} - the renderer
* @param series {Highcharts.Series} - the series
*/
function renderIfNotSeriesBoosting(renderer, series, chart) {
if (renderer &&
series.renderTarget &&
series.canvas &&
!(chart || series.chart).isChartSeriesBoosting()
) {
renderer.render(chart || series.chart);
}
}
function allocateIfNotSeriesBoosting(renderer, series) {
if (renderer &&
series.renderTarget &&
series.canvas &&
!series.chart.isChartSeriesBoosting()
) {
renderer.allocateBufferForSingleSeries(series);
}
}
/**
* An "async" foreach loop. Uses a setTimeout to keep the loop from blocking the
* UI thread.
*
* @private
*
* @param arr {Array} - the array to loop through
* @param fn {Function} - the callback to call for each item
* @param finalFunc {Function} - the callback to call when done
* @param chunkSize {Number} - the number of iterations per timeout
* @param i {Number} - the current index
* @param noTimeout {Boolean} - set to true to skip timeouts
*/
function eachAsync(arr, fn, finalFunc, chunkSize, i, noTimeout) {
i = i || 0;
chunkSize = chunkSize || CHUNK_SIZE;
var threshold = i + chunkSize,
proceed = true;
while (proceed && i < threshold && i < arr.length) {
proceed = fn(arr[i], i);
++i;
}
if (proceed) {
if (i < arr.length) {
if (noTimeout) {
eachAsync(arr, fn, finalFunc, chunkSize, i, noTimeout);
} else if (win.requestAnimationFrame) {
// If available, do requestAnimationFrame - shaves off a few ms
win.requestAnimationFrame(function () {
eachAsync(arr, fn, finalFunc, chunkSize, i);
});
} else {
setTimeout(function () {
eachAsync(arr, fn, finalFunc, chunkSize, i);
});
}
} else if (finalFunc) {
finalFunc();
}
}
}
/**
* Returns true if the current browser supports webgl
*
* @private
* @function hasWebGLSupport
*
* @return {boolean}
*/
function hasWebGLSupport() {
var i = 0,
canvas,
contexts = ['webgl', 'experimental-webgl', 'moz-webgl', 'webkit-3d'],
context = false;
if (typeof win.WebGLRenderingContext !== 'undefined') {
canvas = doc.createElement('canvas');
for (; i < contexts.length; i++) {
try {
context = canvas.getContext(contexts[i]);
if (typeof context !== 'undefined' && context !== null) {
return true;
}
} catch (e) {
}
}
}
return false;
}
/**
* Used for treemap|heatmap.drawPoints
*
* @private
* @function pointDrawHandler
*
* @param {Function} proceed
*
* @return {*}
*/
function pointDrawHandler(proceed) {
var enabled = true,
renderer;
if (this.chart.options && this.chart.options.boost) {
enabled = typeof this.chart.options.boost.enabled === 'undefined' ?
true :
this.chart.options.boost.enabled;
}
if (!enabled || !this.isSeriesBoosting) {
return proceed.call(this);
}
this.chart.isBoosting = true;
// Make sure we have a valid OGL context
renderer = createAndAttachRenderer(this.chart, this);
if (renderer) {
allocateIfNotSeriesBoosting(renderer, this);
renderer.pushSeries(this);
}
renderIfNotSeriesBoosting(renderer, this);
}
var funs = {
patientMax: patientMax,
boostEnabled: boostEnabled,
shouldForceChartSeriesBoosting: shouldForceChartSeriesBoosting,
renderIfNotSeriesBoosting: renderIfNotSeriesBoosting,
allocateIfNotSeriesBoosting: allocateIfNotSeriesBoosting,
eachAsync: eachAsync,
hasWebGLSupport: hasWebGLSupport,
pointDrawHandler: pointDrawHandler
};
// This needs to be fixed.
H.hasWebGLSupport = hasWebGLSupport;
export default funs;