Loading...
Loading...
Compare original and translation side by side
Graphgraph.addNode()graph.addEdge()graph.use()import { Graph } from '@antv/x6';
const graph = new Graph({
container: 'container',
background: { color: '#F2F7FA' },
});
const source = graph.addNode({
shape: 'rect',
x: 40, y: 40, width: 100, height: 40,
label: 'Source',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
const target = graph.addNode({
shape: 'rect',
x: 300, y: 200, width: 100, height: 40,
label: 'Target',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
graph.addEdge({ source, target, attrs: { line: { stroke: '#8f8f8f', strokeWidth: 1 } } });
graph.centerContent();Graphgraph.addNode()graph.addEdge()graph.use()import { Graph } from '@antv/x6';
const graph = new Graph({
container: 'container',
background: { color: '#F2F7FA' },
});
const source = graph.addNode({
shape: 'rect',
x: 40, y: 40, width: 100, height: 40,
label: 'Source',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
const target = graph.addNode({
shape: 'rect',
x: 300, y: 200, width: 100, height: 40,
label: 'Target',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
graph.addEdge({ source, target, attrs: { line: { stroke: '#8f8f8f', strokeWidth: 1 } } });
graph.centerContent();<script src="https://unpkg.com/@antv/x6@3/dist/x6.js"></script>
<script>
const graph = new X6.Graph({
container: 'container',
background: { color: '#F2F7FA' },
});
const source = graph.addNode({
shape: 'rect',
x: 40, y: 40, width: 100, height: 40,
label: 'Source',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
const target = graph.addNode({
shape: 'rect',
x: 300, y: 200, width: 100, height: 40,
label: 'Target',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
graph.addEdge({ source, target, attrs: { line: { stroke: '#8f8f8f', strokeWidth: 1 } } });
graph.centerContent();
</script><script src="https://unpkg.com/@antv/x6@3/dist/x6.js"></script>
<script>
const graph = new X6.Graph({
container: 'container',
background: { color: '#F2F7FA' },
});
const source = graph.addNode({
shape: 'rect',
x: 40, y: 40, width: 100, height: 40,
label: 'Source',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
const target = graph.addNode({
shape: 'rect',
x: 300, y: 200, width: 100, height: 40,
label: 'Target',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
graph.addEdge({ source, target, attrs: { line: { stroke: '#8f8f8f', strokeWidth: 1 } } });
graph.centerContent();
</script>https://sive.antv.antgroup.com/api/v1/context/retrieveGETquerylibrarytopKcontentmaxTokensprogressiveLevel| Parameter | Type | Required | Description |
|---|---|---|---|
| string | ✅ | Search keywords, e.g. |
| string | ✅ | Library name: |
| number | Number of results to return (default: 5) | |
| boolean | Return full reference doc markdown (default: true) | |
| number | Max tokens per result (default: unlimited) | |
| number | Progressive disclosure level: |
curl "https://sive.antv.antgroup.com/api/v1/context/retrieve?query=flowchart+stencil+port&library=x6"https://sive.antv.antgroup.com/api/v1/context/retrieveGETquerylibrarytopKcontentmaxTokensprogressiveLevel| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| string | ✅ | 搜索关键词,例如 |
| string | ✅ | 库名称: |
| number | 返回结果数量(默认值:5) | |
| boolean | 返回完整参考文档的markdown内容(默认值:true) | |
| number | 每个结果的最大token数(默认值:无限制) | |
| number | 渐进式披露级别: |
curl "https://sive.antv.antgroup.com/api/v1/context/retrieve?query=flowchart+stencil+port&library=x6"graph.render()graph.render()// ❌ WRONG — graph.render() is G6 API, not X6
const graph = new Graph({ container: 'container' });
graph.render();
// ✅ CORRECT — X6 auto-renders on addNode/addEdge/fromJSON
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });
graph.addNode({ shape: 'rect', x: 40, y: 40, width: 100, height: 40 });// ❌ 错误——graph.render()是G6的API,不属于X6
const graph = new Graph({ container: 'container' });
graph.render();
// ✅ 正确——X6会在调用addNode/addEdge/fromJSON时自动渲染
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });
graph.addNode({ shape: 'rect', x: 40, y: 40, width: 100, height: 40 });container: 'container'container: 'container'// ❌ WRONG — declaring container variable is forbidden
const container = document.getElementById('container');
const graph = new Graph({ container });
// ✅ CORRECT — string literal, runtime auto-resolves
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });// ❌ 错误——禁止声明container变量
const container = document.getElementById('container');
const graph = new Graph({ container });
// ✅ 正确——使用字符串字面量,运行时自动解析
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });// ❌ WRONG — calling plugin method without registration
graph.toPNG(); // Error: method not found
graph.select(); // Error: method not found
// ✅ CORRECT — register first, then call
import { Graph, Export, Selection } from '@antv/x6';
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });
graph.use(new Export());
graph.use(new Selection({ enabled: true, rubberband: true }));
// Now graph.toPNG() and graph.select() are available// ❌ 错误——未注册插件就调用其方法
graph.toPNG(); // 错误:找不到方法
graph.select(); // 错误:找不到方法
// ✅ 正确——先注册,再调用
import { Graph, Export, Selection } from '@antv/x6';
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });
graph.use(new Export());
graph.use(new Selection({ enabled: true, rubberband: true }));
// 现在可以调用graph.toPNG()和graph.select()了✅ Plugin class (import + | ❌ NOT a plugin (constructor option) |
|---|---|
| |
// ❌ WRONG — importing constructor option as "plugin"
import { Graph, Embedding } from '@antv/x6'; // Embedding doesn't exist!
graph.use(new Embedding()); // Error: not a constructor
// ✅ CORRECT — embedding is a Graph constructor option
import { Graph, Selection } from '@antv/x6';
const graph = new Graph({
container: 'container',
embedding: { enabled: true, findParent: 'bbox' },
mousewheel: { enabled: true, zoomAtMousePosition: true, modifiers: ['ctrl'] },
});
graph.use(new Selection({ enabled: true, rubberband: true }));✅ 插件类(导入后通过 | ❌ 不属于插件(是构造函数选项) |
|---|---|
| |
// ❌ 错误——将构造函数选项当作“插件”导入
import { Graph, Embedding } from '@antv/x6'; // Embedding不存在!
graph.use(new Embedding()); // 错误:不是构造函数
// ✅ 正确——embedding是Graph的构造函数选项
import { Graph, Selection } from '@antv/x6';
const graph = new Graph({
container: 'container',
embedding: { enabled: true, findParent: 'bbox' },
mousewheel: { enabled: true, zoomAtMousePosition: true, modifiers: ['ctrl'] },
});
graph.use(new Selection({ enabled: true, rubberband: true }));// ❌ WRONG — Selection used but not imported
import { Graph } from '@antv/x6';
graph.use(new Selection({...})); // falls back to window.Selection → Illegal constructor
// ✅ CORRECT — every used class imported
import { Graph, Selection, Keyboard, History } from '@antv/x6';
graph.use(new Selection({ enabled: true, rubberband: true }));
graph.use(new Keyboard({ enabled: true }));
graph.use(new History({ enabled: true }));// ❌ 错误——使用了Selection但未导入
import { Graph } from '@antv/x6';
graph.use(new Selection({...})); // 会回退到window.Selection → 非法构造函数
// ✅ 正确——所有使用的类都已导入
import { Graph, Selection, Keyboard, History } from '@antv/x6';
graph.use(new Selection({ enabled: true, rubberband: true }));
graph.use(new Keyboard({ enabled: true }));
graph.use(new History({ enabled: true }));graph.centerContent()graph.centerContent()// ❌ WRONG — no centerContent, content drifts to top-left
graph.addNode({ ... });
graph.addEdge({ ... });
// ✅ CORRECT — content centered after all additions
graph.addNode({ ... });
graph.addEdge({ ... });
graph.centerContent();
// OR: graph.zoomToFit({ padding: 20, maxScale: 1 }) — but NOT both// ❌ 错误——未调用centerContent,内容会偏移到左上角
graph.addNode({ ... });
graph.addEdge({ ... });
// ✅ 正确——添加完成后居中内容
graph.addNode({ ... });
graph.addEdge({ ... });
graph.centerContent();
// 或者:graph.zoomToFit({ padding: 20, maxScale: 1 })——但不要同时使用两者// ❌ WRONG — no background, no default styles
const graph = new Graph({ container: 'container' });
// ✅ CORRECT — mandatory background + default styles
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });
graph.addNode({
shape: 'rect', x: 40, y: 40, width: 100, height: 40,
label: 'Node',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
graph.addEdge({
source: 'node-1', target: 'node-2',
attrs: { line: { stroke: '#8f8f8f', strokeWidth: 1 } },
});// ❌ 错误——未设置背景和默认样式
const graph = new Graph({ container: 'container' });
// ✅ 正确——必须设置背景+默认样式
const graph = new Graph({ container: 'container', background: { color: '#F2F7FA' } });
graph.addNode({
shape: 'rect', x: 40, y: 40, width: 100, height: 40,
label: 'Node',
attrs: { body: { stroke: '#8f8f8f', strokeWidth: 1, fill: '#fff', rx: 6, ry: 6 } },
});
graph.addEdge({
source: 'node-1', target: 'node-2',
attrs: { line: { stroke: '#8f8f8f', strokeWidth: 1 } },
});mousewheelpanningSelection.rubberbandmousewheelpanningSelection.rubberband// ❌ WRONG — panning and mousewheel both grab scroll events
const graph = new Graph({
panning: { enabled: true },
mousewheel: { enabled: true },
});
graph.use(new Selection({ enabled: true, rubberband: true }));
// ✅ CORRECT — modifiers separate the interactions
const graph = new Graph({
panning: { enabled: true, eventTypes: ['leftMouseDown'], modifiers: 'shift' },
mousewheel: { enabled: true, zoomAtMousePosition: true, modifiers: ['ctrl'] },
});
graph.use(new Selection({ enabled: true, rubberband: true }));// ❌ 错误——panning和mousewheel都会捕获滚动事件
const graph = new Graph({
panning: { enabled: true },
mousewheel: { enabled: true },
});
graph.use(new Selection({ enabled: true, rubberband: true }));
// ✅ 正确——使用修饰键区分交互
const graph = new Graph({
panning: { enabled: true, eventTypes: ['leftMouseDown'], modifiers: 'shift' },
mousewheel: { enabled: true, zoomAtMousePosition: true, modifiers: ['ctrl'] },
});
graph.use(new Selection({ enabled: true, rubberband: true }));// ❌ WRONG — TypeScript syntax
private width: number = 100;
const node: Node = graph.addNode({...}) as Node;
// ✅ CORRECT — pure JavaScript only
const node = graph.addNode({ shape: 'rect', x: 40, y: 40 });// ❌ 错误——TypeScript语法
private width: number = 100;
const node: Node = graph.addNode({...}) as Node;
// ✅ 正确——仅使用纯JavaScript
const node = graph.addNode({ shape: 'rect', x: 40, y: 40 });Shape.HTML.registerclass extends NodeShape.HTML.registerclass extends Node// ❌ WRONG — class-based HTML node (2.x pattern)
class MyNode extends Node { ... }
// ✅ CORRECT — Shape.HTML.register (3.x pattern)
import { Graph, Shape } from '@antv/x6';
Shape.HTML.register({
shape: 'my-html',
effect: ['data'],
html(node) {
const div = document.createElement('div');
div.innerHTML = node.getData().content || '';
return div;
},
});// ❌ 错误——基于类的HTML节点(2.x版本写法)
class MyNode extends Node { ... }
// ✅ 正确——使用Shape.HTML.register(3.x版本写法)
import { Graph, Shape } from '@antv/x6';
Shape.HTML.register({
shape: 'my-html',
effect: ['data'],
html(node) {
const div = document.createElement('div');
div.innerHTML = node.getData().content || '';
return div;
},
});| User Intent | Retrieve Query |
|---|---|
| Graph init, container, background | |
| Flowchart / approval flow | |
| DAG / data pipeline | |
| ER diagram / entity relationship | |
| Lineage / data lineage graph | |
| Org chart / hierarchy | |
| UML class diagram | |
| Node config / custom node | |
| Edge config / router / connector | |
| Ports / connection桩 | |
| HTML shape node | |
| Stencil / drag-and-drop panel | |
| Plugin: Selection, History, Clipboard | |
| Plugin: MiniMap, Scroller, Snapline | |
| Plugin: Keyboard, Export, Transform | |
| Panning / mousewheel / embedding | |
| Tools (button-remove, etc.) | |
| Events (click,mouseenter,moved) | |
| Serialization (toJSON, fromJSON) | |
| Animation / gradient | |
| Group / nesting / embedding | |
| 用户意图 | 检索查询语句 |
|---|---|
| 图初始化、容器、背景 | |
| 流程图/审批流 | |
| DAG/数据管道 | |
| ER图/实体关系 | |
| 血缘图/数据血缘 | |
| 组织架构图/层级结构 | |
| UML类图 | |
| 节点配置/自定义节点 | |
| 边配置/路由/连接器 | |
| 端口/连接桩 | |
| HTML形状节点 | |
| Stencil/拖拽面板 | |
| 插件:Selection、History、Clipboard | |
| 插件:MiniMap、Scroller、Snapline | |
| 插件:Keyboard、Export、Transform | |
| 平移/鼠标滚轮/嵌入 | |
| 工具(button-remove等) | |
| 事件(click、mouseenter、moved等) | |
| 序列化(toJSON、fromJSON) | |
| 动画/渐变 | |
| 分组/嵌套/嵌入 | |
@antv/x6Graph@antv/x6Graph