Skip to content

Commit 547d7dd

Browse files
yp0413150120沈建斌
and
沈建斌
authored
feat: 重写瀑布图changeData方法 && 添加单测 (#2212)
Co-authored-by: 沈建斌 <[email protected]>
1 parent a842cf6 commit 547d7dd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Waterfall } from '../../../../src';
2+
import { salesByArea } from '../../../data/sales';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('waterfall changeData', () => {
6+
it('changeData: normal', () => {
7+
const waterfall = new Waterfall(createDiv(), {
8+
width: 400,
9+
height: 300,
10+
data: salesByArea,
11+
xField: 'area',
12+
yField: 'sales',
13+
});
14+
15+
waterfall.render();
16+
17+
expect(waterfall.chart.geometries[0].elements.length).toEqual(salesByArea.length + 1);
18+
19+
const newData = salesByArea.slice(0, 4);
20+
waterfall.changeData(newData);
21+
expect(waterfall.chart.geometries[0].elements.length).toEqual(newData.length + 1);
22+
expect(waterfall.options.data).toEqual(newData);
23+
24+
waterfall.destroy();
25+
});
26+
27+
it('changeData: from empty to have data', () => {
28+
const waterfall = new Waterfall(createDiv(), {
29+
width: 400,
30+
height: 300,
31+
data: [],
32+
xField: 'area',
33+
yField: 'sales',
34+
});
35+
36+
waterfall.render();
37+
38+
expect(waterfall.chart.geometries[0].elements.length).toEqual(0);
39+
40+
waterfall.changeData(salesByArea);
41+
expect(waterfall.chart.geometries[0].elements.length).toEqual(salesByArea.length + 1);
42+
expect(waterfall.options.data).toEqual(salesByArea);
43+
44+
waterfall.destroy();
45+
});
46+
});

src/plots/waterfall/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Plot } from '../../core/plot';
22
import { Adaptor } from '../../core/adaptor';
33
import { WaterfallOptions } from './types';
44
import { adaptor } from './adaptor';
5+
import { transformData } from './utils';
56

67
export { WaterfallOptions };
78

@@ -12,6 +13,16 @@ export class Waterfall extends Plot<WaterfallOptions> {
1213
/** 图表类型 */
1314
public readonly type: string = 'waterfall';
1415

16+
/**
17+
* @override
18+
* @param data
19+
*/
20+
public changeData(data) {
21+
const { xField, yField, total } = this.options;
22+
this.updateOption({ data });
23+
this.chart.changeData(transformData(data, xField, yField, total));
24+
}
25+
1526
/**
1627
* 获取 瀑布图 的适配器
1728
*/

0 commit comments

Comments
 (0)