Skip to content

Commit d2c0f1b

Browse files
authored
[@mantine/charts] DonutChart: Fix valueFormatter prop not working, add labelsType prop support (#7153)
* [DonutChart] Add render label function * remove export * prettier --write
1 parent 931565c commit d2c0f1b

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

packages/@mantine/charts/src/DonutChart/DonutChart.tsx

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Cell,
33
Pie,
4+
PieLabel,
45
PieProps,
56
PieChart as ReChartsPieChart,
67
ResponsiveContainer,
@@ -98,6 +99,9 @@ export interface DonutChartProps
9899
/** Props passed down to recharts `PieChart` component */
99100
pieChartProps?: React.ComponentPropsWithoutRef<typeof ReChartsPieChart>;
100101

102+
/** Type of labels to display, `'value'` by default */
103+
labelsType?: 'value' | 'percent';
104+
101105
/** A function to format values inside the tooltip */
102106
valueFormatter?: (value: number) => string;
103107
}
@@ -118,6 +122,7 @@ const defaultProps: Partial<DonutChartProps> = {
118122
strokeWidth: 1,
119123
startAngle: 0,
120124
endAngle: 360,
125+
labelsType: 'value',
121126
tooltipDataSource: 'all',
122127
};
123128

@@ -131,6 +136,40 @@ const varsResolver = createVarsResolver<DonutChartFactory>(
131136
})
132137
);
133138

139+
const getLabelValue = (
140+
labelsType: DonutChartProps['labelsType'],
141+
value: number,
142+
percent: number,
143+
valueFormatter?: DonutChartProps['valueFormatter']
144+
) => {
145+
if (labelsType === 'percent') {
146+
return `${(percent * 100).toFixed(0)}%`;
147+
}
148+
149+
if (typeof valueFormatter === 'function') {
150+
return valueFormatter(value);
151+
}
152+
153+
return value;
154+
};
155+
156+
const getLabel =
157+
(labelsType: 'value' | 'percent', valueFormatter?: DonutChartProps['valueFormatter']): PieLabel =>
158+
({ x, y, cx, cy, percent, value }) => (
159+
<text
160+
x={x}
161+
y={y}
162+
cx={cx}
163+
cy={cy}
164+
textAnchor={x > cx ? 'start' : 'end'}
165+
fill="var(--chart-labels-color, var(--mantine-color-dimmed))"
166+
fontFamily="var(--mantine-font-family)"
167+
fontSize={12}
168+
>
169+
<tspan x={x}>{getLabelValue(labelsType, value, percent, valueFormatter)}</tspan>
170+
</text>
171+
);
172+
134173
export const DonutChart = factory<DonutChartFactory>((_props, ref) => {
135174
const props = useProps('DonutChart', defaultProps, _props);
136175
const {
@@ -159,6 +198,7 @@ export const DonutChart = factory<DonutChartFactory>((_props, ref) => {
159198
pieChartProps,
160199
valueFormatter,
161200
strokeColor,
201+
labelsType,
162202
...others
163203
} = props;
164204

@@ -205,15 +245,7 @@ export const DonutChart = factory<DonutChartFactory>((_props, ref) => {
205245
paddingAngle={paddingAngle}
206246
startAngle={startAngle}
207247
endAngle={endAngle}
208-
label={
209-
withLabels
210-
? {
211-
fill: 'var(--chart-labels-color, var(--mantine-color-dimmed))',
212-
fontSize: 12,
213-
fontFamily: 'var(--mantine-font-family)',
214-
}
215-
: false
216-
}
248+
label={withLabels ? getLabel(labelsType || 'value', valueFormatter) : false}
217249
labelLine={
218250
withLabelsLine
219251
? {

0 commit comments

Comments
 (0)