Skip to content

Commit e49eddc

Browse files
fix(Pie/DonutChart): respect measure formatter and custom data label (#525)
1 parent c68b162 commit e49eddc

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { enrichEventWithDetails } from '@ui5/webcomponents-react-base/lib/Utils'
33
import { ChartContainer } from '@ui5/webcomponents-react-charts/lib/components/ChartContainer';
44
import { PieChartPlaceholder } from '@ui5/webcomponents-react-charts/lib/PieChartPlaceholder';
55
import { useLegendItemClick } from '@ui5/webcomponents-react-charts/lib/useLegendItemClick';
6-
import React, { CSSProperties, FC, forwardRef, Ref, useCallback, useMemo } from 'react';
7-
import { Cell, Label, Legend, Pie, PieChart as PieChartLib, Tooltip } from 'recharts';
6+
import React, { CSSProperties, FC, forwardRef, Ref, useCallback, useMemo, isValidElement, cloneElement } from 'react';
7+
import { Cell, Label, Legend, Pie, PieChart as PieChartLib, Tooltip, Text } from 'recharts';
88
import { getValueByDataKey } from 'recharts/lib/util/ChartUtils';
99
import { IChartBaseProps } from '../../interfaces/IChartBaseProps';
1010
import { IChartMeasure } from '../../interfaces/IChartMeasure';
@@ -101,14 +101,22 @@ const PieChart: FC<PieChartProps> = forwardRef((props: PieChartProps, ref: Ref<a
101101
[props.measure]
102102
);
103103

104-
const label = useMemo(() => {
105-
if (measure.hideDataLabel) return null;
106-
return {
107-
position: 'outside',
108-
content: measure.DataLabel,
109-
formatter: measure.formatter
110-
};
111-
}, [measure]);
104+
const dataLabel = useCallback(
105+
(props) => {
106+
if (measure.hideDataLabel) return null;
107+
108+
if (isValidElement(measure.DataLabel)) {
109+
return cloneElement(measure.DataLabel, { ...props, config: measure });
110+
}
111+
112+
return (
113+
<Text {...props} alignmentBaseline="middle" className="recharts-pie-label-text">
114+
{measure.formatter(props.value)}
115+
</Text>
116+
);
117+
},
118+
[measure]
119+
);
112120

113121
const tooltipValueFormatter = useCallback((value) => measure.formatter(value), [measure.formatter]);
114122
const chartRef = useConsolidatedRef<any>(ref);
@@ -158,7 +166,8 @@ const PieChart: FC<PieChartProps> = forwardRef((props: PieChartProps, ref: Ref<a
158166
data={dataset}
159167
animationBegin={0}
160168
isAnimationActive={noAnimation === false}
161-
label={label}
169+
labelLine={measure.hideDataLabel !== true}
170+
label={dataLabel}
162171
>
163172
{centerLabel && <Label position={'center'}>{centerLabel}</Label>}
164173
{dataset &&

0 commit comments

Comments
 (0)