diff --git a/packages/g6/__tests__/snapshots/runtime/graph/add-children-data/add-children-data.svg b/packages/g6/__tests__/snapshots/runtime/graph/add-children-data/add-children-data.svg
new file mode 100644
index 0000000000..13c1495d70
--- /dev/null
+++ b/packages/g6/__tests__/snapshots/runtime/graph/add-children-data/add-children-data.svg
@@ -0,0 +1,629 @@
+
\ No newline at end of file
diff --git a/packages/g6/__tests__/snapshots/runtime/graph/add-children-data/default.svg b/packages/g6/__tests__/snapshots/runtime/graph/add-children-data/default.svg
new file mode 100644
index 0000000000..23375ccfcf
--- /dev/null
+++ b/packages/g6/__tests__/snapshots/runtime/graph/add-children-data/default.svg
@@ -0,0 +1,591 @@
+
\ No newline at end of file
diff --git a/packages/g6/__tests__/unit/runtime/graph/add-children-data.spec.ts b/packages/g6/__tests__/unit/runtime/graph/add-children-data.spec.ts
new file mode 100644
index 0000000000..7158187aaa
--- /dev/null
+++ b/packages/g6/__tests__/unit/runtime/graph/add-children-data.spec.ts
@@ -0,0 +1,14 @@
+import { layoutCompactBoxBasic } from '@@/demos';
+import { createDemoGraph } from '@@/utils';
+
+describe('add children data', () => {
+ it('default', async () => {
+ const graph = await createDemoGraph(layoutCompactBoxBasic, { animation: false });
+ await expect(graph).toMatchSnapshot(__filename);
+
+ graph.addChildrenData('Rules', [{ id: 'node-1' }, { id: 'node-2' }]);
+ await graph.render();
+
+ await expect(graph).toMatchSnapshot(__filename, 'add-children-data');
+ });
+});
diff --git a/packages/g6/src/runtime/data.ts b/packages/g6/src/runtime/data.ts
index a376dc9575..8ea95cfe93 100644
--- a/packages/g6/src/runtime/data.ts
+++ b/packages/g6/src/runtime/data.ts
@@ -365,6 +365,14 @@ export class DataController {
this.updateNodeLikeHierarchy(combos);
}
+ public addChildrenData(parentId: ID, childrenData: NodeData[]) {
+ const parentData = this.getNodeLikeDatum(parentId) as NodeData;
+ const childrenId = childrenData.map(idOf);
+ this.addNodeData(childrenData);
+ this.updateNodeData([{ id: parentId, children: [...(parentData.children || []), ...childrenId] }]);
+ this.addEdgeData(childrenId.map((childId) => ({ source: parentId, target: childId })));
+ }
+
protected updateNodeLikeHierarchy(data: NodeLikeData[]) {
if (!this.enableUpdateNodeLikeHierarchy) return;
const { model } = this;
diff --git a/packages/g6/src/runtime/graph.ts b/packages/g6/src/runtime/graph.ts
index fd3d897874..0667a2e993 100644
--- a/packages/g6/src/runtime/graph.ts
+++ b/packages/g6/src/runtime/graph.ts
@@ -612,6 +612,21 @@ export class Graph extends EventEmitter {
this.context.model.addComboData(isFunction(data) ? data(this.getComboData()) : data);
}
+ /**
+ * 为树图节点添加子节点数据
+ *
+ * Add child node data to the tree node
+ * @param parentId - 父节点 ID | parent node ID
+ * @param childrenData - 子节点数据 | child node data
+ * @description
+ * 为组合添加子节点使用 addNodeData / addComboData 方法
+ *
+ * Use addNodeData / addComboData method to add child nodes to the combo
+ */
+ public addChildrenData(parentId: ID, childrenData: NodeData[]) {
+ this.context.model.addChildrenData(parentId, childrenData);
+ }
+
/**
* 更新元素数据
*