Magnitude by category, as bars side by side or stacked, in columns or rows.
| Subscriptions | Services | Marketplace | |
|---|---|---|---|
| North America | $66K | $75K | $31K |
| Europe | $102K | $61K | $36K |
| Asia Pacific | $72K | $50K | $36K |
| Latin America | $29K | $23K | $25K |
| Middle East | $51K | $16K | $13K |
| Africa | $18K | $12K | $9K |
Give it rows of data, the field that names each category (index) and a config that names the series to draw. Each category gets a bar per series, side by side, in the next colour of the chart palette. Bars are at most 24px thick, so the rest of each band is air; they grow from zero with a rounded end and a square base, and bars that touch keep a 2px gap.
Point at a category, drag a finger across the chart or tab to it and use the arrow keys: a soft highlight glides to the category and a tooltip lists its series. The legend hides a series when pressed and spotlights it when pointed at. Bars grow in a quick wave the first time the chart is on screen, and move to their new size and place when the data changes.
Bar Chart is part of the Fabricator style. It's drawn in SVG, so the same component ships for every base, and its layout comes from the same model as the React Native version.
Installation#
bunx --bun fabricator-ui@latest add bar-chart
Usage#
import { BarChart } from "@/components/ui/bar-chart"
import { type ChartConfig } from "@/components/ui/chart-kit"const data = [
{ region: "Europe", subscriptions: 102_000, services: 61_000 },
{ region: "Asia Pacific", subscriptions: 72_000, services: 50_000 },
]
const config = {
subscriptions: { label: "Subscriptions" },
services: { label: "Services" },
} satisfies ChartConfig
<BarChart data={data} index="region" config={config} label="Revenue by region" />Negative values grow down from zero. A missing value leaves its bar out. The chart fills its container's width and is 18rem tall until you give it a height with className.
Values print in full (1,234) until you pass formatValue. It also formats the hidden table, which renders on the server, so use a formatter that writes the same text in Node and in the browser: formatChartNumber(value, { compact: true }) from lib/chart-model gives 43.9K on both, where Intl's compact notation can differ between them.
Examples#
Stacked#
stack="stacked" sets each series on the ones before it, so the column is the category's total. Only the outer end of each column is rounded, and segments keep a 2px gap.
| Resolved | Open | Escalated | |
|---|---|---|---|
| Jan | 182 | 34 | 9 |
| Feb | 205 | 41 | 12 |
| Mar | 231 | 29 | 7 |
| Apr | 198 | 52 | 15 |
| May | 246 | 38 | 11 |
| Jun | 262 | 27 | 6 |
Horizontal#
layout="horizontal" lays the categories down the start edge, for long names. A single series needs no legend: label already names what is plotted.
| Visits | |
|---|---|
| /pricing | 18,420 |
| /docs/getting-started | 15,310 |
| /blog/launch-week | 11,870 |
| /changelog | 8,240 |
| /integrations | 6,930 |
| /careers | 3,160 |
Changing the data#
Bars move to their new place and size when the stacking, the order or the values change, so the eye can follow each one.
| Shipped | Planned | |
|---|---|---|
| Design | 18 | 24 |
| Platform | 31 | 28 |
| Growth | 12 | 20 |
| Mobile | 22 | 19 |
| Data | 9 | 14 |
Colours#
Series take the chart palette in the order of config, and keep their colour when another is hidden. Give a series its own with color: a palette slot, an upstream chart token (chart-1 … chart-5), a role (primary, info, success, …), a CSS variable (var(--brand)) or any CSS colour.
| Token | Hue | Token | Hue |
|---|---|---|---|
--chart-series-1 | blue | --chart-series-5 | magenta |
--chart-series-2 | orange | --chart-series-6 | green |
--chart-series-3 | aqua | --chart-series-7 | violet |
--chart-series-4 | amber | --chart-series-8 | red |
Colour one series per meaning: categories of a single series share its colour, since the bar's length already shows the value. The palette ships with the foundations, chosen separately for light and dark, every slot at 3:1 or more against the page and the card.
Motion#
- Grow: the first time the chart scrolls into view, bars grow from zero in a quick wave along the categories, on the
revealrole, once per mount. - Morph: when
data,config,stack,layoutor the visible series change, each bar moves and resizes on themoverole. A series being shown grows from zero; one being hidden shrinks into it. - Glide: the category highlight and the tooltip glide between categories on the
gliderole.
All three follow the motion speed of the region they sit in. Reduce Motion and the instant speed draw the chart at once and drop the glide; animate={false} does the same for one chart.
API Reference#
BarChart#
All other props go to the root <div>.
| Prop | Type | Default |
|---|---|---|
data | TDatum[] | required |
config | ChartConfig | required |
index | keyof TDatum | required |
label | string | required: the accessible name |
layout | "vertical" | "horizontal" | "vertical" |
stack | "none" | "stacked" | "none" |
formatValue | (value: number, key: string) => string | 1,234 |
formatIndex | (value: string, datum: TDatum) => string | the field as text |
legend | boolean | on for two or more series |
tooltip | boolean | true |
animate | boolean | true |
loading | boolean | false |
empty | ReactNode | "No data" |
onActiveChange | (active: ChartActive<TDatum> | null) => void | - |
ChartConfig#
| Field | Type | Default |
|---|---|---|
label | string | required |
color | ChartColor | the next palette slot |
Data attributes#
| Attribute | Element | Present when |
|---|---|---|
data-slot="bar-chart" | root | always |
data-loading | root | loading is true |
data-slot="chart-plot" | the plot | always |
data-slot="chart-bar" | a bar | always; data-dimmed while another is spotlit |
data-slot="chart-band" | highlight | a category is inspected |
data-slot="chart-legend" | the legend | the legend shows |
data-slot="chart-tooltip" | the tooltip | a category is inspected |
Accessibility#
- The plot is a slider named after
label("Revenue by region: inspect values"). Tab to it, then the arrow keys step through the categories, Home and End jump to the first and last, and Escape lets go. Its value text reads the tooltip: the category and every series' value. - Every value is also in a table hidden from sight, so a screen reader can read the data without inspecting it.
- With two or more series the legend always shows, so no series is told apart by colour alone. Its items are toggle buttons with
aria-pressed. - Text never takes a series colour: values and labels stay in the text colours, beside a coloured key.
loadingsetsaria-busyon the root and dims the last drawing until new data arrives.