Navigation

v2.1.0
Building

Working with charts

Choose the chart for the job, put several under one control, and know when a charting engine is the better tool.

On this page

Master UI draws its own charts. The chart is one SVG that draws more than twenty forms in the theme's tokens, from a line to a sunburst; each form has a page in Charts. Beside it stand smaller components for a single figure, a trend or a share. This guide says which to reach for, shows several charts working together under one choice of period, and says when a charting engine is the better tool.

#Which one

Start from what the reader has to see, not from a shape. Each of these draws from markup, follows the theme with no script and says its numbers in words.

The reader has to seeUse
How values go on over timeA line, or an area for one or two series
What rose and what fell between two momentsA slope; for two values of each thing in a list, a dumbbell
How ranks changedA bump chart
Amounts compared with each otherBars; across when the names are long
How one figure became anotherA waterfall
Spans between two valuesA range chart
What totals are made of, over time or across groupsStacked bars or stacked areas; a marimekko when the groups differ in size
What one whole is made ofA donut or a pie, of five parts at most; a waffle to the percent; a treemap for many parts; a sunburst for two levels; along one bar, a breakdown
How much of a flow reaches each stageA funnel
A few goals, each with a whole of its ownRings
A few things on the same measuresA radar
How readings are spreadA histogram; for several groups at once, a box plot
A pattern across two scalesA heatmap
Two measures against each otherA scatter, with bubbles for a third
One figure and how it movedA stat
Which way a series went, in a sentence or a rowA sparkline
One reading and how near its limit it isA gauge or a meter
A pattern across many small values, with no scales beside itA heatmap of cells
The exact numbers, compared in columnsA table

When a figure says it, do not draw it: a stat with its change reads faster than a chart of two bars.

#A dashboard

Three figures and three charts under one choice of period. Choosing a period changes all of them together, from the same numbers, so what the figures say and what the charts show always agree.

Chart dashboard

<section class="flex flex-col gap-md" data-example="chart-dashboard" aria-labelledby="traffic-title">  <header class="flex flex-wrap items-center justify-between gap-sm">    <div class="flex flex-col gap-3xs">      <h2 class="heading heading-sm" id="traffic-title">Traffic</h2>      <p class="m(0) fg-text-muted" data-traffic="period" aria-live="polite">The last 7 days</p>    </div>    <div class="segmented segmented-sm" role="radiogroup" aria-label="Period">      <label class="segmented-item">        <input type="radio" class="segmented-input" name="traffic-period" value="week" checked>7 days</label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="traffic-period" value="month">30 days</label>      <label class="segmented-item">        <input type="radio" class="segmented-input" name="traffic-period" value="year">12 months</label>    </div>  </header>  <div class="auto-grid auto-grid-fit" style="--auto-grid-min: 11rem">    <div class="card">      <div class="stat">        <p class="stat-label">Visitors</p>        <div class="stat-row">          <p class="stat-value" data-traffic="visitors">12,480</p>          <p class="stat-delta stat-delta-up" data-traffic="visitors-change">            <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">              <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" />              <path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" />            </svg>            <span class="sr-only">Up</span>8.2%</p>        </div>      </div>    </div>    <div class="card">      <div class="stat">        <p class="stat-label">Sign-ups</p>        <div class="stat-row">          <p class="stat-value" data-traffic="signups">642</p>          <p class="stat-delta stat-delta-up" data-traffic="signups-change">            <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">              <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" />              <path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" />            </svg>            <span class="sr-only">Up</span>3.1%</p>        </div>      </div>    </div>    <div class="card">      <div class="stat">        <p class="stat-label">Bounce rate</p>        <div class="stat-row">          <p class="stat-value" data-traffic="bounce">38.4%</p>          <p class="stat-delta stat-delta-down" data-traffic="bounce-change">            <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">              <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" />              <path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" />            </svg>            <span class="sr-only">Down</span>1.6%</p>        </div>      </div>    </div>  </div>  <section class="card" aria-labelledby="traffic-visits">    <div class="card-header">      <h3 class="card-title" id="traffic-visits">Visitors</h3>      <p class="card-description">New and returning</p>    </div>    <figure class="chart" data-traffic="visits"></figure>  </section>  <div class="auto-grid auto-grid-fit" style="--auto-grid-min: 17rem">    <section class="card" aria-labelledby="traffic-sources">      <div class="card-header">        <h3 class="card-title" id="traffic-sources">Sources</h3>        <p class="card-description">Where visitors came from</p>      </div>      <figure class="chart chart-split" style="--chart-size: 9rem" data-traffic="sources"></figure>    </section>    <section class="card" aria-labelledby="traffic-pages">      <div class="card-header">        <h3 class="card-title" id="traffic-pages">Top pages</h3>        <p class="card-description">By visitors</p>      </div>      <figure class="chart" style="--chart-height: 9rem" data-traffic="pages"></figure>    </section>  </div></section>
Choose a period, point at a day, or hide a series from a legend.
  • One control scopes everything under it. The period stands in one row above the figures and the charts, never inside a card. A chart that needs a period of its own belongs to another view.
  • A chart is written again from its data. chart(element, options) on an element that already holds a chart replaces it, and the function it returns stops it. The period's change calls it with the period's numbers.
  • The figure leads, the chart explains. Each card names what it shows in its title; the chart's label says the same with its unit for a reader who does not see the drawing.
  • Each form does one job. The stacked areas show how the total moved and what it was made of, the donut what the whole is made of now, and the bars a ranking with its values written out, since a ranking is read by its numbers.

