Skip to content

React Bar Chart

A React bar chart compares values as animated bars in grouped, stacked or horizontal layouts, each with tooltips, keyboard reading and a table view. Pass data as readonly ChartRow[]: one object per category: the index key plus one number per series key.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/bar-chart.json

Customers per plan

  • Free
  • Pro
  • Team
Use arrow keys to read values.

Stacked bars: customers per plan

Stacked bars show the total and the split in one mark.

Customers per plan

Last six months

  • Free
  • Pro
  • Team

Files to copy

11 folders · 27 files · 4 packages
From theme/src/
  • components/charts/{bar-chart.tsx, chart-annotations.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-sync.tsx, chart-utils.ts}
  • components/charts/chart-parts/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/menu/
  • components/skeleton/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/bar-chart.jsonRegistry item: bar-chart. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Horizontal bars

Best for long category names. Values sit at the tip of each bar.

Top countries

Visitors this month

Patterns

Hatch fills tell the series apart without colour. They switch on by themselves above three series; pass patterns to force them, and give seriesLegend the same flag so the legend swatches match.

Customers per plan

Hatched, so it survives greyscale print

  • Free
  • Pro
  • Team

BarChart props

PropTypeDefaultDescription
data (required)readonly ChartRow[]NoneOne object per category: the index key plus one number per series key, e.g. { day: "Mon", orders: 12 }.
index (required)stringNoneKey of each row that names its category (the axis label), e.g. "day".
series (required)readonly CompareSeries[]NoneThe bars. Give a series a compareKey to mark its previous period on each bar (unstacked).
layout"horizontal" | "vertical""vertical""vertical" = columns rising from the x-axis; "horizontal" = bars from the left (best for long category names).
stackedbooleanfalseStack series on top of each other instead of side by side. Negative values stack downwards from zero (a diverging stack).
showValuesbooleanfalsePrint the value at the end of each bar (single, unstacked series only).
heightnumber260 for columnsChart height in px, axes and labels included.
format((n: number) => string)NoneFormat values in the tooltip and labels, and on the axis (the default axis format follows the tick step).
localestringNoneLocale for the default number format (e.g. "de").
emptyStateReactNodeNoneShown when there is nothing to plot.
patternsbooleantrue when series.length > 3Tell series apart without colour (hatch fills).
gradientbooleantrueA soft sheen down each bar (strongest at the value end).
glowbooleantrueSoft glow on the bars being read, in dark mode.
hiddenSeriesreadonly string[]NoneSeries keys to hide (controlled). Inside a ChartCard the card's legend controls this.
highlightedSeriesstring | nullNoneSeries key to emphasise (the others dim). Inside a ChartCard, legend hover controls this.
annotationsreadonly ChartAnnotation[][]Reference lines / bands on the value axis, event markers and ranges on the categories.
syncbooleantrueInside a ChartSyncProvider: share the active category with the other charts.
tooltipSurfaceTooltipSurface"glass"Tooltip surface style: "glass" (default) or "opaque" for busy charts.
labelsPartial<BarChartLabels>defaultBarChartLabelsThe chart's built-in strings (empty state, table text); a partial object over the English defaults, so pass only the keys you translate. Every other string, for translation: labels={{ instructions: "…" }}; omitted keys keep the English of defaultBarChartLabels. All 3 keys of BarChartLabels are listed in the key table under the props table.
aria-label (required)stringNoneNames the chart for assistive tech (required): say what is plotted, e.g. "Orders per day".
classNamestringNoneAdds extra classes to the outer element.
BarChartLabels keys and English defaults (3)
Pass any key in labels; the rest keep the defaults of defaultBarChartLabels.
KeyEnglish default
instructionsUse arrow keys to read values.
totalTotal
previous(label) => `${label} (previous)`

BarTable props

PropTypeDefaultDescription
data (required)readonly ChartRow[]NoneThe rows to show: one object per row, holding the index key and one number per series key.
index (required)stringNoneThe key in each row that names the row, such as "month". It becomes the first column.
series (required)readonly CompareSeries[]NoneSeries to list as columns; one with a compareKey also gets a previous-period column.
format((n: number) => string)formatCompactFormats each number in the table cells.
indexLabelstringNoneHeader of the first column; the index key when left out.
captionstringNoneVisually hidden caption naming the table for screen readers.
labelsPartial<ChartTableLabels>defaultChartTableLabelsText for i18n (defaults come from the enclosing ChartCard's labels, else English). Every other string, for translation: labels={{ noData: "…" }}; omitted keys keep the English of defaultChartTableLabels. All 2 keys of ChartTableLabels are listed in the key table under the props table.
ChartTableLabels keys and English defaults (2)
Pass any key in labels; the rest keep the defaults of defaultChartTableLabels.
KeyEnglish default
noDataNo data
previous(label: string) => `${label} (previous)`

When to use a bar chart

Use a React bar chart to compare values across categories: plans, countries, products or weeks. Bars start at zero, so their lengths compare honestly. Choose grouped bars to compare series side by side, stacked bars to show a total and its parts, and horizontal bars when the labels are long.

For a ranked list with inline values try the bar list; for positive and negative values, the diverging bar chart; for shares that always add up to 100 percent, the 100% stacked bars. A trend over time reads better in a line chart.

Frequently asked questions

All exports on this page

  • BarChart — A column or bar chart for comparing values across categories, grouped or stacked, with optional previous-period marks.
  • BarTable — The table view of a BarChart: one row per category, one column per series.
  • defaultBarChartLabels — The built-in English strings of BarChart; spread it into your own labels and change what you need.

Keyboard

KeysAction
TabMove through the Chart / Table tabs, then the legend keys, then the chart itself.
← → (↑ ↓)Read the previous or next point; the values are announced.
Home/EndJump to the first or last point.
EscapeClear the active point.
Enter/Spaceon a legend keyHide or show that series.

Accessibility

  • Tab focuses the chart as one stop; the arrow keys read one category at a time and announce each series value in it.
  • The Chart / Table tabs switch to a BarTable: one row per category, one column per series, with a caption and row headers; negative values keep their sign.
  • With more than three series (or patterns) the bars get hatch fills, so the legend works without color.
  • Reduced motion: bars appear at their final length and data changes jump.

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .