1
1
'use client' ;
2
2
3
- import { enrichEventWithDetails , useStylesheet } from '@ui5/webcomponents-react-base' ;
3
+ import { enrichEventWithDetails , useStylesheet , useSyncRef } from '@ui5/webcomponents-react-base' ;
4
4
import { clsx } from 'clsx' ;
5
5
import type { CSSProperties } from 'react' ;
6
6
import { cloneElement , forwardRef , isValidElement , useCallback , useMemo } from 'react' ;
@@ -108,6 +108,8 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
108
108
} = props ;
109
109
110
110
useStylesheet ( styleData , PieChart . displayName ) ;
111
+ const [ componentRef , chartRef ] = useSyncRef ( ref ) ;
112
+ const isDonutChart = props [ 'data-component-name' ] === 'DonutChart' ;
111
113
112
114
const chartConfig = {
113
115
margin : { right : 30 , left : 30 , bottom : 30 , top : 30 , ...( props . chartConfig ?. margin ?? { } ) } ,
@@ -192,12 +194,23 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
192
194
const ex = mx + ( cos >= 0 ? 1 : - 1 ) * 22 ;
193
195
const ey = my ;
194
196
const textAnchor = cos >= 0 ? 'start' : 'end' ;
197
+ const activeLegendItem = chartRef . current ?. querySelector < HTMLLIElement > (
198
+ `.legend-item-${ chartConfig . activeSegment } `
199
+ ) ;
200
+ if ( ! activeLegendItem ?. dataset . activeLegend ) {
201
+ const allLegendItems = chartRef . current ?. querySelectorAll ( '.recharts-legend-item' ) ;
202
+
203
+ allLegendItems . forEach ( ( item ) => item . removeAttribute ( 'data-active-legend' ) ) ;
204
+ activeLegendItem . setAttribute ( 'data-active-legend' , 'true' ) ;
205
+ }
195
206
196
207
return (
197
208
< g >
198
- < text x = { cx } y = { cy } dy = { 8 } textAnchor = "middle" fill = { fill } >
199
- { payload . name }
200
- </ text >
209
+ { isDonutChart && (
210
+ < text x = { cx } y = { cy } dy = { 8 } textAnchor = "middle" fill = { fill } >
211
+ { payload . name }
212
+ </ text >
213
+ ) }
201
214
< Sector
202
215
cx = { cx }
203
216
cy = { cy }
@@ -231,7 +244,7 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
231
244
</ g >
232
245
) ;
233
246
} ,
234
- [ showActiveSegmentDataLabel ]
247
+ [ showActiveSegmentDataLabel , chartConfig . activeSegment , isDonutChart ]
235
248
) ;
236
249
237
250
const renderLabelLine = useCallback (
@@ -248,11 +261,11 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
248
261
if ( chartConfig . activeSegment != null && showActiveSegmentDataLabel ) {
249
262
if ( chartConfig . legendPosition === 'bottom' ) {
250
263
return {
251
- paddingTop : '30px'
264
+ paddingBlockStart : '30px'
252
265
} ;
253
266
} else if ( chartConfig . legendPosition === 'top' ) {
254
267
return {
255
- paddingBottom : '30px'
268
+ paddingBlockEnd : '30px'
256
269
} ;
257
270
}
258
271
}
@@ -265,7 +278,7 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
265
278
return (
266
279
< ChartContainer
267
280
dataset = { dataset }
268
- ref = { ref }
281
+ ref = { componentRef }
269
282
loading = { loading }
270
283
Placeholder = { ChartPlaceholder ?? PieChartPlaceholder }
271
284
style = { style }
@@ -296,6 +309,7 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
296
309
label = { dataLabel }
297
310
activeIndex = { chartConfig . activeSegment }
298
311
activeShape = { chartConfig . activeSegment != null && renderActiveShape }
312
+ rootTabIndex = { - 1 }
299
313
>
300
314
{ centerLabel && < RechartsLabel position = "center" > { centerLabel } </ RechartsLabel > }
301
315
{ dataset &&
0 commit comments