Skip to content

Commit 3b7b5da

Browse files
committed
fix(line): when data all gt 0, or lt 0, set the default min/max
1 parent feb7541 commit 3b7b5da

File tree

8 files changed

+135
-57
lines changed

8 files changed

+135
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Line } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
4+
describe('line', () => {
5+
it('y min, max meta', () => {
6+
const line = new Line(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+
line.render();
18+
// @ts-ignore
19+
window.line = line;
20+
expect(line.options.meta.value.min).toBe(0);
21+
expect(line.options.meta.value.max).toBe(undefined);
22+
23+
line.update({
24+
width: 400,
25+
height: 300,
26+
xField: 'date',
27+
yField: 'value',
28+
data: [
29+
{ date: 'a', value: -10 },
30+
{ date: 'b', value: -20 },
31+
],
32+
});
33+
34+
expect(line.options.meta.value.min).toBe(undefined);
35+
expect(line.options.meta.value.max).toBe(0);
36+
});
37+
});

__tests__/unit/utils/data-spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { adjustYMetaByZero } from '../../../src/utils/data';
2+
3+
describe('data', () => {
4+
it('adjustYMetaByZero', () => {
5+
expect(adjustYMetaByZero([{ y: 0 }, { y: 1 }], 'y')).toEqual({});
6+
expect(adjustYMetaByZero([{ y: 20 }, { y: 20 }], 'y')).toEqual({ min: 0 });
7+
expect(adjustYMetaByZero([{ y: -20 }, { y: -20 }], 'y')).toEqual({ max: 0 });
8+
});
9+
});

src/plots/line/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { deepMix } from '@antv/util';
1+
import { deepMix, size, reduce, get } from '@antv/util';
22
import { Plot } from '../../core/plot';
33
import { Adaptor } from '../../core/adaptor';
4+
import { adjustYMetaByZero } from '../../utils/data';
45
import { LineOptions } from './types';
56
import { adaptor } from './adaptor';
7+
68
import './interactions';
79

810
export { LineOptions };
@@ -15,7 +17,8 @@ export class Line extends Plot<LineOptions> {
1517
* 获取 折线图 默认配置
1618
*/
1719
protected getDefaultOptions(options: LineOptions) {
18-
const { xField } = options;
20+
const { xField, yField, data } = options;
21+
1922
return deepMix({}, super.getDefaultOptions(), {
2023
tooltip: {
2124
showMarkers: true,
@@ -31,6 +34,9 @@ export class Line extends Plot<LineOptions> {
3134
[xField]: {
3235
range: [0, 1],
3336
},
37+
[yField]: {
38+
...adjustYMetaByZero(data, yField),
39+
},
3440
},
3541
isStack: false,
3642
});

src/types/axis.ts

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,4 @@
11
import { AxisOption } from '@antv/g2/lib/interface';
2+
import { Meta } from './meta';
23

3-
export type Axis = AxisOption & {
4-
/**
5-
* 坐标轴类型
6-
*/
7-
readonly type?: string;
8-
/**
9-
* 是否美化
10-
*/
11-
readonly nice?: boolean;
12-
13-
/**
14-
* 坐标轴最小值
15-
*/
16-
readonly min?: number;
17-
18-
/**
19-
* 坐标轴最大值
20-
*/
21-
readonly max?: number;
22-
23-
/**
24-
* min limit
25-
*/
26-
readonly minLimit?: number;
27-
28-
/**
29-
* max limit
30-
*/
31-
readonly maxLimit?: number;
32-
33-
/**
34-
* 期望的坐标轴刻度数量,非最终结果
35-
*/
36-
readonly tickCount?: number;
37-
38-
/**
39-
* 坐标轴刻度间隔
40-
*/
41-
readonly tickInterval?: number;
42-
43-
/**
44-
* 指定 tick 计算方法或自定义计算 tick 的方法
45-
*/
46-
readonly tickMethod?: string | ((scale: any) => any[]);
47-
};
4+
export type Axis = AxisOption & Omit<Meta, 'alias' | 'values' | 'formatter'>;

src/types/common.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { State } from './state';
1010
import { Slider } from './slider';
1111
import { Scrollbar } from './scrollbar';
1212
import { ColorAttr } from './attr';
13+
import { Meta } from './meta';
1314

1415
/** annotation position */
1516
export { AnnotationPosition, RegionPositionBaseOption, TextOption };
@@ -63,15 +64,6 @@ export type Size = {
6364
readonly height: number;
6465
};
6566

66-
/** scale 元信息,取名为 meta */
67-
export type Meta = {
68-
readonly type?: string;
69-
readonly alias?: string;
70-
readonly values?: string[];
71-
readonly range?: number[];
72-
readonly formatter?: (v: any) => string;
73-
};
74-
7567
/** 基础的 Options 配置 */
7668
export type Options = {
7769
// 画布基本配置
@@ -102,7 +94,7 @@ export type Options = {
10294
/** 具体的数据 */
10395
readonly data: Record<string, any>[];
10496
/** 数据字段元信息 */
105-
readonly meta?: Record<string, any>;
97+
readonly meta?: Record<string, Meta>;
10698

10799
// G2 相关
108100
/** 主题,G2 主题,字符串或者 theme object */

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './tooltip';
33
export * from './state';
44
export * from './attr';
55
export * from './statistic';
6+
export * from './meta';

src/types/meta.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/** scale 元信息,取名为 meta */
2+
export type Meta = {
3+
/**
4+
* 坐标轴类型
5+
*/
6+
readonly type?: string;
7+
/**
8+
* 是否美化
9+
*/
10+
readonly nice?: boolean;
11+
/**
12+
* 坐标轴最小值
13+
*/
14+
readonly min?: number;
15+
/**
16+
* 坐标轴最大值
17+
*/
18+
readonly max?: number;
19+
/**
20+
* min limit
21+
*/
22+
readonly minLimit?: number;
23+
/**
24+
* max limit
25+
*/
26+
readonly maxLimit?: number;
27+
/**
28+
* 期望的坐标轴刻度数量,非最终结果
29+
*/
30+
readonly tickCount?: number;
31+
/**
32+
* 坐标轴刻度间隔
33+
*/
34+
readonly tickInterval?: number;
35+
/**
36+
* 指定 tick 计算方法或自定义计算 tick 的方法
37+
*/
38+
readonly tickMethod?: string | ((scale: any) => any[]);
39+
/**
40+
* 字段别名
41+
*/
42+
readonly alias?: string;
43+
/**
44+
* 指定 scale 中的 values 信息
45+
*/
46+
readonly values?: string[];
47+
/**
48+
* scale 转换的范围,0 ~ 1 的数组,表示开始和结束的位置
49+
*/
50+
readonly range?: number[];
51+
/**
52+
* 格式化 tick 值
53+
*/
54+
readonly formatter?: (v: any) => string;
55+
};

src/utils/data.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { get, size } from '@antv/util';
2+
import sizeSensor from 'size-sensor';
3+
import { Data, Datum, Meta } from '../types';
4+
5+
/**
6+
* 查看数据是否是全负数、或者全正数
7+
* @param data
8+
* @param field
9+
*/
10+
export function adjustYMetaByZero(data: Data, field: string): Meta {
11+
const gtZero = data.every((datum: Datum) => get(datum, [field]) > 0);
12+
const ltZero = data.every((datum: Datum) => get(datum, [field]) < 0);
13+
14+
if (gtZero) {
15+
return { min: 0 };
16+
}
17+
if (ltZero) {
18+
return { max: 0 };
19+
}
20+
return {};
21+
}

0 commit comments

Comments
 (0)