arc
G6 内置了圆弧 arc 边,其默认样式如下。
使用方法
如 内置边 一节所示,配置边的方式有两种:实例化图时全局配置,在数据中动态配置。
1 实例化图时全局配置
用户在实例化 Graph 时候可以通过 defaultEdge 指定 shape 为 'arc',即可使用 arc 边。
1. const graph = new G6.Graph({
2. container: 'mountNode',
3. width: 800,
4. height: 600,
5. defaultEdge: {
6. shape: 'arc',
7. // 其他配置
8. },
9. });
2 在数据中动态配置
如果需要使不同节点有不同的配置,可以将配置写入到节点数据中。这种配置方式可以通过下面代码的形式直接写入数据,也可以通过遍历数据的方式写入。
1. const data = {
2. nodes: [
3. ... // 节点
4. ],
5. edges: [{
6. source: 'node0',
7. target: 'node1'
8. shape: 'arc',
9. ... // 其他配置
10. style: {
11. ... // 样式属性,每种边的详细样式属性参见各边文档
12. }
13. },
14. ... // 其他边
15. ]
16. }
配置项说明
arc 边支持以下的配置项,其中 curveOffset 属性是 arc 特有的属性,它控制了圆弧的大小以及弯曲的方向。
1. color: '#87e8de',
2. curveOffset: 20, // 圆弧顶端距离两线中心位置的距离
3. style: {
4. lineWidth: 2,
5. stroke: '#87e8de'
6. },
7. label: '边的标签文字',
8. labelCfg: {
9. refX: 10, // 文本在 x 方向偏移量
10. refY: 10, // 文本在 y 方向偏移量
11. style: {
12. fill: '#595959'
13. }
14. }
| 名称 | 含义 | 类型 | 备注 |
|---|---|---|---|
| color | 边的颜色 | String | 优先级低于 style 中的 stroke |
| curveOffset | 圆弧顶端距离两线中心位置的距离 | Number | 数值绝对值大小控制圆弧的大小,正负控制圆弧弯曲的方向,默认为 20。arc 边特有 |
| style | 边的样式 | Object | Canvas支持的属性 |
| label | 标签文本文字 | String | |
| labelCfg | 文件配置项 | Object |
特殊属性:弧度 curveOffset
curveOffset 属性是 arc 特有的属性,它控制了圆弧的大小以及弯曲的方向。下面代码演示在实例化图时全局配置方法中配置 curveOffset。
1. const data = {
2. nodes: [
3. {
4. id: 'node0',
5. x: 100,
6. y: 100,
7. size: 20,
8. },
9. {
10. id: 'node1',
11. x: 200,
12. y: 200,
13. size: 20,
14. },
15. ],
16. edges: [
17. {
18. source: 'node0',
19. target: 'node1',
20. shape: 'arc',
21. label: 'arc',
22. },
23. ],
24. };
25. const graph = new G6.Graph({
26. container: 'mountNode',
27. width: 800,
28. height: 600,
29. linkCenter: true,
30. defaultEdge: {
31. // shape: 'arc', // 在数据中已经指定 shape,这里无需再次指定
32. curveOffset: -80,
33. },
34. });
35. graph.data(data);
36. graph.render();
注意:上面代码使用了 graph 的配置项 linkCenter: true 以设置 arc 边连入节点的中心,保证美观性。
样式属性 style
Object 类型。配置项与边的通用样式属性相同,见 内置边。基于上面 弧度 curveOffset 中的代码,下面代码在 defaultNode 中增加了 style 配置项进行边的样式的配置,使之达到如下图效果。!
1. const data = {
2. // ... data 内容
3. };
4. const graph = new G6.Graph({
5. // ... 图的其他配置
6. defaultEdge: {
7. // ... 其他配置
8. style: {
9. stroke: '#088',
10. lineWidth: 3,
11. },
12. },
13. });
14. // ...
标签文本配置 labelCfg
Object 类型。其配置与边的通用文本配置相同,见 内置边。基于上面 弧度 curveOffset 中的代码,下面代码在 defaultNode 中增加了 labelCfg 配置项进行文本的配置,使之达到如下图效果。
1. const data = {
2. // ... data 内容
3. };
4. const graph = new G6.Graph({
5. // ... 图的其他配置
6. defaultEdge: {
7. // ... 其他配置
8. labelCfg: {
9. autoRotate: true,
10. refY: -30,
11. refX: 30,
12. },
13. },
14. });
15. // ...
