shadcn.io is not affiliated with official shadcn/ui
Chart examples · Pie
A pie chart with a legend for React
A pie chart with a wrapping legend of browsers below it.
Use a legend instead of labels when there are several categories or the slices are small. ChartLegend renders ChartLegendContent under the pie, with each browser's swatch and name, wrapped onto rows a quarter wide.
Preview
Installation
FAQ
How is the legend attached?
PieChart renders ChartLegend with content set to ChartLegendContent. nameKey is browser on both Pie and ChartLegendContent, so each item looks up chrome and the other keys in config.
How do the legend items wrap?
ChartLegendContent sets className to -translate-y-2 flex-wrap gap-2 *:basis-1/4 *:justify-center. Items wrap, each takes a quarter of the row, and the block is shifted up by translate-y-2.
Where does the legend sit?
ChartLegend keeps the default verticalAlign of bottom. ChartContainer is mx-auto aspect-square max-h-[300px], which leaves room for that legend under the circle.
What text does each legend item show?
config labels are Chrome, Safari, Firefox, Edge, and Other, with colors --chart-1 through --chart-5. The swatch color comes from those entries. This demo has no ChartTooltip.
Related
- A simple pie chartA pie chart of visitors by browser, each
- A pie chart with a labelA pie chart with each slice's value
- A pie chart with a label listA pie chart with each browser's name
- An area chart with a legendA stacked area chart of desktop and
- A multiple bar chartA bar chart grouping desktop and mobile
- A multiple line chartA line chart comparing desktop and mobile