Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/charts] Add valueFormatter support on outside labels #6616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions packages/@mantine/charts/src/PieChart/PieChart.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,36 @@ export function Usage() {

export function WithLabels() {
return (
<div style={{ padding: 40 }}>
<PieChart data={data} withLabels size={200} />
<div
style={{
padding: 40,
gap: 40,
display: 'flex',
flexDirection: 'column',
}}
>
<PieChart data={data} withLabels size={200} labelsPosition="inside" />
<PieChart data={data} withLabels size={200} labelsType="percent" />
<PieChart
data={data}
withLabels
size={200}
labelsPosition="inside"
valueFormatter={(number) =>
new Intl.NumberFormat('en', { currency: 'USD', style: 'currency' }).format(number)
}
/>

<PieChart style={{ width: '350px' }} data={data} withLabels size={200} />
<PieChart style={{ width: '350px' }} data={data} withLabels size={200} labelsType="percent" />
<PieChart
style={{ width: '350px' }}
data={data}
withLabels
size={200}
valueFormatter={(number) =>
new Intl.NumberFormat('en', { currency: 'USD', style: 'currency' }).format(number)
}
/>
</div>
);
}
Expand Down
62 changes: 36 additions & 26 deletions packages/@mantine/charts/src/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@
})
);

const getLabelValue = (
labelsType: PieChartProps['labelsType'],
value: number,
percent: number,
valueFormatter?: PieChartProps['valueFormatter']
) => {
if (typeof valueFormatter === 'function' && labelsType === 'percent') {
console.warn('[@mantine/charts] `valueFormatter` & `labelsType={percent}` are incompatible');

Check warning on line 144 in packages/@mantine/charts/src/PieChart/PieChart.tsx

View workflow job for this annotation

GitHub Actions / test_pull_request

Unexpected console statement
}

if (labelsType === 'percent') return `${(percent * 100).toFixed(0)}%`;

if (typeof valueFormatter === 'function') return valueFormatter(value);

return value;
};

const getInsideLabel =
(labelsType: 'value' | 'percent', valueFormatter?: PieChartProps['valueFormatter']): PieLabel =>
({ cx, cy, midAngle, innerRadius, outerRadius, value, percent }) => {
Expand All @@ -150,15 +167,28 @@
dominantBaseline="central"
className={classes.label}
>
{labelsType === 'percent'
? `${(percent * 100).toFixed(0)}%`
: typeof valueFormatter === 'function'
? valueFormatter(value)
: value}
{getLabelValue(labelsType, value, percent, valueFormatter)}
</text>
);
};

const getOutsideLabel =
(labelsType: 'value' | 'percent', valueFormatter?: PieChartProps['valueFormatter']): PieLabel =>
({ x, y, cx, cy, percent, value }) => (
<text
x={x}
y={y}
cx={cx}
cy={cy}
textAnchor={x > cx ? 'start' : 'end'}
fill="var(--chart-labels-color, var(--mantine-color-dimmed))"
fontFamily="var(--mantine-font-family)"
fontSize={12}
>
<tspan x={x}>{getLabelValue(labelsType, value, percent, valueFormatter)}</tspan>
</text>
);

export const PieChart = factory<PieChartFactory>((_props, ref) => {
const props = useProps('PieChart', defaultProps, _props);
const {
Expand Down Expand Up @@ -237,27 +267,7 @@
withLabels
? labelsPosition === 'inside'
? getInsideLabel(labelsType || 'value', valueFormatter)
: labelsType === 'percent'
? ({ percent, x, y, cx, cy }) => (
<text
x={x}
y={y}
cx={cx}
cy={cy}
textAnchor={x > cx ? 'start' : 'end'}
dominantBaseline="central"
fill="var(--chart-labels-color, var(--mantine-color-dimmed))"
fontFamily="var(--mantine-font-family)"
fontSize={12}
>
<tspan x={x}>{`${(percent * 100).toFixed(0)}%`}</tspan>
</text>
)
: {
fill: 'var(--chart-labels-color, var(--mantine-color-dimmed))',
fontSize: 12,
fontFamily: 'var(--mantine-font-family)',
}
: getOutsideLabel(labelsType || 'value', valueFormatter)
: false
}
labelLine={
Expand Down
Loading