Bar Chart

A composable bar chart with grouped series, pill caps, an animated entrance, an interpolated hover tooltip, and a loading state.

Sales ChannelOnlineRetailDirect

Anatomy

Import the BarChart component and access every part using dot notation. It is built on visx for its scales and SVG and the motion library for the entrance and hover animation.

import {BarChart} from "@blakeui/pro-react";

<BarChart>
  <BarChart.Grid />
  <BarChart.Bar />
  <BarChart.XAxis />
  <BarChart.YAxis />
  <BarChart.Tooltip />
</BarChart>;

Usage

Pass a data array (each row has a category field plus one numeric field per series) and compose the grid, bar series, axes, and tooltip as children. Set xDataKey to the category field; series colors come from the categorical --chart-1 … --chart-5 tokens, while the chart's chrome maps onto BlakeUI's neutral tokens automatically.

import {BarChart} from "@blakeui/pro-react";

const data = [
  {month: "Jan", online: 4200, retail: 2800, direct: 3400},
  {month: "Feb", online: 6300, retail: 4100, direct: 5100},
  // …
];

const usd = (value: number) => `$${(value / 1000).toFixed(0)}k`;

function App() {
  return (
    <BarChart data={data} xDataKey="month">
      <BarChart.Grid horizontal />
      <BarChart.Bar dataKey="online" fill="var(--chart-1)" name="Online" />
      <BarChart.Bar dataKey="retail" fill="var(--chart-2)" name="Retail" />
      <BarChart.Bar dataKey="direct" fill="var(--chart-3)" name="Direct" />
      <BarChart.XAxis />
      <BarChart.YAxis formatValue={usd} />
      <BarChart.Tooltip valuePrefix="$" />
    </BarChart>
  );
}

The chart is keyboard-accessible: focus it and use the left / right arrow keys (or Home / End) to move the crosshair across categories. The dots, the category pill, and the panel follow, and the active category is announced to screen readers through a live region. Every animation respects prefers-reduced-motion.

API Reference

BarChart

The root wrapper. Measures its container, builds the categorical (scaleBand) and value (scaleLinear) scales, and renders the SVG plus the hover and loading layers.

PropTypeDefaultDescription
dataRecord<string, unknown>[]Chart data — one object per category.
childrenReactNodeChart parts (BarChart.Bar, BarChart.Grid, …).
xDataKeystring"name"Key in each row holding the category (x) value.
marginPartial<{ top; right; bottom; left }>{ top: 40, right: 40, bottom: 40, left: 40 }Plot margins around the inner chart area.
animationDurationnumber1100Entrance duration in ms (inclusive of the stagger).
aspectRatiostring"2 / 1"CSS aspect ratio of the chart box (ignored when height is set).
heightnumberFixed pixel height. Overrides aspectRatio.
orientation"vertical" | "horizontal""vertical"Bar layout direction.
stackedbooleanfalseStack series that share a stackId.
barGapnumber0.2Gap between category bands, as a fraction of the step.
barWidthnumberExplicit px width for auto bars (caps the band width).
stackGapnumber0Px gap between stacked segments.
status"loading" | "ready""ready"While "loading", shows a skeleton; flipping to "ready" reveals.
loadingLabelstring""Centered caption shown while loading.
yDomainTweenbooleantrueTween the y-domain on a status change instead of snapping.
yDomainTweenDurationnumber500Duration of the y-domain tween in ms.

Also supports all native div HTML attributes.

BarChart.Bar

A bar series. Self-renders its bars per category; overlay bars (an explicit barWidth) center in the band concentrically, while auto-width bars are grouped side-by-side.

PropTypeDefaultDescription
dataKeystringKey in each row for this series' values.
fillstringcycles var(--chart-1 … 5)Bar fill.
fillOpacitynumber1Fill opacity (e.g. a faded "target" backdrop).
strokestringthe bar fillDatapoint-dot (hover marker) stroke color.
namestringdataKeyLegend / tooltip label for the series.
lineCap"round" | "butt" | number"round"Corner treatment — "round" is a full pill (see Line Cap).
animationType"grow" | "fade""grow"Entrance animation (see Animation).
fadedOpacitynumber0.3Opacity of non-active bars while a category is hovered/focused.
staggerDelaynumberautoPer-category entrance delay step in seconds.
minBarHeightnumber0Minimum rendered bar height in px, so tiny values stay visible.
barWidthnumberExplicit px width → the bar centers in the band (overlay).
stackIdstringStack membership (shared id stacks the series).

BarChart.XAxis

Bottom category axis. Labels are centered under each band.

