wujingjing
2024-07-16 b8dc9eeb53ce361f3142ea8de2c46244ecd9d5d6
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
/**
 * @param {string} shapeType 支持 rect/circel/path 等内置节点
 * @param {string} getShapeStyle 用于覆盖 base-node 默认样式
 * @param {string} icon 图片 url
 * @param {string} labelCfg 文本节点样式
 */
import G6, { ModelConfig } from '@antv/g6';
import defaultStyles from './defaultStyles';
 
const { iconStyles, nodeStyles, anchorPointStyles, nodeLabelStyles } = defaultStyles;
 
function getStyle(options, cfg) {
    return {
        ...cfg,
        // 自定义默认样式
        ...nodeStyles,
        ...options,
        // 当前节点样式
        ...cfg.style,
        // 文本配置
        labelCfg: {
            ...nodeLabelStyles,
            ...cfg.labelCfg,
            ...options.labelCfg,
            style: {
                ...nodeLabelStyles.style,
                ...(cfg.labelCfg ? cfg.labelCfg.style : {}),
            },
        },
        // 图标样式
        iconStyles: {
            ...iconStyles,
            ...cfg.iconStyles,
        },
        // 锚点样式
        anchorPointStyles: {
            ...anchorPointStyles,
            ...cfg.anchorPointStyles,
        },
        ...cfg.nodeStateStyles,
        // 锚点高亮样式
        anchorHotsoptStyles: cfg.anchorHotsoptStyles,
    };
}
 
