logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.47
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 内置节点
        • 节点通用配置项
        • Diamond 菱形
        • Donut 甜甜圈
        • Ellipse 椭圆形
        • Hexagon 六边形
        • Html HTML
        • Image 图片
        • Rect 矩形
        • Star 五角形
        • Triangle 三角形
        • Circle 圆形
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 内置边
        • 边通用配置项
        • Cubic 三次贝塞尔曲线
        • CubicHorizontal 水平三次贝塞尔曲线
        • CubicVertical 垂直三次贝塞尔曲线
        • Line 直线
        • Polyline 折线
        • Quadratic 二次贝塞尔曲线
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 内置组合
        • 组合配置项
        • Circle 圆形
        • Rect 矩形
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 内置布局
      • 布局通用配置项
      • AntvDagre 布局
      • Circular 环形布局
      • ComboCombined 复合布局
      • CompactBox 紧凑树
      • Concentric 同心圆布局
      • D3Force 力导向布局
      • D3Force3D 3D 力导向布局
      • Dagre 布局
      • Dendrogram 生态树
      • Fishbone 鱼骨布局
      • Force 力导向布局
      • ForceAtlas2 力导向布局
      • Fruchterman 力导向布局
      • Grid 网格布局
      • Indented 缩进树
      • MDS 高维数据降维布局
      • Mindmap 脑图树
      • Radial 径向布局
      • Random 随机布局
      • Snake 蛇形布局
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 内置交互
      • AutoAdaptLabel 标签自适应显示
      • BrushSelect 框选
      • ClickSelect 点击选中
      • CollapseExpand 展开/收起元素
      • CreateEdge 创建边
      • DragCanvas 拖拽画布
      • DragElement 拖拽元素
      • DragElementForce 力导向拖拽元素
      • FixElementSize 缩放画布时固定元素大小
      • FocusElement 聚焦元素
      • HoverActivate 悬停激活
      • LassoSelect 套索选择
      • OptimizeViewportTransform 优化视口变换
      • ScrollCanvas 滚动画布
      • ZoomCanvas 缩放画布
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 内置插件
      • Background 背景
      • BubbleSets 气泡集
      • Contextmenu 上下文菜单
      • EdgeBundling 边绑定
      • EdgeFilterLens 边过滤镜
      • Fisheye 鱼眼放大镜
      • Fullscreen 全屏展示
      • GridLine 网格线
      • History 历史记录
      • Hull 轮廓包围
      • Legend 图例
      • Minimap 小地图
      • Snapline 对齐线
      • Timebar 时间条
      • Toolbar 工具栏
      • Tooltip 提示框
      • Watermark 水印
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 内置数据处理
      • MapNodeSize 动态调整节点大小
      • PlaceRadialLabels 径向标签
      • ProcessParallelEdges 平行边
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

节点通用配置项

上一篇
节点总览
下一篇
Diamond 菱形

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

本文介绍内置节点通用属性配置。

NodeOptions

import { Graph } from '@antv/g6';
const graph = new Graph({
node: {
type: 'circle', // 节点类型
style: {}, // 节点样式
state: {}, // 状态样式
palette: {}, // 色板配置
animation: {}, // 动画配置
},
});
属性描述类型默认值必选
type节点类型,内置节点类型名称或自定义节点的名称Typecircle
style节点样式配置,包括颜色、大小等Style-
state不同状态下的样式配置State-
palette定义节点的色板,用于根据不同数据映射颜色Palette-
animation定义节点的动画效果Animation-

Type

指定节点类型,内置节点类型名称或自定义节点的名称。默认为 circle(圆形)。⚠️ 注意:这里决定了主图形的形状。

const graph = new Graph({
node: {
type: 'circle',
},
});

可选值有:

  • circle:圆形节点
  • diamond:菱形节点
  • donut:甜甜圈节点
  • ellipse:椭圆节点
  • hexagon:六边形节点
  • html:HTML 节点
  • image:图片节点
  • rect:矩形节点
  • star:星形节点
  • triangle:三角形节点

