Skip to Content
kindui

Bar Chart

Compare values across categories with columns or horizontal bars.

Use a bar chart to compare a measure across categories, like locations or products, and to compare a few measures side by side or stacked.

Hover a bar, or focus the chart and press the arrow keys, to see its value and dim the other bars.

Installation

npx shadcn@latest add @kind-ui/bar-chart

Composition

BarChart
├── BarChart.Background
├── BarChart.Grid
├── BarChart.XAxis
├── BarChart.YAxis
├── BarChart.Series
│   ├── BarChart.Cell
│   └── BarChart.Labels
├── BarChart.Tooltip
├── BarChart.Legend
└── BarChart.Content

BarChart holds the data, the series config and the accessible name, and draws only the parts you add. Add one BarChart.Series per measure. BarChart.Cell colors a single bar and BarChart.Labels writes values on the bars. The tooltip and legend read their labels and colors from config. BarChart.Content places your own HTML next to the legend.

Usage

Series

Each BarChart.Series plots the dataKey it names, and takes its label and color from the matching config entry. Series sit side by side in each category. Set radius to round the corners and maxBarSize to cap the width:

Press Print or Digital in the legend to highlight that series.

Give series the same stackId to stack them. Set stackOffset="expand" on the chart to stack to 100%, or "sign" to stack negative values below zero:

<BarChart.Series dataKey="print" stackId="loans" />
<BarChart.Series dataKey="digital" stackId="loans" />

Horizontal Bars

Set layout="vertical" for horizontal bars. The axes swap, so the X axis is the number scale and the Y axis names the categories:

<BarChart layout="vertical" {...props}>
  <BarChart.XAxis type="number" />
  <BarChart.YAxis type="category" dataKey="location" width={80} />
  <BarChart.Series dataKey="orders" radius={[0, 4, 4, 0]} />
</BarChart>

Emphasis

By default, hovering a bar dims the other series. Set emphasis="category" to dim the other categories instead, as in the first example, or "none" to turn it off. Category emphasis reads each row's category from the category axis, so those categories must be unique strings. When they aren't, like numbers or repeated labels, pass the series an emphasisKey that returns a unique key for each row. A custom shape, range values or per-series data turn it off for that chart.

Legend and Keyboard

Pressing a legend item highlights its series and dims the others, and screen readers hear "Highlighted Print." Press it again, or press Escape, to clear it. Hidden series keep their slot in each group, and stacks keep their baselines. To hide and show series from the legend instead, see Legend.

Focus the chart and press the arrow keys to move between categories, which shows the tooltip, and Escape to dismiss it.

Patterns and Projections

Give a series a pattern in config to fill its bars with hatch, stripe, duotone, dots or lines, and its legend swatch follows. To pattern only some bars, like projected values, pass projection with a function that picks them:

<BarChart.Series
  dataKey="orders"
  projection={{ isProjected: (row) => row.projected, pattern: { kind: "stripe" } }}
/>

The chart only draws the values you pass; it doesn't forecast them. An explicit fill, BarChart.Cell or custom shape replaces the pattern.

Set material="clay" or "glow" on a series for a finish; see Materials.

Motion

Set animate to grow the bars from zero when the chart mounts, over one second by default. Pass options to change the duration or easing:

<BarChart animate={{ revealDurationMs: 600 }} {...props}>

When the reader prefers reduced motion, bars appear without growing. See Motion.

Loading

Set loading while the data is on its way. The chart keeps its size and shows placeholder bars that rise and fade, or hold still when the reader prefers reduced motion. Screen readers hear loadingLabel ("Loading chart" by default). The legend and tooltip come back when loading turns off:

<BarChart loading={isPending} loadingLabel="Loading orders" {...props}>

API Reference

BarChart

Prop

Type

BarChart.Series

Prop

Type

BarChart and BarChart.Series also take every prop of Recharts' BarChart and Bar, like layout, stackOffset, stackId, radius and maxBarSize.