Skip to content

Commit d109a7e

Browse files
committed
chore: update nodeDepth
1 parent afa22a5 commit d109a7e

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

__tests__/bugs/issue-2353-spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('#2353', () => {
2121
];
2222

2323
it('sankey depth config', () => {
24+
let maxDepth;
2425
const sankey = new Sankey(createDiv(), {
2526
data: DATA,
2627
padding: 0,
@@ -31,7 +32,8 @@ describe('#2353', () => {
3132
sourceField: 'source',
3233
targetField: 'target',
3334
weightField: 'weight',
34-
nodeDepth: (node: Datum) => {
35+
nodeDepth: (node: Datum, n) => {
36+
maxDepth = n;
3537
const { name } = node;
3638
return {
3739
n1: 0,
@@ -49,6 +51,8 @@ describe('#2353', () => {
4951
// 指定了 depth,所以 n4 节点的 x 是画布中间
5052
expect(sankey.chart.views[1].geometries[0].elements[3].shape.getBBox().x).toBe(247.5);
5153

54+
expect(maxDepth).toBe(3);
55+
5256
sankey.destroy();
5357
});
5458
});

docs/api/plots/sankey.en.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ The sankey diagram node layout direction, the default is `the justify`, can choo
7171

7272
<description>**optional** _Function_</description>
7373

74-
The sankey diagram node `depth` configure, use function to return the depth value, started from zero.
74+
The sankey diagram node `depth` configure, use function to return the depth value, started from zero, and we need to ensure contains node in every depth level.
7575

7676
```ts
7777
{
78-
nodeDepth: (datum) => {
78+
nodeDepth: (datum, maxDepth) => {
7979
const { name } = datum;
8080
if (name === 'node1') {
8181
return 0;

docs/api/plots/sankey.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ order: 27
7171

7272
<description>**optional** _Function_</description>
7373

74-
桑基图节点的深度配置,使用回调进行自定义,返回 depth 数值,从 0 开始。
74+
桑基图节点的深度配置,使用回调进行自定义,返回 depth 数值,从 0 开始,并且返回值需要保证所有的层级都有节点
7575

7676
```ts
7777
{
78-
nodeDepth: (datum) => {
78+
nodeDepth: (datum, maxDepth) => {
7979
const { name } = datum;
8080
if (name === 'node1') {
8181
return 0;

src/plots/sankey/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface SankeyOptions extends Omit<Options, 'xField' | 'yField' | 'xAxi
4444
*/
4545
readonly nodeSort?: (a: Datum, b: Datum) => number;
4646
/**
47-
* 节点排放分层的顺序,从 0 开始
47+
* 节点排放分层的顺序,从 0 开始,并且返回值需要保证所有的层级都有节点
4848
*/
4949
readonly nodeDepth?: NodeDepth;
5050
/**

src/utils/transform/sankey.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type NodeAlign = keyof typeof ALIGN_METHOD;
6969
/**
7070
* 节点的 depth 自定义
7171
*/
72-
export type NodeDepth = (datum: Datum) => number;
72+
export type NodeDepth = (datum: Datum, maxDepth: number) => number;
7373

7474
/**
7575
* 布局参数的定义
@@ -107,7 +107,7 @@ const DEFAULT_OPTIONS: Partial<SankeyLayoutOptions> = {
107107
*/
108108
export function getNodeAlignFunction(nodeAlign: NodeAlign, nodeDepth: SankeyOptions['nodeDepth']) {
109109
if (nodeDepth) {
110-
return (node) => nodeDepth(pick(node, ['name']) as any);
110+
return (node, maxDepth) => nodeDepth(node, maxDepth);
111111
}
112112

113113
const func = isString(nodeAlign) ? ALIGN_METHOD[nodeAlign] : isFunction(nodeAlign) ? nodeAlign : null;

0 commit comments

Comments
 (0)