Skip to content

Commit

Permalink
feat: 重写瀑布图changeData方法 && 添加单测 (#2212)
Browse files Browse the repository at this point in the history
Co-authored-by: 沈建斌 <wb-sjb709429@antfin.com>
  • Loading branch information
yp0413150120 and 沈建斌 authored Jan 13, 2021
1 parent a842cf6 commit 547d7dd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
46 changes: 46 additions & 0 deletions __tests__/unit/plots/waterfall/change-data-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Waterfall } from '../../../../src';
import { salesByArea } from '../../../data/sales';
import { createDiv } from '../../../utils/dom';

describe('waterfall changeData', () => {
it('changeData: normal', () => {
const waterfall = new Waterfall(createDiv(), {
width: 400,
height: 300,
data: salesByArea,
xField: 'area',
yField: 'sales',
});

waterfall.render();

expect(waterfall.chart.geometries[0].elements.length).toEqual(salesByArea.length + 1);

const newData = salesByArea.slice(0, 4);
waterfall.changeData(newData);
expect(waterfall.chart.geometries[0].elements.length).toEqual(newData.length + 1);
expect(waterfall.options.data).toEqual(newData);

waterfall.destroy();
});

it('changeData: from empty to have data', () => {
const waterfall = new Waterfall(createDiv(), {
width: 400,
height: 300,
data: [],
xField: 'area',
yField: 'sales',
});

waterfall.render();

expect(waterfall.chart.geometries[0].elements.length).toEqual(0);

waterfall.changeData(salesByArea);
expect(waterfall.chart.geometries[0].elements.length).toEqual(salesByArea.length + 1);
expect(waterfall.options.data).toEqual(salesByArea);

waterfall.destroy();
});
});
11 changes: 11 additions & 0 deletions src/plots/waterfall/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Plot } from '../../core/plot';
import { Adaptor } from '../../core/adaptor';
import { WaterfallOptions } from './types';
import { adaptor } from './adaptor';
import { transformData } from './utils';

export { WaterfallOptions };

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

/**
* @override
* @param data
*/
public changeData(data) {
const { xField, yField, total } = this.options;
this.updateOption({ data });
this.chart.changeData(transformData(data, xField, yField, total));
}

/**
* 获取 瀑布图 的适配器
*/
Expand Down

0 comments on commit 547d7dd

Please sign in to comment.