Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Pie

A simple pie chart for React

A pie chart of visitors by browser, each slice in its own color.

Framework
UI library

The starting point for parts of a whole. Each browser is a slice sized by its visitors and colored from the chart config, and the tooltip names the browser and its count. Keep pies to a handful of slices.

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 each slice built?

Pie sets data={chartData}, dataKey="visitors", and nameKey="browser". visitors is the slice size, from Chrome at 275 to Other at 90. nameKey is the field the tooltip uses as the slice name.

Where does a slice's color come from?

Each row sets fill to var(--color-chrome) or the matching browser key. config maps those keys to var(--chart-1) through var(--chart-5) and to the labels Chrome, Safari, Firefox, Edge, and Other. ChartContainer writes the --color- variables.

Why does the tooltip say Chrome and not visitors?

Pie sets nameKey="browser", so the hovered slice is named chrome (and the other keys). ChartTooltipContent looks that key up in config and shows the label. hideLabel hides the title. cursor={false} turns the hover cursor off.

How do I add a slice?

Add a row to chartData with browser, visitors, and fill, and add the same key to config with a label and a color. Pie reads the whole array. This file doesn't set innerRadius, so the slices meet in the middle.

What sets the order of the slices?

The order of chartData. This demo lists chrome, safari, firefox, edge, then other. There is no ChartLegend and no LabelList, so the names show up in the tooltip.

How do I keep the pie square?

ChartContainer sets mx-auto aspect-square max-h-[250px] inside a w-full max-w-xl frame. That overrides the container's default 16:9 box.