On this page

Charts

BarChart

One value per category or period, drawn as bars. BarChart takes your rows, the field for the x axis and the field for the value, and handles the scale, the tooltip, the keyboard and the screen reader.

When to use it #

Use BarChart to compare a single measure across a few categories (orders by region) or to show it over equally spaced periods (margin by week). Put the measure on y, set format once for the axis, the tooltip and the data table, and pass domain when a zero baseline would flatten the differences. For several series that share an x axis, LineChart usually reads better; for a ranked list with long labels, use RankedBars.

Import #

Terminal
npm i tally-charts
app/margin.tsx
import { BarChart } from 'tally-charts'
import 'tally-charts/theme.css'

Examples #

Props stay serializable here, so the chart renders in a server component. domain keeps the bars from starting at zero, and format="percent" writes the axis and the tooltip.

Margin by week, last 6 weeks
View of Margin by week, last 6 weeks
Margin by week, last 6 weeks
WeekMargin
W131.2%
W232.8%
W333.5%
W432.1%
W534.0%
W635.4%

With onSelect the chart needs a client component, because the handler is a function. Click a bar, or focus the chart and press Enter.

Orders by region, select a bar
View of Orders by region, select a bar

valueLabel="max" labels only the highest bar (the first, on a tie), and valueLabelVariant="pill" draws the label on a filled pill. The pill sits above the bar. BarChart's default top margin of 24 fits it; a Chart needs margin.top of at least 24.

Orders by week, peak labelled
View of Orders by week, peak labelled

x is the category and y the value in both orientations: orientation="horizontal" only puts the categories on the left.

Props #

NameTypeDefaultRequiredDescription
activeIdId | null—No—
animateboolean—No—
aria-labelledbystring—No

Id of an element that names the chart; it wins over title for the name.

aspectRationumber—No

width / height. Used when height is not set.

axisTickSpec | 'none''ends'No

Category axis ticks, or 'none'. Default 'ends'.

axisFormatAxisFormatthe category itselfNo

Category axis labels: a function of the category. Number presets and date formats don't apply to categories and are ignored with a development warning. Default: the category itself.

chartIdstring—No

Prefix for pattern and mask ids. React's useId is only unique within one root, so set this when charts render in several React roots on one page.

childrenReactNode—No—
classNamestring—No—
datareadonly D[]—Yes—
dataTableboolean | DataTableOptionstrueNo

Hidden data table for screen readers: true, false, or { caption, columns } to set its caption and rename column headers. Default true.

descriptionstring—No

Accessible description, usually the takeaway sentence.

dimInactiveboolean—No—
domainreadonly [number, number]—No

Value domain. Inferred from the data (including zero) when omitted.

emptyReactNode"No data"No

Rendered instead of the plot when data is an empty array. Default "No data".

fillPerItem<D, Fill>history 'hatch-fade', last bar 'solid'No

Default: history 'hatch-fade', last bar 'solid'.

formatFormatSpec'number'No

Format for tooltips, announcements and the data table. Default 'number'.

heightnumber240 unless `aspectRatio` is setNo

Height in px. Default 240 unless aspectRatio is set.

idAccessor<D, Id>—No—
initialWidthnumber640No

Width used for server rendering and the first client render. Default 640.

keyboardHintstring"Use arrow keys to move between points, Home and End for first and last, Escape to clear.", plus " Press Enter to select." while the active point (or the first, before one is active) can be selected with `onSelect`No

Instructions read with the chart when it takes keyboard focus. Default: "Use arrow keys to move between points, Home and End for first and last, Escape to clear.", plus " Press Enter to select." while the active point (or the first, before one is active) can be selected with onSelect. A custom text is used as is. Set it to translate the text; "" turns it off.

labelAccessor<D, string>`x`No

Category label for tooltips, announcements and the data table. Defaults to x.

marginPartial<Margin>—No—
onActiveChange(id: Id | null, point: ChartPoint<D> | null) => void—No

Called with the new active id, and its point; point.datum is the row from data.

onSelect(id: Id, point: ChartPoint<D>) => void—No

Called on click, Enter or Space; point.datum is the row from data.

orientation'vertical' | 'horizontal''vertical'No

Default 'vertical'.

paddingnumber0.35No

Inner band padding, 0–1. Default 0.35.

styleCSSProperties—No—
targetnumber | { value: number; label?: string }—No

A target value, optionally with a label.

titlestring—No

Accessible name of the chart; also the default caption of the data table.

tonePerItem<D, Tone>'accent'No

Default 'accent'.

tooltipChartTooltip<D>trueNo

Tooltip on hover and focus: true for the default pill, or a function that renders the content for a point (point.datum is your row). Default true.

valueFormatFormatSpec`format`No

Format for value labels. Defaults to format.

valueLabel'last' | 'all' | 'max' | 'none''last'No

Which bars get a value label: 'max' is the highest one. Default 'last'.

valueLabelVariant'text' | 'pill''text'No

'pill' draws value labels on a filled rounded rectangle. On the tallest bar it sits in the top margin, which fits at the default margin.top of 24; a smaller one clips it. Default 'text'.

widthnumber—No

Fixed width in px; turns off container measuring.

xAccessor<D, string | number>—Yes

Category field: the band axis in both orientations.

yAccessor<D, number>—Yes

Value field.

Keyboard and screen readers #

The chart is a single tab stop. Tabbing in shows the first bar with its tooltip and announces it. Arrow keys move between bars, Home and End jump to the first and last, Enter or Space selects the active bar when the chart has onSelect, and Esc clears the active bar. Esc is only consumed when a bar is active, so an enclosing dialog still closes.

Screen readers get the chart title and description, then an announcement for each bar the keyboard moves to, in the form “1 of 12, Jul 6, 37.5%”: the position, the label and the formatted value. A visually hidden data table with every value follows the chart, so browse mode can read it row by row.

The full model is in the accessibility guide.