Bar chart

Slant / Data

Bars for one series, or several grouped or stacked.

Demo

Sessions by hour

Usage

@tuttn/slant
import { BarChart } from '@tuttn/slant'

// One series: a bar per entry.
const data = [{ label: '00:00', value: 64 }, { label: '02:00', value: 38 }, /* … */]
<BarChart data={data} label="Agent sessions by hour" unit="sessions"
  ticks={['00:00', '06:00', '12:00', '18:00', '23:00']} defaultActive={7} />

// Several series: grouped side by side, or stacked, with a value axis.
const series = [
  { id: 'passed', label: 'Passed', values: [42, 51, 38, 47, 33] },
  { id: 'failed', label: 'Failed', values: [3, 5, 2, 4, 6] },
]
<BarChart categories={['Mon', 'Tue', 'Wed', 'Thu', 'Fri']} series={series}
  variant="stacked" label="Test runs by day" unit="runs" yAxis />

// Horizontal: a 28px row per category, its name at the start, the value axis
// along the bottom, and each value at its bar's end. Up and Down walk the rows.
<BarChart data={models} orientation="horizontal" label="Accuracy by setup" yAxis valueLabels />

// Whiskers: an interval per value, from low to high ("0.82, 0.78 to 0.86").
<BarChart label="Accuracy by model" format={value => value.toFixed(2)} data={[
  { label: 'Model A', value: 0.82, low: 0.78, high: 0.86 },
  { label: 'Model B', value: 0.74, low: 0.69, high: 0.8 },
]} />
// With several series, the intervals ride on each series.
<BarChart categories={days} series={[{ id: 'p50', label: 'p50', values: p50, intervals: [[104, 151], null, …] }, …]} />

// Negative values grow the other way from a stronger zero line.
<BarChart data={[{ label: 'Mon', value: 12 }, { label: 'Tue', value: -8 }]} label="Change in runs" yAxis />

// Values formatted for the tooltip, the table and announcements; the data table shown.
<BarChart data={data} label="Revenue by month" format={value => `$${value}k`} table />

// Uncontrolled, the cursor starts at defaultActive; pass active and
// onActiveChange to drive it from outside.
<BarChart data={data} label="Agent sessions by hour" active={hour} onActiveChange={setHour} />

Props

PropTypeDefaultDescription
dataBarChartDatum[]—One series: a bar per entry, in order from the left (from the top when horizontal).
categoriesstring[]—Several series: the category names along the bottom (down the start when horizontal), one per group or stack.
seriesChartSeries[]—Several series, each `{ id, label, values }` with one value per category, and optionally `intervals` for whiskers. Takes over from `data`.
variant'grouped' | 'stacked''grouped'With several series: bars side by side in each category, or stacked into one. A stack piles positive values only; a negative one adds nothing.
orientation'vertical' | 'horizontal''vertical'`vertical` bars rise from the bottom; `horizontal` bars grow rightward from the value axis's zero, one row per category with its name at the start, and Up and Down walk the categories.
labelstring—The chart's accessible name.
unitstring—Read after each value, e.g. "sessions".
heightnumber110The height of the bars' area in pixels; the axis labels add their own row. The width follows the container. Horizontal, it defaults to 28px per category instead.
format(value: number) => stringvalue => String(value)Formats each value in the tooltip, the table and announcements.
ticksstring[]—Labels spread evenly along the category axis, first to last, in place of the category names (down the rows when horizontal). Pass `[]` for none.
yAxisbooleanfalseDraws the value axis: round ticks with hairline gridlines, at the left (vertical) or along the bottom (horizontal).
valueLabelsbooleanfalseWrites each bar's formatted value at its end (horizontal) or above it (vertical), where it fits. Stacked, each stack's total.
tablebooleanfalseShows the data table under the chart. It is always there for screen readers.
activenumber—The category the keyboard cursor sits on, for a controlled chart.
defaultActivenumber0The category the keyboard cursor starts on.
onActiveChange(index: number) => void—Called with the category index when the keyboard moves the cursor.
ChartSeries{ id: string; label: string; values: (number | null)[]; color?: 1 | 2 | 3 | 4 | 5; intervals?: ([low, high] | null)[] }—One entry of series. intervals draws a whisker per value; null draws none.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root, which carries data-orientation="horizontal" when lying down.

Keyboard

Tabfocuses the plot, a single tab stop, and shows the cursor’s tooltip
Right / Leftthe next or previous category (horizontal: the next or previous series in the row)
Down / Upthe next or previous series in the category (horizontal: the next or previous row)
Home / Endthe first or the last category
Escapehides the tooltip until the next step

Accessibility

  • The plot is a group with the chart role description, named by label and described by a text summary: how many points, and each series’ highest and lowest value. The drawing itself is hidden.
  • Every step is spoken through the root’s polite live region: “Tue, Failed: 5 runs”, and with an interval, the value then messages.chartInterval (“Model A, 0.82, 0.78 to 0.86”). Nothing wraps; at either end the cursor stays.
  • The full data is always a table for screen readers (the intervals included); table shows it under the chart. A long name cut with an ellipsis in a horizontal chart is still whole in the table, the tooltip and the announcements.
  • With two or more series a legend names them, so no series is told apart by colour alone. Series take --sl-chart-1 to --sl-chart-5 in order, or their color slot.
  • Hovering a category’s band (a horizontal row, its name included) shows its tooltip without moving the cursor or speaking.

Horizontal

orientation="horizontal" lays the categories down the plot, one row each (28px a row unless height says otherwise), with each name at the start of its row, cut with an ellipsis past about 40% of the width. Bars grow rightward from the value axis’s zero, and yAxis draws that axis’s ticks along the bottom with vertical gridlines. Since the rows run down, Up and Down walk them and Left and Right walk the series in a row.

Negative values, whiskers and value labels

The value axis always holds zero, so a negative value grows from it the other way (down, or toward the start when horizontal), rounded at its data end, and a stronger zero line runs under the bars. Stacks pile positive values only: a negative one adds nothing to its stack.

A datum’s low and high (or a series’ intervals) draw a whisker: a hairline from low to high with end caps across the bar’s centre, in the text colour softened so it shows on every bar colour. The scale reaches the interval ends too. Stacked bars draw no whiskers, though the tooltip, table and announcements still read them.

valueLabels writes each bar’s formatted value past its end, clear of its whisker: above an upright bar (under a negative one), after a horizontal one. A label that would cross a neighbour, the names or the edge is left out. Stacked, each stack shows its total.

Direction

The drawing is physical: SVG never mirrors, so under dir="rtl" the first bar stays on the left, a horizontal chart’s names stay at the left with its bars growing rightward, the arrow keys follow the drawing, and the axis labels keep their order.

Before 0.10 each bar was its own tab stop in a list; now the chart is one stop and the walk is announced, like every chart on the core.

Full source