PeakUIPro

BarChart

Bar chart with grouped, stacked and horizontal layouts

Usage

BarChart is imported from its own subpath and needs the optional recharts peer.

npm install recharts
import {BarChart} from "@peakui-pro/react/bar-chart";

Anatomy

import {BarChart} from "@peakui-pro/react/bar-chart";

// Only the root is this package's own; the parts are the charting library's components under
// PeakUI names. The root is not exported from `@peakui-pro/react`, so an app that renders no
// chart never needs the peer installed, not even to render on a server.
export default () => (
  <BarChart aria-label="Revenue by month" data={[]}>
    <BarChart.Grid />
    <BarChart.XAxis />
    <BarChart.YAxis />
    <BarChart.Bar dataKey="revenue" />
    <BarChart.Tooltip content={<BarChart.TooltipContent />} />
  </BarChart>
);

Accessibility

The plot takes focus and answers the arrow keys, so aria-label names it, not the wrapper. A bar is told apart by colour and by its position, so give each series a name the tooltip can print, and keep a gap between grouped bars.

// Named by a string, or by the element that already carries the metric's title.
<BarChart aria-label="Revenue by month" data={data} />
<BarChart aria-labelledby="revenue-title" data={data} />

// A chart that only repeats a figure printed beside it leaves the keyboard layer out.
<BarChart accessibilityLayer={false} data={data} />

Examples

Grouped

Two series side by side in each category: no stackId, so every bar starts at the baseline.

Stacked

One stackId across the series: each bar sits on the one below it, and the column is the total.

Horizontal

layout="vertical" swaps the axes: the categories run down the y axis and the bars run along x. The x axis then takes type="number" and the y axis type="category".

Horizontal Stacked

Comparison

KPI With Bar Chart

KPI.Chart renders an area. A bar chart is composed by hand beside the KPI parts.

Custom Tooltip

Customization

Tailwind CSS

<BarChart className="rounded-xl bg-surface p-4" data={data} />

Global CSS

@layer components {
  .chart {
    --chart-axis-size: 11px;
    --chart-grid-opacity: 0.4;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The BarChart component uses these CSS classes (view source styles):

Base Classes

  • .chart - The block every cartesian chart carries; axis type, grid, tooltip cursor and dots
  • .bar-chart - This chart's own block

Slots

  • [data-slot="bar-chart"] - The root element

CSS Variables

VariablePurpose
--chart-axis-sizeType size of the axis labels; set on .chart
--chart-grid-opacityHow far the grid recedes behind the data; set on .chart
--chart-1 … --chart-5The series ramp, derived from the theme's accent

API Reference

BarChart

PropTypeDefaultDescription
dataRecord<string, number | string | null>[]-Required. One object per category; a null field is a missing observation
heightnumber | `${number}%` 300Height of the plot
widthnumber | `${number}%` '100%'Width of the plot
margin{top?: number; right?: number; bottom?: number; left?: number}{top: 8, right: 8, bottom: 0, left: 0}Space between the plot and the root's edges
layout'horizontal' | 'vertical''horizontal''horizontal' stands the bars on the x axis; 'vertical' lays them along it
accessibilityLayerbooleantrueWhether the plot is focusable and answers the arrow keys
childrenReactNode-The chart's parts
aria-labelstring-Names the plot, which is what takes focus — not the wrapping <div>
aria-labelledbystring-Names the plot by another element's id
classNamestring-Additional classes
...restComponentProps<'div'>-Native <div> attributes, on the wrapper

BarChart.Bar

The charting library's Bar, re-exported. See the Recharts Bar API.

BarChart.XAxis

The charting library's XAxis, re-exported. See the Recharts XAxis API.

BarChart.YAxis

The charting library's YAxis, re-exported. See the Recharts YAxis API.

BarChart.Grid

The charting library's CartesianGrid, re-exported. See the Recharts CartesianGrid API.

BarChart.Tooltip

The charting library's Tooltip, re-exported. See the Recharts Tooltip API.

BarChart.TooltipContent

ChartTooltip.Content, for the content prop of BarChart.Tooltip. Its props are on the ChartTooltip page.

On this page