From 16293fe7e69ace8a97365248007fe0433abbd863 Mon Sep 17 00:00:00 2001 From: "lixuefei.1313" Date: Fri, 10 Oct 2025 10:17:02 +0800 Subject: [PATCH 1/3] feat: support nodeDepthKey in sankey --- packages/vgrammar-sankey/src/interface.ts | 2 ++ packages/vgrammar-sankey/src/layout.ts | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/vgrammar-sankey/src/interface.ts b/packages/vgrammar-sankey/src/interface.ts index 80c8da217..68a63d599 100644 --- a/packages/vgrammar-sankey/src/interface.ts +++ b/packages/vgrammar-sankey/src/interface.ts @@ -94,6 +94,8 @@ export interface SankeyOptions { iterations?: number; /** parse the key of node, the defaultValue */ nodeKey?: string | number | ((datum: SankeyNodeDatum) => string | number); + /** parse the key of depth, the defaultValue */ + depthKey?: string | number | ((datum: SankeyNodeDatum) => string | number); /** sort link by this function */ linkSortBy?: (a: SankeyLinkElement, b: SankeyLinkElement) => number; /** sort node by this function */ diff --git a/packages/vgrammar-sankey/src/layout.ts b/packages/vgrammar-sankey/src/layout.ts index 16a1fe469..ccac97ab2 100644 --- a/packages/vgrammar-sankey/src/layout.ts +++ b/packages/vgrammar-sankey/src/layout.ts @@ -83,6 +83,7 @@ export class SankeyLayout { private options: SankeyOptions; private _getNodeKey?: (datum: SankeyNodeDatum) => string; + private _getNodeDepth?: (datum: SankeyNodeDatum) => number; private _alignFunc: ( node: SankeyNodeElement, maxDepth: number, @@ -110,8 +111,12 @@ export class SankeyLayout { this.options = Object.assign({}, SankeyLayout.defaultOptions, options); const keyOption = this.options.nodeKey; const keyFunc = isFunction(keyOption) ? keyOption : keyOption ? field(keyOption as string) : null; - this._getNodeKey = keyFunc; + + const depthOption = this.options.depthKey; + const depthFunc = isFunction(depthOption) ? depthOption : depthOption ? field(depthOption as string) : null; + this._getNodeDepth = depthFunc; + this._logger = Logger.getInstance(); this._alignFunc = isFunction(this.options.setNodeLayer) ? (node: SankeyNodeElement) => { @@ -264,7 +269,7 @@ export class SankeyLayout { nodeMap[nodeKey].value = undefined; } else { const nodeElement: SankeyNodeElement = { - depth, + depth: this._getNodeDepth ? this._getNodeDepth(node) ?? depth : depth, datum: node, index: index, key: nodeKey, @@ -487,7 +492,8 @@ export class SankeyLayout { if (node) { // 防止用户只设置了部分节点的层级 - node.depth = setNodeLayer ? setNodeLayer(node.datum) ?? depth : depth; + const tempDepth = setNodeLayer ? setNodeLayer(node.datum) ?? depth : depth; + node.depth = this._getNodeDepth ? this._getNodeDepth(node) ?? tempDepth : tempDepth; if (setNodeLayer) { maxDepth = Math.max(node.depth, maxDepth); From 3376361c352ea68aa6586dd8e7eab9d895d21660 Mon Sep 17 00:00:00 2001 From: "lixuefei.1313" Date: Fri, 10 Oct 2025 10:19:20 +0800 Subject: [PATCH 2/3] docs: update changlog of rush --- .../feat-support-node-depth_2025-10-10-02-19.json | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 common/changes/@visactor/vgrammar-core/feat-support-node-depth_2025-10-10-02-19.json diff --git a/common/changes/@visactor/vgrammar-core/feat-support-node-depth_2025-10-10-02-19.json b/common/changes/@visactor/vgrammar-core/feat-support-node-depth_2025-10-10-02-19.json new file mode 100644 index 000000000..6d8a5e316 --- /dev/null +++ b/common/changes/@visactor/vgrammar-core/feat-support-node-depth_2025-10-10-02-19.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "comment": "feat: support nodeDepthKey in sankey\n\n", + "type": "none", + "packageName": "@visactor/vgrammar-core" + } + ], + "packageName": "@visactor/vgrammar-core", + "email": "lixuef1313@163.com" +} \ No newline at end of file From 4f59ef9e3138087ee4b2558e819cac7c31f2131f Mon Sep 17 00:00:00 2001 From: "lixuefei.1313" Date: Fri, 10 Oct 2025 11:06:32 +0800 Subject: [PATCH 3/3] feat: support nodeDepthKey in sankey --- packages/vgrammar-sankey/src/layout.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vgrammar-sankey/src/layout.ts b/packages/vgrammar-sankey/src/layout.ts index ccac97ab2..5f92b31b0 100644 --- a/packages/vgrammar-sankey/src/layout.ts +++ b/packages/vgrammar-sankey/src/layout.ts @@ -493,7 +493,7 @@ export class SankeyLayout { if (node) { // 防止用户只设置了部分节点的层级 const tempDepth = setNodeLayer ? setNodeLayer(node.datum) ?? depth : depth; - node.depth = this._getNodeDepth ? this._getNodeDepth(node) ?? tempDepth : tempDepth; + node.depth = this._getNodeDepth ? this._getNodeDepth(node.datum) ?? tempDepth : tempDepth; if (setNodeLayer) { maxDepth = Math.max(node.depth, maxDepth);