Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 玫瑰图修改 changeData && 添加单测 #2200

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions __tests__/unit/plots/rose/change-data-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { salesByArea, subSalesByArea } from '../../../data/sales';
import { Rose } from '../../../../src';
import { createDiv } from '../../../utils/dom';

describe('rose-changeData', () => {
it('basic rose: change Data', () => {
const rosePlot = new Rose(createDiv(), {
width: 400,
height: 300,
data: salesByArea,
xField: 'area',
yField: 'sales',
});

rosePlot.render();
expect(rosePlot.chart.geometries[0].elements.length).toEqual(salesByArea.length);

const newData = salesByArea.slice(0, 3);
rosePlot.changeData(newData);
expect(rosePlot.chart.geometries[0].elements.length).toEqual(newData.length);
expect(rosePlot.options.data).toEqual(newData);

rosePlot.destroy();
});

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

rosePlot.render();

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

rosePlot.destroy();
});

it('group rose: change data', () => {
const rosePlot = new Rose(createDiv(), {
width: 400,
height: 300,
data: subSalesByArea,
xField: 'area',
yField: 'sales',
isGroup: true,
seriesField: 'series',
});

rosePlot.render();

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

const newData = subSalesByArea.filter((item) => item.area !== '东北' || item.series !== '消费者');
rosePlot.changeData(newData);
expect(rosePlot.chart.geometries[0].elements.length).toEqual(newData.length);
expect(rosePlot.options.data).toEqual(newData);

rosePlot.destroy();
});

it('group rose: from empty to have data', () => {
const rosePlot = new Rose(createDiv(), {
width: 400,
height: 300,
data: [],
xField: 'area',
yField: 'sales',
isGroup: true,
seriesField: 'series',
});

rosePlot.render();

rosePlot.changeData(subSalesByArea);
expect(rosePlot.chart.geometries[0].elements.length).toEqual(subSalesByArea.length);
expect(rosePlot.options.data).toEqual(subSalesByArea);

rosePlot.destroy();
});

it('stacked rose: change data', () => {
const rosePlot = new Rose(createDiv(), {
width: 400,
height: 300,
data: subSalesByArea,
xField: 'area',
yField: 'sales',
isStack: true,
seriesField: 'series',
});

rosePlot.render();

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

const newData = subSalesByArea.filter((item) => item.area !== '东北' || item.series !== '消费者');
rosePlot.changeData(newData);
expect(rosePlot.chart.geometries[0].elements.length).toEqual(newData.length);
expect(rosePlot.options.data).toEqual(newData);

rosePlot.destroy();
});

it('stack rose: from empty to have data', () => {
const rosePlot = new Rose(createDiv(), {
width: 400,
height: 300,
data: [],
xField: 'area',
yField: 'sales',
isStack: true,
seriesField: 'series',
});

rosePlot.render();

rosePlot.changeData(subSalesByArea);
expect(rosePlot.chart.geometries[0].elements.length).toEqual(subSalesByArea.length);
expect(rosePlot.options.data).toEqual(subSalesByArea);

rosePlot.destroy();
});
});
9 changes: 9 additions & 0 deletions src/plots/rose/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export class Rose extends Plot<RoseOptions> {
/** 玫瑰图 */
public type: string = 'rose';

/**
* @override
* @param data
*/
public changeData(data) {
this.updateOption({ data });
this.chart.changeData(data);
}

/**
* 获取默认的 options 配置项
*/
Expand Down