使用 Iconfont
简介
为什么使用 iconfont? 兼容性好、种类多、多色等。在此不做过多介绍,请直接移步阿里巴巴-iconfont平台。
效果

下载字体图标
直接去到阿里巴巴字体图标库搜索下载即可,简要操作流程是:搜索图标(例如篮球) -> 选择自己喜欢的图标添加入库 -> 点击页面右上角的购物车可以看到我们加入的图标 -> 添加至项目,如果没有项目到话可以新建一个 -> 在我到项目里面点击下载至本地 -> 解压。如果一切操作正常的话可以得到如下解压文件:
选中红色区域的所有文件(这里面很多文件是不需要的,为了方便起见,我们全部复制即可,不需要的也不会被打包),复制到项目里面,一般放在目录 'static/icons' 或者 'assets/icons' 下面,如果没有的话可以新建目录,当然你也可以放到任意你喜欢的位置,只要引入的时候路径对即可,到此 iconfont 引入完毕。
PS: 本案文件目录为 '/static/icons'
引入G6
多种引入方式,请移步快速上手。PS: 本案例简单粗暴,通过CDN的方式引入。
1. <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.g6-3.2.0/build/g6.js"></script>
添加字体图标
引入方式可自行选择,下面为在 HTML 中引入的例子:
1. <style>
2. @import "/static/icons/iconfont.css";
3. </style>
使用字体
1. G6.registerNode('iconfont', {
2. draw(cfg, group) {
3. const {
4. backgroundConfig: backgroundStyle,
5. style,
6. labelCfg: labelStyle,
7. } = cfg;
9. if (backgroundStyle) {
10. group.addShape('circle', {
11. attrs: {
12. x: 0,
13. y: 0,
14. r: cfg.size,
15. ...backgroundStyle,
16. },
17. });
18. }
20. const keyShape = group.addShape('text', {
21. attrs: {
22. x: 0,
23. y: 0,
24. fontFamily: 'iconfont', // 对应css里面的font-family: "iconfont";
25. textAlign: 'center',
26. textBaseline: 'middle',
27. text: cfg.text,
28. fontSize: cfg.size,
29. ...style,
30. },
31. });
32. const labelY = backgroundStyle ? cfg.size * 2 : cfg.size;
34. group.addShape('text', {
35. attrs: {
36. x: 0,
37. y: labelY,
38. textAlign: 'center',
39. text: cfg.label,
40. ...labelStyle.style,
41. },
42. });
43. return keyShape;
44. },
45. });
47. const COLOR = '#40a9ff';
48. const graph = new G6.TreeGraph({
49. container: 'mountNode',
50. width: 800,
51. height: 600,
52. modes: {
53. default: ['collapse-expand', 'drag-canvas', 'drag-node'],
54. },
55. defaultNode: {
56. backgroundConfig: {
57. backgroundType: 'circle',
58. fill: COLOR,
59. stroke: 'LightSkyBlue',
60. },
61. shape: 'iconfont',
62. size: 12,
63. style: {
64. fill: '#fff',
65. },
66. labelCfg: {
67. style: {
68. fill: COLOR,
69. fontSize: 12,
70. },
71. },
72. },
73. // 布局相关
74. layout: {
75. type: 'compactBox',
76. direction: 'LR',
77. getId(d) {
78. return d.id;
79. },
80. getHeight() {
81. return 16;
82. },
83. getWidth() {
84. return 16;
85. },
86. getVGap() {
87. return 20;
88. },
89. getHGap() {
90. return 60;
91. },
92. },
93. });
95. graph.edge(({ target }) => {
96. const fill =
97. target.get('model').backgroundConfig &&
98. target.get('model').backgroundConfig.fill;
99. return {
100. shape: 'cubic-horizontal',
101. color: fill || COLOR,
102. label: target.get('model').relation || '',
103. labelCfg: {
104. style: {
105. fill: fill || COLOR,
106. fontSize: 12,
107. },
108. },
109. };
110. });
112. const data = {
113. isRoot: true,
114. id: 'Root',
115. label: '可疑人员王**',
116. text: '', // 对应字体图标的Unicode码,
117. style: {
118. fill: 'red',
119. },
120. labelCfg: {
121. style: {
122. fill: 'red',
123. },
124. },
125. backgroundConfig: null, // 自定义项,用于判读是否需要圆背景
126. size: 30,
127. children: [
128. {
129. id: 'SubTreeNode1',
130. label: '**网咖',
131. text: '',
132. relation: '上网',
133. children: [
134. {
135. id: 'SubTreeNode2',
136. label: '多伦多',
137. text: '',
138. },
139. {
140. id: 'id1',
141. label: '小王',
142. text: '',
143. children: [
144. {
145. id: 'SubTreeNode1.2.1',
146. label: '182****2123',
147. text: '',
148. },
149. {
150. id: 'SubTreeNode4',
151. label: '今晚在吗',
152. text: '',
153. },
154. ],
155. },
156. ],
157. },
158. {
159. id: 'SubTreeNode3',
160. label: 'subway',
161. text: '',
162. children: [
163. {
164. id: 'SubTreeNode3.1',
165. label: '王五',
166. text: '',
167. },
168. {
169. id: 'SubTreeNode3.2',
170. label: '张三',
171. text: '',
172. },
173. ],
174. },
175. {
176. id: 'SubTreeNode5',
177. label: '小花',
178. relation: '老婆',
179. text: '',
180. backgroundConfig: {
181. fill: 'Coral',
182. },
183. style: {
184. fill: '#fff',
185. },
186. labelCfg: {
187. style: {
188. fill: 'Coral',
189. },
190. },
191. children: [
192. {
193. id: 'SubTreeNode1.2.1',
194. label: '182****2123',
195. text: '',
196. relation: '通话',
197. backgroundConfig: {
198. fill: 'Coral',
199. },
200. style: {
201. fill: '#fff',
202. },
203. labelCfg: {
204. style: {
205. fill: 'Coral',
206. },
207. },
208. },
209. {
210. id: 'SubTreeNode3.3',
211. label: '凶器',
212. text: '',
213. relation: '指纹',
214. backgroundConfig: {
215. fill: 'Coral',
216. },
217. style: {
218. fill: '#fff',
219. },
220. labelCfg: {
221. style: {
222. fill: 'Coral',
223. },
224. },
225. },
226. ],
227. },
228. {
229. id: 'SubTreeNode6',
230. label: '马航37*',
231. relation: '乘坐',
232. text: '',
233. },
234. ],
235. };
237. graph.data(data);
238. graph.render();
注意事项
看了代码大家应该很清楚了,实质就是用了 text 图形,但有几个需要注意的地方:1、text的fontFamily必须和iconfont.css里面的font-family保持一致:
2、data 里面的 text 使用的是 Unicode,需要自行复制。


