/
circle.ts
184 lines (173 loc) · 4.69 KB
/
circle.ts
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
import GGroup from '@antv/g-canvas/lib/group';
import { IShape } from '@antv/g-canvas/lib/interfaces'
import deepMix from '@antv/util/lib/deep-mix';
import { Item, NodeConfig, ShapeStyle } from '@g6/types'
import Global from '../../global'
import Shape from '../shape'
// 带有图标的圆,可用于拓扑图中
Shape.registerNode('circle', {
// 自定义节点时的配置
options: {
size: 60,
style: {
x: 0,
y: 0,
stroke: Global.defaultShapeStrokeColor,
fill: Global.defaultShapeFillColor,
lineWidth: 1
},
labelCfg: {
style: {
fill: '#595959'
},
offset: 0
},
// 节点上左右上下四个方向上的链接circle配置
linkPoints: {
top: false,
right: false,
bottom: false,
left: false,
// circle的大小
size: 3,
lineWidth: 1,
fill: '#72CC4A',
stroke: '#72CC4A'
},
// 节点中icon配置
icon: {
// 是否显示icon,值为 false 则不渲染icon
show: false,
// icon的地址,字符串类型
img: 'https://gw.alipayobjects.com/zos/basement_prod/012bcf4f-423b-4922-8c24-32a89f8c41ce.svg',
width: 16,
height: 16
}
},
shapeType: 'circle',
// 文本位置
labelPosition: 'center',
drawShape(cfg: NodeConfig, group: GGroup): IShape {
const { icon: defaultIcon } = this.options;
const style = this.getShapeStyle(cfg);
const icon = deepMix({}, defaultIcon, cfg.icon);
const keyShape: IShape = group.addShape('circle', {
attrs: style,
draggable: true
});
const { width, height, show } = icon;
if (show) {
const image = group.addShape('image', {
attrs: {
x: -width / 2,
y: -height / 2,
...icon
},
className: 'circle-icon'
});
image.set('capture', false);
}
this.drawLinkPoints(cfg, group);
return keyShape;
},
/**
* 绘制节点上的LinkPoints
* @param {Object} cfg data数据配置项
* @param {Group} group Group实例
*/
drawLinkPoints(cfg: NodeConfig, group: GGroup) {
const { linkPoints: defaultLinkPoints } = this.options;
const linkPoints = deepMix({}, defaultLinkPoints, cfg.linkPoints);
const { top, left, right, bottom, size: markSize,
...markStyle } = linkPoints;
const size = this.getSize(cfg);
const r = size[0] / 2;
if (left) {
// left circle
group.addShape('circle', {
attrs: {
...markStyle,
x: -r,
y: 0,
r: markSize
},
className: 'link-point-left',
isAnchorPoint: true
});
}
if (right) {
// right circle
group.addShape('circle', {
attrs: {
...markStyle,
x: r,
y: 0,
r: markSize
},
className: 'link-point-right',
isAnchorPoint: true
});
}
if (top) {
// top circle
group.addShape('circle', {
attrs: {
...markStyle,
x: 0,
y: -r,
r: markSize
},
className: 'link-point-top',
isAnchorPoint: true
});
}
if (bottom) {
// bottom circle
group.addShape('circle', {
attrs: {
...markStyle,
x: 0,
y: r,
r: markSize
},
className: 'link-point-bottom',
isAnchorPoint: true
});
}
},
/**
* 获取节点的样式,供基于该节点自定义时使用
* @param {Object} cfg 节点数据模型
* @return {Object} 节点的样式
*/
getShapeStyle(cfg: NodeConfig): ShapeStyle {
const { style: defaultStyle } = this.options;
const strokeStyle = {
stroke: cfg.color
};
// 如果设置了color,则覆盖默认的stroke属性
const style = deepMix({}, defaultStyle, strokeStyle, cfg.style);
const size = this.getSize(cfg);
const r = size[0] / 2;
const styles = Object.assign({}, {
x: 0,
y: 0,
r
}, style);
return styles;
},
update(cfg: NodeConfig, item: Item) {
const group = item.getContainer();
const size = this.getSize(cfg);
// 下面这些属性需要覆盖默认样式与目前样式,但若在 cfg 中有指定则应该被 cfg 的相应配置覆盖。
const strokeStyle = {
stroke: cfg.color,
r: size[0] / 2
};
// 与 getShapeStyle 不同在于,update 时需要获取到当前的 style 进行融合。即新传入的配置项中没有涉及的属性,保留当前的配置。
const keyShape = item.get('keyShape');
const style = deepMix({}, keyShape.attr(), strokeStyle, cfg.style);
this.updateShape(cfg, item, style, true);
this.updateLinkPoints(cfg, group);
}
}, 'single-node');