Skip to content

Commit c5ff5fd

Browse files
lessmosthustcc
andauthored
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 <[email protected]>
1 parent c4a1794 commit c5ff5fd

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Bar } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
4+
describe('bar', () => {
5+
it('y min, max meta', () => {
6+
const plot = new Bar(createDiv(), {
7+
width: 400,
8+
height: 300,
9+
yField: 'date',
10+
xField: 'value',
11+
data: [
12+
{ date: 'a', value: 10 },
13+
{ date: 'b', value: 20 },
14+
],
15+
});
16+
17+
plot.render();
18+
expect(plot.chart.getScaleByField('value').min).toBe(0);
19+
expect(plot.chart.getScaleByField('value').max).toBe(20);
20+
21+
plot.update({
22+
width: 400,
23+
height: 300,
24+
yField: 'date',
25+
xField: 'value',
26+
data: [
27+
{ date: 'a', value: -10 },
28+
{ date: 'b', value: -20 },
29+
],
30+
});
31+
32+
expect(plot.chart.getScaleByField('value').min).toBe(-20);
33+
expect(plot.chart.getScaleByField('value').max).toBe(0);
34+
35+
plot.destroy();
36+
});
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Column } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
4+
describe('column', () => {
5+
it('y min, max meta', () => {
6+
const plot = new Column(createDiv(), {
7+
width: 400,
8+
height: 300,
9+
xField: 'date',
10+
yField: 'value',
11+
data: [
12+
{ date: 'a', value: 10 },
13+
{ date: 'b', value: 20 },
14+
],
15+
});
16+
17+
plot.render();
18+
expect(plot.chart.getScaleByField('value').min).toBe(0);
19+
expect(plot.chart.getScaleByField('value').max).toBe(20);
20+
21+
plot.update({
22+
width: 400,
23+
height: 300,
24+
xField: 'date',
25+
yField: 'value',
26+
data: [
27+
{ date: 'a', value: -10 },
28+
{ date: 'b', value: -20 },
29+
],
30+
});
31+
32+
expect(plot.chart.getScaleByField('value').min).toBe(-20);
33+
expect(plot.chart.getScaleByField('value').max).toBe(0);
34+
35+
plot.destroy();
36+
});
37+
});

src/plots/column/adaptor.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { conversionTag } from '../../adaptor/conversion-tag';
1515
import { connectedArea } from '../../adaptor/connected-area';
1616
import { interval } from '../../adaptor/geometries';
1717
import { flow, transformLabel, deepAssign } from '../../utils';
18+
import { adjustYMetaByZero } from '../../utils/data';
1819
import { percent } from '../../utils/transform/percent';
1920
import { Datum } from '../../types';
2021
import { ColumnOptions } from './types';
@@ -88,7 +89,7 @@ function geometry(params: Params<ColumnOptions>): Params<ColumnOptions> {
8889
*/
8990
function meta(params: Params<ColumnOptions>): Params<ColumnOptions> {
9091
const { options } = params;
91-
const { xAxis, yAxis, xField, yField } = options;
92+
const { xAxis, yAxis, xField, yField, data } = options;
9293

9394
return flow(
9495
scale(
@@ -100,6 +101,7 @@ function meta(params: Params<ColumnOptions>): Params<ColumnOptions> {
100101
[xField]: {
101102
type: 'cat',
102103
},
104+
[yField]: adjustYMetaByZero(data, yField),
103105
}
104106
)
105107
)(params);

0 commit comments

Comments
 (0)