Skip to content

Commit 1b3b07a

Browse files
authored
fix(chord): 修复和弦图 state 设置不生效 (#2633)
* fix(chord): 修复和弦图 state 设置不生效 同 dual-axes 存在多view的情况 * test(chord): 补充和弦图单测
1 parent 0882b32 commit 1b3b07a

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Chord } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
import { simulateMouseEvent } from '../../../utils/event';
4+
import { populationMovementData as DATA } from '../../../data/chord-population';
5+
6+
describe('chord: state', () => {
7+
const plot = new Chord(createDiv(), {
8+
height: 500,
9+
data: DATA,
10+
sourceField: 'source',
11+
targetField: 'target',
12+
weightField: 'value',
13+
});
14+
15+
plot.render();
16+
17+
it('set state', async () => {
18+
plot.render();
19+
20+
plot.setState('selected', (data) => (data as any).name === DATA[0].source);
21+
expect(plot.getStates().length).toBe(1);
22+
23+
plot.chart.views[1].geometries[0].elements[0].setState('selected', false);
24+
expect(plot.getStates().length).toBe(0);
25+
26+
plot.chart.views[1].geometries[0].elements[0].setState('selected', true);
27+
expect(plot.getStates().length).toBe(1);
28+
29+
// 取消 selected
30+
plot.setState('selected', (data) => (data as any).name === DATA[0].source, false);
31+
expect(plot.getStates().length).toBe(0);
32+
});
33+
34+
it('interactions + getState', () => {
35+
plot.update({ interactions: [{ type: 'element-active' }] });
36+
37+
expect(plot.getStates().length).toBe(0);
38+
// edge view
39+
const element = plot.chart.views[0].geometries[0].elements[0];
40+
simulateMouseEvent(element.shape, 'mouseenter');
41+
expect(plot.getStates().length).toBe(1);
42+
expect(plot.getStates()[0].data).toMatchObject({ source: DATA[0].source });
43+
expect(plot.getStates()[0].state).toBe('active');
44+
});
45+
46+
afterAll(() => {
47+
plot.destroy();
48+
});
49+
});

src/plots/chord/index.ts

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { each } from '@antv/util';
2+
import { Element } from '@antv/g2';
13
import { Plot } from '../../core/plot';
24
import { Adaptor } from '../../core/adaptor';
3-
import { ChordOptions } from './types';
5+
import { StateName, StateCondition, StateObject } from '../../types';
6+
import { getAllElementsRecursively } from '../../utils';
47
import { adaptor } from './adaptor';
58
import { DEFAULT_OPTIONS } from './constant';
9+
import { ChordOptions } from './types';
610

711
export type { ChordOptions };
812

@@ -20,6 +24,42 @@ export class Chord extends Plot<ChordOptions> {
2024
/** 图表类型 */
2125
public type: string = 'chord';
2226

27+
/**
28+
* @override
29+
* 设置状态
30+
* @param type 状态类型,支持 'active' | 'inactive' | 'selected' 三种
31+
* @param conditions 条件,支持数组
32+
* @param status 是否激活,默认 true
33+
*/
34+
public setState(type: StateName, condition: StateCondition, status: boolean = true) {
35+
const elements = getAllElementsRecursively(this.chart);
36+
37+
each(elements, (ele: Element) => {
38+
if (condition(ele.getData())) {
39+
ele.setState(type, status);
40+
}
41+
});
42+
}
43+
44+
/**
45+
* @override
46+
* 获取状态
47+
*/
48+
public getStates(): StateObject[] {
49+
const elements = getAllElementsRecursively(this.chart);
50+
51+
const stateObjects: StateObject[] = [];
52+
each(elements, (element: Element) => {
53+
const data = element.getData();
54+
const states = element.getStates();
55+
each(states, (state) => {
56+
stateObjects.push({ data, state, geometry: element.geometry, element });
57+
});
58+
});
59+
60+
return stateObjects;
61+
}
62+
2363
protected getDefaultOptions() {
2464
return Chord.getDefaultOptions();
2565
}

0 commit comments

Comments
 (0)