#Colors from the theme

A chart of one series is ink. It needs no color to be read, and it stands beside the monochrome interface around it without asking for attention.

Several series take a category each. Left to itself a chart gives them in a fixed order, blue, orange, teal, violet, amber, pink, green and sky, in which neighbors stay apart for the common kinds of color blindness in both schemes. When a color follows a thing across a product, name the series' category yourself: Tokyo is blue in the chart, in its legend, in its badge and in its row of a table, wherever it stands in the list.

  • A hidden series does not repaint the others. A series keeps its category when another leaves the chart.
  • A category is not a status. Success, warning and danger say how something is; a series that is failing says so in words, in a badge or a sentence beside the chart. The one form that takes the status inks is the waterfall, whose steps up and down are a change with a sign, as a stat's is.
  • Text is never in a series' color. Names and values take the text tokens, and a mark of the color stands beside them: a block for a bar or a slice, a stroke for a line.
  • Steps of one color are for one measure. A heatmap's cells, a funnel's stages and the outer ring of a sunburst take a share of one color, never a second hue.
  • The theme reaches every mark. Marks read the category's --color-category-<name>-mark token and the ink reads --chart-color, so the color scheme, a theme and an override scoped to one region all apply, with nothing drawn again.

#One scale

A chart has one scale. Two measures of different size, visitors in thousands and a rate in percent, are two charts one above the other on the same steps of time, never one chart with a scale on each side: a reader cannot tell which line belongs to which side, and the place where two lines cross means nothing.

A bar's scale starts at zero, since a bar's length is its value. A line may start higher when the change, not the amount, is the point; say so in the chart's label.

#Accessibility

  • A chart is a list. Each point is a list item named with its values, so a screen reader reads "Thu: New 1,420, Returning 820" with no script, and the arrow keys walk the same points for a reader who sees them.
  • The legend is controls. Each entry is a label around a real checkbox, reached with Tab and toggled with Space.
  • The readout is a convenience. It appears under the pointer, with the focus and under a finger, and never holds a value that is nowhere else. Where the exact numbers are the point, write them: values: true on a few bars, or a table beside the chart.
  • Color is never alone. A series is named in the legend and in the readout.
  • Motion follows the reader. With reduced motion the charts appear drawn.

#When to use an engine

The chart is drawn for a few hundred marks, each an element a reader can reach with the keys. Reach for a charting engine when the data is past that:

  • thousands of points, or values that stream in many times a second;
  • a zoom, a pan or a brush over the plot;
  • a shape the chart does not draw: a map, a network or a tree, a candlestick, a Sankey diagram, or a density that needs a kernel, such as a violin or a ridgeline.

An engine that draws in SVG takes the theme's tokens as they are, in fill and stroke. One that draws on a canvas cannot read a custom property, and a token is written with light-dark(), so resolve each color where the chart stands: a hidden probe inside the chart's root takes the token as its color, and one pixel painted with it gives the color as rgba(). Resolve again when the color scheme or the theme changes, and keep the same rules as above: one scale, categories in a fixed order, a legend that names every series and the numbers as text.

theme.js
const probe = document.createElement('span')probe.style.cssText = 'position:absolute;visibility:hidden'root.append(probe)function resolve(token) {  probe.style.color = `var(${token})`  pixel.fillStyle = getComputedStyle(probe).color  pixel.fillRect(0, 0, 1, 1)  const [r, g, b, a] = pixel.getImageData(0, 0, 1, 1).data  return `rgba(${r}, ${g}, ${b}, ${a / 255})`}