Skip to content

Commit adc8883

Browse files
authored
feat(sankey): 分别对 node 和 edge 添加 交互属性配置 (#3081)
* feat(sankey): 分别对 node 和 edge 添加 交互属性配置 * feat(sankey): 分别对 node 和 edge 添加 交互属性配置 -2 Co-authored-by: ai-qing-hai <[email protected]>
1 parent 052b505 commit adc8883

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Sankey } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
import { delay } from '../../../utils/delay';
4+
import { ALIPAY_DATA } from '../../../data/sankey-energy';
5+
6+
describe('sankey', () => {
7+
it('changeData', async () => {
8+
const data = ALIPAY_DATA.slice(0, ALIPAY_DATA.length - 5);
9+
const sankey = new Sankey(createDiv(), {
10+
height: 500,
11+
data,
12+
sourceField: 'source',
13+
targetField: 'target',
14+
weightField: 'value',
15+
edgeInteractions: [{ type: 'element-single-selected', enable: true }],
16+
nodeInteractions: [{ type: 'element-highlight', enable: true }],
17+
});
18+
19+
sankey.render();
20+
21+
expect(Object.keys(sankey.chart.views[0].interactions)[0]).toBe('element-single-selected');
22+
expect(Object.keys(sankey.chart.views[1].interactions)[0]).toBe('element-highlight');
23+
24+
sankey.update({
25+
edgeInteractions: [{ type: 'element-single-selected', enable: false }],
26+
nodeInteractions: [{ type: 'element-highlight', enable: false }],
27+
});
28+
29+
expect(sankey.chart.views[0].interactions).toEqual({});
30+
expect(sankey.chart.views[1].interactions).toEqual({});
31+
32+
sankey.destroy();
33+
});
34+
});

docs/api/plots/sankey.en.md

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ State style configuration of Sankey node.
5757

5858
`markdown:docs/common/state-style.zh.md`
5959

60+
#### nodeInteractions
61+
62+
<description>**optional** _Interaction[]_</description>
63+
64+
Interaction configuration of Sankey node.
65+
6066
#### edgeStyle
6167

6268
<description>**optional** _StyleAttr | Function_</description>
@@ -73,6 +79,12 @@ State style configuration of Sankey edge.
7379

7480
`markdown:docs/common/color.en.md`
7581

82+
#### edgeInteractions
83+
84+
<description>**optional** _Interaction[]_</description>
85+
86+
Interaction configuration of Sankey edge.
87+
7688
#### nodeWidthRatio
7789

7890
<description>**optional** _number_</description>

docs/api/plots/sankey.zh.md

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ order: 27
5757

5858
`markdown:docs/common/state-style.zh.md`
5959

60+
#### nodeInteractions
61+
62+
<description>**optional** _Interaction[]_</description>
63+
64+
桑基图节点交互的配置。
65+
6066
#### edgeStyle
6167

6268
<description>**optional** _StyleAttr | Function_</description>
@@ -73,6 +79,12 @@ order: 27
7379

7480
`markdown:docs/common/state-style.zh.md`
7581

82+
#### nodeInteractions
83+
84+
<description>**optional** _Interaction[]_</description>
85+
86+
桑基图边交互的配置。
87+
7688
#### nodeWidthRatio
7789

7890
<description>**optional** _number_</description>

src/plots/sankey/adaptor.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { uniq } from '@antv/util';
2-
import { interaction, theme } from '../../adaptor/common';
2+
import { theme } from '../../adaptor/common';
33
import { Params } from '../../core/adaptor';
44
import { deepAssign, flow } from '../../utils';
55
import { polygon, edge } from '../../adaptor/geometries';
@@ -146,6 +146,39 @@ export function nodeDraggable(params: Params<SankeyOptions>): Params<SankeyOptio
146146
return params;
147147
}
148148

149+
/**
150+
* Interaction 配置
151+
* @param params
152+
*/
153+
function interaction(params: Params<SankeyOptions>): Params<SankeyOptions> {
154+
const { chart, options } = params;
155+
const { interactions = [] } = options;
156+
157+
const nodeInteractions = [].concat(interactions, options.nodeInteractions || []);
158+
const edgeInteractions = [].concat(interactions, options.edgeInteractions || []);
159+
160+
const nodeView = chart.views[0];
161+
const edgeView = chart.views[1];
162+
163+
nodeInteractions.forEach((i) => {
164+
if (i?.enable === false) {
165+
nodeView.removeInteraction(i.type);
166+
} else {
167+
nodeView.interaction(i.type, i.cfg || {});
168+
}
169+
});
170+
171+
edgeInteractions.forEach((i) => {
172+
if (i?.enable === false) {
173+
edgeView.removeInteraction(i.type);
174+
} else {
175+
edgeView.interaction(i.type, i.cfg || {});
176+
}
177+
});
178+
179+
return params;
180+
}
181+
149182
/**
150183
* 图适配器
151184
* @param chart

src/plots/sankey/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,12 @@ export interface SankeyOptions extends Omit<Options, 'data' | 'xField' | 'yField
115115
* 节点位置是否可以拖拽,默认为 false
116116
*/
117117
readonly nodeDraggable?: boolean;
118+
/**
119+
* 边交互
120+
*/
121+
readonly edgeInteractions?: Options['interactions'];
122+
/**
123+
* 节点交互
124+
*/
125+
readonly nodeInteractions?: Options['interactions'];
118126
}

0 commit comments

Comments
 (0)