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
  | import React, { useEffect, useState } from 'react'; 
 |  import ReactDOM from 'react-dom'; 
 |  import { data } from './data'; 
 |  import G6 from '@antv/g6'; 
 |  import "./index.css" 
 |    
 |  export default function() { 
 |    const ref = React.useRef(null); 
 |    let graph = null; 
 |    
 |    useEffect(() => { 
 |      if (!graph) { 
 |        graph = new G6.Graph({ 
 |          container: ReactDOM.findDOMNode(ref.current), 
 |          width: document.documentElement.clientWidth, 
 |          height: document.documentElement.clientHeight, 
 |          modes: { 
 |            default: [ 
 |              'drag-canvas',  
 |              'zoom-canvas', 
 |              'drag-node', 
 |            ], 
 |            edit: ['click-select'] 
 |          }, 
 |          layout: { 
 |            type: 'dagre', 
 |            direction: 'LR', 
 |          }, 
 |          defaultNode: { 
 |            shape: 'node', 
 |            type: 'rect', 
 |            labelCfg: { 
 |              style: { 
 |                fill: '#000000A6', 
 |                fontSize: 14, 
 |              }, 
 |            }, 
 |            style: { 
 |              // 仅在 keyShape 上生效 
 |              fill: 'lightblue', 
 |              stroke: '#888', 
 |              lineWidth: 1, 
 |              radius: 7, 
 |            }, 
 |            linkPoints: { 
 |              top: true, 
 |              bottom: true, 
 |              left: true, 
 |              right: true, 
 |              // ... 四个圆的样式可以在这里指定 
 |            }, 
 |          }, 
 |          defaultEdge: { 
 |            shape: 'polyline', 
 |          }, 
 |          nodeStateStyles: { 
 |            // 各状态下的样式,平铺的配置项仅在 keyShape 上生效。需要在其他 shape 样式上响应状态变化则写法不同,参见上文提到的 配置状态样式 链接 
 |            hover: { 
 |              fillOpacity: 0.1, 
 |              lineWidth: 1, 
 |            }, 
 |          }, 
 |        }); 
 |      } 
 |      graph.data(data); 
 |      graph.render(); 
 |       
 |      // 监听鼠标进入节点事件 
 |      graph.on('node:mouseenter', (evt) => { 
 |        const node = evt.item; 
 |        // 激活该节点的 hover 状态 
 |        graph.setItemState(node, 'hover', true); 
 |      }); 
 |    
 |      // 监听鼠标离开节点事件 
 |      graph.on('node:mouseleave', (evt) => { 
 |        const node = evt.item; 
 |        // 关闭该节点的 hover 状态 
 |        graph.setItemState(node, 'hover', false); 
 |      }); 
 |    
 |      graph.on('node:click', (evt) => { 
 |        const node = evt.item; 
 |        const model = node.getModel(); 
 |        // 在这里打开模态框或者编辑表格,传入model以便用于初始化编辑内容 
 |        showModal(model); 
 |      }); 
 |    }, []); 
 |    
 |    return <div ref={ref}></div>; 
 |  } 
 |  
  |