From ef4d49b946ba68a99887681c340f071aa9d66830 Mon Sep 17 00:00:00 2001 From: "liufu.lf" Date: Mon, 22 Mar 2021 14:53:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E9=9D=A2=E7=A7=AF=E5=9B=BE?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20startOnZero?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/plots/area/start-on-zero-spec.ts | 35 +++++++++++++++++++ docs/api/plots/area.en.md | 9 +++++ docs/api/plots/area.zh.md | 10 ++++++ src/plots/area/adaptor.ts | 4 +++ src/plots/area/types.ts | 2 ++ 5 files changed, 60 insertions(+) create mode 100644 __tests__/unit/plots/area/start-on-zero-spec.ts diff --git a/__tests__/unit/plots/area/start-on-zero-spec.ts b/__tests__/unit/plots/area/start-on-zero-spec.ts new file mode 100644 index 0000000000..ab07a26f3c --- /dev/null +++ b/__tests__/unit/plots/area/start-on-zero-spec.ts @@ -0,0 +1,35 @@ +import { Area } from '../../../../src'; +import { createDiv } from '../../../utils/dom'; + +describe('area', () => { + it('start on zero', () => { + const data = [ + { date: '19/05/2006', type: 'FF', value: 3600 }, + { date: '19/06/2006', type: 'FG', value: 2500 }, + { date: '19/07/2006', type: 'Lab', value: -2000 }, + { date: '19/08/2006', type: 'SF', value: 800 }, + { date: '19/09/2006', type: 'Ind/Oth', value: 1700 }, + ]; + const area = new Area(createDiv(), { + width: 400, + height: 300, + data, + xField: 'date', + yField: 'value', + startOnZero: false, + isStack: false, + }); + + area.render(); + const geometry = area.chart.geometries[0]; + // @ts-ignore + expect(geometry.startOnZero).toBeFalsy(); + expect(area.chart.getScaleByField('value').min).toBe(-2000); + area.update({ + startOnZero: true, + }); + // @ts-ignore + expect(area.chart.geometries[0].startOnZero).toBeTruthy(); + area.destroy(); + }); +}); diff --git a/docs/api/plots/area.en.md b/docs/api/plots/area.en.md index 31531ab1cd..1cf2525326 100644 --- a/docs/api/plots/area.en.md +++ b/docs/api/plots/area.en.md @@ -47,6 +47,15 @@ Whether the plot is Stacked Area. Whether the plot is Percent Area. When the plot is Percent Area, isStack is `true` by default. +#### startOnZero + +**optional** _boolean_ _default:_ `true` + +Whether the plot is filled from 0 datum,isStack must be false when used. + +startOnZero-true + +startOnZero-false #### areaStyle **optional** _StyleAttr | Function_ diff --git a/docs/api/plots/area.zh.md b/docs/api/plots/area.zh.md index 1f888985e7..56ea742f84 100644 --- a/docs/api/plots/area.zh.md +++ b/docs/api/plots/area.zh.md @@ -47,6 +47,16 @@ order: 1 是否百分比面积图,百分比时默认开启 isStack。 +#### startOnZero + +**optional** _boolean_ _default:_ `true` + +积图是否从 0 基准线开始填充,使用时 isStack 需为 false。 + +startOnZero-true + +startOnZero-false + #### areaStyle **optional** _StyleAttr | Function_ diff --git a/src/plots/area/adaptor.ts b/src/plots/area/adaptor.ts index c5f7f0214c..e7970d128e 100644 --- a/src/plots/area/adaptor.ts +++ b/src/plots/area/adaptor.ts @@ -29,6 +29,7 @@ function geometry(params: Params): Params { yField, tooltip, seriesField, + startOnZero, } = options; const pointState = pointMapping?.state; @@ -60,6 +61,9 @@ function geometry(params: Params): Params { tooltip: tooltipOptions, // label 不传递给各个 geometry adaptor,由 label adaptor 处理 label: undefined, + args: { + startOnZero, + }, }, }); const second = deepAssign({}, primary, { options: { tooltip: false } }); diff --git a/src/plots/area/types.ts b/src/plots/area/types.ts index fc43376476..0d4218fda4 100644 --- a/src/plots/area/types.ts +++ b/src/plots/area/types.ts @@ -22,4 +22,6 @@ export interface AreaOptions extends Options { readonly line?: LineGeometryOptions['line']; /** 面积图数据点图形样式 */ readonly point?: PointGeometryOptions['point'] & Pick; + /** 积图是否从 0 基准线开始填充 */ + readonly startOnZero?: boolean; } From ab265b0c0dd1d33f22d39b7aba0602c458bf54b4 Mon Sep 17 00:00:00 2001 From: "liufu.lf" Date: Mon, 22 Mar 2021 16:00:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?docs:=20=E7=BE=8E=E5=8C=96=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/plots/area.en.md | 5 +++-- docs/api/plots/area.zh.md | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/api/plots/area.en.md b/docs/api/plots/area.en.md index 1cf2525326..56ff2aba69 100644 --- a/docs/api/plots/area.en.md +++ b/docs/api/plots/area.en.md @@ -53,9 +53,10 @@ Whether the plot is Percent Area. When the plot is Percent Area, isStack is `tru Whether the plot is filled from 0 datum,isStack must be false when used. -startOnZero-true +| true | false | +| ------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | +| startOnZero-true | startOnZero-false | -startOnZero-false #### areaStyle **optional** _StyleAttr | Function_ diff --git a/docs/api/plots/area.zh.md b/docs/api/plots/area.zh.md index 56ea742f84..36253e7535 100644 --- a/docs/api/plots/area.zh.md +++ b/docs/api/plots/area.zh.md @@ -53,9 +53,9 @@ order: 1 积图是否从 0 基准线开始填充,使用时 isStack 需为 false。 -startOnZero-true - -startOnZero-false +| true | false | +| ------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | +| startOnZero-true | startOnZero-false | #### areaStyle From 66dd321b033242afc767e33aa7d2522089ac7862 Mon Sep 17 00:00:00 2001 From: "liufu.lf" Date: Mon, 22 Mar 2021 16:38:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=20series=20field?= =?UTF-8?q?=20=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/unit/plots/area/percent-spec.ts | 17 +++++++++++++++++ __tests__/unit/plots/area/stack-spec.ts | 17 +++++++++++++++++ src/plots/area/adaptor.ts | 4 ++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/__tests__/unit/plots/area/percent-spec.ts b/__tests__/unit/plots/area/percent-spec.ts index 17a9ff4d45..bb7942b8ed 100644 --- a/__tests__/unit/plots/area/percent-spec.ts +++ b/__tests__/unit/plots/area/percent-spec.ts @@ -51,4 +51,21 @@ describe('area', () => { expect(currenData[0].g2plot_percent).toBeUndefined(); area.destroy(); }); + it('percent area with no series field', () => { + const area = new Area(createDiv('percent*area', document.body, 'percent_container'), { + width: 400, + height: 300, + data: percentData, + xField: 'year', + yField: 'value', + isPercent: true, + }); + + area.render(); + + // render + expect(area.options.isPercent).toBe(true); + expect(area.chart.geometries[0].getAdjust('stack')).toBeUndefined(); + area.destroy(); + }); }); diff --git a/__tests__/unit/plots/area/stack-spec.ts b/__tests__/unit/plots/area/stack-spec.ts index a44844db45..a24d7ddba7 100644 --- a/__tests__/unit/plots/area/stack-spec.ts +++ b/__tests__/unit/plots/area/stack-spec.ts @@ -32,6 +32,23 @@ describe('area', () => { // @ts-ignore expect(area.chart.getOptions().legends.type.position).toBe('top-left'); + area.destroy(); + }); + it('stack area with no series field', () => { + const area = new Area(createDiv(), { + width: 400, + height: 300, + data: partySupport.filter((o) => ['FF', 'Lab'].includes(o.type)), + xField: 'date', + yField: 'value', + isStack: true, + }); + + area.render(); + + expect(area.options.isStack).toBe(true); + expect(area.chart.geometries[0].getAdjust('stack')).toBeUndefined(); + area.destroy(); }); }); diff --git a/src/plots/area/adaptor.ts b/src/plots/area/adaptor.ts index e7970d128e..25e0b6ad9d 100644 --- a/src/plots/area/adaptor.ts +++ b/src/plots/area/adaptor.ts @@ -116,8 +116,8 @@ function label(params: Params): Params { */ function adjust(params: Params): Params { const { chart, options } = params; - const { isStack, isPercent } = options; - if (isPercent || isStack) { + const { isStack, isPercent, seriesField } = options; + if ((isPercent || isStack) && seriesField) { each(chart.geometries, (g: Geometry) => { g.adjust('stack'); });