1- import c3 , { ChartAPI } from 'c3' ;
2- import React , { useEffect , useRef , useState } from ' react' ;
1+ import c3 , { ChartAPI } from "c3" ;
2+ import React , { useEffect , useRef , useState } from " react" ;
33
4- import { PieChartFallback , PieChartFallbackProps } from ' ./PieChartFallback' ;
4+ import { PieChartFallback , PieChartFallbackProps } from " ./PieChartFallback" ;
55
6- import { FormattedEntry } from ' ../../utils/pie' ;
7- import { LEGEND_COLORS } from ' ../../constants/colors' ;
6+ import { FormattedEntry } from " ../../utils/pie" ;
7+ import { LEGEND_COLORS } from " ../../constants/colors" ;
88
9- import ' ./piechart.css' ;
9+ import " ./piechart.css" ;
1010
1111interface PieChartProps extends PieChartFallbackProps {
12- chartData : FormattedEntry [ ] | null ,
12+ chartData : FormattedEntry [ ] | null ;
1313}
1414
1515const PieChart = ( props : PieChartProps ) => {
@@ -20,26 +20,30 @@ const PieChart = (props: PieChartProps) => {
2020
2121 // Redraw chart on data change
2222 useEffect ( ( ) => {
23- if ( ! chartData || ! chartRef . current )
24- return ;
25-
23+ if ( ! chartData || ! chartRef . current ) return ;
24+
2625 const newChart = c3 . generate ( {
2726 bindto : chartRef . current ,
2827 data : {
2928 columns : chartData ,
30- type : ' pie' ,
29+ type : " pie" ,
3130 } ,
3231 color : {
3332 pattern : LEGEND_COLORS ,
34- }
33+ } ,
34+ tooltip : {
35+ format : {
36+ value : ( value , ratio ) =>
37+ `${ String ( value ) } \n${ ( ratio * 100 ) . toFixed ( 1 ) } %` ,
38+ } ,
39+ } ,
3540 } ) ;
3641 setC3Chart ( newChart ) ;
3742 } , [ chartData ] ) ;
3843
3944 // Suppress continuous resize calls that may cause stutter and bad UX
4045 useEffect ( ( ) => {
41- if ( ! chartContainerRef . current || ! c3Chart )
42- return ;
46+ if ( ! chartContainerRef . current || ! c3Chart ) return ;
4347
4448 const resizeChart = ( ) => c3Chart . resize ( ) ;
4549
@@ -52,23 +56,22 @@ const PieChart = (props: PieChartProps) => {
5256
5357 const chartContainerObserver = new ResizeObserver ( resizeActionHandler ) ;
5458 chartContainerObserver . observe ( chartContainerRef . current ) ;
55-
59+
5660 return ( ) => {
5761 clearTimeout ( resizeTimeout ) ;
5862 chartContainerObserver . disconnect ( ) ;
59- }
63+ } ;
6064 } , [ c3Chart ] ) ;
6165
62-
63- if ( ! chartData || ! chartData . length ) {
64- return < PieChartFallback { ...props } loading = { ! chartData } />
66+ if ( ! chartData || ! chartData . length ) {
67+ return < PieChartFallback { ...props } loading = { ! chartData } /> ;
6568 }
6669
6770 return (
68- < div className = ' pie-chart-container' ref = { chartContainerRef } >
69- < div ref = { chartRef } className = ' pie-chart' />
71+ < div className = " pie-chart-container" ref = { chartContainerRef } >
72+ < div ref = { chartRef } className = " pie-chart" />
7073 </ div >
71- )
72- }
74+ ) ;
75+ } ;
7376
74- export default PieChart
77+ export default PieChart ;
0 commit comments