From 88e68a71c8ce7491cb0f1862e672af04c8790d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=BB=BA=E6=96=8C?= Date: Mon, 11 Jan 2021 17:14:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20rose=20changeData=20&&=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/unit/plots/rose/change-data-spec.ts | 129 ++++++++++++++++++ src/plots/rose/index.ts | 9 ++ 2 files changed, 138 insertions(+) create mode 100644 __tests__/unit/plots/rose/change-data-spec.ts diff --git a/__tests__/unit/plots/rose/change-data-spec.ts b/__tests__/unit/plots/rose/change-data-spec.ts new file mode 100644 index 0000000000..a989fa5447 --- /dev/null +++ b/__tests__/unit/plots/rose/change-data-spec.ts @@ -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(); + }); +}); diff --git a/src/plots/rose/index.ts b/src/plots/rose/index.ts index e72f08ad31..561b60ecb6 100644 --- a/src/plots/rose/index.ts +++ b/src/plots/rose/index.ts @@ -44,4 +44,13 @@ export class Rose extends Plot { protected getSchemaAdaptor(): Adaptor { return adaptor; } + + /** + * @override + * @param data + */ + public changeData(data) { + this.updateOption({ data }); + this.chart.changeData(data); + } } From ab4ab750eb4940fd52a82b9f39278e51dced0a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=BB=BA=E6=96=8C?= Date: Mon, 11 Jan 2021 17:39:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=B0=86public=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=BD=AE=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plots/rose/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/plots/rose/index.ts b/src/plots/rose/index.ts index 561b60ecb6..7445d216b7 100644 --- a/src/plots/rose/index.ts +++ b/src/plots/rose/index.ts @@ -10,6 +10,15 @@ export class Rose extends Plot { /** 玫瑰图 */ public type: string = 'rose'; + /** + * @override + * @param data + */ + public changeData(data) { + this.updateOption({ data }); + this.chart.changeData(data); + } + /** * 获取默认的 options 配置项 */ @@ -44,13 +53,4 @@ export class Rose extends Plot { protected getSchemaAdaptor(): Adaptor { return adaptor; } - - /** - * @override - * @param data - */ - public changeData(data) { - this.updateOption({ data }); - this.chart.changeData(data); - } }