-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Expand file tree
/
Copy pathChordEdge.ts
More file actions
215 lines (188 loc) · 7.42 KB
/
ChordEdge.ts
File metadata and controls
215 lines (188 loc) · 7.42 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
/*
* 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.
*/
import type { PathProps, PathStyleProps } from 'zrender/src/graphic/Path';
import type PathProxy from 'zrender/src/core/PathProxy';
import { extend, isString } from 'zrender/src/core/util';
import * as graphic from '../../util/graphic';
import SeriesData from '../../data/SeriesData';
import { GraphEdge } from '../../data/Graph';
import type Model from '../../model/Model';
import { getSectorCornerRadius } from '../helper/sectorHelper';
import { saveOldStyle } from '../../animation/basicTransition';
import ChordSeriesModel, { ChordEdgeItemOption, ChordEdgeLineStyleOption, ChordNodeItemOption } from './ChordSeries';
import { setStatesStylesFromModel, toggleHoverEmphasis } from '../../util/states';
import { getECData } from '../../util/innerStore';
export class ChordPathShape {
// Souce node, two points forming an arc
s1: [number, number] = [0, 0];
s2: [number, number] = [0, 0];
sStartAngle: number = 0;
sEndAngle: number = 0;
// Target node, two points forming an arc
t1: [number, number] = [0, 0];
t2: [number, number] = [0, 0];
tStartAngle: number = 0;
tEndAngle: number = 0;
cx: number = 0;
cy: number = 0;
// series.r0 of ChordSeries
r: number = 0;
value: number = 0;
clockwise: boolean = true;
}
interface ChordEdgePathProps extends PathProps {
shape?: Partial<ChordPathShape>
}
export class ChordEdge extends graphic.Path<ChordEdgePathProps> {
shape: ChordPathShape;
constructor(
nodeData: SeriesData<ChordSeriesModel>,
edgeData: SeriesData,
edgeIdx: number,
startAngle: number
) {
super();
getECData(this).dataType = 'edge';
this.updateData(nodeData, edgeData, edgeIdx, startAngle, true);
}
buildPath(ctx: PathProxy | CanvasRenderingContext2D, shape: ChordPathShape): void {
if (shape.value <= 0) {
return;
}
// Start from n11
ctx.moveTo(shape.s1[0], shape.s1[1]);
const ratio = 0.7;
const clockwise = shape.clockwise;
// Draw the arc from n11 to n12
ctx.arc(shape.cx, shape.cy, shape.r, shape.sStartAngle, shape.sEndAngle, !clockwise);
// Bezier curve to cp1 and then to n21
ctx.bezierCurveTo(
(shape.cx - shape.s2[0]) * ratio + shape.s2[0],
(shape.cy - shape.s2[1]) * ratio + shape.s2[1],
(shape.cx - shape.t1[0]) * ratio + shape.t1[0],
(shape.cy - shape.t1[1]) * ratio + shape.t1[1],
shape.t1[0],
shape.t1[1]
);
// Draw the arc from n21 to n22
ctx.arc(shape.cx, shape.cy, shape.r, shape.tStartAngle, shape.tEndAngle, !clockwise);
// Bezier curve back to cp2 and then to n11
ctx.bezierCurveTo(
(shape.cx - shape.t2[0]) * ratio + shape.t2[0],
(shape.cy - shape.t2[1]) * ratio + shape.t2[1],
(shape.cx - shape.s1[0]) * ratio + shape.s1[0],
(shape.cy - shape.s1[1]) * ratio + shape.s1[1],
shape.s1[0],
shape.s1[1]
);
ctx.closePath();
}
updateData(
nodeData: SeriesData<ChordSeriesModel>,
edgeData: SeriesData,
edgeIdx: number,
startAngle: number,
firstCreate?: boolean
): void {
const seriesModel = nodeData.hostModel as ChordSeriesModel;
const edge = edgeData.graph.getEdgeByIndex(edgeIdx);
const layout = edge.getLayout();
const itemModel = edge.node1.getModel<ChordNodeItemOption>();
const edgeModel = edgeData.getItemModel<ChordEdgeItemOption>(edge.dataIndex);
const lineStyle = edgeModel.getModel('lineStyle');
const emphasisModel = edgeModel.getModel('emphasis');
const focus = emphasisModel.get('focus');
const shape: ChordPathShape = extend(
getSectorCornerRadius(itemModel.getModel('itemStyle'), layout, true),
layout
);
const el = this;
// Ignore NaN data.
if (isNaN(shape.sStartAngle) || isNaN(shape.tStartAngle)) {
// Use NaN shape to avoid drawing shape.
el.setShape(shape);
return;
}
if (firstCreate) {
el.setShape(shape);
applyEdgeFill(el, edge, nodeData, lineStyle);
}
else {
saveOldStyle(el);
applyEdgeFill(el, edge, nodeData, lineStyle);
graphic.updateProps(el, {
shape: shape
}, seriesModel, edgeIdx);
}
toggleHoverEmphasis(
this,
focus === 'adjacency'
? edge.getAdjacentDataIndices()
: focus,
emphasisModel.get('blurScope'),
emphasisModel.get('disabled')
);
setStatesStylesFromModel(el, edgeModel, 'lineStyle');
edgeData.setItemGraphicEl(edge.dataIndex, el);
}
}
function applyEdgeFill(
edgeShape: ChordEdge,
edge: GraphEdge,
nodeData: SeriesData<ChordSeriesModel>,
lineStyleModel: Model<ChordEdgeLineStyleOption>
) {
const node1 = edge.node1;
const node2 = edge.node2;
const edgeStyle = edgeShape.style as PathStyleProps;
edgeShape.setStyle(lineStyleModel.getLineStyle());
const color = lineStyleModel.get('color');
switch (color) {
case 'source':
// TODO: use visual and node1.getVisual('color');
edgeStyle.fill = nodeData.getItemVisual(node1.dataIndex, 'style').fill;
edgeStyle.decal = node1.getVisual('style').decal;
break;
case 'target':
edgeStyle.fill = nodeData.getItemVisual(node2.dataIndex, 'style').fill;
edgeStyle.decal = node2.getVisual('style').decal;
break;
case 'gradient':
const sourceColor = nodeData.getItemVisual(node1.dataIndex, 'style').fill;
const targetColor = nodeData.getItemVisual(node2.dataIndex, 'style').fill;
if (isString(sourceColor) && isString(targetColor)) {
// Gradient direction is perpendicular to the mid-angles
// of source and target nodes.
const shape = edgeShape.shape;
const sMidX = (shape.s1[0] + shape.s2[0]) / 2;
const sMidY = (shape.s1[1] + shape.s2[1]) / 2;
const tMidX = (shape.t1[0] + shape.t2[0]) / 2;
const tMidY = (shape.t1[1] + shape.t2[1]) / 2;
edgeStyle.fill = new graphic.LinearGradient(
sMidX, sMidY, tMidX, tMidY,
[
{ offset: 0, color: sourceColor },
{ offset: 1, color: targetColor }
],
true
);
}
break;
}
}