Style

定义节点的样式,包括颜色、大小等。

const graph = new Graph({
node: {
style: {},
},
});

一个完整的节点由以下几部分构成:

  • key :节点的主图形,表示节点的主要形状,例如矩形、圆形等;
  • label :文本标签,通常用于展示节点的名称或描述;
  • icon :图标图形,通常用于展示节点的图标,可以是图片或者文本图标;
  • badge :默认位于节点右上角的徽标;
  • halo :主图形周围展示的光晕效果的图形;
  • port :节点上的连接点,用于连接边。

以下样式配置将按原子图形依次说明:

主图形样式

主图形是节点的核心部分,定义了节点的基本形状和外观:

属性描述类型默认值必选
collapsed当前节点/组合是否折叠booleanfalse
cursor节点鼠标移入样式,配置项stringdefault
fill节点填充色string#1783FF
fillOpacity节点填充色透明度number | string1
increasedLineWidthForHitTesting当 lineWidth 较小时,可交互区域也随之变小,有时我们想增大这个区域,让“细线”更容易被拾取到number0
lineCap节点描边端点样式round | square | buttbutt
lineDash节点描边虚线样式number[]-
lineDashOffset节点描边虚线偏移量number-
lineJoin节点描边连接处样式round | bevel | mitermiter
lineWidth节点描边宽度number1
opacity节点透明度number | string1
shadowBlur节点阴影模糊度number-
shadowColor节点阴影颜色string-
shadowOffsetX节点阴影在 x 轴方向上的偏移量number | string-
shadowOffsetY节点阴影在 y 轴方向上的偏移量number | string-
shadowType节点阴影类型inner | outerouter
size节点大小,快捷设置节点宽高,配置项number | number[]32
stroke节点描边色string#000
strokeOpacity节点描边色透明度number | string1
transformtransform 属性允许你旋转、缩放、倾斜或平移给定节点string-
transformOrigin旋转与缩放中心,也称作变换中心string-
visibility节点是否可见visible | hiddenvisible
x节点 x 坐标number0
y节点 y 坐标number0
z节点 z 坐标number0
zIndex节点渲染层级number0

Size

节点大小,快捷设置节点宽高,支持三种配置方式:

  • number:表示节点宽高相同为指定值
  • [number, number]:表示节点宽高分别为数组元素依次表示节点的宽度、高度
  • [number, number, number]:表示节点宽高分别为数组元素依次表示节点的宽度、高度以及深度

Cursor

可选值有:auto | default | none | context-menu | help | pointer | progress | wait | cell | crosshair | text | vertical-text | alias | copy | move | no-drop | not-allowed | grab | grabbing | all-scroll | col-resize | row-resize | n-resize | e-resize | s-resize | w-resize | ne-resize | nw-resize | se-resize | sw-resize | ew-resize | ns-resize | nesw-resize | nwse-resize | zoom-in | zoom-out

示例:

const graph = new Graph({
node: {
style: {
fill: '#1783FF', // 填充色
stroke: '#000', // 描边色
lineWidth: 2, // 描边宽度
},
},
});

效果如下:

createGraph(
{
data: {
nodes: [{ id: 'node1', style: { x: 120, y: 40 } }],
},
node: {
style: { fill: '#1783FF', stroke: '#000', lineWidth: 2 },
},
},
{ width: 240, height: 100 },
);

标签样式

标签用于显示节点的文本信息:

