Loading...
Loading...
G6 v5 图可视化代码生成技能,支持网络图、树形图、流程图等多种图类型的初始化、布局、交互和插件配置
npx skill4agent add antvis/chart-visualization-skills antv-g6-graphcontainernew Graph({...})new G6.Graph()graph.render()await graph.render(){ nodes: [...], edges: [...], combos?: [...] }iddatasourcetargetidgraph.data()node.styleedge.style(datum: NodeData | EdgeData) => valuestyle.labelTextlabellabelCfgstyle.sizelayout{ type: 'force', ... }forcepreventOverlapnodeSized3-forcecollidetreeToGraphData()graph.render()behaviors'drag-canvas''zoom-canvas''drag-element''click-select'{ type: 'click-select', multiple: true }pluginsbehaviors'minimap''grid-line''tooltip'{ type: 'tooltip', getContent: (e, items) => '...' }// 错误: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();// 错误:直接在顶层放业务属性
{ id: 'node1', label: 'Node 1', value: 100 }
// 正确:业务属性放在 data 字段
{ id: 'node1', data: { label: 'Node 1', value: 100 } }// 错误:v4 labelCfg
node: {
labelCfg: { style: { fill: '#333' } }
}
// 正确:v5 style.labelText
node: {
style: {
labelText: (d) => d.data.label,
labelFill: '#333',
labelFontSize: 14,
}
}// 错误:v4 modes
modes: {
default: ['drag-canvas', 'zoom-canvas'],
edit: ['create-edge'],
}
// 正确:v5 直接 behaviors 数组
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],// 错误: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; // ✅
}// 错误: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' } });// 错误:遗漏 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 });常见变体错误:(把字符串 ID 当变量名使用,变量未定义 → ReferenceError → 白屏)container: container
// 错误: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'
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();| 图类型 | 推荐布局 | 典型场景 |
|---|---|---|
| 网络图/关系图 | | 社交网络、知识图谱 |
| 层次/流程图 | | 组织架构、工作流 |
| 树形图 | | 文件树、思维导图 |
| 环形图 | | 循环依赖、环形关系 |
| 网格图 | | 棋盘布局、矩阵关系 |
| 同心圆 | | 中心辐射关系 |
| 辐射布局 | | 以某节点为中心的辐射 |
| 类型名 | 形状 | 适用场景 |
|---|---|---|
| 圆形 | 通用节点,网络图 |
| 矩形 | 流程图、UML |
| 椭圆 | 通用,强调纵向 |
| 菱形 | 决策节点 |
| 六边形 | 蜂窝布局 |
| 三角形 | 特殊标记 |
| 五角星 | 特殊标记、评分 |
| 环形 | 带进度的节点 |
| 图片 | 头像、图标节点 |
| HTML | 富文本自定义节点 |
| 类型名 | 形状 | 适用场景 |
|---|---|---|
| 直线 | 简单图、拓扑图 |
| 三次贝塞尔曲线 | 通用,弧形效果 |
| 水平三次曲线 | 水平流程图 |
| 垂直三次曲线 | 垂直流程图 |
| 二次贝塞尔曲线 | 轻量弧形边 |
| 折线 | 正交布局 |
| 自环 | 节点自身的循环 |
| 布局名 | 类型 | 特点 |
|---|---|---|
| 力导向 | 物理模拟,自然分布 |
| 力导向 | 基于 D3,可配置力类型 |
| 力导向 | 快速,支持 GPU 加速 |
| 力导向 | 大规模图,聚类效果好 |
| 层次 | DAG,自动分层 |
| 层次 | AntV 优化版 Dagre |
| 环形 | 节点排列为圆形 |
| 同心圆 | 按属性值分环 |
| 网格 | 规则网格排列 |
| 辐射 | 以某节点为中心辐射 |
| 降维 | 保持节点相对距离 |
| 随机 | 调试用 |
| 树形 | 紧凑树,节省空间 |
| 树形 | 思维导图风格 |
| 树形 | 树状图 |
| 树形 | 缩进树 |
| 行为名 | 描述 |
|---|---|
| 拖拽画布 |
| 滚轮缩放画布 |
| 滚轮平移画布 |
| 拖拽节点/边/combo |
| 力导向图中拖拽节点 |
| 点击选中元素 |
| 框选元素 |
| 套索选择 |
| 悬停激活元素 |
| 折叠/展开节点(树图) |
| 交互式创建边 |
| 聚焦元素(缩放到指定元素) |
| 缩放时保持元素大小不变 |
| 自动显示/隐藏标签(防重叠) |
| 大规模图视口优化 |
| 插件名 | 描述 |
|---|---|
| 网格背景线 |
| 背景颜色/图片 |
| 水印 |
| 缩略图导航 |
| 图例 |
| 元素提示框 |
| 工具栏(缩放、撤销等) |
| 右键菜单 |
| 撤销/重做 |
| 时间轴过滤 |
| 鱼眼放大效果 |
| 边捆绑 |
| 边过滤镜头 |
| 元素轮廓包围 |
| 气泡集合 |
| 对齐辅助线 |
| 全屏 |
selectedactivehighlightinactivedisabled// 在 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', []); // 清除所有状态// 内置主题
const graph = new Graph({
theme: 'light', // 默认
// theme: 'dark',
});
// 动态切换主题
graph.setTheme('dark');
graph.render();// 添加元素
graph.addNodeData([{ id: 'n3', data: { label: '新节点' } }]);
graph.addEdgeData([{ source: 'n1', target: 'n3' }]);
// 更新元素
graph.updateNodeData([{ id: 'n1', style: { fill: 'red' } }]);
// 删除元素
graph.removeNodeData(['n3']);
// 更新数据后需要重新渲染
graph.draw();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,
},
},node: {
palette: {
type: 'group', // 按分类映射颜色
field: 'category', // 数据中的分类字段
color: 'tableau10', // 内置色板名
},
},transforms: [
{
type: 'map-node-size',
field: 'value',
range: [16, 60],
},
],transforms: [
{
type: 'process-parallel-edges',
offset: 15,
},
],
edge: {
type: 'quadratic',
},// 增
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();// 元素事件(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-initg6-core-data-structureg6-core-graph-apig6-core-eventsg6-core-custom-elementg6-core-transforms-animationg6-node-circleg6-node-rectg6-node-imageg6-node-diamond-ellipse-hexagong6-node-star-triangle-donutg6-node-htmlg6-node-reactg6-combo-overviewg6-edge-lineg6-edge-cubicg6-edge-cubic-directionalg6-edge-polylineg6-edge-quadratic-loopg6-layout-forceg6-layout-dagreg6-layout-circularg6-layout-gridg6-layout-mindmapg6-layout-advancedg6-layout-combo-fishboneg6-core-transforms-animationg6-transform-parallel-edges-radialg6-behavior-click-selectg6-behavior-drag-elementg6-behavior-canvas-navg6-behavior-hover-activateg6-behavior-lasso-collapseg6-behavior-create-edge-focusg6-behavior-advancedg6-plugin-tooltipg6-plugin-minimapg6-plugin-contextmenu-toolbarg6-plugin-history-legendg6-plugin-fisheye-hull-watermarkg6-plugin-timebar-gridlineg6-plugin-background-snaplineg6-plugin-edge-bundling-bubbleg6-plugin-fullscreen-titleg6-state-overviewg6-theme-overviewg6-pattern-network-graphg6-pattern-tree-graphg6-pattern-flow-chart