Charts - Pie demos
Demos that use pie charts for multi-level data, angles, labels, padding, and custom legend or tooltip.
TwoLevelPieChart
import { PieChart } from '@mui/x-charts/PieChart';
import Box from '@mui/material/Box';
const data1 = [
{ label: 'Group A', value: 400 },
{ label: 'Group B', value: 300 },
{ label: 'Group C', value: 300 },
{ label: 'Group D', value: 200 },
];
const data2 = [
{ label: 'A1', value: 100 },
{ label: 'A2', value: 300 },
{ label: 'B1', value: 100 },
{ label: 'B2', value: 80 },
{ label: 'B3', value: 40 },
{ label: 'B4', value: 30 },
{ label: 'B5', value: 50 },
{ label: 'C1', value: 100 },
{ label: 'C2', value: 200 },
{ label: 'D1', value: 150 },
{ label: 'D2', value: 50 },
];
export default function TwoLevelPieChart() {
return (
<Box sx={{ width: '100%', height: 300 }}>
<PieChart
series={[
{
innerRadius: 0,
outerRadius: 60,
data: data1,
},
{
innerRadius: 80,
outerRadius: 100,
data: data2,
},
]}
hideLegend
/>
</Box>
);
}
- Group A
- Group B
- Group C
- Group D
- Group E
- Group F
import { PieChart } from '@mui/x-charts/PieChart';
const data = [
{ label: 'Group A', value: 400 },
{ label: 'Group B', value: 300 },
{ label: 'Group C', value: 300 },
{ label: 'Group D', value: 200 },
{ label: 'Group E', value: 278 },
{ label: 'Group F', value: 189 },
];
export default function StraightAnglePieChart() {
return (
<PieChart
series={[
{
startAngle: -90,
endAngle: 90,
data,
},
]}
height={200}
width={200}
/>
);
}
import type { DefaultizedPieValueType } from '@mui/x-charts/models';
import { PieChart, pieClasses } from '@mui/x-charts/PieChart';
const data = [
{ label: 'Group A', value: 400, color: '#0088FE' },
{ label: 'Group B', value: 300, color: '#00C49F' },
{ label: 'Group C', value: 300, color: '#FFBB28' },
{ label: 'Group D', value: 200, color: '#FF8042' },
];
const sizing = {
margin: { right: 5 },
width: 200,
height: 200,
hideLegend: true,
};
const TOTAL = data.map((item) => item.value).reduce((a, b) => a + b, 0);
const getArcLabel = (params: DefaultizedPieValueType) => {
const percent = params.value / TOTAL;
return `${(percent * 100).toFixed(0)}%`;
};
export default function PieChartWithCustomizedLabel() {
return (
<PieChart
series={[
{
outerRadius: 80,
data,
arcLabel: getArcLabel,
},
]}
sx={{
[`& .${pieClasses.arcLabel}`]: {
fill: 'white',
fontSize: 14,
},
}}
{...sizing}
/>
);
}
- A
- B
- C
- D
import * as React from 'react';
import { PieChart } from '@mui/x-charts/PieChart';
import { useDrawingArea } from '@mui/x-charts/hooks';
import { styled } from '@mui/material/styles';
const data = [
{ value: 5, label: 'A' },
{ value: 10, label: 'B' },
{ value: 15, label: 'C' },
{ value: 20, label: 'D' },
];
const size = {
width: 200,
height: 200,
};
const StyledText = styled('text')(({ theme }) => ({
fill: theme.palette.text.primary,
textAnchor: 'middle',
dominantBaseline: 'central',
fontSize: 20,
}));
function PieCenterLabel({ children }: { children: React.ReactNode }) {
const { width, height, left, top } = useDrawingArea();
return (
<StyledText x={left + width / 2} y={top + height / 2}>
{children}
</StyledText>
);
}
export default function PieChartWithCenterLabel() {
return (
<PieChart series={[{ data, innerRadius: 80 }]} {...size}>
<PieCenterLabel>Center label</PieCenterLabel>
</PieChart>
);
}
- Circle
- Diamond
- Star
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.