container 参数必填,传入 DOM 元素 ID 字符串或 DOM 元素对象new Graph({...}) 构造函数, 不得使用 new G6.Graph() (v4 写法)graph.render() 返回 Promise,异步渲染;若需等待完成请 await graph.render(){ nodes: [...], edges: [...], combos?: [...] }id(字符串);业务数据放在 data 字段source 和 target,值为节点 idgraph.data() 方法传数据node.style / edge.style 配置,支持静态值和回调函数(datum: NodeData | EdgeData) => valuestyle.labelText 设置( 不是 label 或 labelCfg)style.size 设置(单个数值或 [width, height] 数组)layout 配置放在 Graph 选项中: { type: 'force', ... }force 布局 不支持 preventOverlap / nodeSize(G6 v4 参数,v5 静默忽略);防重叠请改用 d3-force + collidetreeToGraphData() 转换graph.render() 后会持续迭代behaviors 为字符串数组或配置对象数组'drag-canvas', 'zoom-canvas', 'drag-element', 'click-select'{ type: 'click-select', multiple: true }plugins 为数组,与 behaviors 类似'minimap', 'grid-line', 'tooltip'{ type: 'tooltip', getContent: (e, items) => '...' }javascript
// 错误:v4 chainable API
const graph = new G6.Graph({ ... });
graph.data(data);
graph.render();
graph.node((node) => ({ ... })); // v4 回调
// 正确:v5 构造函数
const graph = new Graph({
container: 'container',
data: { nodes: [...], edges: [...] },
node: { style: { ... } },
});
graph.render();
javascript
// 错误:直接在顶层放业务属性
{ id: 'node1', label: 'Node 1', value: 100 }
// 正确:业务属性放在 data 字段
{ id: 'node1', data: { label: 'Node 1', value: 100 } }
javascript
// 错误:v4 labelCfg
node: {
labelCfg: { style: { fill: '#333' } }
}
// 正确:v5 style.labelText
node: {
style: {
labelText: (d) => d.data.label,
labelFill: '#333',
labelFontSize: 14,
}
}
javascript
// 错误:v4 modes
modes: {
default: ['drag-canvas', 'zoom-canvas'],
edit: ['create-edge'],
}
// 正确:v5 直接 behaviors 数组
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
javascript
// 错误:attributes 是计算后的样式对象,不含节点 data,访问 data.color 抛 TypeError
render(attributes, container) {
const { data } = attributes; // undefined
const fill = data.color; // TypeError → 白屏
}
// 正确:通过 node.style 回调把 data 字段映射为自定义样式属性
// ① Graph 配置
node: {
type: 'my-node',
style: { color: (d) => d.data.color },
},
// ② render() 中直接从 attributes 读取
render(attributes, container) {
const { color = '#1783FF' } = attributes; // ✅
}
javascript
// 错误:extend 已从 G6 v5 正式版移除,导入后调用会报 "extend is not a function"
import { Graph, extend } from '@antv/g6';
const extendedGraph = extend(Graph, {
nodes: { 'my-node': MyNodeFn },
});
// 错误:v4 的 group.addShape() API
const MyNode = (node) => (model) => {
const group = node.group();
group.addShape('circle', { attrs: { r: 20 } });
};
// 正确:BaseNode 类 + register()
import { BaseNode, Circle, ExtensionCategory, Graph, register } from '@antv/g6';
class MyNode extends BaseNode {
render(attributes, container) {
super.render(attributes, container);
this.upsert('key', Circle, { cx: 0, cy: 0, r: 20, fill: '#1783FF' }, container);
}
}
register(ExtensionCategory.NODE, 'my-node', MyNode);
const graph = new Graph({ node: { type: 'my-node' } });
javascript
// 错误:遗漏 container
const graph = new Graph({ width: 800, height: 600 });
// 正确:container 必填,值为字符串 ID 或 DOM 元素
const graph = new Graph({ container: 'container', width: 800, height: 600 });
// 或传入 DOM 元素
const graph = new Graph({ container: document.getElementById('container'), width: 800, height: 600 });
常见变体错误:
container: container(把字符串 ID 当变量名使用,变量未定义 → ReferenceError → 白屏)
javascript
// 错误:combo-combined / force / d3-force 等布局是异步迭代的
// autoFit 在布局迭代开始前执行,节点全堆在原点,包围盒为零 → 缩放异常 → 白屏
const graph = new Graph({
autoFit: 'view', // ❌ 异步布局下不能在此设置
layout: { type: 'combo-combined' },
});
graph.render();
// 正确:不设置 autoFit,在 AFTER_LAYOUT 事件后调用 fitView
import { Graph, GraphEvent } from '@antv/g6';
const graph = new Graph({
layout: { type: 'combo-combined' },
});
graph.on(GraphEvent.AFTER_LAYOUT, () => graph.fitView({ padding: 20 }));
graph.render();
同步布局(
dagre、grid、circular等)不受此影响,可以直接用autoFit: 'view'。
javascript
import { Graph } from '@antv/g6';
const graph = new Graph({
// 1. 容器
container: 'container', // DOM id 或 HTMLElement
width: 800,
height: 600,
autoFit: 'view', // 可选:'center' | 'view' | false
// 2. 数据
data: {
nodes: [
{ id: 'n1', data: { label: '节点1' } },
{ id: 'n2', data: { label: '节点2' } },
],
edges: [
{ source: 'n1', target: 'n2' },
],
},
// 3. 节点样式
node: {
type: 'circle', // 节点类型
style: {
size: 40,
fill: '#1783FF',
stroke: '#fff',
lineWidth: 2,
labelText: (d) => d.data.label,
labelPlacement: 'bottom',
},
},
// 4. 边样式
edge: {
type: 'line',
style: {
stroke: '#aaa',
lineWidth: 1,
endArrow: true,
},
},
// 5. 布局
layout: {
type: 'force',
preventOverlap: true,
nodeSize: 40,
},
// 6. 交互
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
// 7. 插件(可选)
plugins: ['grid-line'],
// 8. 主题(可选)
theme: 'light', // 'light' | 'dark'
});
graph.render();
图类型推荐布局典型场景网络图/关系图force / fruchterman社交网络、知识图谱层次/流程图dagre / antv-dagre组织架构、工作流树形图compact-box / mindmap文件树、思维导图环形图circular循环依赖、环形关系网格图grid棋盘布局、矩阵关系同心圆concentric中心辐射关系辐射布局radial以某节点为中心的辐射
类型名形状适用场景circle圆形通用节点,网络图rect矩形流程图、UMLellipse椭圆通用,强调纵向diamond菱形决策节点hexagon六边形蜂窝布局triangle三角形特殊标记star五角星特殊标记、评分donut环形带进度的节点image图片头像、图标节点htmlHTML富文本自定义节点
类型名形状适用场景line直线简单图、拓扑图cubic三次贝塞尔曲线通用,弧形效果cubic-horizontal水平三次曲线水平流程图cubic-vertical垂直三次曲线垂直流程图quadratic二次贝塞尔曲线轻量弧形边polyline折线正交布局loop自环节点自身的循环
布局名类型特点force力导向物理模拟,自然分布d3-force力导向基于 D3,可配置力类型fruchterman力导向快速,支持 GPU 加速force-atlas2力导向大规模图,聚类效果好dagre层次DAG,自动分层antv-dagre层次AntV 优化版 Dagrecircular环形节点排列为圆形concentric同心圆按属性值分环grid网格规则网格排列radial辐射以某节点为中心辐射mds降维保持节点相对距离random随机调试用compact-box树形紧凑树,节省空间mindmap树形思维导图风格dendrogram树形树状图indented树形缩进树
行为名描述drag-canvas拖拽画布zoom-canvas滚轮缩放画布scroll-canvas滚轮平移画布drag-element拖拽节点/边/combodrag-element-force力导向图中拖拽节点click-select点击选中元素brush-select框选元素lasso-select套索选择hover-activate悬停激活元素collapse-expand折叠/展开节点(树图)create-edge交互式创建边focus-element聚焦元素(缩放到指定元素)fix-element-size缩放时保持元素大小不变auto-adapt-label自动显示/隐藏标签(防重叠)optimize-viewport-transform大规模图视口优化
插件名描述grid-line网格背景线background背景颜色/图片watermark水印minimap缩略图导航legend图例tooltip元素提示框toolbar工具栏(缩放、撤销等)contextmenu右键菜单history撤销/重做timebar时间轴过滤fisheye鱼眼放大效果edge-bundling边捆绑edge-filter-lens边过滤镜头hull元素轮廓包围bubble-sets气泡集合snapline对齐辅助线fullscreen全屏
G6 v5 内置 5 种状态: selected、 active、 highlight、 inactive、 disabled
javascript
// 在 Graph 配置中为状态设置样式
node: {
style: {
fill: '#1783FF',
},
state: {
selected: {
fill: '#ff6b6b',
stroke: '#ff4d4d',
lineWidth: 3,
},
hover: {
fill: '#40a9ff',
},
},
},
// 动态设置状态
graph.setElementState('node1', 'selected');
graph.setElementState('node1', ['selected', 'highlight']);
graph.setElementState('node1', []); // 清除所有状态
javascript
// 内置主题
const graph = new Graph({
theme: 'light', // 默认
// theme: 'dark',
});
// 动态切换主题
graph.setTheme('dark');
graph.render();
javascript
// 添加元素
graph.addNodeData([{ id: 'n3', data: { label: '新节点' } }]);
graph.addEdgeData([{ source: 'n1', target: 'n3' }]);
// 更新元素
graph.updateNodeData([{ id: 'n1', style: { fill: 'red' } }]);
// 删除元素
graph.removeNodeData(['n3']);
// 更新数据后需要重新渲染
graph.draw();
javascript
node: {
style: {
size: (d) => d.data.size || 30,
fill: (d) => {
const colorMap = { type1: '#1783FF', type2: '#FF6B6B', type3: '#52C41A' };
return colorMap[d.data.type] || '#ccc';
},
labelText: (d) => d.data.name,
},
},
javascript
node: {
palette: {
type: 'group', // 按分类映射颜色
field: 'category', // 数据中的分类字段
color: 'tableau10', // 内置色板名
},
},
javascript
transforms: [
{
type: 'map-node-size',
field: 'value',
range: [16, 60],
},
],
javascript
transforms: [
{
type: 'process-parallel-edges',
offset: 15,
},
],
edge: {
type: 'quadratic',
},
javascript
// 增
graph.addNodeData([{ id: 'n3', data: { label: '新节点' } }]);
graph.addEdgeData([{ source: 'n1', target: 'n3' }]);
graph.draw();
// 删
graph.removeNodeData(['n3']); // 关联边自动删除
graph.draw();
// 改
graph.updateNodeData([{ id: 'n1', data: { label: '更新' } }]);
graph.draw();
// 查
const node = graph.getNodeData('n1');
const selected = graph.getElementDataByState('node', 'selected');
const zoom = graph.getZoom();
// 视口
await graph.fitView({ padding: 20 });
await graph.focusElement('n1', { duration: 500 });
await graph.zoomTo(1.5);
// 状态
graph.setElementState('n1', 'selected');
graph.setElementState('n1', []); // 清除
// 销毁
graph.destroy();
javascript
// 元素事件(node/edge/combo + 事件类型)
graph.on('node:click', (e) => console.log(e.target.id));
graph.on('edge:pointerover', (e) => graph.setElementState(e.target.id, 'active'));
graph.on('canvas:click', () => { /* 点击空白 */ });
// 生命周期事件
import { GraphEvent } from '@antv/g6';
graph.on(GraphEvent.AFTER_RENDER, () => console.log('渲染完成'));
graph.on(GraphEvent.AFTER_LAYOUT, () => console.log('布局完成'));
g6-core-graph-init:Graph 初始化完整配置g6-core-data-structure:数据结构规范g6-core-graph-api:Graph 实例 API(增删改查、视口、状态)g6-core-events:事件系统(元素事件、画布事件、生命周期)g6-core-custom-element:自定义节点/边(register + BaseNode/BaseEdge)g6-core-transforms-animation:数据变换(map-node-size)与动画配置g6-node-circle:圆形(通用)g6-node-rect:矩形(流程图)g6-node-image:图片节点g6-node-diamond-ellipse-hexagon:菱形/椭圆/六边形g6-node-star-triangle-donut:五角星/三角形/环形进度g6-node-html:HTML 富文本节点g6-node-react:React/Vue 自定义节点(@antv/g6-extension-react)g6-combo-overview:Combo 分组(circle/rect,折叠展开)g6-edge-line:直线边g6-edge-cubic:三次贝塞尔曲线边g6-edge-cubic-directional:有向三次曲线(cubic-horizontal 水平 / cubic-vertical 垂直)g6-edge-polyline:折线边g6-edge-quadratic-loop:二次曲线与自环边g6-layout-force:力导向(force/d3-force)g6-layout-dagre:层次/流程图(dagre)g6-layout-circular:环形g6-layout-grid:网格g6-layout-mindmap:思维导图g6-layout-advanced:同心圆/辐射/mds/fruchtermang6-layout-combo-fishbone:复合布局(combo-combined)+ 鱼骨布局(fishbone)g6-core-transforms-animation:map-node-size 与动画配置g6-transform-parallel-edges-radial:平行边处理(process-parallel-edges)+ 径向标签(place-radial-labels)g6-behavior-click-select:点击选中g6-behavior-drag-element:拖拽节点g6-behavior-canvas-nav:画布拖拽+缩放g6-behavior-hover-activate:悬停激活g6-behavior-lasso-collapse:套索选择 + 折叠展开g6-behavior-create-edge-focus:创建边 + 聚焦元素g6-behavior-advanced:fix-element-size / auto-adapt-label / drag-element-forceg6-plugin-tooltip:悬停提示框g6-plugin-minimap:缩略图g6-plugin-contextmenu-toolbar:右键菜单 + 工具栏g6-plugin-history-legend:撤销重做 + 图例g6-plugin-fisheye-hull-watermark:鱼眼放大 + 轮廓包围 + 水印g6-plugin-timebar-gridline:时间轴 + 网格线g6-plugin-background-snapline:画布背景(background)+ 对齐线(snapline)g6-plugin-edge-bundling-bubble:边绑定(edge-bundling)+ 气泡集(bubble-sets)g6-plugin-fullscreen-title:全屏(fullscreen)+ 图标题(title)g6-state-overview:元素状态系统g6-theme-overview:主题系统g6-pattern-network-graph:网络关系图g6-pattern-tree-graph:树形图/组织架构g6-pattern-flow-chart:流程图