Skip to content

Commit eb44c0f

Browse files
yp0413150120沈建斌
and
沈建斌
authored
feat: 玫瑰图修改 changeData && 添加单测 (#2200)
* feat: rose changeData && 添加单测 * fix: 将public方法置前 Co-authored-by: 沈建斌 <[email protected]>
1 parent a817274 commit eb44c0f

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { salesByArea, subSalesByArea } from '../../../data/sales';
2+
import { Rose } from '../../../../src';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('rose-changeData', () => {
6+
it('basic rose: change Data', () => {
7+
const rosePlot = new Rose(createDiv(), {
8+
width: 400,
9+
height: 300,
10+
data: salesByArea,
11+
xField: 'area',
12+
yField: 'sales',
13+
});
14+
15+
rosePlot.render();
16+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(salesByArea.length);
17+
18+
const newData = salesByArea.slice(0, 3);
19+
rosePlot.changeData(newData);
20+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(newData.length);
21+
expect(rosePlot.options.data).toEqual(newData);
22+
23+
rosePlot.destroy();
24+
});
25+
26+
it('basic rose: from empty to have data', () => {
27+
const rosePlot = new Rose(createDiv(), {
28+
width: 400,
29+
height: 300,
30+
data: [],
31+
xField: 'area',
32+
yField: 'sales',
33+
});
34+
35+
rosePlot.render();
36+
37+
rosePlot.changeData(salesByArea);
38+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(salesByArea.length);
39+
expect(rosePlot.options.data).toEqual(salesByArea);
40+
41+
rosePlot.destroy();
42+
});
43+
44+
it('group rose: change data', () => {
45+
const rosePlot = new Rose(createDiv(), {
46+
width: 400,
47+
height: 300,
48+
data: subSalesByArea,
49+
xField: 'area',
50+
yField: 'sales',
51+
isGroup: true,
52+
seriesField: 'series',
53+
});
54+
55+
rosePlot.render();
56+
57+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(subSalesByArea.length);
58+
59+
const newData = subSalesByArea.filter((item) => item.area !== '东北' || item.series !== '消费者');
60+
rosePlot.changeData(newData);
61+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(newData.length);
62+
expect(rosePlot.options.data).toEqual(newData);
63+
64+
rosePlot.destroy();
65+
});
66+
67+
it('group rose: from empty to have data', () => {
68+
const rosePlot = new Rose(createDiv(), {
69+
width: 400,
70+
height: 300,
71+
data: [],
72+
xField: 'area',
73+
yField: 'sales',
74+
isGroup: true,
75+
seriesField: 'series',
76+
});
77+
78+
rosePlot.render();
79+
80+
rosePlot.changeData(subSalesByArea);
81+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(subSalesByArea.length);
82+
expect(rosePlot.options.data).toEqual(subSalesByArea);
83+
84+
rosePlot.destroy();
85+
});
86+
87+
it('stacked rose: change data', () => {
88+
const rosePlot = new Rose(createDiv(), {
89+
width: 400,
90+
height: 300,
91+
data: subSalesByArea,
92+
xField: 'area',
93+
yField: 'sales',
94+
isStack: true,
95+
seriesField: 'series',
96+
});
97+
98+
rosePlot.render();
99+
100+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(subSalesByArea.length);
101+
102+
const newData = subSalesByArea.filter((item) => item.area !== '东北' || item.series !== '消费者');
103+
rosePlot.changeData(newData);
104+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(newData.length);
105+
expect(rosePlot.options.data).toEqual(newData);
106+
107+
rosePlot.destroy();
108+
});
109+
110+
it('stack rose: from empty to have data', () => {
111+
const rosePlot = new Rose(createDiv(), {
112+
width: 400,
113+
height: 300,
114+
data: [],
115+
xField: 'area',
116+
yField: 'sales',
117+
isStack: true,
118+
seriesField: 'series',
119+
});
120+
121+
rosePlot.render();
122+
123+
rosePlot.changeData(subSalesByArea);
124+
expect(rosePlot.chart.geometries[0].elements.length).toEqual(subSalesByArea.length);
125+
expect(rosePlot.options.data).toEqual(subSalesByArea);
126+
127+
rosePlot.destroy();
128+
});
129+
});

src/plots/rose/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export class Rose extends Plot<RoseOptions> {
1010
/** 玫瑰图 */
1111
public type: string = 'rose';
1212

13+
/**
14+
* @override
15+
* @param data
16+
*/
17+
public changeData(data) {
18+
this.updateOption({ data });
19+
this.chart.changeData(data);
20+
}
21+
1322
/**
1423
* 获取默认的 options 配置项
1524
*/

0 commit comments

Comments
 (0)