API 参考

配置

new Graph() 可传入的数据、节点、边、布局、交互、标签和主题

new Graph(options) 的参数类型是 GraphOptions。必填字段只有 container 和 data。节点外观、边外观、布局、交互和主题都有默认值,需要差异化时再覆盖。

const graph = new Graph({
  container: 'container',
  data: {
    nodes: [{ id: 'a' }, { id: 'b' }],
    edges: [{ id: 'a-b', source: 'a', target: 'b' }],
  },
  node: { style: { size: 28, fill: '#5366ad' } },
  edge: { style: { endArrow: true } },
  layout: { type: 'dagre', rankdir: 'TB' },
  behaviors: ['zoom-canvas', 'drag-canvas', 'click-select'],
  theme: 'light',
});

各主题的视觉效果和完整示例在配置指南里: 数据、节点、边、标签、布局、交互、主题。

GraphOptions

字段类型说明
containerstring | HTMLElement画布容器。字符串按元素 id 查找
dataGraphData节点、边,以及可选的 Combo
nodeNodeConfig节点形状、样式和状态样式
edgeEdgeConfig边形状、样式、箭头和状态样式
comboComboConfigCombo 外观。折叠和嵌套仍是实验能力
labelLabelOptions节点和边标签共用的缩放、碰撞策略
layoutLayoutConfig布局类型和算法参数
behaviorsBehaviorOption[]交互列表。省略时启用滚轮缩放和拖拽平移
transformsTransformOption[]绘制前的数据转换,例如平行边
pluginsPluginOption[]已注册插件
themeThemeOption'light'、'dark',或在预设上覆盖 token
autoFitOnResizeboolean | number | FitViewOptions容器尺寸变化后是否重新 fitView()。默认 false,保留当前相机

运行中能热更新的是 node、edge、label 和 theme,通过 graph.updateOptions()。

图数据

const data: GraphData = {
  nodes: [{ id: 'a', data: { cluster: 'svc' } }],
  edges: [{ source: 'a', target: 'b', data: { weight: 1 } }],
  combos: [{ id: 'group', collapsed: false }],
};

节点 GraphNode

字段必填说明
id是图内唯一
name否节点名称
data否业务字段。样式函数和 Tooltip 从这里读取
x / y否已有坐标。无布局或作为布局初始位置时使用
fx / fy否布局时固定的坐标
states否初始状态,如 ['selected']
comboId否所属 Combo 的 id

边 GraphEdge

字段必填说明
source是起点节点 id
target是终点节点 id
id否省略时由引擎生成
data否业务字段
states否初始状态

Combo ComboData

字段说明
id必填,唯一
parentId父 Combo,用于嵌套
collapsed初始是否折叠
style覆盖 combo.style 的局部样式
data业务字段
states初始状态

节点

node?: {
  type?: 'circle' | 'rect' | string;
  labelRenderer?: string;
  decorations?: NodeDecorationConfig[];
  autoExternalLabelPlacement?: boolean;
  style?: NodeStyleConfig | ((node: GraphNode) => Partial<NodeStyleConfig>);
  stateStyles?: Partial<Record<ElementState, NodeStateStyleOverride>>;
}

type 省略或为 'circle' 时画圆形,'rect' 画圆角矩形。其他字符串必须是已经 register('node', ...) 的类型名。

style 的每个视觉字段都可以是固定值,或 (node: GraphNode) => 值。label: false 和 icon: false 表示不绘制该项。

样式字段说明
fill / stroke / strokeWidth / opacity填充、描边和透明度
size圆形直径或未分别指定宽高时的尺寸
width / height / borderRadius矩形尺寸和圆角
label文本,或返回文本的函数。未配置时不绘制节点标签;false 表示明确不显示
labelFontSize / labelFontFamily / labelFontWeight / labelColor文本外观
labelPlacement'center' | 'top' | 'bottom' | 'left' | 'right',默认 'bottom'
labelOffsetX / labelOffsetY相对放置点的偏移
labelBackground / labelBackgroundPadding / labelMaxWidth文本底色、内边距和最大宽度
icon / iconType图标内容。iconType 为 'image' 或 'text'
iconWidth / iconHeight / iconFontFamily / iconFontSize / iconColor图标尺寸和文字图标样式
iconOffsetX / iconOffsetY图标偏移

stateStyles 在元素进入对应状态时覆盖 fill、stroke、strokeWidth、opacity、haloWidth。状态怎么开关见 Graph 状态方法。

labelRenderer 是已注册的自定义标签渲染器名称。decorations 用来挂 badge 等节点装饰,渲染器通过 register('decoration', ...) 注册。字段和碰撞规则见 标签 与 自定义节点。

边

edge?: {
  type?: 'line' | 'arc' | 'polyline' | 'cubic' | string;
  labelRenderer?: string;
  parallelEdges?: boolean | ParallelEdgesConfig;
  style?: { /* 见下表,值可以是函数 */ };
  stateStyles?: Partial<Record<ElementState, EdgeStateStyleOverride>>;
}

