Line chart
Lines over categories, one series or several.
Demo
Usage
import { LineChart } from '@tuttn/slant'
// One series: label and value pairs. A null leaves a gap in the line.
const data = [{ label: 'Mon', value: 120 }, { label: 'Tue', value: 132 }, { label: 'Wed', value: null }, /* … */]
<LineChart data={data} label="Requests per day" unit="requests" />
// Several series share the categories; colour follows each series' slot.
// The legend's names are buttons that show and hide each line.
<LineChart label="Response time by hour" unit="ms" categories={hours}
series={[{ id: 'p50', label: 'p50', values: p50 }, { id: 'p95', label: 'p95', values: p95 }]}
ticks={['00:00', '06:00', '12:00', '18:00', '22:00']} />
// A numeric x: points at their steps on a linear axis, with axis titles and a
// crosshair that reads the step and the value on the axes.
<LineChart label="Loss by step" x={[0, 50, 100, 200, 400, 800, 1600]} formatX={v => v.toLocaleString('en')}
xLabel="Step" yLabel="Loss" crosshair
series={[{ id: 'train', label: 'Train', values: train }, { id: 'eval', label: 'Eval', values: evalLoss }]} />
// A dot on every point, no value axis, and the table shown under the chart.
<LineChart data={data} label="Requests per day" markers yAxis={false} table />
// Uncontrolled, the cursor starts at defaultActive and the legend at
// defaultHidden; pass active/onActiveChange and hidden/onHiddenChange to drive them.
<LineChart data={data} label="Requests per day" active={day} onActiveChange={setDay} />
<LineChart categories={hours} series={latency} label="Response time" hidden={hidden} onHiddenChange={setHidden} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
data | ChartDatum[] | — | One series: a point per entry, in order from the left. A `null` value leaves a gap. |
categories | string[] | — | Several series: the category names along the bottom, one per point. |
series | ChartSeries[] | — | Several series, each `{ id, label, values }` with one value per category (`null` for a gap). Takes over from `data`. |
label | string | — | The chart's accessible name. |
unit | string | — | Read after each value, e.g. "requests". |
height | number | 160 | The height of the plot in pixels; the axis labels add their own row. The width follows the container. |
format | (value: number) => string | value => String(value) | Formats each value in the tooltip, the table and announcements. |
ticks | string[] | — | Labels spread evenly along the bottom edge, first to last, in place of the category names. Pass `[]` for none. |
yAxis | boolean | true | Draws the value axis: round ticks at the left with hairline gridlines. |
table | boolean | false | Shows the data table under the chart. It is always there for screen readers. |
markers | boolean | false | Puts a dot on every point. Without it, only each line's last point, any point standing alone between gaps, and the point shown have one. |
active | number | — | The category the keyboard cursor sits on, for a controlled chart. |
defaultActive | number | 0 | The category the keyboard cursor starts on. |
onActiveChange | (index: number) => void | — | Called with the category index when the keyboard moves the cursor. |
x | readonly number[] | — | One number per category, ascending: a training step, a size, a time. With it the points sit at those positions on a linear x-axis with nice numeric ticks, instead of evenly, and announcements read the x value. Without `categories` (or `data` labels), the categories are named by the formatted x values. |
formatX | (value: number) => string | value => String(value) | Formats an x value in the tooltip, the crosshair's readout, the table and announcements, and on the axis. Without it, x values read as plain digits and the axis ticks as short numbers (1.2K). |
xLabel | string | — | The x-axis title, under the axis; with `x`, the x value's name in announcements: "Step". |
yLabel | string | — | The y-axis title, over the value axis; with `x`, the value's name in announcements: "Loss". |
hidden | readonly string[] | — | The ids of the series hidden from the drawing, for a controlled legend. With two or more series, the legend's buttons show and hide them. |
defaultHidden | readonly string[] | [] | The ids of the series hidden at first, when uncontrolled. |
onHiddenChange | (ids: string[]) => void | — | Called with the new list of hidden ids, in series order, when a legend button is pressed. |
crosshair | boolean | false | Adds a horizontal hairline at the shown point's value to the vertical one, and small readouts on the axes with its x (the category, or the formatted `x`) and its value. Visual only: announcements already read both. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root. The HTML `hidden` attribute is not among them: `hidden` is the legend's list of hidden series. |
Keyboard
| Tab | focuses the plot, a single tab stop, and shows the cursor's category |
| Right / Left | moves to the next or previous category, without wrapping |
| Down / Up | moves to the next or previous shown series at the category, in the legend's order |
| Home / End | moves to the first or last category |
| Escape | hides the tooltip until the next step |
| Enter / Space | on a legend name, shows or hides that series |
Accessibility
- The plot is a group with the
chartrole description, named bylabeland described by a summary: how many points it spans, then each shown series' highest and lowest value. - Every step is announced through the root's polite live region, as “14:00, p95: 381 ms”. With
xit reads the x and the value by the axes' names instead: “Eval, Step 200, Loss 1.46”, the category first whencategories(ordatalabels) name them; withoutxLabelandyLabelthe names arexandy. - The drawing is hidden from assistive technology, and the data is always there as a table for screen readers;
tableshows it under the chart too. Withxthe table has an x column, and it keeps every series, hidden ones too. - With two or more series the legend names them, so no series is told apart by colour alone. Its names are toggle buttons in a group named “Show series”, with
aria-pressedtrue while a series shows; a hidden series' key goes hollow and its name is struck through. The last series showing isaria-disabledand can't be hidden. - Hovering a category's column shows every shown series' value there without moving the keyboard cursor or speaking. The crosshair's readouts are visual only: the announcement already reads both values.
One series draws in the accent; several take --sl-chart-1 … --sl-chart-5 by their place in series, or by each series' color, so hiding one recolours none. A hidden series leaves the drawing, the value scale, the walk, the tooltip and the summary. A null value breaks the line, and the table and announcements read it as "no value". Each line's last point always has a dot; markers puts one on every point. Under reduced motion the lines appear whole instead of drawing in.
Numeric x
x gives each category a number, ascending, and the points sit at those numbers on a linear axis instead of evenly, with nice numeric ticks fitted to the data (no forced zero). formatX writes the x values everywhere they're read and the ticks; without it the ticks are short numbers (1.2K). Without categories or data labels, the formatted x values name the categories. The hover columns still meet halfway between points, however uneven their spacing.
Axis titles and the crosshair
xLabel and yLabel are drawn level, in rows of their own: the x title centred under the tick labels, the y title over the value ticks at the top-left, so neither crowds a tick label however narrow the chart. crosshair adds a level hairline at the shown point's value to the vertical one, and small readouts in the tooltip's surface on the axes: the x (the category, or the formatted x) at the bottom and the value at the left. It follows the keyboard cursor, Up and Down included; hovering picks the line nearest the pointer.
Right to left
The drawing is physical: SVG never mirrors, so under dir="rtl" the first category (or smallest x) stays on the left, Right still moves to the next one, and the axis labels keep their order. The legend and the table around the plot follow the page's direction.