@@ -87,16 +87,17 @@ describe('中心文本 - 指标卡', () => {
87
87
} ) ;
88
88
89
89
it ( '自定义中心文本内容: title & content, 动态数据' , ( ) => {
90
+ const totalValue = config . data . reduce ( ( a , b ) => a + b . value , 0 ) ;
90
91
const pie = new Pie ( createDiv ( ) , {
91
92
...config ,
92
93
innerRadius : 0.64 ,
93
94
statistic : {
94
95
title : {
95
- formatter : ( item , data ) => ( Array . isArray ( data ) ? '总计' : data [ 'type' ] ) ,
96
+ formatter : ( datum ) => ( ! datum ? '总计' : datum [ 'type' ] ) ,
96
97
} ,
97
98
content : {
98
- formatter : ( item , data ) => {
99
- return Array . isArray ( data ) ? ' test\ntest' : typeof data . value === 'number' ? ` ${ data . value } ` : '' ;
99
+ formatter : ( datum , data ) => {
100
+ return ! datum ? ` test\ntest ${ data . reduce ( ( a , b ) => a + b . value , 0 ) } ` : ` ${ datum . value } ` ;
100
101
} ,
101
102
rotate : ( 30 / 180 ) * Math . PI ,
102
103
} ,
@@ -108,7 +109,7 @@ describe('中心文本 - 指标卡', () => {
108
109
const annotations = getAnnotations ( pie . chart ) ;
109
110
expect ( annotations . length ) . toBeGreaterThan ( 0 ) ;
110
111
expect ( annotations [ 0 ] . component . get ( 'content' ) ) . toBe ( '总计' ) ;
111
- expect ( annotations [ 1 ] . component . get ( 'content' ) ) . toBe ( ' test\ntest' ) ;
112
+ expect ( annotations [ 1 ] . component . get ( 'content' ) ) . toBe ( ` test\ntest ${ totalValue } ` ) ;
112
113
} ) ;
113
114
114
115
it ( '自定义中心文本内容: update statistic title & content, 动态数据' , async ( ) => {
@@ -117,11 +118,11 @@ describe('中心文本 - 指标卡', () => {
117
118
...pie . options ,
118
119
statistic : {
119
120
title : {
120
- formatter : ( item , data ) => ( Array . isArray ( data ) ? '总计' : data [ 'type' ] ) ,
121
+ formatter : ( datum ) => ( ! datum ? '总计' : datum [ 'type' ] ) ,
121
122
} ,
122
123
content : {
123
- formatter : ( item , data ) => {
124
- return Array . isArray ( data ) ? 'test\ntest' : typeof data . value === 'number' ? `${ data . value } ` : '' ;
124
+ formatter : ( datum ) => {
125
+ return ! datum ? 'test\ntest' : `${ datum . value } ` ;
125
126
} ,
126
127
rotate : ( 30 / 180 ) * Math . PI ,
127
128
} ,
@@ -184,92 +185,29 @@ describe('中心文本 - 指标卡', () => {
184
185
expect ( annotations [ 1 ] . extra . style ) . toMatchObject ( { fill : 'pink' } ) ;
185
186
} ) ;
186
187
187
- // 暂时不提供 annotations 配置
188
- // it('append annotation', () => {
189
- // const pie = new Pie(createDiv(), {
190
- // ...config,
191
- // innerRadius: 0.64,
192
- // statistic: {
193
- // title: {
194
- // formatter: () => '',
195
- // },
196
- // content: {
197
- // formatter: () => '无数据',
198
- // },
199
- // },
200
- // annotations: [
201
- // {
202
- // type: 'text',
203
- // top: true,
204
- // position: ['50%', '20%'],
205
- // content: '达标区间',
206
- // style: {
207
- // fill: '#aaaaaa',
208
- // textAlign: 'end',
209
- // textBaseline: 'top',
210
- // fontWeight: 300,
211
- // },
212
- // offsetX: -10,
213
- // offsetY: 6,
214
- // },
215
- // ],
216
- // });
217
-
218
- // pie.render();
219
- // const annotations = getAnnotations(pie.chart);
220
- // expect(annotations.length).toBe(3);
221
- // expect(annotations[0].component.get('type')).toBe('text');
222
- // expect(annotations[0].extra.content).toBe('达标区间');
223
- // expect(annotations[1].extra.content).toBe('');
224
- // });
225
-
226
- // it('关闭 stastic,自定义 annotation', async () => {
227
- // const pie = new Pie(createDiv(), {
228
- // ...config,
229
- // innerRadius: 0.64,
230
- // statistic: null,
231
- // annotations: [
232
- // {
233
- // type: 'image',
234
- // position: ['50%', '50%'],
235
- // src: 'https://gw.alipayobjects.com/zos/antfincdn/FLrTNDvlna/antv.png',
236
- // offsetX: -28,
237
- // offsetY: 30,
238
- // style: {
239
- // width: 56,
240
- // height: 56,
241
- // },
242
- // },
243
- // ],
244
- // });
245
-
246
- // pie.render();
247
-
248
- // const annotations = getAnnotations(pie.chart);
249
- // expect(annotations.length).toBe(1);
250
- // expect(annotations[0].component.get('type')).toBe('image');
251
-
252
- // await delay(500);
253
- // pie.update({
254
- // ...pie.options,
255
- // annotations: [
256
- // {
257
- // type: 'text',
258
- // position: ['50%', '50%'],
259
- // content: '自定义标注文本',
260
- // style: {
261
- // textAlign: 'center',
262
- // },
263
- // },
264
- // ],
265
- // });
266
- // pie.render();
267
-
268
- // const newAnnotations = getAnnotations(pie.chart);
269
- // expect(newAnnotations.length).toBe(1);
270
- // expect(newAnnotations[0].component.get('type')).toBe('text');
271
- // expect(newAnnotations[0].component.get('content')).toBe('自定义标注文本');
272
- // });
188
+ it ( '自定义中心文本样式: with callback' , async ( ) => {
189
+ await delay ( 500 ) ;
190
+ pie . update ( {
191
+ ...pie . options ,
192
+ statistic : {
193
+ title : {
194
+ formatter : ( ) => '' ,
195
+ style : { fill : 'red' } ,
196
+ } ,
197
+ content : {
198
+ style : { fill : 'pink' } ,
199
+ } ,
200
+ } ,
201
+ } ) ;
202
+
203
+ pie . render ( ) ;
204
+ const annotations = getAnnotations ( pie . chart ) ;
205
+ expect ( annotations . length ) . toBe ( 2 ) ;
206
+ expect ( annotations [ 0 ] . component . get ( 'content' ) ) . toBe ( '' ) ;
207
+ expect ( annotations [ 0 ] . extra . key ) . toBe ( 'statistic' ) ;
208
+ expect ( annotations [ 0 ] . extra . style ) . toMatchObject ( { fill : 'red' } ) ;
209
+ expect ( annotations [ 1 ] . extra . style ) . toMatchObject ( { fill : 'pink' } ) ;
210
+ } ) ;
273
211
274
212
it ( '总计值为空' , ( ) => {
275
213
const pie = new Pie ( createDiv ( ) , {
0 commit comments