File tree 8 files changed +135
-57
lines changed
8 files changed +135
-57
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change 1
- import { deepMix } from '@antv/util' ;
1
+ import { deepMix , size , reduce , get } from '@antv/util' ;
2
2
import { Plot } from '../../core/plot' ;
3
3
import { Adaptor } from '../../core/adaptor' ;
4
+ import { adjustYMetaByZero } from '../../utils/data' ;
4
5
import { LineOptions } from './types' ;
5
6
import { adaptor } from './adaptor' ;
7
+
6
8
import './interactions' ;
7
9
8
10
export { LineOptions } ;
@@ -15,7 +17,8 @@ export class Line extends Plot<LineOptions> {
15
17
* 获取 折线图 默认配置
16
18
*/
17
19
protected getDefaultOptions ( options : LineOptions ) {
18
- const { xField } = options ;
20
+ const { xField, yField, data } = options ;
21
+
19
22
return deepMix ( { } , super . getDefaultOptions ( ) , {
20
23
tooltip : {
21
24
showMarkers : true ,
@@ -31,6 +34,9 @@ export class Line extends Plot<LineOptions> {
31
34
[ xField ] : {
32
35
range : [ 0 , 1 ] ,
33
36
} ,
37
+ [ yField ] : {
38
+ ...adjustYMetaByZero ( data , yField ) ,
39
+ } ,
34
40
} ,
35
41
isStack : false ,
36
42
} ) ;
Original file line number Diff line number Diff line change 1
1
import { AxisOption } from '@antv/g2/lib/interface' ;
2
+ import { Meta } from './meta' ;
2
3
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' > ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { State } from './state';
10
10
import { Slider } from './slider' ;
11
11
import { Scrollbar } from './scrollbar' ;
12
12
import { ColorAttr } from './attr' ;
13
+ import { Meta } from './meta' ;
13
14
14
15
/** annotation position */
15
16
export { AnnotationPosition , RegionPositionBaseOption , TextOption } ;
@@ -63,15 +64,6 @@ export type Size = {
63
64
readonly height : number ;
64
65
} ;
65
66
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
-
75
67
/** 基础的 Options 配置 */
76
68
export type Options = {
77
69
// 画布基本配置
@@ -102,7 +94,7 @@ export type Options = {
102
94
/** 具体的数据 */
103
95
readonly data : Record < string , any > [ ] ;
104
96
/** 数据字段元信息 */
105
- readonly meta ?: Record < string , any > ;
97
+ readonly meta ?: Record < string , Meta > ;
106
98
107
99
// G2 相关
108
100
/** 主题,G2 主题,字符串或者 theme object */
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * from './tooltip';
3
3
export * from './state' ;
4
4
export * from './attr' ;
5
5
export * from './statistic' ;
6
+ export * from './meta' ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments