Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Charts

Free

Stacked Pie Chart

Two rings of monthly slices, desktop inside and mobile around it, to compare how two series split across the same months. For React on Recharts.

Framework
Charting library

Preview

Preview width

About

Use it to compare two splits of the same categories, desktop against mobile or this year against last, when the shape of each split matters more than exact values. To compare totals per month, a grouped bar chart is clearer.

The desktop months fill a pie in the center; the mobile months form a ring around it, 10px out. Each month has one color on both, so you can follow January from the inside to the outside.

Installation

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

Usage

import { ChartPieStacked } from "@/components/charts/chart-pie-stacked";
<ChartPieStacked />

Customize

  • Two <Pie>s share the <PieChart>: desktopData with outerRadius={60}, mobileData with innerRadius={70} and outerRadius={90}.
  • Each row's fill, var(--color-<month>), ties a month's color across both rings.
  • labelFormatter returns chartConfig[payload[0].dataKey].label, so the tooltip's title is Desktop or Mobile.
  • indicator="line", nameKey="month" and labelKey="visitors" set the rest of the tooltip.

FAQ

How are the two rings built?

Two <Pie>s in one <PieChart>: desktop with outerRadius={60}, mobile with innerRadius={70} and outerRadius={90}. The difference, 10px, is the gap between them.

How do matching months get the same color?

Each row of both arrays has fill: "var(--color-<month>)", so the same month gets the same color on both rings from one chartConfig entry.

How does the tooltip know which ring I'm on?

<ChartTooltipContent indicator="line" nameKey="month" labelKey="visitors" labelFormatter={…} />: the formatter reads the hovered dataKey (desktop or mobile) and returns its chartConfig label.

Why don't the rings grow with the chart?

outerRadius and innerRadius take percentages as well as pixels: outerRadius="50%" for the inner pie and innerRadius="60%" with outerRadius="80%" for the ring scale with the chart.

Can I add a third ring?

Add another <Pie> with its own data and an innerRadius past the previous outerRadius. Every ring shares the tooltip.

API Reference

ChartContainer

PropType
configChartConfig

Each series by its data key: its label, an icon, and a color (or a * theme color per light and dark), read by the tooltip and legend and set * as --color-<key>.

initialDimension?{ height: number; width: number; }

The size it renders at before it's measured, so the first paint isn't * empty.

Type
{ height: number; width: number; }
Default
INITIAL_DIMENSION
className?string

Merged with the part's classes.

ChartLegend

ChartLegendContent

<div>

PropType
hideIcon?boolean

Hides each series' icon (from config), leaving its color swatch.

Default
false
nameKey?string

The key in each legend item's data to look its label up by in config. * Defaults to the item's dataKey.

className?string

Merged with the part's classes.

ChartStyle

<style>

PropType
configChartConfig

The chart config whose colors it writes as --color-<key> variables.

idstring

The element's id.

ChartTooltip

ChartTooltipContent

<div>

PropType
hideIndicator?boolean

Hides each item's color indicator.

Default
false
hideLabel?boolean

Hides the tooltip's label, the hovered point's.

Default
false
indicator?"dashed" | "dot" | "line"

The color indicator's shape: a dot, a line or dashed.

Type
"dashed" | "dot" | "line"
Default
"dot"
labelFormatter?(label: any, payload: TooltipPayload) → React.ReactNode

Formats the tooltip's label; its value is untyped, as in recharts 3.8.

Type
(label: any, payload: TooltipPayload) → React.ReactNode
Parameters

label – any

payload – TooltipPayload

Returns
React.ReactNode
labelKey?string

The key in the data to read the label from, looked up in config.

nameKey?string

The key in each item's data to look its name up by in config.

className?string

Merged with the part's classes.