Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

A donut chart with an active sector for React

A browser donut with the Chrome slice drawn at a larger outer radius so it sticks out from the ring. The highlight is the first slice.

A browser donut. The Chrome slice is drawn with a larger outer radius so it sticks out. The rest of the ring stays put. The highlight is the first slice, not a hover.

Use a popped slice to point at one category while the whole stays visible. Do not leave it as a constant if the choice should follow the user. A Table shows every count at once. In React, Base UI does not push Chrome outward.

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.
Use email insteadNo credit card

Anatomy

Chart anatomy: container, grid, axis, bar, tooltip, legend

Usage

<div className="w-full max-w-xl rounded-md border bg-background p-4">
  <ChartContainer
    className="mx-auto aspect-square max-h-[250px]"
    config={chartConfig}
  >
    <PieChart>
      <ChartTooltip
        content={<ChartTooltipContent hideLabel />}
        cursor={false}
      />
      <Pie
        data={chartData}
        dataKey="visitors"
        innerRadius={60}
        nameKey="browser"
        shape={renderSector}
        strokeWidth={5}
      />
    </PieChart>
  </ChartContainer>
</div>

FAQ

How does the Chrome slice stick out?

Pie sets shape to renderSector. When index equals ACTIVE_INDEX, which is 0, that function passes outerRadius plus 10 to Sector. The Chrome row is first, so Chrome is the slice that grows.

How do I pop out a different slice?

Change ACTIVE_INDEX. The order is Chrome, Safari, Firefox, Edge, Other. Pie still uses dataKey visitors, nameKey browser, innerRadius 60, and strokeWidth 5.

Where do the slice colors come from?

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

What does the tooltip show for a slice?

ChartTooltipContent sets hideLabel, and cursor is false. The name comes from nameKey browser, looked up in config, and the value is visitors.

How big is the donut?

ChartContainer is mx-auto aspect-square max-h-[250px]. The hole is innerRadius 60. The active slice is drawn 10px past the outerRadius the other slices use.

What does strokeWidth do here?

Pie sets strokeWidth to 5, which is the stroke around every slice, including the one renderSector pushes out. That function only changes outerRadius.

API Reference

Chart

Sets config={chartConfig} and hideLabel. The rest is on the Chart page.