Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

A bar chart for React

Six months of desktop visitors as rounded vertical bars. Short month names sit on the axis, and hover shows the count.

Six rounded bars, one per month, for desktop visitors. The axis shows Jan through Jun. Hover a bar and the tooltip gives that month's count.

Use it to compare one number across a short list of months. If people need every figure in a column, use a Table. In React, Base UI turns the config color into a variable. You still pass the months.

Preview

Preview width

Installation

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

Anatomy

Chart anatomy: container, grid, axis, bar, tooltip, legend

Usage

<div className="w-full max-w-xl rounded-md border bg-background p-4">
  <ChartContainer config={chartConfig}>
    <BarChart accessibilityLayer data={chartData}>
      <CartesianGrid vertical={false} />
      <XAxis
        axisLine={false}
        dataKey="month"
        tickFormatter={formatMonth}
        tickLine={false}
        tickMargin={10}
      />
      <ChartTooltip
        content={<ChartTooltipContent hideLabel />}
        cursor={false}
      />
      <Bar dataKey="desktop" fill="var(--color-desktop)" radius={8} />
    </BarChart>
  </ChartContainer>
</div>

FAQ

How are the bar corners rounded?

Bar sets radius to 8, so every corner of the desktop bars is rounded by 8px. The series is dataKey desktop, filled with var(--color-desktop).

Where does the bar color come from?

config gives desktop the label Desktop and color var(--chart-1). ChartContainer writes that as --color-desktop, which is what fill reads.

How are the month ticks shortened?

XAxis uses dataKey month and tickFormatter set to formatMonth, which returns value.slice(0, 3). axisLine and tickLine are off, and tickMargin is 10.

Why are the grid lines horizontal only?

CartesianGrid sets vertical to false, so the plot shows horizontal lines. BarChart also sets accessibilityLayer, which lets the bars be reached from the keyboard.

What does the tooltip show?

ChartTooltip renders ChartTooltipContent with hideLabel, and cursor is false. The row is named Desktop from config, and the value is that month's desktop count.

How big is the chart?

The frame is w-full max-w-xl with a border and padding. ChartContainer has no height class here, so it keeps the default 16:9 box.

API Reference

Chart

Sets config={chartConfig} and hideLabel. The rest is on the Chart page.