From c5ff5fde7b4fbe48a34d2c869e956100448aefb6 Mon Sep 17 00:00:00 2001 From: zqlu Date: Wed, 25 Nov 2020 14:05:34 +0800 Subject: [PATCH] fix: auto adjust scale min to zero for column/bar (#2012) * fix: auto adjust scale min to zero for column/bar * Update y-meta-min-max-spec.ts * Update y-meta-min-max-spec.ts Co-authored-by: hustcc --- .../unit/plots/bar/y-meta-min-max-spec.ts | 37 +++++++++++++++++++ .../unit/plots/column/y-meta-min-max-spec.ts | 37 +++++++++++++++++++ src/plots/column/adaptor.ts | 4 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 __tests__/unit/plots/bar/y-meta-min-max-spec.ts create mode 100644 __tests__/unit/plots/column/y-meta-min-max-spec.ts diff --git a/__tests__/unit/plots/bar/y-meta-min-max-spec.ts b/__tests__/unit/plots/bar/y-meta-min-max-spec.ts new file mode 100644 index 0000000000..828804e3db --- /dev/null +++ b/__tests__/unit/plots/bar/y-meta-min-max-spec.ts @@ -0,0 +1,37 @@ +import { Bar } from '../../../../src'; +import { createDiv } from '../../../utils/dom'; + +describe('bar', () => { + it('y min, max meta', () => { + const plot = new Bar(createDiv(), { + width: 400, + height: 300, + yField: 'date', + xField: 'value', + data: [ + { date: 'a', value: 10 }, + { date: 'b', value: 20 }, + ], + }); + + plot.render(); + expect(plot.chart.getScaleByField('value').min).toBe(0); + expect(plot.chart.getScaleByField('value').max).toBe(20); + + plot.update({ + width: 400, + height: 300, + yField: 'date', + xField: 'value', + data: [ + { date: 'a', value: -10 }, + { date: 'b', value: -20 }, + ], + }); + + expect(plot.chart.getScaleByField('value').min).toBe(-20); + expect(plot.chart.getScaleByField('value').max).toBe(0); + + plot.destroy(); + }); +}); diff --git a/__tests__/unit/plots/column/y-meta-min-max-spec.ts b/__tests__/unit/plots/column/y-meta-min-max-spec.ts new file mode 100644 index 0000000000..5476f7ff2f --- /dev/null +++ b/__tests__/unit/plots/column/y-meta-min-max-spec.ts @@ -0,0 +1,37 @@ +import { Column } from '../../../../src'; +import { createDiv } from '../../../utils/dom'; + +describe('column', () => { + it('y min, max meta', () => { + const plot = new Column(createDiv(), { + width: 400, + height: 300, + xField: 'date', + yField: 'value', + data: [ + { date: 'a', value: 10 }, + { date: 'b', value: 20 }, + ], + }); + + plot.render(); + expect(plot.chart.getScaleByField('value').min).toBe(0); + expect(plot.chart.getScaleByField('value').max).toBe(20); + + plot.update({ + width: 400, + height: 300, + xField: 'date', + yField: 'value', + data: [ + { date: 'a', value: -10 }, + { date: 'b', value: -20 }, + ], + }); + + expect(plot.chart.getScaleByField('value').min).toBe(-20); + expect(plot.chart.getScaleByField('value').max).toBe(0); + + plot.destroy(); + }); +}); diff --git a/src/plots/column/adaptor.ts b/src/plots/column/adaptor.ts index 3d518b83bf..c873831d96 100644 --- a/src/plots/column/adaptor.ts +++ b/src/plots/column/adaptor.ts @@ -15,6 +15,7 @@ import { conversionTag } from '../../adaptor/conversion-tag'; import { connectedArea } from '../../adaptor/connected-area'; import { interval } from '../../adaptor/geometries'; import { flow, transformLabel, deepAssign } from '../../utils'; +import { adjustYMetaByZero } from '../../utils/data'; import { percent } from '../../utils/transform/percent'; import { Datum } from '../../types'; import { ColumnOptions } from './types'; @@ -88,7 +89,7 @@ function geometry(params: Params): Params { */ function meta(params: Params): Params { const { options } = params; - const { xAxis, yAxis, xField, yField } = options; + const { xAxis, yAxis, xField, yField, data } = options; return flow( scale( @@ -100,6 +101,7 @@ function meta(params: Params): Params { [xField]: { type: 'cat', }, + [yField]: adjustYMetaByZero(data, yField), } ) )(params);