shadcn.io is not affiliated with official shadcn/ui
Chart examples · Pie
A donut chart with text for React
A donut chart with the total number of visitors in its center.
Use it when the total matters as much as the split. The ring shows visitors by browser, and a Label inside the Pie writes the sum of all slices and the word Visitors in the hole, centered on the chart's viewBox.
Preview
Installation
FAQ
How is the total drawn in the hole?
Pie nests a Label whose content is renderTotal. When viewBox has cx and cy, that function returns an SVG text anchored at the center with textAnchor and dominantBaseline set to middle.
How is the center number calculated?
renderTotal sums chartData with reduce, starting at 0 and adding each visitors value, then calls toLocaleString(). These rows total 1,125: Chrome 275, Safari 200, Firefox 287, Edge 173, and Other 190.
How are the number and the word Visitors stacked?
The first tspan prints the total at viewBox.cy with className fill-foreground font-bold text-3xl. The second prints Visitors at viewBox.cy + 24 with className fill-muted-foreground.
How big is the hole the text sits in?
Pie sets innerRadius to 60 and strokeWidth to 5. dataKey is visitors and nameKey is browser.
Related
- A donut chartA donut chart of visitors by browser, a
- An interactive pie chartA donut chart with an active sector, a
- A donut chart with an active sectorA donut chart with its first sector
- A radial chart with textA radial bar chart with one rounded ring
- A simple pie chartA pie chart of visitors by browser, each
- A pie chart with a legendA pie chart with a wrapping legend of