polyline

G6 内置了折线 polyline 边,其默认样式如下。polyline - 图1

使用方法

如 内置边 一节所示,配置边的方式有两种:实例化图时全局配置,在数据中动态配置。

1 实例化图时全局配置

用户在实例化 Graph 时候可以通过 defaultEdge 指定 shape 为 'polyline',即可使用 polyline 边。

1. const graph = new G6.Graph({
2. container: 'mountNode',
3. width: 800,
4. height: 600,
5. defaultEdge: {
6. shape: 'polyline',
7. // 其他配置
8. },
9. });

2 在数据中动态配置

如果需要使不同节点有不同的配置,可以将配置写入到节点数据中。这种配置方式可以通过下面代码的形式直接写入数据,也可以通过遍历数据的方式写入。

1. const data = {
2. nodes: [
3. ... // 节点
4. ],
5. edges: [{
6. source: 'node0',
7. target: 'node1'
8. shape: 'polyline',
9. ... // 其他配置
10. style: {
11. ...  // 样式属性,每种边的详细样式属性参见各边文档
12. }
13. },
14. ... // 其他边
15. ]
16. }

配置项说明

polyline 边支持以下的配置项:

1. color: '#87e8de',
2. style: {
3. offset: 20,  // 拐弯处距离节点最小距离
4. radius: 10,  // 拐弯处的圆角弧度,若不设置则为直角
5. lineWidth: 2,
6. stroke: '#87e8de'
7. },
8. label: '边的标签文字',
9. labelCfg: {
10. refX: 10,  // 文本在 x 方向偏移量
11. refY: 10,  // 文本在 y 方向偏移量
12. style: {
13. fill: '#595959'
14. }
15. }
名称 含义 类型 备注
color 边的颜色 String 优先级低于 style 中的 stroke
style 边的样式 Object Canvas支持的属性
style.radius 拐弯处的圆角弧度 Number 若不设置则为直角,polyline 特有
style.offset 拐弯处距离节点最小距离 Number 默认为 5,polyline 特有
label 标签文本文字 String
labelCfg 文件配置项 Object

样式属性 style

Object 类型。与其他类型的边不同的是,polyline 的 style 含有两个特殊属性:

  • radius ,弯折处的圆角半径,不设置则默认为直角。
  • offset ,距离端点的最小距离,默认值为 5。

其它配置项与边的通用样式属性相同,见 内置边下面代码演示在实例化图时全局配置方法中配置 style,以达到下图效果。polyline - 图2

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: 'polyline',
21. label: 'polyline',
22. },
23. ],
24. };
25. const graph = new G6.Graph({
26. container: 'mountNode',
27. width: 800,
28. height: 600,
29. defaultEdge: {
30. // shape: 'polyline',  // 在数据中已经指定 shape,这里无需再次指定
31. style: {
32. radius: 10,
33. offset: 10,
34. stroke: 'steelblue',
35. lineWidth: 5,
36. },
37. },
38. });
39. graph.data(data);
40. graph.render();

标签文本配置 labelCfg

Object 类型。其它配置与边的通用文本配置相同,见 内置边。基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了 labelCfg 配置项进行文本的配置,使之达到如下图效果。polyline - 图3

1. const data = {
2. // ... data 内容
3. };
4. const graph = new G6.Graph({
5. // ... 图的其他配置
6. defaultEdge: {
7. // ... 其他配置
8. labelCfg: {
9. refY: -10,
10. refX: 60,
11. },
12. },
13. });
14. // ...