Skip to content

Commit 1949db6

Browse files
authored
fix(Pie- & DonutChart): improve activeSegment handling & fix focus behavior (#6695)
Fixes #6683
1 parent bca723b commit 1949db6

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

packages/charts/src/components/DonutChart/DonutChart.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const DonutChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
1717
...props.chartConfig
1818
};
1919

20-
return <PieChart {...props} ref={ref} chartConfig={chartConfig} />;
20+
return <PieChart {...props} ref={ref} chartConfig={chartConfig} data-component-name="DonutChart" />;
2121
});
2222

2323
DonutChart.displayName = 'DonutChart';

packages/charts/src/components/PieChart/PieChart.module.css

+7
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44
outline: none;
55
}
66
}
7+
8+
[data-active-legend] {
9+
background: color-mix(in srgb, var(--sapSelectedColor), transparent 87%);
10+
:global(.recharts-legend-item-text) {
11+
color: var(--sapTextColor) !important;
12+
}
13+
}

packages/charts/src/components/PieChart/PieChart.tsx

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { enrichEventWithDetails, useStylesheet } from '@ui5/webcomponents-react-base';
3+
import { enrichEventWithDetails, useStylesheet, useSyncRef } from '@ui5/webcomponents-react-base';
44
import { clsx } from 'clsx';
55
import type { CSSProperties } from 'react';
66
import { cloneElement, forwardRef, isValidElement, useCallback, useMemo } from 'react';
@@ -108,6 +108,8 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
108108
} = props;
109109

110110
useStylesheet(styleData, PieChart.displayName);
111+
const [componentRef, chartRef] = useSyncRef(ref);
112+
const isDonutChart = props['data-component-name'] === 'DonutChart';
111113

112114
const chartConfig = {
113115
margin: { right: 30, left: 30, bottom: 30, top: 30, ...(props.chartConfig?.margin ?? {}) },
@@ -192,12 +194,23 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
192194
const ex = mx + (cos >= 0 ? 1 : -1) * 22;
193195
const ey = my;
194196
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+
}
195206

196207
return (
197208
<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+
)}
201214
<Sector
202215
cx={cx}
203216
cy={cy}
@@ -231,7 +244,7 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
231244
</g>
232245
);
233246
},
234-
[showActiveSegmentDataLabel]
247+
[showActiveSegmentDataLabel, chartConfig.activeSegment, isDonutChart]
235248
);
236249

237250
const renderLabelLine = useCallback(
@@ -248,11 +261,11 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
248261
if (chartConfig.activeSegment != null && showActiveSegmentDataLabel) {
249262
if (chartConfig.legendPosition === 'bottom') {
250263
return {
251-
paddingTop: '30px'
264+
paddingBlockStart: '30px'
252265
};
253266
} else if (chartConfig.legendPosition === 'top') {
254267
return {
255-
paddingBottom: '30px'
268+
paddingBlockEnd: '30px'
256269
};
257270
}
258271
}
@@ -265,7 +278,7 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
265278
return (
266279
<ChartContainer
267280
dataset={dataset}
268-
ref={ref}
281+
ref={componentRef}
269282
loading={loading}
270283
Placeholder={ChartPlaceholder ?? PieChartPlaceholder}
271284
style={style}
@@ -296,6 +309,7 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
296309
label={dataLabel}
297310
activeIndex={chartConfig.activeSegment}
298311
activeShape={chartConfig.activeSegment != null && renderActiveShape}
312+
rootTabIndex={-1}
299313
>
300314
{centerLabel && <RechartsLabel position="center">{centerLabel}</RechartsLabel>}
301315
{dataset &&

0 commit comments

Comments
 (0)