11import { View } from '../../../dependents' ;
22import StatisticHtml , { IStatisticHtml } from './statistic' ;
3- import * as statisticTemplate from './statistic-template' ;
4- import { debounce , each , isString , isObject , isFunction , keys } from '@antv/util' ;
5- import { LooseMap } from '../../../interface/types' ;
3+ import { getTemplate } from './statistic-template' ;
4+ import { debounce , each } from '@antv/util' ;
65import Ring , { DonutViewConfig } from '../layer' ;
6+ import { LooseMap } from '../../../interface/types' ;
77
88interface IRingStatistic extends IStatisticHtml {
99 view : View ;
1010 plot : any ;
1111}
1212
13+ interface StatisticData {
14+ name : string ;
15+ value : string ;
16+ itemData ?: LooseMap ;
17+ color ?: string ;
18+ }
19+
1320export default class RingStatistic extends StatisticHtml {
1421 protected view : View ;
1522 protected plot : Ring ;
@@ -29,7 +36,7 @@ export default class RingStatistic extends StatisticHtml {
2936 this . view . on (
3037 `interval:${ triggerOnEvent } ` ,
3138 debounce ( ( e ) => {
32- const displayData = this . parseStatisticData ( e . data . data ) ;
39+ const displayData = this . parseStatisticData ( 'item' , e . data . data , e . data . color ) ;
3340 const htmlString = this . getStatisticHtmlString ( displayData ) ;
3441 this . updateHtml ( htmlString ) ;
3542 } , 150 )
@@ -51,7 +58,7 @@ export default class RingStatistic extends StatisticHtml {
5158 } else {
5259 /** 用户没有指定文本内容时,默认显示总计 */
5360 const data = this . getTotalValue ( ) ;
54- displayData = this . parseStatisticData ( data ) ;
61+ displayData = this . parseStatisticData ( 'total' , data ) ;
5562 }
5663 /** 中心文本显示 */
5764 let htmlString ;
@@ -87,33 +94,29 @@ export default class RingStatistic extends StatisticHtml {
8794 return data ;
8895 }
8996
90- private parseStatisticData ( data ) {
97+ private parseStatisticData ( type : string , data , color ?: string ) {
9198 const plot = this . plot ;
9299 const { angleField, colorField } = plot . options ;
93100 const angleScale = plot . getScaleByField ( angleField ) ;
94101 const colorScale = plot . getScaleByField ( colorField ) ;
95102
96- if ( colorField ) {
97- return {
98- name : colorScale . getText ( data [ colorField ] ) ,
99- value : angleScale . getText ( data [ angleField ] ) ,
100- } ;
103+ const statisticData : StatisticData = {
104+ name : colorScale ? colorScale . getText ( data [ colorField ] ) : null ,
105+ value : angleScale . getText ( data [ angleField ] ) ,
106+ } ;
107+
108+ if ( type === 'item' ) {
109+ // 每一个扇形区域的数据
110+ statisticData . itemData = data ;
111+ statisticData . color = color ;
101112 }
102113
103- return angleScale . getText ( data [ angleField ] ) ;
114+ return statisticData ;
104115 }
105116
106117 private getStatisticTemplate ( data ) {
107118 const size = this . getStatisticSize ( ) ;
108- let htmlString ;
109- /** 如果文本内容为string或单条数据 */
110- if ( isString ( data ) ) {
111- htmlString = statisticTemplate . getSingleDataTemplate ( data , this . statisticClass , size ) ;
112- } else if ( isObject ( data ) && keys ( data ) . length === 2 ) {
113- /** 如果文本内容为两条数据 */
114- const content = data as LooseMap ;
115- htmlString = statisticTemplate . getTwoDataTemplate ( content . name , content . value , this . statisticClass , size ) ;
116- }
119+ const htmlString = getTemplate ( data . name , data . value , this . statisticClass , size ) ;
117120 /** 更为复杂的文本要求用户自行制定html模板 */
118121 return htmlString ;
119122 }
@@ -127,15 +130,14 @@ export default class RingStatistic extends StatisticHtml {
127130 }
128131
129132 private getStatisticHtmlString ( data ) : string {
130- const triggerOnConfig = this . options . triggerOn ;
133+ const htmlContent = this . options . htmlContent ;
131134 let htmlString : string ;
132- if ( isString ( triggerOnConfig ) ) {
135+ if ( htmlContent ) {
136+ htmlString = htmlContent ( data ) ;
137+ } else {
133138 htmlString = this . getStatisticTemplate ( data ) ;
134139 }
135- if ( isFunction ( triggerOnConfig ) ) {
136- htmlString = triggerOnConfig ( data ) ;
137- htmlString = `<div class="ring-guide-html ${ this . statisticClass } ">${ htmlString } </div>` ;
138- }
140+
139141 return htmlString ;
140142 }
141143}
0 commit comments