#
luxiaotao1123
2024-01-25 f72591f60b2428db2b1b0f384f8c2df005fba538
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
import React, { useEffect, useRef } from 'react';
import { Graph, Extensions, extend } from '@antv/g6';
 
const ExtGraph = extend(Graph, {
    transforms: { 'transform-v4-data': Extensions.TransformV4Data },
    edges: {
        'cubic-horizontal-edge': Extensions.CubicHorizontalEdge,
        'cubic-vertical-edge': Extensions.CubicVerticalEdge,
    },
    behaviors: {
        'activate-relations': Extensions.ActivateRelations,
    },
});
 
const G6 = () => {
    const containerRef = useRef(null);
    const graphRef = useRef(null);
 
    useEffect(() => {
        const container = containerRef.current;
        const width = container.scrollWidth;
        const height = container.scrollHeight || 500;
 
        const graph = new ExtGraph({
            container,
            width,
            height,
            transforms: [
                {
                    type: 'transform-v4-data',
                    activeLifecycle: ['read'],
                },
            ],
            modes: {
                default: ['drag-canvas', 'zoom-canvas', 'drag-node', 'collapse-expand-tree', 'click-select'],
            },
            theme: {
                type: 'spec',
                specification: {
                    node: {
                        dataTypeField: 'cluster',
                    },
                },
            },
            node: (model) => {
                return {
                    id: model.id,
                    data: {
                        ...model.data,
                        type: 'rect-node',
                        // lodLevels: [],
                        keyShape: {
                            width: 50,
                            height: 20,
                        },
                        labelShape: {
                            text: model.id,
                            position: 'bottom',
                            maxWidth: '120%',
                            lod: Math.floor(Math.random() * 5 - 3),
                            fontSize: 8,
                        },
                        labelBackgroundShape: {},
                        anchorPoints:
                            model.data.layoutDirection === 'TB'
                                ? [
                                    [0.5, 0],
                                    [0.5, 1],
                                ]
                                : [
                                    [0, 0.5],
                                    [1, 0.5],
                                ],
                        animates: {
                            update: [
                                {
                                    fields: ['x', 'y'],
                                    duration: 500,
                                    shapeId: 'group',
                                    order: 0,
                                },
                                {
                                    fields: ['opacity'],
                                    duration: 500,
                                    shapeId: 'keyShape',
                                    order: 1,
                                },
                                {
                                    fields: ['opacity'],
                                    states: ['active', 'selected'],
                                    duration: 500,
                                    shapeId: 'haloShape',
                                },
                            ],
                        },
                    },
                };
            },
            edge: {
                type: 'cubic-horizontal-edge',
                keyShape: {
                    opacity: 0.5,
                    endArrow: true,
                },
            },
            layout: {
                type: 'compactBox',
                getHeight() {
                    return 20;
                },
                getWidth() {
                    return 50;
                },
                getVGap() {
                    return 10;
                },
                getHGap() {
                    return 30;
                },
            },
            edgeState: {
                active: {
                    lineWidth: 3,
                },
            },
        });
 
        graphRef.current = graph;
 
        // 加载数据
        fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/algorithm-category.json')
            .then((res) => res.json())
            .then((data) => {
                graph.once('afterlayout', () => {
                    graph.fitCenter();
                });
                graph.read({
                    type: 'treeData',
                    value: data,
                });
            });
 
        // 返回一个清理函数
        return () => {
            if (graphRef.current) {
                graphRef.current.destroy();
                graphRef.current = null;
            }
        };
 
    }, []);
 
    return <div ref={containerRef} style={{ width: '100%', height: '1000px' }} />;
};
 
export default G6;