BarChart
StableBars that compare amounts. Highlight one bar, or stack up to four parts.
Also known as bar graph, column chart, histogram, stacked bar
Install with npm install @tattva/ui (setup), or copy the code from the kit in Downloads.
One bar stands out
<div className="w-full">
<BarChart
highlight="North shop"
measure="Sales"
data={[{ label: "North shop", value: 4200 }, { label: "East shop", value: 2900 }, { label: "West shop", value: 1700 }, { label: "Online", value: 900 }, { label: "Other", value: 400 }]}
formatValue={(n) => "$" + n.toLocaleString("en-US")}
unit="dollars"
ariaLabel="Sales by shop"
/>
</div>One bar is the point, so it gets the main color and the rest are grey. Numbers sit at the ends of the bars.
When to use it
Compares amounts with bars that always start at zero. One bar can be highlighted while the rest stay grey, so the bar that tells the story stands out.
Made of: Key (stacked) · Number scale · Category names · Bars · Number labels · Pop-up
Use it for
- Comparing amounts across categories, such as shops or months.
- Pointing at one category: set highlight.
- Showing what an amount is made of, with up to four stacked parts.
Do and avoid
Orders per day
Orders, Mon to Thu
Orders on Thursday
Orders
Do
- Start every bar at zero. Never cut the scale.
- Highlight a bar when it is the story, and leave the rest grey.
- Lay bars sideways for long names or more than a handful of bars.
- Keep stacks to four parts, and list the key in the same order as the stack.
Avoid
- Don't give every bar its own color. One set of bars takes one color.
- Don't draw a chart with one bar. Use a StatTile.
- Don't squeeze numbers inside bars that are too small. They sit at the bar end, and the table has them all.
- Don't use a pie or donut to show parts of a whole. Use a stacked bar.
On a phone
- The chart measures its container and redraws at that width. Long category labels are shortened with an ellipsis to fit.
- Touch or drag on a bar to show the tooltip. Vertical swipes still scroll the page, and the tooltip stays until you tap elsewhere.
- Horizontal bars suit a phone best, because labels have room to the left and the chart grows downward.
Seen in products
- The UK Analysis Function checklist says bar axes start at zero and are never broken, and that legend order matches chart order.
- Mailchimp and GitLab Pajamas discourage pie charts, and Mailchimp recommends a percent bar, reported in the Supernova roundup.
- Datawrapper lists a brand strategy of using the brand color for the one emphasized category with neutrals for the rest.
- WCAG 1.4.11 asks for 3:1 for graphic parts needed to understand the content and exempts parts whose information is also given in visible text labels or values.
Properties
| Prop | Type | Default | Description |
|---|---|---|---|
| datarequired | BarDatum[] | none | One entry per category: label, and value (single series) or values (stacked). Values must be zero or more. |
| orientation | "horizontal" | "vertical" | "horizontal" | Horizontal reads best for long labels and many categories. |
| series | BarSeries[] | none | Stack segment definitions, at most four. When set the chart is stacked and each datum uses values. |
| highlight | string | none | Label of the one category to emphasize. Others use the neutral. |
| measure | string | "Value" | Name of the measure for single series charts, used in announcements and the tooltip. |
| formatValue | (n: number) => string | formatNumber | Formats axis ticks, value labels and the tooltip. |
| unit | string | "" | Unit word read after each value by screen readers. |
| height | number | none | Total height in px. Vertical charts default to 240. Horizontal charts size to their rows. |
| ariaLabel | string | "Bar chart" | Name for the chart region. |
| className | string | none | Extra classes on the wrapper. |
"Show code under the preview gives the code for the example on the stage."
In Figma
In Figma, one BarChart switches all of this from its properties panel, across 2 variants. Each property sets the React prop beside it, so design and code use the same names.
| Figma property | React | Values |
|---|---|---|
| Orientation | orientation | Horizontal, Vertical |
States
States this part has beyond its default look. Build each one your screen can reach. States set by a property are in the Code tab.
- hover
- Moving the pointer over a bar or touching it shades its row and shows a tooltip with the values.
- focus
- The chart can be focused with the keyboard; arrow keys step through bars, Home and End jump to the ends and Escape clears the tooltip.
- highlight
- Pass highlight with a category label to colour that bar and grey out the rest.
- stacked
- Pass series with values per row to stack segments, add a legend and let arrow keys move between segments.
- empty
- With no data, only the axis is drawn.
- truncated labels
- Category labels that do not fit are cut off with an ellipsis, and totals too wide for a vertical bar are left out.
Tokens
Change one of these and every part that uses it follows.
--chart-1--chart-2--chart-3--chart-4--chart-neutral--chart-grid--chart-axis--surface--surface-hover--radius-overlay
Built in
- You can tab to the chart. Arrow keys move between bars and, in a stacked chart, between parts. Home and End jump to the first and last bar.
- Each step is read out as the part or measure, the category, the value and the unit.
- Stacked parts have a small gap between them, and the key names each part in stack order.
- The pop-up is hidden from screen readers, because the same values are read out as you move.
What you need to do
- Put it in a ChartFrame with a summary and a table.
- Give it an ariaLabel, a measure and a unit, so each bar is read with what it measures.
Built toward WCAG 2.2 AA and checked automatically; no independent audit yet. Read the accessibility guide