shadcn.io is not affiliated with official shadcn/ui
About
Use it when the slices are big enough to hold a word and you'd rather not spend space on a legend. It keeps the card compact, which suits small dashboard tiles. With six or more slices, or long names, the Pie Chart with Legend reads better.
Five slices, each with its browser name centered inside it, set in the background color so the text reads on every chart color.
Installation
Usage
import { ChartPieLabelList } from "@/components/charts/chart-pie-label-list";<ChartPieLabelList />Customize
<LabelList dataKey="browser" formatter={…} />inside<Pie>writes one name per sector, looked up inchartConfig.className="fill-background"andstroke="none"style the text;fontSize={12}keeps it small.[&_.recharts-text]:fill-backgroundonChartContainercovers Recharts' text elements too.<ChartTooltipContent hideLabel nameKey="visitors" />shows the count on hover.
FAQ
How are the names placed inside the slices?
<LabelList dataKey="browser" /> inside <Pie> places one label per sector, centered in it. fontSize={12} and stroke="none" keep the text small and clean.
Where do the capitalized names come from?
formatter on <LabelList> maps each browser value to chartConfig[value].label, so the sector shows "Chrome", not "chrome".
How do the labels stay readable on colored slices?
className="fill-background" on <LabelList>, and [&_.recharts-text]:fill-background on ChartContainer, paint the text in the background token so it contrasts with the sectors.
API Reference
ChartContainer
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>
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>
configChartConfig
The chart config whose colors it writes as --color-<key> variables.
idstring
The element's id.
ChartTooltip
ChartTooltipContent
<div>
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– anypayload– 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.
Related
Pie Chart with Labels
Pie chartsEach slice's value printed beside it.
Pie Chart with Custom Labels
Pie chartsSlice labels drawn outside the pie in the foreground color.
Pie Chart with Legend
Pie chartsA pie with a legend of its slices underneath.
Bar Chart with Custom Labels
Bar chartsHorizontal bars with the name inside each bar and the value after it.
Donut Chart
Pie chartsA pie with a hollow center: a donut.
Pie Chart
Pie chartsA plain pie, one slice per browser.