边的样式函数签名是 (source, target, edge) => 值。

样式字段说明
stroke / lineWidth / opacity / lineDash颜色、宽度、透明度和虚线
hitAreaWidth可点中的屏幕像素宽度。省略时等于最终线宽
curvature / curveOffset弧线和曲线的弯曲
endArrow / startArrow / arrowSize终点箭头、起点箭头和尺寸
radius折线拐角半径
router折线路由:'orth' | 'tree' | 'indented' | 'fishbone'
label 及 labelFontSize、labelFontFamily、labelColor边标签内容。未配置 label 时不绘制;字号默认 10
labelPlacement'start' | 'center' | 'end'
labelBackground / labelBackgroundPadding / labelAutoRotate底色、内边距,以及是否沿边旋转

stateStyles 可覆盖 stroke、lineWidth、hitAreaWidth、opacity、lineDash。

parallelEdges: true 使用默认间距拆开同端点的多条边。对象形式:

字段默认说明
enabled开启是否分离
offset20相邻边的图坐标间距
mergeBy按 source/target当前只支持按两端分组
includeReversetrueA→B 与 B→A 算同一组
minOffset0非中心边的最小绝对偏移
maxOffset80最大绝对偏移

也可以把 'process-parallel-edges' 放进 transforms,两种写法的效果相同。详见 平行边。

标签策略

label 控制引擎如何显示已经算好的节点和边标签,不决定文本内容。

字段默认说明
scaleWithZoomtrue节点标签尺寸是否跟随相机缩放。边标签保持屏幕尺寸
hideOnLowZoomtrue缩放低于阈值时隐藏标签
hideOnCollisiontrue与更早的标签重叠时隐藏
nodeMinZoom0.3节点标签的最小缩放
edgeMinZoom0.5边标签的最小缩放

布局

layout?: {
  type: string;
  worker?: boolean;
  width?: number;
  height?: number;
  center?: [number, number];
  [algorithmOption: string]: unknown;
}

type 使用内置名称或已注册的布局名,例如 'force'、'dagre'、'mindmap'。其余字段传给对应算法。worker: true 把支持的算法放到 Web Worker。width、height、center 省略时按画布尺寸计算。

可选类型、参数和运行时重跑见 布局配置。

交互

behaviors 的每一项是行为名,或 { type, ...选项 }。

省略整个字段时等价于 ['zoom-canvas', 'drag-canvas']。传入数组后,数组就是完整列表,[] 表示关闭默认的缩放和平移。

type作用可写选项
zoom-canvas滚轮缩放sensitivity 默认 1,minZoom 默认 0.1,maxZoom 默认 20
drag-canvas拖拽平移ignoreModifierKeys 默认 true,按住修饰键时不平移,留给框选
drag-node拖拽节点无额外选项
drag-edge拖拽边的端点重新连线无额外选项
click-select点击选中节点或边multiple 默认 false,trigger 默认 'shift',还可为 'ctrl' 或 'meta'
hover-highlight悬停高亮邻域degree 默认 1,inactiveState 可选
highlight-node悬停节点时高亮该节点、一度邻居和相连的边无额外选项
highlight-edge悬停边时高亮边和两端节点无额外选项
brush-select矩形框选key 为 'shift'、'ctrl' 或 null,throttleMs 默认 100
lasso-select套索选择与 brush-select 相同
tooltip浮层见下表

tooltip 选项:

字段默认说明
trigger'hover''hover' 或 'click'
getContent节点 id 或边的两端返回字符串或 HTMLElement。自定义字符串按 HTML 写入,不可信内容需要调用方转义
enable始终显示返回 false 时不显示该元素
offset[10, 10]相对指针的像素偏移
delay80悬停出现前的等待毫秒
className'g6lite-tooltip'浮层类名
containerdocument.body浮层挂载节点

getContent 收到的 TooltipContext 包含 itemType、nodeId 或边的 edgeId / source / target、data 和原始 event。

行为组合和事件见 交互配置。自定义行为用 register('behavior', ...)。

主题

theme?: 'light' | 'dark' | ({ type: 'light' | 'dark' } & Partial<ThemeTokens>)

对象形式先取 type 对应的预设,再用同层字段覆盖。可覆盖的 token 包括 nodeFill、nodeSize、nodeStroke、nodeStrokeWidth、edgeStroke、edgeLineWidth、edgeArrow、canvasBackground、edgeDefaultColor、edgeCategoryPalette、nodeStateStyles、edgeStateStyles。

import { resolveTheme } from '@antv/g6-lite';

const tokens = resolveTheme({ type: 'light', nodeFill: '#1677ff' });

resolveTheme() 返回一份新的完整 ThemeTokens,不修改预设。

插件与转换

plugins?: Array<{ type: string; [option: string]: unknown }>
transforms?: Array<'process-parallel-edges' | string | { type: string; options?: object }>

plugins 和自定义 transforms 的 type 必须先用 register() 注册。内置转换名目前是 'process-parallel-edges'。