image

Image

G6 内置了 image 节点,其默认样式如下。标签文本位于图片下方。image - 图1

使用方法

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

1 实例化图时全局配置

用户在实例化 Graph 时候可以通过 defaultNode 指定 shape 为 'image',即可使用 image 节点。

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

2 在数据中动态配置

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

1. const data = {
2. nodes: [{
3. id: 'node0',
4. img: 'https://yyb.gtimg.com/aiplat/page/product/visionimgidy/img/demo6-16a47e5d31.jpg?max_age=31536000',
5. shape: 'image',
6. size: 200,
7. label: 'AntV Team',
8. labelCfg: {
9. position: 'bottom'
10. },
11. // 裁剪图片配置
12. clipCfg: {
13. show: false,
14. type: 'circle',
15. r: 15
16. }
17. },
18. ... // 其他节点
19. ],
20. edges: [
21. ... // 边
22. ]
23. }

配置项说明

image 节点支持以下的配置项:

1. img: 'https://yyb.gtimg.com/aiplat/page/product/visionimgidy/img/demo6-16a47e5d31.jpg?max_age=31536000',
2. size: 200,
3. labelCfg: {
4. position: 'bottom'
5. },
6. // 裁剪图片配置
7. clipCfg: {
8. show: false,
9. type: 'circle',
10. // circle
11. r: 15,
12. // ellipse
13. rx: 10,
14. ry: 15,
15. // rect
16. width: 15,
17. height: 15,
18. // 坐标
19. x: 0,
20. y: 0
21. }
名称 含义 类型 备注
img 图片 URL 地址 String image 节点特有
size 图片大小 Number Array
labelCfg 文件配置项 Object
clipCfg 裁剪图片的配置项 Object 默认不裁剪,image 节点特有

剪裁

clipCfg

名称 含义 类型 备注
type 裁剪的图片形状 String 支持 'circle'、'rect'、'ellipse'
x 裁剪图形的 x 坐标 Number 默认为 0,类型为 'circle'、'rect'、'ellipse' 时生效
y 裁剪图形的 y 坐标 Number 默认为 0,类型为 'circle'、'rect'、'ellipse' 时生效
show 是否启用裁剪功能 Boolean 默认不裁剪,值为 false
r 剪裁圆形的半径 Number 剪裁 type 为 'circle' 时生效
width 剪裁矩形的宽度 Number 剪裁 type 为 'rect' 时生效
height 剪裁矩形的长度 Number 剪裁 type 为 'rect' 时生效
rx 剪裁椭圆的长轴半径 Number 剪裁 type 为 'ellipse' 时生效
ry 剪裁椭圆的短轴半径 Number 剪裁 type 为 'ellipse' 时生效

所有的裁剪类型都提供了默认值。下面代码演示在实例化图时全局配置 clipCfg 的最简形式:

1. const data = {
2. nodes: [
3. {
4. x: 100,
5. y: 100,
6. shape: 'image',
7. label: 'image',
8. },
9. ],
10. };
11. const graph = new G6.Graph({
12. container: 'mountNode',
13. width: 800,
14. height: 600,
15. defaultNode: {
16. // shape: 'image',  // 在数据中已经指定 shape,这里无需再次指定
17. clipCfg: {
18. show: true,
19. type: 'circle',
20. },
21. },
22. });
23. graph.data(data);
24. graph.render();

裁剪类型

圆形剪裁

circle当剪裁配置 clipCfg 中的裁剪类型 type 为 'circle' 时,如下配置可以得到下图效果:

1. clipCfg: {
2. show: true,
3. type: 'circle',
4. r: 100
5. }

image - 图2

矩形剪裁

rect

当剪裁配置 clipCfg 中的裁剪类型 type 为 'rect' 时,如下配置可以得到下图效果:

1. clipCfg: {
2. show: true,
3. type: 'rect',
4. x: -50,
5. y: -50,
6. width: 100,
7. height: 100
8. }

image - 图3

椭圆剪裁

ellipse

当剪裁配置 clipCfg 中的裁剪类型 type 为 'ellipse' 时,如下配置可以得到下图效果:

1. clipCfg: {
2. show: true,
3. type: 'ellipse',
4. rx: 100,
5. ry: 60
6. }

image - 图4