属性描述类型默认值必选
label是否显示节点标签booleantrue
labelCursor鼠标移入节点标签时显示的样式,配置项stringdefault
labelFill节点标签文字颜色string-
labelFontFamily节点标签字体族string-
labelFontSize节点标签字体大小number12
labelFontStyle节点标签字体样式normal | italic | oblique-
labelFontVariant节点标签字体变种normal | small-caps | string-
labelFontWeight节点标签字体粗细normal | bold | bolder | lighter | number-
labelLeading行间距number0
labelLetterSpacing节点标签字间距number | string-
labelLineHeight节点标签行高number | string-
labelMaxLines节点标签最大行数number1
labelMaxWidth节点标签最大宽度,配置项number | string200%
labelOffsetX节点标签在 x 轴方向上的偏移量number0
labelOffsetY节点标签在 y 轴方向上的偏移量number0
labelPadding节点标签内边距number | number[]0
labelPlacement节点标签相对于节点主图形的位置,配置项stringbottom
labelText节点标签文字内容string-
labelTextAlign节点标签文本水平对齐方式start | center | middle | end | left | `right'left
labelTextBaseline节点标签文本基线top | hanging | middle | alphabetic | ideographic | bottom-
labelTextDecorationColor节点标签文本装饰线颜色string-
labelTextDecorationLine节点标签文本装饰线string-
labelTextDecorationStyle节点标签文本装饰线样式solid | double | dotted | dashed | wavy-
labelTextOverflow节点标签文本溢出处理方式clip | ellipsis | string-
labelTextPath节点标签文本路径Path-
labelWordWrap节点标签是否开启自动折行。开启 labelWordWrap 后,超出 labelMaxWidth 的部分自动换行booleanfalse
labelZIndex节点标签渲染层级number0

LabelPlacement

可选值有:left | right | top | bottom | left-top | left-bottom | right-top | right-bottom | top-left | top-right | bottom-left | bottom-right | center | bottom

LabelMaxWidth

开启自动折行 labelWordWrap 后,超出该宽度则换行:

  • string: 表示以相对于节点宽度的百分比形式定义最大宽度。例如 50% 表示标签宽度不超过节点宽度的一半
  • number: 表示以像素值为单位定义最大宽度。例如 100 表示标签的最大宽度为 100 像素

比如,设置多行标签文字:

{
"labelWordWrap": true,
"labelMaxWidth": 200,
"labelMaxLines": 3
}

示例:

const graph = new Graph({
node: {
style: {
label: true, // 是否显示节点标签
labelText: '节点名称', // 标签文字内容
labelFill: '#000', // 标签文字颜色
labelFontSize: 12, // 标签字体大小
labelFontWeight: 'normal', // 标签字体粗细
labelPlacement: 'bottom', // 标签相对于节点主图形的位置
},
},
});

效果如下:

createGraph(
{
data: {
nodes: [
{
id: 'node1',
style: {
x: 120,
y: 40,
label: true,
labelText: '节点名称',
labelFill: '#000',
labelFontSize: 12,
labelFontWeight: 'normal',
labelPlacement: 'bottom',
},
},
],
},
},
{ width: 240, height: 100 },
);

标签背景样式

标签背景用于显示节点标签的背景:

属性描述类型默认值
labelBackground节点标签背景是否显示booleanfalse
labelBackgroundCursor节点标签背景鼠标移入样式,配置项stringdefault
labelBackgroundFill节点标签背景填充色string-
labelBackgroundFillOpacity节点标签背景透明度number1
labelBackgroundHeight节点标签背景高度string | number-
labelBackgroundLineDash节点标签背景虚线配置number | string |(number | string )[]-
labelBackgroundLineDashOffset节点标签背景虚线偏移量number-
labelBackgroundLineWidth节点标签背景描边线宽number-
labelBackgroundRadius节点标签背景圆角半径
- number: 统一设置四个圆角半径
- number[]: 分别设置四个圆角半径,不足则自动补充
number | number[]0
labelBackgroundShadowBlur节点标签背景阴影模糊程度number-
labelBackgroundShadowColor节点标签背景阴影颜色string-
labelBackgroundShadowOffsetX节点标签背景阴影 X 方向偏移number-
labelBackgroundShadowOffsetY节点标签背景阴影 Y 方向偏移number-
labelBackgroundStroke节点标签背景描边颜色string-
labelBackgroundStrokeOpacity节点标签背景描边透明度number | string1
labelBackgroundVisibility节点标签背景是否可见visible | hidden-
labelBackgroundZIndex节点标签背景渲染层级number1

示例:

const graph = new Graph({
node: {
style: {
labelBackground: true, // 是否显示节点标签背景
labelBackgroundFill: '#000', // label背景填充
labelBackgroundRadius: 10, // label背景圆角
labelBackgroundFillOpacity: 0.5, // label背景透明度
},
},
});

效果如下:

createGraph(
{
data: {
nodes: [
{
id: 'node1',
style: {
x: 120,
y: 40,
label: true,
labelText: '节点名称',
labelFill: '#000',
labelFontSize: 12,
labelFontWeight: 'normal',
labelPlacement: 'bottom',
labelBackground: true,
labelBackgroundFill: '#000',
labelBackgroundRadius: 10,
labelBackgroundFillOpacity: 0.5,
},
},
],
},
},
{ width: 240, height: 100 },
);

光晕样式

属性描述类型默认值必选
halo节点光晕是否显示booleanfalse
haloCursor节点光晕鼠标移入样式,配置项strigdefault
haloDraggable节点光晕是否允许拖拽booleantrue
haloDroppable节点光晕是否允许接收被拖拽的元素booleantrue
haloFillRule节点光晕填充规则nonzero | evenodd-
haloFilter节点光晕滤镜string-
haloLineWidth节点光晕描边宽度number3
haloPointerEvents节点光晕效果是否响应指针事件,配置项stringnone
haloStroke节点光晕描边色,此属性用于设置节点周围光晕的颜色,帮助突出显示节点string与主图形的填充色 fill 一致
haloStrokeOpacity节点光晕描边色透明度number0.25
haloVisibility节点光晕可见性visible | hiddenvisible
haloZIndex节点光晕渲染层级number-1

PointerEvents

可选值有: visible | visiblepainted | visiblestroke | non-transparent-pixel | visiblefill | visible | painted | fill | stroke | all | none | auto | inherit | initial | unset

示例:

const graph = new Graph({
node: {
style: {
halo: true, // 是否显示节点光晕
haloStroke: '#FF0000', // 节点光晕描边色
haloLineWidth: 10, // 节点光晕描边宽度
},
},
});

效果如下:

createGraph(
{
data: {
nodes: [
{
id: 'node1',
style: {
x: 120,
y: 40,
},
},
],
},
node: {
style: {
halo: true,
haloStroke: '#FF0000',
haloLineWidth: 10,
},
},
},
{ width: 240, height: 100 },
);

图标样式

属性描述类型默认值
icon是否显示节点图标booleantrue
iconFill节点图标文字颜色string-
iconFontFamily节点图标字体族string-
iconFontSize节点图标字体大小number16
iconFontStyle节点图标字体样式normal | italic | obliquenormal
iconFontVariant节点图标字体变种normal | small-caps | stringnormal
iconFontWeight节点图标字体粗细number | stringnormal
iconHeight节点图标高度number主图形高度的一半
iconLetterSpacing节点图标文本字间距number | string-
iconLineHeight节点图标文本行高number | string-
iconMaxLines节点图标文本最大行数number1
iconRadius节点图标圆角半径number0
iconSrc节点图片来源。其优先级高于 iconTextstring-
iconText节点图标文字string-
iconTextAlign节点图标文本水平对齐方式start | center | middle | end | left | rightleft
iconTextBaseline节点图标文本基线top | hanging | middle | alphabetic | ideographic | bottomalphabetic
iconTextDecorationColor节点图标文本装饰线颜色string-
iconTextDecorationLine节点图标文本装饰线string-
iconTextDecorationStyle节点图标文本装饰线样式solid | double | dotted | dashed | wavysolid
iconTextOverflow节点图标文本溢出处理方式clip | ellipsis | stringclip
iconWidth节点图标宽度number主图形宽度的一半
iconWordWrap节点图标文本是否自动换行boolean-

示例:

const graph = new Graph({
node: {
style: {
iconText: '文本', // 图标文本
iconFill: '#FF0000', // 图标文本颜色
iconFontSize: 14, // 图标文本大小
iconFontWeight: 'bold', // 图标文本粗细
iconFontStyle: 'italic', // 图标文本样式
},
},
});

效果如下:

createGraph(
{
data: {
nodes: [
{
id: 'node1',
style: {
x: 120,
y: 40,
},
},
],
},
node: {
style: {
iconText: '文本',
iconFill: '#FF0000',
iconFontSize: 14,
iconFontWeight: 'bold',
iconFontStyle: 'italic',
},
},
},
{ width: 240, height: 100 },
);

徽标样式

属性描述类型默认值
badge节点是否显示徽标booleantrue
badgePalette节点徽标的背景色板string[][#7E92B5, #F4664A, #FFBE3A]
badges节点徽标设置BadgeStyleProps[]-

BadgeStyleProps

属性描述类型默认值
background节点徽标是否显示背景booleantrue
backgroundCursor节点徽标背景鼠标移入样式,配置项stringdefault
backgroundFill节点徽标背景填充色。若不指定,优先考虑 badgePalette 按顺序分配string-
backgroundFillOpacity节点徽标背景填充透明度number1
backgroundFilter节点徽标背景滤镜string-
backgroundHeight节点徽标背景高度number | string-
backgroundLineDash节点徽标背景虚线配置number | string |(number | string )[]-
backgroundLineDashOffset节点徽标背景虚线偏移量number-
backgroundLineWidth节点徽标背景描边线宽number-
backgroundRadius节点徽标背景圆角半径
- number: 统一设置四个圆角半径
- number[]: 分别设置四个圆角半径,会补足缺省的分量
- string: 与 CSS padding 属性类似,使用空格分隔
number | number[] | string0
backgroundShadowBlur节点徽标背景阴影模糊程度number-
backgroundShadowColor节点徽标背景阴影颜色string-
backgroundShadowOffsetX节点徽标背景阴影 X 方向偏移number-
backgroundShadowOffsetY节点徽标背景阴影 Y 方向偏移number-
backgroundStroke节点徽标背景描边颜色string-
backgroundStrokeOpacity节点徽标背景描边透明度number | string1
backgroundVisibility节点徽标背景是否可见visible | hidden-
backgroundZIndex节点徽标背景渲染层级number-
fill节点徽标文字颜色string-
fontFamily节点徽标字体族string-
fontSize节点徽标字体大小number8
fontStyle节点徽标字体样式normal | italic | obliquenormal
fontVariant节点徽标字体变种normal | small-caps | stringnormal
fontWeight节点徽标字体粗细number | stringnormal
lineHeight节点徽标行高string | number-
lineWidth节点徽标行宽string | number-
maxLines节点徽标文本最大行数number1
offsetX节点徽标在 x 轴方向上的偏移量number0
offsetY节点徽标在 y 轴方向上的偏移量number0
padding节点徽标内边距number | number[]0
placement节点徽标相对于节点主图形的位置。若不指定,默认从右上角顺时针依次排放left | right | top | bottom | left-top | left-bottom | right-top | right-bottom | top-left | top-right | bottom-left | bottom-right-
text节点徽标文字内容string-
textAlign节点徽标文本水平对齐方式start | center | middle | end | left | rightleft
textBaseline节点徽标文本基线top | hanging | middle | alphabetic | ideographic | bottomalphabetic
textDecorationColor节点徽标文本装饰线颜色string-
textDecorationLine节点徽标文本装饰线string-
textDecorationStyle节点徽标文本装饰线样式solid | double | dotted | dashed | wavysolid
textOverflow节点徽标文本溢出处理方式clip | ellipsis | stringclip
visibility节点徽标是否可见visible | hidden-
wordWrap节点徽标文本是否自动换行boolean-
zIndex节点徽标渲染层级number3

例如,给一个节点添加三个不同含义的徽标:

const graph = new Graph({
node: {
style: {
badge: true, // 是否显示徽标
badges: [
{ text: 'A', placement: 'right-top' },
{ text: 'Important', placement: 'right' },
{ text: 'Notice', placement: 'right-bottom' },
],
badgePalette: ['#7E92B5', '#F4664A', '#FFBE3A'], // 徽标的背景色板
badgeFontSize: 7, // 徽标字体大小
},
},
});

效果如下图所示:

createGraph(
{
autoFit: 'center',
data: {
nodes: [{ id: 'node1', states: ['focus'] }],
},
node: {
style: {
badge: true,
badges: [
{ text: 'A', placement: 'right-top' },
{ text: 'Important', placement: 'right' },
{ text: 'Notice', placement: 'right-bottom' },
],
badgePalette: ['#7E92B5', '#F4664A', '#FFBE3A'],
badgeFontSize: 7,
},
},
},
{ width: 200, height: 100 },
);

连接桩样式

属性描述类型默认值必选
port节点是否显示连接桩booleantrue
ports节点连接桩配置项,支持配置多个连接桩PortStyleProps[]

PortStyleProps

属性描述类型默认值必选
key节点连接桩的键值,默认为节点连接桩的索引string-
placement节点连接桩相对于节点主图形的位置left | right | top | bottom | center | left-top | left-bottom | right-top | right-bottom | top-left | top-right | bottom-left | bottom-right | [number, number]-✓
r节点连接桩半径
- 如果设置为 undefined,则连接桩被视为一个点,不在画布上显示但存在,边会优先连接到最近的连接桩
- 如果设置为数字,则连接桩被视为一个圆,圆的半径由此处指定
number-
linkToCenter边是否连接到节点连接桩的中心
- 若为 true,则边连接到节点连接桩的中心
- 若为 false,则边连接到节点连接桩的边缘
booleanfalse
cursor节点连接桩鼠标移入样式,配置项stringdefault
fill节点连接桩填充颜色string-
fillOpacity节点连接桩填充透明度number1
isBillboard节点连接桩是否为Billboard 效果boolean-
isSizeAttenuation节点连接桩是否启用大小衰减boolean-
lineDash节点连接桩描边虚线配置number | string |(number | string )[]-
lineDashOffset节点连接桩描边虚线偏移量number-
lineWidth节点连接桩描边线宽number-
shadowBlur节点连接桩阴影模糊程度number-
shadowColor节点连接桩阴影颜色string-
shadowOffsetX节点连接桩阴影 X 方向偏移number-
shadowOffsetY节点连接桩阴影 Y 方向偏移number-
stroke节点连接桩描边颜色string-
strokeOpacity节点连接桩描边透明度number | string1
visibility节点连接桩是否可见visible | hiddenvisible
zIndex节点连接桩渲染层级number2

例如,给一个节点显示添加四个连接桩:

const graph = new Graph({
node: {
style: {
port: true,
ports: [
{ key: 'top', placement: 'top', fill: '#7E92B5' },
{ key: 'right', placement: 'right', fill: '#F4664A' },
{ key: 'bottom', placement: 'bottom', fill: '#FFBE3A' },
{ key: 'left', placement: [0, 0.5], fill: '#D580FF' },
],
portR: 3,
portLineWidth: 1,
portStroke: '#fff',
},
},
});

效果如下图所示:

createGraph(
{
autoFit: 'center',
data: {
nodes: [{ id: 'node1', states: ['focus'] }],
},
node: {
style: {
port: true,
ports: [
{ key: 'top', placement: 'top', fill: '#7E92B5' },
{ key: 'right', placement: 'right', fill: '#F4664A' },
{ key: 'bottom', placement: 'bottom', fill: '#FFBE3A' },
{ key: 'left', placement: [0, 0.5], fill: '#D580FF' },
],
portR: 3,
portLineWidth: 1,
portStroke: '#fff',
},
},
},
{ width: 200, height: 100 },
);

State

在一些交互行为中,比如点击选中一个节点或鼠标悬停激活一个边,仅仅是在该元素做了某些状态的标识。为了将这些状态反应到终端用户所见的视觉空间中,我们需要为不同的状态设置不同的图元素样式,以响应该图元素状态的变化。

G6 提供了几种内置的状态,包括选中(selected)、高亮(highlight)、激活(active)、不活跃(inactive)和禁用(disabled)。此外,它还支持自定义状态,以满足更特定的需求。对于每个状态,开发者可以定义一套样式规则,这些规则会覆盖元素的默认样式。

数据结构如下:

type NodeState = {
[state: string]: NodeStyle;
};

例如,当节点处于 focus 状态时,可以为其添加一个宽度为 3 且颜色为橙色的描边。

const graph = new Graph({
node: {
state: {
focus: {
lineWidth: 3, // 描边宽度
stroke: 'orange', // 描边颜色
},
},
},
});

效果如下图所示:

createGraph(
{
autoFit: 'center',
data: {
nodes: [{ id: 'node1', states: ['focus'] }],
},
node: {
state: {
focus: {
lineWidth: 3,
stroke: 'orange',
},
},
},
},
{ width: 200, height: 100 },
);

Animation

定义节点的动画效果,支持下列两种配置方式:

  1. 关闭节点全部动画
{
"node": {
"animation": false
}
}
  1. 配置阶段动画

阶段动画是指节点在进入画布、更新、离开画布时的动画效果。目前支持的阶段包括:

  • enter: 节点进入画布时的动画
  • update: 节点更新时的动画
  • exit: 节点离开画布时的动画
  • show: 节点从隐藏状态显示时的动画
  • hide: 节点隐藏时的动画
  • collapse: 节点收起时的动画
  • expand: 节点展开时的动画

你可以参考 动画范式 使用动画语法来配置节点,如:

{
"node": {
"animation": {
"update": [
{
"fields": ["x", "y"], // 更新时只对 x 和 y 属性进行动画
"duration": 1000, // 动画持续时间
"easing": "linear" // 缓动函数
}
],
}
}

也可以使用内置的动画效果:

{
"node": {
"animation": {
"enter": "fade", // 使用渐变动画
"update": "translate", // 使用平移动画
"exit": "fade" // 使用渐变动画
}
}
}

你可以传入 false 来关闭特定阶段的动画:

{
"node": {
"animation": {
"enter": false // 关闭节点入场动画
}
}
}

Palette

定义节点的色板,即预定义节点颜色池,并根据规则进行分配,将颜色映射到 fill 属性。

有关色板的定义,请参考 色板。

属性描述类型默认值
color色板颜色。如果色板注册过,可以直接指定其注册名,也接受一个颜色数组string | string[]-
field指定元素数据中的分组字段。若不指定,默认取 id 作为分组字段string | ((datum) => string)id
invert是否反转色板booleanfalse
type指定当前色板类型。
- group: 离散色板
- value: 连续色板
group | valuegroup

如将一组数据按 category 字段分配节点颜色,使得同类别的节点颜色相同:

{
"node": {
"palette": {
"type": "group",
"field": "category",
"color": ["#1783FF", "#F08F56", "#D580FF", "#00C9C9", "#7863FF"]
}
}
}

效果如下图所示:

createGraph(
{
data: {
nodes: new Array(10)
.fill(0)
.map((_, i) => ({ id: `node-${i}`, data: { category: ['A', 'B', 'C', 'D', 'E'][i % 5] } })),
},
layout: { type: 'grid', cols: 10 },
node: {
palette: {
type: 'group',
field: 'category',
color: ['#1783FF', '#F08F56', '#D580FF', '#00C9C9', '#7863FF'],
},
},
},
{ width: 600, height: 100 },
);

也可以使用默认配置:

{
"node": {
"palette": "tableau" // tableau 为色板名,默认根据 ID 分配颜色
}
}

效果如下图所示:

createGraph(
{
data: {
nodes: new Array(10)
.fill(0)
.map((_, i) => ({ id: `node-${i}`, data: { category: ['A', 'B', 'C', 'D', 'E'][i % 5] } })),
},
layout: { type: 'grid', cols: 10 },
node: {
palette: 'tableau',
},
},
{ width: 600, height: 100 },
);