Skip to content

Commit

Permalink
fix: auto adjust scale min to zero for column/bar (#2012)
Browse files Browse the repository at this point in the history
* 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 <i@hust.cc>
  • Loading branch information
lessmost and hustcc authored Nov 25, 2020
1 parent c4a1794 commit c5ff5fd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
37 changes: 37 additions & 0 deletions __tests__/unit/plots/bar/y-meta-min-max-spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
37 changes: 37 additions & 0 deletions __tests__/unit/plots/column/y-meta-min-max-spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
4 changes: 3 additions & 1 deletion src/plots/column/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -88,7 +89,7 @@ function geometry(params: Params<ColumnOptions>): Params<ColumnOptions> {
*/
function meta(params: Params<ColumnOptions>): Params<ColumnOptions> {
const { options } = params;
const { xAxis, yAxis, xField, yField } = options;
const { xAxis, yAxis, xField, yField, data } = options;

return flow(
scale(
Expand All @@ -100,6 +101,7 @@ function meta(params: Params<ColumnOptions>): Params<ColumnOptions> {
[xField]: {
type: 'cat',
},
[yField]: adjustYMetaByZero(data, yField),
}
)
)(params);
Expand Down

0 comments on commit c5ff5fd

Please sign in to comment.