PropTypeDefaultDescription
tickFormat(category: string, index) => stringCustom tick formatter.
showAllLabelsbooleanfalseRender every category label past maxLabels.
maxLabelsnumber12Cap on rendered labels; past this they are thinned.
tickerHalfWidthnumber50Reserved half-width (px) for tick collision handling.

BarChart.YAxis

Left (or right) value axis.

PropTypeDefaultDescription
numTicksnumber5Approximate number of ticks.
formatValue(value: number) => stringCustom tick formatter (overrides formatLargeNumbers).
formatLargeNumbersbooleantrueFormat values ≥ 1000 as compact (12000"12k").
orientation"left" | "right""left"Side of the chart the labels render on.
showAllLabelsbooleantrueRender every category label (horizontal layout).
maxLabelsnumber20Cap on category labels (horizontal layout).

BarChart.Grid

Chart grid lines mapped onto --separator, with optional soft end-fades.

PropTypeDefaultDescription
horizontalbooleantrueRender horizontal (value) grid lines.
verticalbooleanfalseRender vertical (category) grid lines.
fadeHorizontalbooleantrueSoft-fade the horizontal lines at their ends.
fadeVerticalbooleanfalseSoft-fade the vertical lines at their ends.
numTicksRowsnumber5Number of horizontal lines.
strokestring"var(--separator)"Line color.
strokeDasharraystringSVG dash pattern. Omit for solid lines.

BarChart.Tooltip

Declares the hover tooltip. Renders nothing itself — the chart reads its props and draws the crosshair, per-bar dots, category pill, and floating panel.

PropTypeDefaultDescription
showCursorbooleantrueShow the hover crosshair + datapoint dots.
valuePrefixstringText rendered before each value as it rolls (e.g. "$").
labelFormatter(category: string) => stringCustom formatter for the tooltip header (the category).

Animation

Bars animate in on mount and on the load→ready reveal. animationType="grow" (the default) scales each bar up from the baseline; animationType="fade" blurs and fades it in. Either way the bars are auto-staggered left-to-right so the whole entrance settles within ~1.2s, on a cubic-bezier(0.85, 0, 0.15, 1) curve. animationDuration tunes the total, and staggerDelay overrides the per-category step. Under prefers-reduced-motion the bars appear instantly with no stagger.

<BarChart data={data} xDataKey="month" animationDuration={1100}>
  <BarChart.Bar dataKey="online" animationType="fade" fill="var(--chart-1)" />
</BarChart>

Line Cap

lineCap controls a bar's corner treatment. "round" (the default) renders a full capsule — min(width, height) / 2 on every corner — so tall bars float as pills and short bars degrade to a lozenge without ever inverting. "butt" squares the corners; a number sets an explicit uniform radius (clamped the same way).

<BarChart.Bar dataKey="units" fill="var(--chart-1)" lineCap="round" />
<BarChart.Bar dataKey="units" fill="var(--chart-1)" lineCap="butt" />
<BarChart.Bar dataKey="units" fill="var(--chart-1)" lineCap={4} />

Horizontal & Stacked

Set orientation="horizontal" on the root to swap the category and value axes, and stacked (with each Bar sharing a stackId) to stack series into a single bar per category; stackGap adds a px gap between the stacked segments.

Units sold by product
Revenue by channelOnlineRetailDirect
Installs by platformOrganicPaidReferral

Loading

Set status="loading" to show a grouped shimmer skeleton while data resolves; flip it to "ready" and the chart tweens its y-domain and reveals the real bars with the grow + stagger entrance. Drive status from your data layer. BarChart.Loading is a turnkey shortcut for <BarChart status="loading" />.

Sales Channel
Loading channels…
import {BarChart} from "@blakeui/pro-react";
import {useState} from "react";

function App() {
  const [status, setStatus] = useState<"loading" | "ready">("loading");

  return (
    <BarChart data={data} xDataKey="month" status={status} loadingLabel="Loading channels…">
      <BarChart.Grid horizontal />
      <BarChart.Bar dataKey="online" fill="var(--chart-1)" name="Online" />
      <BarChart.XAxis />
      <BarChart.YAxis />
      <BarChart.Tooltip />
    </BarChart>
  );
}

Theming

Series colors are driven by the categorical chart palette (--chart-1 … --chart-5) — set each series' fill. All other chrome maps onto neutral/semantic tokens: grid lines use --separator; the crosshair, axis labels, and loading skeleton use --muted; values and the dot fill use --foreground / --surface; the tooltip panel uses --overlay with --overlay-shadow; and the active-category pill is an inverse chip (--background text on a --foreground fill). Override any token in your theme to restyle the chart.

Dependencies

The bar chart is built on visx (@visx/scale, @visx/grid, @visx/event) for its scales and SVG, motion for the entrance, hover, and loading animation, and react-use-measure for responsive sizing.

On this page