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 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..5f92b31b0 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.datum) ?? tempDepth : tempDepth; if (setNodeLayer) { maxDepth = Math.max(node.depth, maxDepth);