shadcn.io is an independent project built on shadcn/ui
On this page9 sections
Chart is a thin layer over Recharts. You build the graph from Recharts' own parts, a BarChart with its Bar, XAxis and CartesianGrid, inside ChartContainer, which gives each series a color from one config and sizes the chart to its box. The tooltip and the legend read their labels from the same config.
When people need exact values rather than the shape of the data, use a Table. For finished chart types built on TanStack Charts, see the charts section.
Installation
Anatomy

Usage
import { Bar, BarChart, CartesianGrid, XAxis } from "recharts";
import {
type ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent,
} from "@/components/ui/chart";Each key in the config matches a key in the data. ChartContainer turns each color into a CSS variable, --color-desktop for the desktop key, which the matching Bar fills from.
const chartData = [
{ desktop: 186, mobile: 80, month: "January" },
{ desktop: 305, mobile: 200, month: "February" },
{ desktop: 237, mobile: 120, month: "March" },
];
const chartConfig = {
desktop: { color: "var(--chart-1)", label: "Desktop" },
mobile: { color: "var(--chart-2)", label: "Mobile" },
} satisfies ChartConfig;
export const VisitorsChart = () => (
<ChartContainer className="min-h-[200px] w-full" config={chartConfig}>
<BarChart data={chartData}>
<CartesianGrid vertical={false} />
<XAxis axisLine={false} dataKey="month" tickLine={false} />
<ChartTooltip content={<ChartTooltipContent />} />
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
</BarChart>
</ChartContainer>
);Accessibility
Recharts turns its accessibility layer on by default, and accessibilityLayer in the demos only says so: the chart's svg is a Tab stop with the application role, and the arrow keys move the tooltip from point to point. While that layer is on, the card ChartTooltipContent draws is a live region, as Recharts' own tooltip is, so a screen reader reads out the values as the arrow keys move it. The svg shows a focus outline when the keyboard reaches it, and none after a click. Pass title and desc to the Recharts chart to name and describe it, label every series in config, and put the figures that matter in text beside the chart.
- Tab
- Moves focus to the chart. The first time it gets focus, the tooltip opens on the first data point.
- Shift+Tab
- Moves focus to the previous focusable element and closes the tooltip.
- Arrow Right
- Moves the tooltip to the next data point. On an axis with
reversedset, it moves to the previous one, which is still to the right. - Arrow Left
- Moves the tooltip to the previous data point, or to the next one on a reversed axis.
- Enter
- Hides the tooltip, or shows it again on the same point.
Demos
Example
Six months of desktop and mobile visitors as two bar series, with no axis, grid or tooltip, which is the least a chart needs. ChartContainer takes the config, and each Bar fills from the --color-desktop or --color-mobile variable it writes. The colors are fixed hex values, so they don't change in the dark theme. Start here to check that the data and the config line up.
Grid
CartesianGrid with vertical set to false draws horizontal guide lines behind the same two series, and the container recolors them to the theme's border color. Guides help when bar heights are close and people compare them across the plot. There are still no labels, so on its own the grid tells nobody which bar is which month.
Axis
XAxis reads the month key and cuts each tick to its first three letters with tickFormatter, with the axis line and tick marks off and tickMargin set to 10 for room under the bars. The container gives the tick text the muted foreground color. Add an axis whenever the categories aren't obvious from a legend or a heading.
Example tooltip
ChartTooltip with ChartTooltipContent adds a card with the month and both values when a bar is hovered, or when the chart has keyboard focus. The series names come from the label entries in config, and the markers are dots, the default indicator. ChartTooltipContent reads that config from ChartContainer and throws when it's rendered anywhere else.
Legend
ChartLegend with ChartLegendContent names Desktop and Mobile under the plot, each beside a small square in its series color, with the labels taken from config. The tooltip stays, since the legend names the series while the tooltip gives one month's numbers. Add a legend when two or more series share the plot and no heading already names them.
Pie
Visitors by browser as a donut: one Pie with innerRadius set, each slice filled from the --color- variable its data row names in fill. nameKey on the Pie, on ChartTooltipContent and on ChartLegendContent points at the browser field, so the tooltip and the legend take each slice's label from config. Reach for it when a handful of parts make up one whole.
Line
Desktop and mobile visitors as two curved lines, with a dot in the series color on every month and a larger one where the tooltip sits. Each series sets theme in config instead of color: a darker blue and teal on the light theme, lighter ones under the .dark class, so both lines keep their contrast. Use lines when the trend between months matters more than each value.
Tooltip
Four static cards, not a live chart, carry callouts that name three parts of the tooltip card: the label, a series name and the color indicator. They show a label over two rows of dots, a dashed indicator with the label hidden, a single series with its label beside a line, and a lone dot. Pick the look with indicator and hideLabel on ChartTooltipContent.
RTL
Arabic month names and series labels, with dir set to rtl on ChartContainer and reversed on the XAxis, so January sits on the right. The tooltip and the legend read right to left, and tickFormatter and labelFormatter print each month's whole name, since Arabic doesn't shorten them. On the reversed axis the arrow keys still move the tooltip the way they point.
FAQ
How do I set the colors of a chart series in React?
Give each data key an entry in config with a label and a color. ChartContainer writes the color as a CSS variable named --color- plus the key, so the desktop series fills from var(--color-desktop). Keep keys to letters, digits and dashes, since a key with a space makes a variable name the browser drops.
Installing the chart adds --chart-1 to --chart-5 to your theme, each with a light and a dark value, so a color that points at one follows the theme. For a pair of your own, set theme to an object with light and dark colors in place of color, as the Line demo does. The dark one applies under a .dark class.
How do I add a tooltip to a chart and format its values?
Render ChartTooltip inside the Recharts chart with ChartTooltipContent as its content, as the Example tooltip demo does. The content reads config from ChartContainer and throws outside it. Set indicator to dot, line or dashed, and hideLabel or hideIndicator to drop parts, as the Tooltip demo shows.
labelFormatter rewrites the heading, a date into a full month for example. formatter takes over a whole row: once you pass it, the row shows what it returns in place of the indicator, the name and the value, so draw those yourself. Without it, numbers are printed with toLocaleString().
Why is my chart not showing, or the wrong size?
ChartContainer is a 16:9 box by default, and Recharts fills whatever height that box has. Replace the ratio with aspect-auto and no height, and the box is 0 pixels tall, so once Recharts measures it nothing draws. Give it a height, as in aspect-auto h-[250px], or a minimum such as min-h-[200px].
The box also needs a width. Add w-full, as every demo on this page does, so it takes its parent's width even as an item in a flex row. Before it's measured, the chart paints at the size in initialDimension, 320 by 200 by default.
Can keyboard and screen reader users read a chart?
Keyboard users can. Recharts turns its accessibility layer on by default, so the chart is a Tab stop and the arrow keys walk the tooltip from point to point, as the Accessibility section lists.
Screen reader users hear each point's values as the tooltip moves, because the card ChartTooltipContent draws is a live region while the layer is on, but the shape of the data reaches them only point by point. The chart's svg has no name until you pass title and desc to the Recharts chart. Say the figures that matter in text next to the chart, or offer the data as a Table, and give every series a label so color isn't the only way to tell them apart.
How do I add a legend to a chart?
Render ChartLegend inside the Recharts chart with ChartLegendContent as its content, as the Legend demo does. Each entry shows the series' icon from config, or a square in its color, beside its label. hideIcon keeps the square even when the config has an icon.
The legend sits under the plot. Set verticalAlign to top on ChartLegend to move it above, and the content swaps its padding to the side that faces the plot. When each data item names its own config key, such as a browser name on each slice of a pie, pass the name of that field as nameKey, as the Pie demo does.
Can I use a chart in a Next.js server component?
Not directly. Recharts runs on hooks and has no client directive of its own, and the demos pass functions such as tickFormatter, which can't cross from the server to the client. The chart file starts with "use client", and so does every demo.
Put the chart in its own file that starts with "use client", and render it from your server page, passing the data as a prop. The data can still be fetched on the server, as long as it is plain values that can be serialized.
How do I let people switch which series a chart shows?
Keep the active key in state and render one Bar whose dataKey is that key and whose fill is the matching --color- variable. The first chart on this page does this with a Desktop and a Mobile button in its header, each showing that series' total for the period.
The demo gives each button aria-pressed, true for the series that's showing, so a screen reader hears which is on, and data-active gives that button a muted background. Do the same in your own header, since a background alone says nothing to a screen reader.
When should I use a chart instead of a table?
Use a chart when the shape of the data is the point: a trend over three months, one series overtaking another, a month that stands out. A Table is better when people look up exact values, compare figures to the cent, or need to copy them.
Many dashboards show both, the chart for the pattern and a table or a few headline numbers for the figures. For finished chart types with their own docs, such as line, area and pie charts on TanStack Charts, see the charts section.
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>
accessibilityLayer?boolean
Passed by Recharts' Tooltip, on unless the chart turns it off: the * card is then a live region, as Recharts' own tooltip is, so a screen * reader reads each point the arrow keys move to.
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.











