1
1
import { IGroup } from '@antv/g2' ;
2
2
import { Gauge } from '../../../../../src' ;
3
- import { MASK_VIEW_ID } from '../../../../../src/plots/gauge/constants' ;
4
3
import { createDiv } from '../../../../utils/dom' ;
4
+ import { getPixelColor } from '../../../../utils/getPixelColor' ;
5
5
6
6
describe ( 'gauge' , ( ) => {
7
7
it ( 'no indicator' , async ( ) => {
@@ -92,15 +92,20 @@ describe('gauge', () => {
92
92
93
93
gauge . render ( ) ;
94
94
95
- expect ( gauge . chart . views . length ) . toBe ( 2 ) ;
96
-
95
+ expect ( gauge . chart . views . length ) . toBe ( 1 ) ;
97
96
gauge . update ( { indicator : { } } ) ;
98
- expect ( gauge . chart . views . length ) . toBe ( 3 ) ;
99
- expect ( gauge . chart . views [ 2 ] . id ) . toBe ( MASK_VIEW_ID ) ;
97
+ expect ( gauge . chart . views . length ) . toBe ( 2 ) ;
100
98
101
99
gauge . destroy ( ) ;
102
100
} ) ;
103
101
102
+ function getAllShapes ( view ) {
103
+ return view . geometries [ 0 ] . elements . reduce ( ( r , ele ) => {
104
+ r . push ( ...( ele . shape as IGroup ) . getChildren ( ) ) ;
105
+ return r ;
106
+ } , [ ] ) ;
107
+ }
108
+
104
109
it ( 'meter gauge: custom steps' , ( ) => {
105
110
const gauge = new Gauge ( createDiv ( ) , {
106
111
type : 'meter' ,
@@ -110,10 +115,18 @@ describe('gauge', () => {
110
115
} ) ;
111
116
112
117
gauge . render ( ) ;
113
-
114
- expect ( gauge . chart . views . length ) . toBe ( 2 ) ;
115
- const maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
116
- expect ( maskShape . getChildren ( ) . length ) . toBe ( 99 ) ;
118
+ expect ( gauge . chart . views . length ) . toBe ( 1 ) ;
119
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBe ( 100 ) ;
120
+
121
+ for ( let i = 0 ; i < 10 ; i ++ ) {
122
+ const steps = ( 100 * Math . random ( ) ) | 0 ;
123
+ gauge . update ( { meter : { steps } , range : { ticks : [ 0 , 1 / 3 , 2 / 3 , 1 ] } } ) ;
124
+ // 存在交接
125
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBeGreaterThanOrEqual ( steps ) ;
126
+ // 不存在交接
127
+ gauge . update ( { meter : { steps } , range : { ticks : [ 0 , 1 ] } } ) ;
128
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBe ( steps ) ;
129
+ }
117
130
118
131
gauge . destroy ( ) ;
119
132
} ) ;
@@ -123,26 +136,21 @@ describe('gauge', () => {
123
136
type : 'meter' ,
124
137
percent : 0.75 ,
125
138
indicator : null ,
139
+ range : { ticks : [ 0 , 1 ] } ,
126
140
meter : { steps : 100 , stepRatio : 1 } ,
127
141
} ) ;
128
142
129
143
gauge . render ( ) ;
130
-
131
- expect ( gauge . chart . views . length ) . toBe ( 2 ) ;
132
- let maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
133
- expect ( maskShape . getChildren ( ) . length ) . toBe ( 0 ) ;
144
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBe ( 1 ) ;
134
145
135
146
gauge . update ( { meter : { steps : 40 , stepRatio : 0.2 } } ) ;
136
- maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
137
- expect ( maskShape . getChildren ( ) . length ) . toBe ( 39 ) ;
147
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBe ( 40 ) ;
138
148
139
149
gauge . update ( { meter : { stepRatio : 1.2 } } ) ;
140
- maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
141
- expect ( maskShape . getChildren ( ) . length ) . toBe ( 0 ) ;
150
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBe ( 1 ) ;
142
151
143
152
gauge . update ( { meter : { stepRatio : - 0.2 } } ) ;
144
- maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
145
- expect ( maskShape . getChildren ( ) . length ) . toBe ( 0 ) ;
153
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBe ( 1 ) ;
146
154
147
155
gauge . destroy ( ) ;
148
156
} ) ;
@@ -157,37 +165,16 @@ describe('gauge', () => {
157
165
158
166
gauge . render ( ) ;
159
167
160
- expect ( gauge . chart . views . length ) . toBe ( 2 ) ;
161
- const maskShape1 = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
162
- expect ( maskShape1 . getChildren ( ) . length ) . toBe ( 99 ) ;
168
+ expect ( gauge . chart . views . length ) . toBe ( 1 ) ;
169
+ expect ( getAllShapes ( gauge . chart . views [ 0 ] ) . length ) . toBeGreaterThanOrEqual ( 100 ) ;
163
170
164
- const shape1 = maskShape1 . getChildren ( ) [ 0 ] . getBBox ( ) ;
171
+ const bbox1 = getAllShapes ( gauge . chart . views [ 0 ] ) [ 0 ] . getBBox ( ) ;
165
172
166
173
gauge . update ( { indicator : { } } ) ;
167
- expect ( gauge . chart . views . length ) . toBe ( 3 ) ;
168
- const maskShape2 = gauge . chart . views [ 2 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
169
- expect ( shape1 ) . toEqual ( maskShape2 . getChildren ( ) [ 0 ] . getBBox ( ) ) ;
170
-
171
- gauge . destroy ( ) ;
172
- } ) ;
173
-
174
- it ( 'meter gauge: custom theme.background' , ( ) => {
175
- const gauge = new Gauge ( createDiv ( ) , {
176
- type : 'meter' ,
177
- percent : 0.75 ,
178
- indicator : null ,
179
- theme : { background : 'red' } ,
180
- } ) ;
181
-
182
- gauge . render ( ) ;
183
-
174
+ // indicator 绘制在 view0
175
+ const group2 = getAllShapes ( gauge . chart . views [ 1 ] ) [ 0 ] ;
184
176
expect ( gauge . chart . views . length ) . toBe ( 2 ) ;
185
- let maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
186
- expect ( maskShape . getChildren ( ) [ 0 ] . attr ( 'fill' ) ) . toBe ( 'red' ) ;
187
-
188
- gauge . update ( { theme : 'dark' } ) ;
189
- maskShape = gauge . chart . views [ 1 ] . geometries [ 0 ] . elements [ 0 ] . shape as IGroup ;
190
- expect ( maskShape . getChildren ( ) [ 0 ] . attr ( 'fill' ) ) . toBe ( gauge . chart . getTheme ( ) . background ) ;
177
+ expect ( bbox1 ) . toEqual ( group2 . getBBox ( ) ) ;
191
178
192
179
gauge . destroy ( ) ;
193
180
} ) ;
0 commit comments