Area chart

Slant / Charts

Filled lines, overlapping or stacked.

Demo

Usage

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

// One series: label and value pairs. A null breaks the line and its area.
<AreaChart data={data} label="Requests per day" unit="requests" />

// Several series, overlapping (the default): each area fills down to zero.
<AreaChart label="Agent minutes by week" unit="minutes" categories={weeks}
  series={[{ id: 'builds', label: 'Builds', values: builds }, { id: 'reviews', label: 'Reviews', values: reviews }]} />

// Stacked: each series sits on the ones before it, so the top line is the total.
<AreaChart label="Agent minutes by week" unit="minutes" categories={weeks} series={series} variant="stacked" />

// Uncontrolled, the cursor starts at defaultActive; pass active and
// onActiveChange to drive it from outside.
<AreaChart data={data} label="Requests per day" active={day} onActiveChange={setDay} />

Props

PropTypeDefaultDescription
variant'overlap' | 'stacked''overlap'With several series: each area filled down to the zero line, overlapping, or stacked on the series before it, so the top line is the total.
dataChartDatum[]—One series: a point per entry, in order from the left. A `null` value leaves a gap.
categoriesstring[]—Several series: the category names along the bottom, one per point.
seriesChartSeries[]—Several series, each `{ id, label, values }` with one value per category (`null` for a gap). Takes over from `data`.
labelstring—The chart's accessible name.
unitstring—Read after each value, e.g. "requests".
heightnumber160The height of the plot in pixels; the axis labels add their own row. The width follows the container.
format(value: number) => stringvalue => String(value)Formats each value in the tooltip, the table and announcements.
ticksstring[]—Labels spread evenly along the bottom edge, first to last, in place of the category names. Pass `[]` for none.
yAxisbooleantrueDraws the value axis: round ticks at the left with hairline gridlines.
tablebooleanfalseShows the data table under the chart. It is always there for screen readers.
markersbooleanfalsePuts 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.
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.
xreadonly 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) => stringvalue => 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).
xLabelstring—The x-axis title, under the axis; with `x`, the x value's name in announcements: "Step".
yLabelstring—The y-axis title, over the value axis; with `x`, the value's name in announcements: "Loss".
hiddenreadonly 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.
defaultHiddenreadonly 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.
crosshairbooleanfalseAdds 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.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root.

AreaChart is a LineChart with a fill of each series' colour under its line, at about a tenth of its strength, and takes the same props plus variant. Stacked, the series pile up in the legend's order from the baseline; a missing or negative value adds nothing to the stack. The tooltip, the table and the announcements always give each series' own value, not the running total.

Since 0.16 it has LineChart's additions too. With two or more series the legend's names are toggle buttons (hidden, defaultHidden, onHiddenChange): a hidden series leaves the drawing, the scale and the stack, so the areas left close up over it, while the table keeps it. x places the points on a numeric axis, xLabel and yLabel draw axis titles, and crosshair adds a level hairline and readouts on the axes; stacked, the value readout gives the top of the stack, where the hairline meets the axis.

Keyboard

The plot is one tab stop. Left and Right step through the categories, Up and Down through the series at one, and Home and End jump to the ends; nothing wraps. Each step is announced, as "W12, Reviews: 395 minutes", through the root's polite live region. Escape hides the tooltip until the next step. Up and Down skip a hidden series, and Enter or Space on a legend name shows or hides it.

Accessibility

The plot is a group named by label and described by a summary of the range and each series' highest and lowest value. The drawing is hidden from assistive technology; the data is always there as a table for screen readers, and table shows it. Two or more series get a legend, so none is told apart by colour alone. Hovering a column shows every series' value there without moving the cursor or speaking. Under reduced motion the lines and fills appear at once.

Right to left

The drawing is physical: under dir="rtl" the first category stays on the left, Right still moves to the next one, and the axis labels keep their order.