Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Pie

A pie chart with a custom label for React

A pie chart printing each slice's value beside it with a custom label.

Framework
UI library

Use a custom label when the default one doesn't say what you need. The label prop takes a function that draws each slice's value as text beside it, in the foreground color, and the label lines are turned off.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How is the number beside each slice drawn?

Pie sets label to renderLabel and labelLine to false. renderLabel returns an SVG text at the x and y it receives, copying textAnchor and dominantBaseline, and the text is payload.visitors.

How is that text colored?

The text element uses className fill-foreground. ChartContainer is mx-auto aspect-square max-h-[250px] with px-0, which leaves the side labels the width of the frame.

What do dataKey and nameKey do here?

dataKey is visitors, so the slice angle and the printed number are the visitor count. nameKey is browser, which is the slice name passed through to the chart.

What does the tooltip call each slice?

ChartTooltipContent sets hideLabel and nameKey to visitors. That key is labeled Visitors in config, so the tooltip row uses that name and the visitor count.

How are the slices colored?

Each row sets fill to var(--color-chrome) or the matching browser variable. config maps chrome through other to --chart-1 through --chart-5 and to labels Chrome, Safari, Firefox, Edge, and Other.

Which numbers appear beside the slices?

Chrome is 275, Safari 200, Firefox 187, Edge 173, and Other 90. labelLine is false, so those numbers sit at the label position with no connector.