ECharts graph 类型的图表绘制

由于最近项目中需要绘制树形图,其展示形式大致如下:

image-20210129143654597

在项目中,目前定义的图表全部使用的是React + echarts-next-for-react,根据查看Echarts Demo 查看基本的树类型,大致都只能绘制成如下:image-20210129144431878

经过查看文档发现类型为graph能够满足需求。

在这个类型的series中重点有三个参数nodes、links、categoris,其类型都是数组。

nodes主要是来显示各个点的名称、位置、id等其他信息

links主要用来连接各个点: { source: ‘0’, target: ‘1’ }[]

categories主要就是用来对点的分类了。

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
const basicSpace = 300;
const xAxisStart = -2000;
const yAxisStart = 400;
const symbolSize = 50;

export default {
nodes: [
{
id: '0',
name: null,
symbolSize: 0,
x: xAxisStart,
y: yAxisStart,
category: 0,
},
{
id: '1',
name: null,
symbol: `image://${imgURL}`,
symbolSize,
x: xAxisStart,
y: yAxisStart + basicSpace,
category: 0,
},
{
id: '2',
name: null,
symbolSize: 0,
x: xAxisStart,
y: yAxisStart + basicSpace * 2,
category: 0,
},

{
id: '3',
name: null,
symbol: `image://${imgURL}`,
symbolSize: 36,
x: xAxisStart + basicSpace,
y: yAxisStart,
category: 0,
},
{
id: '4',
name: null,
symbol: `image://${imgURL}`,
symbolSize: 36,
x: xAxisStart + basicSpace,
y: yAxisStart + basicSpace,
category: 0,
},
{
id: '5',
name: null,
symbol: `image://${imgURL}`,
symbolSize: 36,
x: xAxisStart + basicSpace,
y: yAxisStart + basicSpace * 2,
category: 0,
},

{
id: '6',
name: null,
symbolSize: 0,
x: xAxisStart + basicSpace * 2,
y: yAxisStart,
category: 0,
},
{
id: '7',
name: null,
symbolSize: 0,
x: xAxisStart + basicSpace * 2,
y: yAxisStart + basicSpace,
value: 4,
category: 0,
},
{
id: '8',
name: null,
symbolSize: 0,
x: xAxisStart + basicSpace * 2,
y: yAxisStart + basicSpace * 2,
category: 0,
},
{
id: '9',
name: 'Center',
symbol: `image://${imgURL}`,
symbolSize,
label: {
position: 'bottom',
},
x: xAxisStart + basicSpace * 3,
y: yAxisStart + basicSpace,
category: 0,
},
{
id: '10',
name: null,
symbolSize: 0,
x: xAxisStart + basicSpace * 4,
y: yAxisStart + basicSpace,
value: 4,
category: 0,
},

{
id: '11',
name: 'Node',
symbolSize: 0,
x: xAxisStart + basicSpace * 3,
y: yAxisStart + basicSpace * 6,
category: 0,
},
{
id: '12',
name: null,
symbolSize: 0,
x: xAxisStart + basicSpace * 4,
y: yAxisStart + basicSpace * 6,
category: 0,
},

{
id: '13',
name: 'Node 1',
symbolSize: 0,
x: xAxisStart + basicSpace * 3,
y: yAxisStart + basicSpace * 9,
category: 0,
},

{
id: '14',
name: null,
symbolSize: 0,
label: {
position: 'bottom',
},
x: xAxisStart + basicSpace * 4,
y: yAxisStart + basicSpace * 9,
category: 0,
},
],
links: [
{
source: '0',
target: '1',
},
{
source: '2',
target: '1',
},
{
source: '3',
target: '0',
},
{
source: '4',
target: '1',
},
{
source: '5',
target: '2',
},
{
source: '6',
target: '3',
},
{
source: '6',
target: '7',
},
{
source: '8',
target: '7',
},
{
source: '7',
target: '4',
},
{
source: '8',
target: '5',
},
{
source: '9',
target: '7',
},
{
source: '10',
target: '9',
},
{
source: '11',
target: '9',
},
{
source: '12',
target: '11',
},
{
source: '13',
target: '11',
},
{
source: '14',
target: '13',
},
],
categories: [
{
name: 'internet',
},
],
};

这个就是前面0~15个节点的基本关系,而后的是根据不同的数据来便利生成的,主要是规则就是,先生成空节点,并且link到目的点,然后再生成带图的点,再link到上一个点,如此循环,就可以大致满足需求了。

image-20210129145425028