-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Expand file tree
/
Copy pathLargeLineDraw.ts
More file actions
347 lines (299 loc) · 9.9 KB
/
Copy pathLargeLineDraw.ts
File metadata and controls
347 lines (299 loc) · 9.9 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* 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.
*/
// TODO Batch by color
import * as graphic from '../../util/graphic';
import * as lineContain from 'zrender/src/contain/line';
import * as quadraticContain from 'zrender/src/contain/quadratic';
import { PathProps } from 'zrender/src/graphic/Path';
import SeriesData from '../../data/SeriesData';
import { StageHandlerProgressParams, LineStyleOption, ColorString } from '../../util/types';
import Model from '../../model/Model';
import { getECData } from '../../util/innerStore';
import Element from 'zrender/src/Element';
import tokens from '../../visual/tokens';
class LargeLinesPathShape {
polyline = false;
curveness = 0;
segs: ArrayLike<number> = [];
}
interface LargeLinesPathProps extends PathProps {
shape?: Partial<LargeLinesPathShape>
}
interface LargeLinesCommonOption {
polyline?: boolean
lineStyle?: LineStyleOption & {
curveness?: number
}
}
/**
* Data which can support large lines.
*/
type LargeLinesData = SeriesData<Model<LargeLinesCommonOption> & {
seriesIndex?: number
}>;
class LargeLinesPath extends graphic.Path {
shape: LargeLinesPathShape;
__startIndex: number;
private _off: number = 0;
hoverDataIdx: number = -1;
notClear: boolean;
constructor(opts?: LargeLinesPathProps) {
super(opts);
}
reset() {
this.notClear = false;
this._off = 0;
}
getDefaultStyle() {
return {
stroke: tokens.color.neutral99,
fill: null as ColorString
};
}
getDefaultShape() {
return new LargeLinesPathShape();
}
buildPath(ctx: CanvasRenderingContext2D, shape: LargeLinesPathShape) {
const segs = shape.segs;
const curveness = shape.curveness;
let i;
if (shape.polyline) {
for (i = this._off; i < segs.length;) {
const count = segs[i++];
if (count > 0) {
ctx.moveTo(segs[i++], segs[i++]);
for (let k = 1; k < count; k++) {
ctx.lineTo(segs[i++], segs[i++]);
}
}
}
}
else {
for (i = this._off; i < segs.length;) {
const x0 = segs[i++];
const y0 = segs[i++];
const x1 = segs[i++];
const y1 = segs[i++];
ctx.moveTo(x0, y0);
if (curveness > 0) {
const x2 = (x0 + x1) / 2 - (y0 - y1) * curveness;
const y2 = (y0 + y1) / 2 - (x1 - x0) * curveness;
ctx.quadraticCurveTo(x2, y2, x1, y1);
}
else {
ctx.lineTo(x1, y1);
}
}
}
if (this.incremental) {
this._off = i;
this.notClear = true;
}
}
findDataIndex(x: number, y: number) {
const shape = this.shape;
const segs = shape.segs;
const curveness = shape.curveness;
const lineWidth = this.style.lineWidth;
if (shape.polyline) {
let dataIndex = 0;
for (let i = 0; i < segs.length;) {
const count = segs[i++];
if (count > 0) {
const x0 = segs[i++];
const y0 = segs[i++];
for (let k = 1; k < count; k++) {
const x1 = segs[i++];
const y1 = segs[i++];
if (lineContain.containStroke(x0, y0, x1, y1, lineWidth, x, y)) {
return dataIndex;
}
}
}
dataIndex++;
}
}
else {
let dataIndex = 0;
for (let i = 0; i < segs.length;) {
const x0 = segs[i++];
const y0 = segs[i++];
const x1 = segs[i++];
const y1 = segs[i++];
if (curveness > 0) {
const x2 = (x0 + x1) / 2 - (y0 - y1) * curveness;
const y2 = (y0 + y1) / 2 - (x1 - x0) * curveness;
if (quadraticContain.containStroke(
x0, y0, x2, y2, x1, y1, lineWidth, x, y
)) {
return dataIndex;
}
}
else {
if (lineContain.containStroke(
x0, y0, x1, y1, lineWidth, x, y
)) {
return dataIndex;
}
}
dataIndex++;
}
}
return -1;
}
contain(x: number, y: number): boolean {
const localPos = this.transformCoordToLocal(x, y);
const rect = this.getBoundingRect();
x = localPos[0];
y = localPos[1];
if (rect.contain(x, y)) {
// Cache found data index.
const dataIdx = this.hoverDataIdx = this.findDataIndex(x, y);
return dataIdx >= 0;
}
this.hoverDataIdx = -1;
return false;
}
getBoundingRect() {
// Ignore stroke for large symbol draw.
let rect = this._rect;
if (!rect) {
const shape = this.shape;
const points = shape.segs;
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (let i = 0; i < points.length;) {
const x = points[i++];
const y = points[i++];
minX = Math.min(x, minX);
maxX = Math.max(x, maxX);
minY = Math.min(y, minY);
maxY = Math.max(y, maxY);
}
rect = this._rect = new graphic.BoundingRect(minX, minY, maxX, maxY);
}
return rect;
}
}
class LargeLineDraw {
group = new graphic.Group();
private _newAdded: LargeLinesPath[];
/**
* Update symbols draw by new data
*/
updateData(data: LargeLinesData) {
this._clear();
const lineEl = this._create();
lineEl.setShape({
segs: data.getLayout('linesPoints')
});
this._setCommon(lineEl, data);
};
/**
* @override
*/
incrementalPrepareUpdate(data: LargeLinesData) {
this.group.removeAll();
this._clear();
};
/**
* @override
*/
incrementalUpdate(taskParams: StageHandlerProgressParams, data: LargeLinesData) {
const lastAdded = this._newAdded[0];
const linePoints = data.getLayout('linesPoints');
const oldSegs = lastAdded && lastAdded.shape.segs;
// Merging the exists. Each element has 1e4 points.
// Consider the performance balance between too much elements and too much points in one shape(may affect hover optimization)
if (oldSegs && oldSegs.length < 2e4) {
const oldLen = oldSegs.length;
const newSegs = new Float32Array(oldLen + linePoints.length);
// Concat two array
newSegs.set(oldSegs);
newSegs.set(linePoints, oldLen);
lastAdded.setShape({
segs: newSegs
});
}
else {
// Clear
this._newAdded = [];
const lineEl = this._create();
lineEl.incremental = true;
lineEl.setShape({
segs: linePoints
});
this._setCommon(lineEl, data);
lineEl.__startIndex = taskParams.start;
}
}
/**
* @override
*/
remove() {
this._clear();
}
eachRendered(cb: (el: Element) => boolean | void) {
this._newAdded[0] && cb(this._newAdded[0]);
}
private _create() {
const lineEl = new LargeLinesPath({
cursor: 'default',
ignoreCoarsePointer: true
});
this._newAdded.push(lineEl);
this.group.add(lineEl);
return lineEl;
}
private _setCommon(lineEl: LargeLinesPath, data: LargeLinesData, isIncremental?: boolean) {
const hostModel = data.hostModel;
lineEl.setShape({
polyline: hostModel.get('polyline'),
curveness: hostModel.get(['lineStyle', 'curveness'])
});
lineEl.useStyle(
hostModel.getModel('lineStyle').getLineStyle()
);
lineEl.style.strokeNoScale = true;
const style = data.getVisual('style');
if (style && style.stroke) {
lineEl.setStyle('stroke', style.stroke);
}
lineEl.setStyle('fill', null);
const ecData = getECData(lineEl);
// Enable tooltip
// PENDING May have performance issue when path is extremely large
ecData.seriesIndex = hostModel.seriesIndex;
lineEl.on('mousemove', function (e) {
ecData.dataIndex = null;
const dataIndex = lineEl.hoverDataIdx;
if (dataIndex > 0) {
// Provide dataIndex for tooltip
ecData.dataIndex = dataIndex + lineEl.__startIndex;
}
});
};
private _clear() {
this._newAdded = [];
this.group.removeAll();
};
}
export default LargeLineDraw;