LargeSymbolDraw.js
7.91 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var graphic = require("../../util/graphic");
var _symbol = require("../../util/symbol");
var createSymbol = _symbol.createSymbol;
var IncrementalDisplayable = require("zrender/lib/graphic/IncrementalDisplayable");
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* global Float32Array */
// TODO Batch by color
var BOOST_SIZE_THRESHOLD = 4;
var LargeSymbolPath = graphic.extendShape({
shape: {
points: null
},
symbolProxy: null,
buildPath: function (path, shape) {
var points = shape.points;
var size = shape.size;
var symbolProxy = this.symbolProxy;
var symbolProxyShape = symbolProxy.shape;
var ctx = path.getContext ? path.getContext() : path;
var canBoost = ctx && size[0] < BOOST_SIZE_THRESHOLD; // Do draw in afterBrush.
if (canBoost) {
return;
}
for (var i = 0; i < points.length;) {
var x = points[i++];
var y = points[i++];
if (isNaN(x) || isNaN(y)) {
continue;
}
symbolProxyShape.x = x - size[0] / 2;
symbolProxyShape.y = y - size[1] / 2;
symbolProxyShape.width = size[0];
symbolProxyShape.height = size[1];
symbolProxy.buildPath(path, symbolProxyShape, true);
}
},
afterBrush: function (ctx) {
var shape = this.shape;
var points = shape.points;
var size = shape.size;
var canBoost = size[0] < BOOST_SIZE_THRESHOLD;
if (!canBoost) {
return;
}
this.setTransform(ctx); // PENDING If style or other canvas status changed?
for (var i = 0; i < points.length;) {
var x = points[i++];
var y = points[i++];
if (isNaN(x) || isNaN(y)) {
continue;
} // fillRect is faster than building a rect path and draw.
// And it support light globalCompositeOperation.
ctx.fillRect(x - size[0] / 2, y - size[1] / 2, size[0], size[1]);
}
this.restoreTransform(ctx);
},
findDataIndex: function (x, y) {
// TODO ???
// Consider transform
var shape = this.shape;
var points = shape.points;
var size = shape.size;
var w = Math.max(size[0], 4);
var h = Math.max(size[1], 4); // Not consider transform
// Treat each element as a rect
// top down traverse
for (var idx = points.length / 2 - 1; idx >= 0; idx--) {
var i = idx * 2;
var x0 = points[i] - w / 2;
var y0 = points[i + 1] - h / 2;
if (x >= x0 && y >= y0 && x <= x0 + w && y <= y0 + h) {
return idx;
}
}
return -1;
}
});
function LargeSymbolDraw() {
this.group = new graphic.Group();
}
var largeSymbolProto = LargeSymbolDraw.prototype;
largeSymbolProto.isPersistent = function () {
return !this._incremental;
};
/**
* Update symbols draw by new data
* @param {module:echarts/data/List} data
*/
largeSymbolProto.updateData = function (data) {
this.group.removeAll();
var symbolEl = new LargeSymbolPath({
rectHover: true,
cursor: 'default'
});
symbolEl.setShape({
points: data.getLayout('symbolPoints')
});
this._setCommon(symbolEl, data);
this.group.add(symbolEl);
this._incremental = null;
};
largeSymbolProto.updateLayout = function (data) {
if (this._incremental) {
return;
}
var points = data.getLayout('symbolPoints');
this.group.eachChild(function (child) {
if (child.startIndex != null) {
var len = (child.endIndex - child.startIndex) * 2;
var byteOffset = child.startIndex * 4 * 2;
points = new Float32Array(points.buffer, byteOffset, len);
}
child.setShape('points', points);
});
};
largeSymbolProto.incrementalPrepareUpdate = function (data) {
this.group.removeAll();
this._clearIncremental(); // Only use incremental displayables when data amount is larger than 2 million.
// PENDING Incremental data?
if (data.count() > 2e6) {
if (!this._incremental) {
this._incremental = new IncrementalDisplayable({
silent: true
});
}
this.group.add(this._incremental);
} else {
this._incremental = null;
}
};
largeSymbolProto.incrementalUpdate = function (taskParams, data) {
var symbolEl;
if (this._incremental) {
symbolEl = new LargeSymbolPath();
this._incremental.addDisplayable(symbolEl, true);
} else {
symbolEl = new LargeSymbolPath({
rectHover: true,
cursor: 'default',
startIndex: taskParams.start,
endIndex: taskParams.end
});
symbolEl.incremental = true;
this.group.add(symbolEl);
}
symbolEl.setShape({
points: data.getLayout('symbolPoints')
});
this._setCommon(symbolEl, data, !!this._incremental);
};
largeSymbolProto._setCommon = function (symbolEl, data, isIncremental) {
var hostModel = data.hostModel; // TODO
// if (data.hasItemVisual.symbolSize) {
// // TODO typed array?
// symbolEl.setShape('sizes', data.mapArray(
// function (idx) {
// var size = data.getItemVisual(idx, 'symbolSize');
// return (size instanceof Array) ? size : [size, size];
// }
// ));
// }
// else {
var size = data.getVisual('symbolSize');
symbolEl.setShape('size', size instanceof Array ? size : [size, size]); // }
// Create symbolProxy to build path for each data
symbolEl.symbolProxy = createSymbol(data.getVisual('symbol'), 0, 0, 0, 0); // Use symbolProxy setColor method
symbolEl.setColor = symbolEl.symbolProxy.setColor;
var extrudeShadow = symbolEl.shape.size[0] < BOOST_SIZE_THRESHOLD;
symbolEl.useStyle( // Draw shadow when doing fillRect is extremely slow.
hostModel.getModel('itemStyle').getItemStyle(extrudeShadow ? ['color', 'shadowBlur', 'shadowColor'] : ['color']));
var visualColor = data.getVisual('color');
if (visualColor) {
symbolEl.setColor(visualColor);
}
if (!isIncremental) {
// Enable tooltip
// PENDING May have performance issue when path is extremely large
symbolEl.seriesIndex = hostModel.seriesIndex;
symbolEl.on('mousemove', function (e) {
symbolEl.dataIndex = null;
var dataIndex = symbolEl.findDataIndex(e.offsetX, e.offsetY);
if (dataIndex >= 0) {
// Provide dataIndex for tooltip
symbolEl.dataIndex = dataIndex + (symbolEl.startIndex || 0);
}
});
}
};
largeSymbolProto.remove = function () {
this._clearIncremental();
this._incremental = null;
this.group.removeAll();
};
largeSymbolProto._clearIncremental = function () {
var incremental = this._incremental;
if (incremental) {
incremental.clearDisplaybles();
}
};
var _default = LargeSymbolDraw;
module.exports = _default;