Skip to content

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

Free

An interactive pie chart for React

A donut of desktop visitors by month. January is pulled out, wrapped in a second ring, and its count sits in the center.

A donut of desktop visitors by month. January is pulled out and wrapped in a second ring. The center shows that slice's count and the word Visitors. The active slice is a constant.

Use it to feature one slice without hiding the rest. Wire the index to a real selection, such as Tabs, if it should change. In React, Base UI does not know that January is active.

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 w-full max-w-[300px]"
    config={chartConfig}
  >
    <PieChart>
      <ChartTooltip
        content={<ChartTooltipContent hideLabel />}
        cursor={false}
      />
      <Pie
        data={desktopData}
        dataKey="desktop"
        innerRadius={60}
        nameKey="month"
        shape={renderSector}
        strokeWidth={5}
      >
        <Label content={renderActiveValue} />
      </Pie>
    </PieChart>
  </ChartContainer>
</div>

FAQ

How do I choose which slice is active?

ACTIVE_INDEX is 0, the first row, January. Change that number. shape is renderSector, which checks index === ACTIVE_INDEX. This file doesn't wire the index to a click or a hover.

What does the active slice look like?

For that index, renderSector draws one Sector with outerRadius plus 10, then a second Sector from outerRadius + 12 to outerRadius + 25. Every other index is a single Sector at the normal outerRadius.

How does the center get the active value?

Label uses content={renderActiveValue} inside Pie. It prints desktopData[ACTIVE_INDEX].desktop with toLocaleString, and the caption Visitors. The number uses text-3xl. The caption sits at viewBox.cy + 24.

How is the hole sized?

Pie sets innerRadius={60}, dataKey="desktop", nameKey="month", and strokeWidth={5}. The stroke separates the slices. The rows are january through may, and each fill is var(--color-january) and the same pattern for the other months.

What does the tooltip call each slice?

ChartTooltipContent sets hideLabel. nameKey="month" on Pie names the slice january (and the other months), and the tooltip looks that key up in config for January, February, March, April, and May. cursor={false} turns the hover cursor off.

How do I put the month name in the center?

The caption is the string Visitors. The active row's month is a key in config, so chartConfig[desktopData[ACTIVE_INDEX].month].label is January when the index is 0. The chart frame is aspect-square w-full max-w-[300px].

API Reference

Chart

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