# Bar Chart (/ui/components/react/bar-chart)

A bar chart compares 1 measure across categories or periods. Like every chart here it is a recipe: [Recharts](https://recharts.github.io) draws it and the [chart layer](/ui/components/react/charts) themes it. Each example below is a complete file to paste and adapt.

## Installation [#installation]

Recharts is the only extra dependency, with `react-is` at the same version as `react`:

```bash
pnpm add recharts react-is   # or: npm install recharts react-is / yarn add recharts react-is / bun add recharts react-is
```

The examples import the chart layer from `@/components/chart`. If your project doesn't have it yet, copy it from the [Charts](/ui/components/react/charts#the-chart-layer) page first.

## Examples [#examples]

### Default [#default]

Bars with a 4 px outer radius from `chartMarks.barRadius`, and a `maxBarSize` so a chart with few bars doesn't grow fat ones. On mount they grow from the baseline one after another.

```tsx
'use client'

import { Bar, BarChart, CartesianGrid, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { month: 'Jan', orders: 1240 },
  { month: 'Feb', orders: 1410 },
  { month: 'Mar', orders: 1360 },
  { month: 'Apr', orders: 1680 },
  { month: 'May', orders: 1920 },
  { month: 'Jun', orders: 1790 },
  { month: 'Jul', orders: 2140 },
  { month: 'Aug', orders: 2030 },
  { month: 'Sep', orders: 2360 },
  { month: 'Oct', orders: 2710 },
  { month: 'Nov', orders: 2540 },
  { month: 'Dec', orders: 3080 },
]

const compact = new Intl.NumberFormat('en-US', { notation: 'compact' })
const count = new Intl.NumberFormat('en-US')

export default function BarChartDefault() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart>
        <BarChart responsive data={data} aria-label="Orders by month">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="month" tickLine={false} axisLine={false} />
          <YAxis
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Bar
            name="Orders"
            dataKey="orders"
            fill={chartColors[0]}
            radius={[chartMarks.barRadius, chartMarks.barRadius, 0, 0]}
            maxBarSize={40}
            {...motion.bars}
          />
        </BarChart>
      </Chart>
    </div>
  )
}
```

### Stacked [#stacked]

`BarStack` stacks the bars inside it and rounds the stack as 1 shape, where a `radius` on each `Bar` would round every segment. A stroke in the surface color separates the segments.

```tsx
'use client'

import { Bar, BarChart, BarStack, CartesianGrid, Legend, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { month: 'Jul', online: 16700, retail: 8300, wholesale: 6500 },
  { month: 'Aug', online: 15800, retail: 7900, wholesale: 6000 },
  { month: 'Sep', online: 18200, retail: 8800, wholesale: 7100 },
  { month: 'Oct', online: 20900, retail: 9700, wholesale: 8200 },
  { month: 'Nov', online: 19600, retail: 9300, wholesale: 7500 },
  { month: 'Dec', online: 23400, retail: 10800, wholesale: 8700 },
]

const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })

export default function BarChartStacked() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <BarChart responsive data={data} aria-label="Revenue by channel, stacked by month">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="month" tickLine={false} axisLine={false} />
          <YAxis
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
          <Legend itemSorter={null} content={<ChartLegend />} />
          <BarStack radius={[chartMarks.barRadius, chartMarks.barRadius, 0, 0]}>
            <Bar
              name="Online store"
              dataKey="online"
              fill={chartColors[0]}
              stroke="var(--background)"
              maxBarSize={48}
              {...motion.bars}
            />
            <Bar
              name="Retail"
              dataKey="retail"
              fill={chartColors[1]}
              stroke="var(--background)"
              maxBarSize={48}
              {...motion.bars}
            />
            <Bar
              name="Wholesale"
              dataKey="wholesale"
              fill={chartColors[2]}
              stroke="var(--background)"
              maxBarSize={48}
              {...motion.bars}
            />
          </BarStack>
        </BarChart>
      </Chart>
    </div>
  )
}
```

### Grouped [#grouped]

Without a `BarStack`, bars that share a category sit side by side. `barGap` sets the space between them.

```tsx
'use client'

import { Bar, BarChart, CartesianGrid, Legend, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { quarter: 'Q1', newCustomers: 1840, returning: 2960 },
  { quarter: 'Q2', newCustomers: 2210, returning: 3380 },
  { quarter: 'Q3', newCustomers: 2590, returning: 3720 },
  { quarter: 'Q4', newCustomers: 3340, returning: 4810 },
]

const compact = new Intl.NumberFormat('en-US', { notation: 'compact' })
const count = new Intl.NumberFormat('en-US')

export default function BarChartGrouped() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <BarChart responsive data={data} barGap={4} aria-label="New and returning customers by quarter">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="quarter" tickLine={false} axisLine={false} />
          <YAxis
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Legend itemSorter={null} content={<ChartLegend />} />
          <Bar
            name="New"
            dataKey="newCustomers"
            fill={chartColors[0]}
            radius={[chartMarks.barRadius, chartMarks.barRadius, 0, 0]}
            maxBarSize={40}
            {...motion.bars}
          />
          <Bar
            name="Returning"
            dataKey="returning"
            fill={chartColors[1]}
            radius={[chartMarks.barRadius, chartMarks.barRadius, 0, 0]}
            maxBarSize={40}
            {...motion.bars}
          />
        </BarChart>
      </Chart>
    </div>
  )
}
```

### Horizontal [#horizontal]

A ranking reads best as horizontal bars, sorted largest first, with room for long labels. Recharts calls this `layout="vertical"`: the category axis is the `YAxis`, and its `width="auto"` fits the longest label.

```tsx
'use client'

import { Bar, BarChart, CartesianGrid, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { product: 'Linen shirt', sales: 48200 },
  { product: 'Canvas tote', sales: 39600 },
  { product: 'Wool beanie', sales: 31400 },
  { product: 'Leather belt', sales: 26900 },
  { product: 'Cotton socks', sales: 18700 },
  { product: 'Enamel pin', sales: 9300 },
]

const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })

export default function BarChartHorizontal() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart>
        <BarChart responsive data={data} layout="vertical" aria-label="Top 6 products by sales">
          <CartesianGrid horizontal={false} />
          <XAxis
            type="number"
            niceTicks="snap125"
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
          />
          <YAxis type="category" dataKey="product" tickLine={false} axisLine={false} width="auto" />
          <Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
          <Bar
            name="Sales"
            dataKey="sales"
            fill={chartColors[0]}
            radius={[0, chartMarks.barRadius, chartMarks.barRadius, 0]}
            maxBarSize={20}
            {...motion.bars}
          />
        </BarChart>
      </Chart>
    </div>
  )
}
```

### Negative [#negative]

For values on both sides of zero, color carries the sign: each row has its own `fill`, the success color of `chartColors` above the line and the error color below. A `ReferenceLine` marks zero.

```tsx
'use client'

import { Bar, BarChart, CartesianGrid, ReferenceLine, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const months = [
  { month: 'Jan', net: 420 },
  { month: 'Feb', net: 310 },
  { month: 'Mar', net: -180 },
  { month: 'Apr', net: 260 },
  { month: 'May', net: 540 },
  { month: 'Jun', net: -90 },
  { month: 'Jul', net: -340 },
  { month: 'Aug', net: 150 },
  { month: 'Sep', net: 480 },
  { month: 'Oct', net: 620 },
  { month: 'Nov', net: -210 },
  { month: 'Dec', net: 710 },
]

const data = months.map((row) => ({
  ...row,
  fill: row.net < 0 ? chartColors[4] : chartColors[1],
}))

const signed = new Intl.NumberFormat('en-US', { signDisplay: 'exceptZero' })

export default function BarChartNegative() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart>
        <BarChart responsive data={data} aria-label="Net new subscribers by month">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="month" tickLine={false} axisLine={false} />
          <YAxis
            tickFormatter={(value) => signed.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => signed.format(value)} />} />
          <ReferenceLine y={0} stroke="var(--border-strong)" />
          <Bar
            name="Net new subscribers"
            dataKey="net"
            radius={[chartMarks.barRadius, chartMarks.barRadius, 0, 0]}
            maxBarSize={40}
            {...motion.bars}
          />
        </BarChart>
      </Chart>
    </div>
  )
}
```

## Accessibility [#accessibility]

- Pass `aria-label` to the chart with what it shows (`"Orders by month"`). It becomes the accessible name of the SVG.
- The chart takes focus, and the arrow keys move the tooltip from bar to bar.
- A chart with 2 or more series has a `Legend`, and the tooltip names every series, so color is never the only cue. In the diverging chart the sign is in the value too.
- Nothing animates under reduced motion.
