1
- import { isNumber } from '@antv/util' ;
1
+ import { isNumber , min , max } from '@antv/util' ;
2
2
import { Params } from '../../core/adaptor' ;
3
3
import { flow , deepAssign } from '../../utils' ;
4
4
import { point } from '../../adaptor/geometries' ;
@@ -8,18 +8,36 @@ import { getQuadrantDefaultConfig, getPath, getMeta } from './util';
8
8
import { ScatterOptions } from './types' ;
9
9
10
10
/**
11
- * 散点图 data.length === 1 时居中显示,
11
+ * 散点图默认美观
12
+ * ① data.length === 1 ② 所有数据 y 值相等 ③ 所有数据 x 值相等
12
13
* @param params
13
14
* @returns params
14
15
*/
15
16
export function transformOptions ( options : ScatterOptions ) : ScatterOptions {
16
- const { data = [ ] } = options ;
17
- // 仅对 data.length === 1 的情况进行处理
18
- if ( data . length === 1 ) {
19
- return deepAssign ( { } , options , {
20
- meta : getMeta ( options ) ,
21
- } ) ;
17
+ const { data = [ ] , xField, yField } = options ;
18
+
19
+ if ( data . length ) {
20
+ const xValues = data . map ( ( d ) => d [ xField ] ) ;
21
+ const minX = min ( xValues ) ;
22
+ const maxX = max ( xValues ) ;
23
+ const yValues = data . map ( ( d ) => d [ yField ] ) ;
24
+ const minY = min ( yValues ) ;
25
+ const maxY = max ( yValues ) ;
26
+ if ( minX === maxX && minY === maxY ) {
27
+ return deepAssign ( { } , options , {
28
+ meta : getMeta ( options , [ 'x' , 'y' ] ) ,
29
+ } ) ;
30
+ } else if ( minX === maxX ) {
31
+ return deepAssign ( { } , options , {
32
+ meta : getMeta ( options , [ 'x' ] ) ,
33
+ } ) ;
34
+ } else if ( minY === maxY ) {
35
+ return deepAssign ( { } , options , {
36
+ meta : getMeta ( options , [ 'y' ] ) ,
37
+ } ) ;
38
+ }
22
39
}
40
+
23
41
return options ;
24
42
}
25
43
@@ -84,14 +102,9 @@ function geometry(params: Params<ScatterOptions>): Params<ScatterOptions> {
84
102
*/
85
103
export function meta ( params : Params < ScatterOptions > ) : Params < ScatterOptions > {
86
104
const { options } = params ;
87
- const { data, xAxis, yAxis, xField, yField } = options ;
88
-
89
- let newOptions = options ;
90
- // 仅对 data.length === 1 的情况进行处理
91
- if ( data . length === 1 ) {
92
- newOptions = transformOptions ( deepAssign ( { } , options , { meta : getMeta ( options ) } ) ) ;
93
- }
105
+ const { xAxis, yAxis, xField, yField } = options ;
94
106
107
+ const newOptions = transformOptions ( options ) ;
95
108
return flow (
96
109
scale ( {
97
110
[ xField ] : xAxis ,
0 commit comments