11import { BaseG2Chart } from '@/views/chat/component/BaseG2Chart.ts'
22import type { ChartAxis , ChartData } from '@/views/chat/component/BaseChart.ts'
33import type { G2Spec } from '@antv/g2'
4- import { endsWith , filter , replace } from 'lodash-es '
4+ import { checkIsPercent } from '@/views/chat/component/charts/utils.ts '
55
66export class Pie extends BaseG2Chart {
77 constructor ( id : string ) {
@@ -18,39 +18,14 @@ export class Pie extends BaseG2Chart {
1818 }
1919
2020 // %
21- const _data = [ ]
22- let isPercent = false
23- const notEmptyData = filter (
24- data ,
25- ( d ) =>
26- d &&
27- d [ y [ 0 ] . value ] !== null &&
28- d [ y [ 0 ] . value ] !== undefined &&
29- d [ y [ 0 ] . value ] !== 0 &&
30- d [ y [ 0 ] . value ] !== '0'
31- )
32- if ( notEmptyData . length > 0 ) {
33- const v = notEmptyData [ 0 ] [ y [ 0 ] . value ] + ''
34- if ( endsWith ( v . trim ( ) , '%' ) ) {
35- isPercent = true
36- }
37- }
38- for ( let i = 0 ; i < data . length ; i ++ ) {
39- const v = data [ i ]
40- const _v = { ...v }
41- if ( isPercent ) {
42- const formatValue = replace ( v [ y [ 0 ] . value ] , '%' , '' )
43- _v [ y [ 0 ] . value ] = Number ( formatValue )
44- }
45- _data . push ( _v )
46- }
21+ const _data = checkIsPercent ( y [ 0 ] , data )
4722
4823 const options : G2Spec = {
4924 ...this . chart . options ( ) ,
5025 type : 'interval' ,
5126 coordinate : { type : 'theta' , outerRadius : 0.8 } ,
5227 transform : [ { type : 'stackY' } ] ,
53- data : _data ,
28+ data : _data . data ,
5429 encode : {
5530 y : y [ 0 ] . value ,
5631 color : series [ 0 ] . value ,
@@ -62,11 +37,11 @@ export class Pie extends BaseG2Chart {
6237 {
6338 position : 'outside' ,
6439 text : ( data : any ) =>
65- `${ data [ series [ 0 ] . value ] } : ${ data [ y [ 0 ] . value ] } ${ isPercent ? '%' : '' } ` ,
40+ `${ data [ series [ 0 ] . value ] } : ${ data [ y [ 0 ] . value ] } ${ _data . isPercent ? '%' : '' } ` ,
6641 } ,
6742 ] ,
6843 tooltip : ( data ) => {
69- return { name : y [ 0 ] . name , value : `${ data [ y [ 0 ] . value ] } ${ isPercent ? '%' : '' } ` }
44+ return { name : y [ 0 ] . name , value : `${ data [ y [ 0 ] . value ] } ${ _data . isPercent ? '%' : '' } ` }
7045 } ,
7146 }
7247
0 commit comments