triangle
Triangle
G6 内置了三角形 Triangle 节点,其默认样式如下。标签文本位于三角形下方。
使用方法
如 内置节点 一节所示,配置节点的方式有两种:实例化图时全局配置,在数据中动态配置。
1 实例化图时全局配置
用户在实例化 Graph 时候可以通过 defaultNode 指定 shape 为 'triangle',即可使用 triangle 节点。
1. const graph = new G6.Graph({
2. container: 'mountNode',
3. width: 800,
4. height: 600,
5. defaultNode: {
6. shape: 'triangle',
7. // 其他配置
8. },
9. });
2 在数据中动态配置
如果需要使不同节点有不同的配置,可以将配置写入到节点数据中。这种配置方式可以通过下面代码的形式直接写入数据,也可以通过遍历数据的方式写入。
1. const data = {
2. nodes: [{
3. id: 'node0',
4. shape: 'triangle',
5. ... // 其他配置
6. },
7. ... // 其他节点
8. ],
9. edges: [
10. ... // 边
11. ]
12. }
配置项说明
triangle 节点支持以下的配置项:
| 名称 | 含义 | 类型 | 备注 |
|---|---|---|---|
| size | 三角形的边长 | Number | Array |
| direction | 三角形的方向 | String | 可取值:up、down、left、right,默认为up。 |
| style | 三角形默认样式 | Object | Canvas支持的属性 |
| labelCfg | 文件配置项 | Object | |
| stateStyles | 各状态下的样式 | Object | 只对keyShape起作用 |
| linkPoints | 三角形上的链接点 | Object | 默认不显示 |
| icon | 三角形上icon配置 | Object | 默认不显示icon |
三角形方向 direction
String 类型。可取值有:'up'</code>、<code>'down'</code>、<code>'left'</code>、<code>'right'</code>。默认为 <code>'up'。通过设置 direction,可以修改三角形的方向。下面代码演示在实例化图时全局配置方法中配置 direction。
1. const graph = new G6.Graph({
2. container: 'mountNode',
3. width: 800,
4. height: 600,
5. defaultNode: {
6. shape: 'triangle',
7. direction: 'down',
8. },
9. });




上图分别是将
direction配置为'up','down','left','right'的结果
样式属性 style
Object 类型。通过 style 配置来修改节点的填充色、描边等属性。下面代码演示在实例化图时全局配置方法中配置 style,使之达到如下图效果。
1. const data = {
2. nodes: [
3. {
4. x: 100,
5. y: 100,
6. shape: 'triangle',
7. label: 'triangle',
8. },
9. ],
10. };
11. const graph = new G6.Graph({
12. container: 'mountNode',
13. width: 800,
14. height: 600,
15. defaultNode: {
16. // shape: 'triangle',// 在数据中已经指定 shape,这里无需再次指定
17. direction: 'up',
18. size: 100,
19. style: {
20. fill: '#bae637',
21. stroke: '#eaff8f',
22. lineWidth: 5,
23. },
24. },
25. });
26. graph.data(data);
27. graph.render();
标签文本配置 labelCfg
Object 类型。通过 labelCfg 配置标签文本。基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了 labelCfg 配置项进行文本的配置,使之达到如下图效果。
1. const data = {
2. // ... data 内容
3. };
4. const graph = new G6.Graph({
5. // ... 图的其他属性
6. defaultNode: {
7. // ... 节点其他属性
8. labelCfg: {
9. position: 'center',
10. style: {
11. fill: '#9254de',
12. fontSize: 18,
13. },
14. },
15. },
16. });
17. // ...
边的连入点 linkPoints
Object 类型。通过配置 linkPoints ,可以指定圆周围「上、下、左、右」四个方向上边的连入点。说明 虽然每个三角形只有三个顶点,但不同方向的三角形顶点位置不同。
| 名称 | 含义 | 类型 | 备注 |
|---|---|---|---|
| top | 是否显示上部的连接点 | Boolean | 默认为false |
| bottom | 是否显示底部的连接点 | Boolean | 默认为false |
| left | 是否显示左侧的连接点 | Boolean | 默认为false |
| right | 是否显示右侧的连接点 | Boolean | 默认为false |
| size | 连接点的大小 | Number | 默认为3 |
| fill | 连接点的填充色 | String | 默认为#72CC4A |
| stroke | 连接点的边框颜色 | String | 默认为#72CC4A |
| lineWidth | 连接点边框的宽度 | Number | 默认为1 |
基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了 linkPoints 配置项进行连入点的配置,使之达到如下图效果。
1. const data = {
2. // ... data 内容
3. };
4. const graph = new G6.Graph({
5. // ... 图的其他属性
6. defaultNode: {
7. // ... 其他属性
8. linkPoints: {
9. top: true,
10. bottom: true,
11. left: true,
12. right: true,
13. fill: '#fff',
14. size: 5,
15. },
16. },
17. });
18. // ...
图标 icon
Object 类型。通过配置 icon,可以在圆上显示小图标。
| 名称 | 含义 | 类型 | 备注 |
|---|---|---|---|
| show | 是否显示 icon | Boolean | 默认为 false,不显示 |
| width | icon 的宽度 | Number | 默认为 16 |
| height | icon 的高度 | Number | 默认为 16 |
| img | icon 的图片地址 | String | 默认有一个如下图中的图片 |
| offset | icon 的偏移量 | Number | 默认为 0,triangle 节点的 icon 特有的配置 |
基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了 icon 配置项进行图标的配置,使之达到如下图效果。
1. const data = {
2. // ... data 内容
3. };
4. const graph = new G6.Graph({
5. // ... 图的其他属性
6. defaultNode: {
7. // ... 其他属性
8. icon: {
9. show: true,
10. width: 30,
11. height: 30,
12. offset: 20,
13. },
14. },
15. });
16. // ...