export default (g6: typeof G6) => {
    // 从 base-node 中扩展方形节点
    g6.registerNode(
        'rect-node',
        {
            shapeType: 'rect',
            // 当前节点的样式集合
            getShapeStyle(cfg) {
                const width = cfg.style.width || 80;
                const height = cfg.style.height || 40;
 
                return getStyle.call(
                    this,
                    {
                        width,
                        height,
                        radius: 5,
                        // 将图形中心坐标移动到图形中心, 用于方便鼠标位置计算
                        x: -width / 2,
                        y: -height / 2,
                        percent: {
                            label: '80%',
                            labelCfg: {
                                style: {
                                    ...nodeLabelStyles.style,
                                    x: width / 6,
                                    y: -height / 6,
                                    textBaseline: 'bottom',
                                    textAlign: 'left',
                                    fill: 'green',
                                },
                            },
                        },
                    },
                    cfg
                );
            },
        },
        'base-node'
    );
 
    // 扩展圆形节点
    g6.registerNode(
        'circle-node',
        {
            shapeType: 'circle',
            getShapeStyle(cfg) {
                const r = cfg.style.r || 30;
 
                return getStyle.call(
                    this,
                    {
                        r, // 半径
                        // 将图形中心坐标移动到图形中心, 用于方便鼠标位置计算
                        x: 0,
                        y: 0,
                    },
                    cfg
                );
            },
        },
        'base-node'
    );
 
    // 扩展椭圆形
    g6.registerNode(
        'ellipse-node',
        {
            shapeType: 'ellipse',
            getShapeStyle(cfg) {
                return getStyle.call(
                    this,
                    {
                        rx: 50,
                        ry: 30,
                        // 将图形中心坐标移动到图形中心, 用于方便鼠标位置计算
                        x: 0,
                        y: 0,
                    },
                    cfg
                );
            },
        },
        'base-node'
    );
 
    // 扩展模态节点
    g6.registerNode(
        'modelRect-node',
        {
            shapeType: 'rect',
            getShapeStyle(cfg) {
                const width = cfg.style.width || 200;
                const height = cfg.style.height || 80;
 
                return getStyle.call(
                    this,
                    {
                        width,
                        height,
                        radius: 5,
                        // 将图形中心坐标移动到图形中心, 用于方便鼠标位置计算
                        x: -width / 2,
                        y: -height / 2,
                    },
                    cfg
                );
            },
        },
        'base-node'
    );
 
    // 扩展菱形
    g6.registerNode(
        'diamond-node',
        {
            shapeType: 'path', // 非内置 shape 要指定为path
            getShapeStyle(cfg) {
                const path = this.getPath(cfg);
 
                return getStyle.call(
                    this,
                    {
                        path,
                        x: 0,
                        y: 0,
                    },
                    cfg
                );
            },
            // 返回菱形的路径
            getPath(cfg) {
                const size = cfg.style.size || [100, 100]; // 如果没有 size 时的默认大小
                const width = size[0];
                const height = size[1];
 
                //  / 1 \
                // 4     2
                //  \ 3 /
                return [
                    ['M', 0, -height / 2], // 上部顶点
                    ['L', width / 2, 0], // 右侧顶点
                    ['L', 0, height / 2], // 下部顶点
                    ['L', -width / 2, 0], // 左侧顶点
                    ['Z'], // 封闭
                ];
            },
        },
        'base-node'
    );
 
    // 扩展or
    g6.registerNode(
        'or-node',
        {
            shapeType: 'path', // 非内置 shape 要指定为path
            getShapeStyle(cfg: ModelConfig) {
                const size = cfg.style.size || [40, 70]; // 如果没有 size 时的默认大小
                const height = size[1];
                const path = this.getPath(cfg);
 
                return getStyle.call(
                    this,
                    {
                        path,
                        x: 0,
                        y: 0,
                        label: '或',
                        labelCfg: {
                            y: (3 * height) / 7,
                        },
                    },
                    cfg
                );
            },
            getPath(cfg) {
                const size = cfg.style.size || [40, 70]; // 如果没有 size 时的默认大小
                const width = size[0];
                const height = size[1];
 
                return [
                    ['M', -width / 2, (2 * height) / 3], // 左下部顶点
                    ['Q', 0, -height / 2, width / 2, (2 * height) / 3], // 上部顶点+右下部顶点
                    ['Q', 0, height / 2, -width / 2, (2 * height) / 3], // 下部顶点+右下部顶点
                    ['Z'], // 封闭
                ];
            },
        },
        'base-node'
    );
 
    // 扩展 and
    g6.registerNode(
        'and-node',
        {
            shapeType: 'path', // 非内置 shape 要指定为path
            getShapeStyle(cfg) {
                const size = cfg.style.size || [40, 70]; // 如果没有 size 时的默认大小
                const height = size[1];
                const path = this.getPath(cfg);
 
                return getStyle.call(
                    this,
                    {
                        path,
                        x: 0,
                        y: 0,
                        label: '与',
                        labelCfg: {
                            y: (3 * height) / 7,
                        },
                    },
                    cfg
                );
            },
            getPath(cfg) {
                const size = cfg.style.size || [40, 70]; // 如果没有 size 时的默认大小
                const width = size[0];
                const height = size[1];
 
                return [
                    ['M', -width / 2, (2 * height) / 3], // 左下部顶点
                    ['Q', 0, -height / 2, width / 2, (2 * height) / 3], // 上部顶点+右下部顶点
                    ['Q', 0, height / 2, -width / 2, (2 * height) / 3], // 下部顶点+右下部顶点
                    ['Z'], // 封闭
                ];
            },
        },
        'base-node'
    );
 
    // 扩展三角形节点
    g6.registerNode(
        'triangle-node',
        {
            shapeType: 'path',
            getShapeStyle(cfg) {
                const path = this.getPath(cfg);
 
                return getStyle.call(
                    this,
                    {
                        direction: cfg.direction || 'up',
                        path,
                        x: 0,
                        y: 0,
                    },
                    cfg
                );
            },
            getPath(cfg) {
                const direction = cfg.direction || 'up';
                const size = cfg.style.size || [60, 100]; // 如果没有 size 时的默认大小
                const width = size[0];
                const height = size[1];
 
                const path = [
                    // ['M', 0, -height / 2], // 上部顶点
                    // ['L', -width / 2, 0], // 左侧顶点
                    // ['L', width / 2, 0], // 右侧顶点
                    ['Z'], // 封闭
                ] as any[];
 
                if (direction === 'up') {
                    path.unshift(
                        ['M', 0, -height / 2],
                        ['L', -width / 2, 0], // 左侧顶点
                        ['L', width / 2, 0] // 右侧顶点
                    );
                } else {
                    path.unshift(
                        ['M', 0, height / 2],
                        ['L', -width / 2, 0], // 左侧顶点
                        ['L', width / 2, 0] // 右侧顶点
                    );
                }
 
                return path;
            },
            getAnchorPoints(cfg) {
                return (
                    cfg.anchorPoints || [
                        [0.5, 0],
                        [0, 1],
                        [1, 1],
                    ]
                );
            },
        },
        'base-node'
    );
};