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 see | Use |
|---|---|
| How values go on over time | A line, or an area for one or two series |
| What rose and what fell between two moments | A slope; for two values of each thing in a list, a dumbbell |
| How ranks changed | A bump chart |
| Amounts compared with each other | Bars; across when the names are long |
| How one figure became another | A waterfall |
| Spans between two values | A range chart |
| What totals are made of, over time or across groups | Stacked bars or stacked areas; a marimekko when the groups differ in size |
| What one whole is made of | A 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 stage | A funnel |
| A few goals, each with a whole of its own | Rings |
| A few things on the same measures | A radar |
| How readings are spread | A histogram; for several groups at once, a box plot |
| A pattern across two scales | A heatmap |
| Two measures against each other | A scatter, with bubbles for a third |
| One figure and how it moved | A stat |
| Which way a series went, in a sentence or a row | A sparkline |
| One reading and how near its limit it is | A gauge or a meter |
| A pattern across many small values, with no scales beside it | A heatmap of cells |
| The exact numbers, compared in columns | A 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>import { chart } from '@master/ui/behaviors/chart.js'import { pages, periods, sources } from './data.js'/** * A dashboard of three charts under one choice of period. The chart behavior writes each chart from the * period's numbers, and writes them again when the period changes, so the figures and the charts always show * the same slice. Returns the function that stops it. */export function mount(root) { const part = (name) => root.querySelector(`[data-traffic="${name}"]`) let detach = [] function draw(name) { const period = periods[name] part('period').textContent = period.name for (const figure of ['visitors', 'signups', 'bounce']) { const [value, change, up] = period[figure] part(figure).textContent = value // The direction is a word as well as a color and an arrow: "Up 12.4%". const way = Object.assign(root.ownerDocument.createElement('span'), { className: 'sr-only', textContent: up ? 'Up ' : 'Down ' }) part(`${figure}-change`).replaceChildren(way, change) part(`${figure}-change`).className = `stat-delta ${up ? 'stat-delta-up' : 'stat-delta-down'}` } for (const stop of detach) stop() detach = [ chart(part('visits'), { type: 'area', stacked: true, label: `Visitors, new and returning, ${period.name.toLowerCase()}`, labels: period.labels, series: [ { name: 'Returning', category: 'sky', values: period.returning }, { name: 'New', category: 'blue', values: period.fresh }, ], }), chart(part('sources'), { type: 'donut', label: `Visitors by source, ${period.name.toLowerCase()}`, center: { value: period.visitors[0], label: 'Visitors' }, series: sources.map((source, index) => ({ ...source, value: period.sources[index] })), }), chart(part('pages'), { type: 'bar', horizontal: true, values: true, ticks: 3, label: `Pages by visitors, ${period.name.toLowerCase()}`, labels: pages, series: [{ name: 'Visitors', values: period.pages }], }), ] } const controller = new AbortController() root.addEventListener('change', (event) => { if (event.target.name === 'traffic-period') draw(event.target.value) }, { signal: controller.signal }) draw(root.querySelector('input[name="traffic-period"]:checked')?.value ?? 'week') return () => { controller.abort() for (const stop of detach) stop() }}/** * A site's traffic over three periods. Each period holds everything the dashboard shows, so choosing one * changes the figures and the charts together and they always agree. A series names its category, and the * category, not its place in the list, decides its color. */const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']const months = ['Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov']export const periods = { week: { name: 'The last 7 days', visitors: ['12,480', '8.2%', true], signups: ['642', '3.1%', true], bounce: ['38.4%', '1.6%', false], labels: days, fresh: [980, 1240, 1130, 1420, 1510, 890, 760], returning: [620, 710, 690, 820, 860, 470, 380], sources: [5240, 3870, 2120, 1250], pages: [4820, 3112, 2240, 1360, 948], }, month: { name: 'The last 30 days', visitors: ['51,930', '12.4%', true], signups: ['2,718', '6.8%', true], bounce: ['39.1%', '0.4%', true], labels: Array.from({ length: 30 }, (_, index) => (index < 17 ? `Oct ${index + 15}` : `Nov ${index - 16}`)), fresh: [950, 1000, 760, 680, 1050, 1120, 1160, 1230, 1190, 840, 730, 1130, 1210, 1270, 1320, 1250, 880, 770, 1180, 1300, 1350, 1420, 1330, 940, 830, 1020, 1280, 1170, 1460, 1510], returning: [540, 580, 400, 350, 590, 630, 660, 700, 670, 430, 360, 640, 690, 720, 750, 710, 450, 390, 670, 740, 770, 810, 760, 480, 410, 620, 710, 690, 820, 860], sources: [21400, 16900, 8530, 5100], pages: [19840, 12760, 9310, 5620, 3980], }, year: { name: 'The last 12 months', visitors: ['584,200', '31.0%', true], signups: ['28,940', '24.6%', true], bounce: ['41.7%', '2.9%', false], labels: months, fresh: [29100, 29600, 30200, 30900, 31500, 31100, 31900, 32600, 32400, 33200, 33600, 33700], returning: [15600, 16000, 16300, 16600, 16900, 16800, 17200, 17600, 17400, 17800, 18100, 18100], sources: [238000, 196400, 97300, 52500], pages: [221500, 148200, 104700, 63900, 45100], },}export const sources = [ { name: 'Search', category: 'blue' }, { name: 'Direct', category: 'teal' }, { name: 'Social', category: 'violet' }, { name: 'Referral', category: 'amber' },]export const pages = ['Installation', 'Button', 'Dialog', 'Default theme', 'Command']- 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
labelsays 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>-marktoken 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: trueon 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.
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})`}