Skip to content

Line chart

Pro

Lines over time: one subject with a wash under it and others to compare with, read by pointer, finger and keyboard. Not an M3 component (@windmorph/react-pro).

import { LineChart, ChartLegend, ChartTooltip } from '@windmorph/react-pro/charts';

Part of Windmorph Pro, paid components on the free core, not on sale yet. See the Pro guide for what it includes and to register interest.

Purpose

How something changed over time: one subject with a wash under its line, and other lines to compare it with, such as this year against last. People read it by pointer, finger and keyboard, and in a ChartFrame every value is in a table too.

M3 has no charts, so this one follows Material's data visualisation guidance with M3's colour roles, type scale and springs. For a total made of parts over time, use a Bar chart; for one number and how it moved, a Stat tile. A line chart has one value axis: two measures on different scales are two charts.

Places and series

  • places are the steps along the bottom, oldest first. Each has a key, a short tick for the axis ("1 Jun") and a full name for the readout, the table and screen readers ("Week of 1 June").
  • Each of the series has a key, a label and one value per place, in the places' order. null, or anything that isn't a finite number, is no value there.
  • Keep the keys steady: a place that keeps its key when the period changes moves instead of being redrawn, and so does a series that keeps its own.
  • subject is the series the chart is about: it gets the wash under its line. With one series it is that one; with several and no subject, no line has a wash.
  • curve="monotone", the default, is smooth and never passes a value; curve="linear" joins the points with straight lines.
  • Series take the colour slots in order and keep theirs when another is hidden, but a series added before them moves their colours along. Pin slot (1 to 8) to keep a series' colour when series come and go, so the same thing is the same colour on every chart of a page. Eight series are drawn; a ninth and later are only in a frame's table view, and development builds warn.
  • quiet on a series draws it only to compare with, such as the previous period's line beside this one: in the charts' quiet colour (--wm-chart-quiet, M3's outline) and always patterned, its line in a dash and two dots, with texture on or off, in the legend, the readout and forced colours too. It takes no colour slot, so the other series keep theirs, and its slot is ignored. One a chart: development builds warn about a quiet series with a slot, two quiet series, and a quiet subject.
  • The side labels run from nought, or from the least value below it, to a round number in about four steps: in full under 10,000 and compact above, with the decimals the step needs. Under 224px of plot plus place labels every other one shows. The bottom labels thin out by container queries, every few counted back from the newest, which always shows, so the server's HTML is right at any width.
  • formatValue writes the figures in the readout, the table and what screen readers hear, so put the unit there, as the temperature example does; formatAxis writes the side labels. Both default to the page's locale.
  • minHeight (240px at the default text size, growing with larger text) is the least height of the plot plus its place labels; the legend sits above them, and the chart grows to the height its container gives it. Inside a ChartFrame, a chart with no minHeight of its own fills the frame's, its legend included, and the plot takes the rest, at least half of it.

Reading it

  • The pointer and a finger find the nearest place, not a mark: a hairline stands there, a point sits on each line, and the readout lists every series on show at that place, the value first and strong, then whose it is with its key. The readout keeps to the side of the place with more room.
  • A finger scrubs sideways while the page still scrolls up and down. A tap reads its place and keeps it, and so does a click once the pointer leaves; a finger that scrolls the page instead reads nothing.
  • From the keyboard the chart is one tab stop, after its legend: a slider over the places. The arrow keys step, Home and End go to the first and the newest place, and Page Up and Page Down move a tenth of the places. It starts on the newest place, or on the one just pressed. Right to left, the arrows follow the chart: Arrow Left moves forward. Escape hides the readout, wherever focus is for one the pointer shows; it shows again on another place, once the pointer leaves and comes back, or as the chart takes focus again.
  • Screen readers hear the slider's value: the place's name, then each series on show with its value ("Week of 1 June: This year 1,180, Last year 980"). labels.describePlace writes your own sentence. The slider is a native range input, so a phone's screen reader can step it too.
  • The readout is hidden from assistive technology: it adds to the value and never replaces it, and every value is in a frame's table view as well.

Below nought, and gaps

  • With a value below nought, the axis runs below a nought line, drawn as the baseline, and the subject's wash runs from its line to that line.
  • A place with no value is a gap: the line breaks there, no point shows when it's read, and the readout, the value and the table say "No value" (labels.noValue). There's no need to invent a zero.

Motion

  • The lines arrive once, as the chart first comes into view: rising from the nought line (entrance="rise", the default), or drawn on from the start (entrance="draw"). A chart already on screen as the page loads shows its final picture at once, from the server's HTML. Under reduced motion the chart shows its final picture at once, wherever it is on the page.
  • When the numbers change, the lines move from where they are to the new values on the theme's spatial spring, keeping their speed when a change comes mid-move. Places are matched by key: a kept point slides to its new place, a new one comes in along the line, and one that left slides off the plot's edge (a window moving on) or merges into the line (one taken from the middle). With no key kept, a new grain (days to weeks), the old line reshapes into the new one.
  • Hiding a series fades its line while the others rescale. Off screen, a change lands at once, and an entrance waits until the chart is seen.
  • Under reduced motion nothing moves: the lines take their new shape at once, the draw-on is instant, and only the fades run.

The legend and the readout

  • With two or more series the chart draws its legend above the plot: a filter chip for each series, with its key, that hides and shows it. The last one shown can't be hidden: its chip keeps its look and its tab stop, and says why (labels.lastSeries). legend={false} leaves it out, and legend shows it for one series.
  • hiddenSeries, defaultHiddenSeries and onHiddenSeriesChange keep which are hidden, controlled or not. A hidden series leaves the readout and the value, and the axis rescales to what shows.
  • ChartLegend and ChartTooltip are those parts on their own, for a layout of your own: one legend for several charts, or a readout for a chart you draw yourself. ChartLegend is a list of keys, or chips once it has hiddenSeries, defaultHiddenSeries or onHiddenSeriesChange. Each item's key draws its slot's pattern: its slot, else the one its chartColor(n) colour names, else its order's, so give each item its series' slot (or colour) where the order differs from the chart's, and mark: 'line' for a line's dashes; texture is on from two items (texture={false} turns it off). An item for a quiet series takes quiet, for the quiet pattern, and color: 'var(--wm-chart-quiet)'. ChartTooltip takes the same slot and quiet on its rows and the same texture; it is drawn as the core's rich tooltip and is hidden from assistive technology, so what it shows must be said another way too.
  • chartColor(slot) gives a slot's colour as the charts draw it, for those parts' color or a mark of your own: chartColor(2) follows the theme, its mode and contrast level, and an app's --wm-chart-color-2. It reads the palette a chart, a legend or a readout brings to the page, so use it where one of them shows, and pin the series to the same slots with slot.
  • With two or more series, each line has a dash pattern of its own (slot 1 stays solid), and its keys to match, for anyone who doesn't tell the colours apart; texture={false} turns them off. Forced colours always draw them. Printed, the colours stay.

In a frame

Put the chart in a ChartFrame: its title names the chart, so it needs no aria-label, and its switch shows the same numbers as a table, a row per place and a column per series, every series past the eighth included, with the chart's own figures and "No value" for a gap. placeLabel names the places' column ("Week"). The frame keeps the chart's height for the table, dims the lines while pending, and draws the chart on its card's colour. Outside a frame, name the chart with aria-label or aria-labelledby; development builds warn about a chart with neither.

When not to use

For a total made of parts over time, use a Bar chart; for one number and its trend, a Stat tile.

See also

  • Bar chart: Columns over places: one series, or several stacked into a total, read by pointer, finger and keyboard. Not an M3 component (@windmorph/react-pro).
  • Stat tile: One number and how it moved: what it is, the figure, its change against something, and a small trend under the number. Not an M3 component (@windmorph/react-pro).
  • Chart frame: The card a chart sits in: its title names the chart, a switch shows the same numbers as a table, and it has empty, loading, pending and error states. Not an M3 component (@windmorph/react-pro).