Skip to content
Focsd homeFocsd homeFocsd
Forms

Charts

Accessible charts: bar, line, area, stacked, scatter and donut, plus sparklines, gauges and stat tiles. Every chart is also a summary, a data table and keyboard-explorable points.

@focsd-dev/charts is a separate package, so apps without charts don’t pay for it. It has three elements: focsd-chart (bar, line, area, stacked bars and areas, scatter, donut), focsd-sparkline and focsd-gauge. The stat tile, the animated number and the status dot are in @focsd-dev/components: see Stat.

Terminal window
npm install @focsd-dev/charts
import '@focsd-dev/tokens/tokens.css';
import '@focsd-dev/charts';

A chart is a figure named by its caption and described by a generated sentence. You give it rows (data), the column for the categories (x) and the series (series, one { key, label } per column). Then:

  • The summary is words: “Area chart, 6 categories, 3 series. Highest: Online, Jun, $4,100.”
  • Each point is named “Series, category: value”, and the points are one tab stop. Arrow keys move between points; Home and End jump to the ends.
  • The Show data table button lists every value in a real table.
  • Series differ by pattern and marker shape as well as color, so a chart works in grayscale.
  • Numbers use Intl.NumberFormat options in format, in the page language.

Set type="area". The fill is drawn with a pattern as well as a color, and the line on top keeps its marker shape.

HTML
<div class="full">
<focsd-chart
id="guide-area"
type="area"
label="Sales by channel"
x="month"></focsd-chart>
</div>
<script type="module">
const channels = [
{ key: 'online', label: 'Online' },
{ key: 'stores', label: 'Stores' },
{ key: 'partners', label: 'Partners' },
];
const months = [
{ month: 'Jan', online: 2100, stores: 1400, partners: 600 },
{ month: 'Feb', online: 2400, stores: 1300, partners: 700 },
{ month: 'Mar', online: 3000, stores: 1500, partners: 650 },
{ month: 'Apr', online: 3600, stores: 1700, partners: 800 },
{ month: 'May', online: 3900, stores: 1600, partners: 900 },
{ month: 'Jun', online: 4100, stores: 1800, partners: 950 },
];
const dollars = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };
const chart = document.getElementById('guide-area');
chart.series = channels;
chart.format = dollars;
chart.data = months;
</script>

An area chart reads like a line chart: Left and Right move between categories, Up and Down between series.

Add stacked to bars or areas. Each point also says its share ("1,400 of 4,100"), the summary names the highest total, and the table gains a Total column.

HTML
<div class="full">
<focsd-chart
id="guide-stacked"
stacked
label="Sales by channel, stacked"
x="month"></focsd-chart>
</div>
<script type="module">
const channels = [
{ key: 'online', label: 'Online' },
{ key: 'stores', label: 'Stores' },
{ key: 'partners', label: 'Partners' },
];
const months = [
{ month: 'Jan', online: 2100, stores: 1400, partners: 600 },
{ month: 'Feb', online: 2400, stores: 1300, partners: 700 },
{ month: 'Mar', online: 3000, stores: 1500, partners: 650 },
{ month: 'Apr', online: 3600, stores: 1700, partners: 800 },
{ month: 'May', online: 3900, stores: 1600, partners: 900 },
{ month: 'Jun', online: 4100, stores: 1800, partners: 950 },
];
const dollars = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };
const chart = document.getElementById('guide-stacked');
chart.series = channels;
chart.format = dollars;
chart.data = months;
</script>

stacked applies to bars and areas, and is ignored for lines, scatter and donut. Positive and negative values stack separately from zero. Up and Down still move to the next series in legend order, not up the visual stack.

A scatter has a number on both axes. Point x at the numeric column; each series is a column of y values. The arrow keys follow the x order, and the table lists every point.

HTML
<div class="full">
<focsd-chart
id="guide-scatter"
type="scatter"
label="Price and units sold"
x="price"></focsd-chart>
</div>
<script type="module">
const chart = document.getElementById('guide-scatter');
chart.series = [
{ key: 'units', label: 'Units sold' },
{ key: 'returns', label: 'Returns' },
];
chart.data = [
{ price: 5, units: 410, returns: 12 },
{ price: 10, units: 360, returns: 20 },
{ price: 15, units: 280, returns: 18 },
{ price: 20, units: 190, returns: 25 },
{ price: 25, units: 120, returns: 22 },
{ price: 30, units: 80, returns: 30 },
];
</script>

Scatter data uses the same rows. x is the key of the numeric column, and each series is a y column; for one series you can set y instead of series. A row whose x is not a finite number is skipped. Left and Right move to the next larger or smaller x, Up and Down to the point with the nearest x in the neighboring series, and Home and End to the smallest and largest x. The summary gives the ranges: “x from 5 to 30, y from 12 to 410”.

Charts are drawn left to right in every direction, because time runs that way in most right-to-left charts. Add mirror to flip the categories and move the value axis to the right; Left and Right then follow what you see.

HTML
<div class="full">
<focsd-chart
id="guide-mirror"
mirror
dir="rtl"
label="Sales by channel"
x="month"></focsd-chart>
</div>
<script type="module">
const channels = [
{ key: 'online', label: 'Online' },
{ key: 'stores', label: 'Stores' },
{ key: 'partners', label: 'Partners' },
];
const months = [
{ month: 'Jan', online: 2100, stores: 1400, partners: 600 },
{ month: 'Feb', online: 2400, stores: 1300, partners: 700 },
{ month: 'Mar', online: 3000, stores: 1500, partners: 650 },
{ month: 'Apr', online: 3600, stores: 1700, partners: 800 },
{ month: 'May', online: 3900, stores: 1600, partners: 900 },
{ month: 'Jun', online: 4100, stores: 1800, partners: 950 },
];
const dollars = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };
const chart = document.getElementById('guide-mirror');
chart.series = channels;
chart.format = dollars;
chart.data = months;
</script>

Charts draw left to right even on right-to-left pages, because most right-to-left charts show time that way. mirror flips the categories (and the grouped bar order and the scatter x axis), moves the value axis to the right and swaps Left and Right so the keys follow what you see. Home and End stay logical: the first and last category. mirror is off under dir="rtl" unless you set it.

A sparkline is a word-sized image of a series, named by a sentence. It is not interactive: put the detail in a chart.

Signups up 325% this week.

HTML
<div class="stack">
<p>
Signups
<focsd-sparkline id="guide-spark" label="Signups per day" show-last></focsd-sparkline>
up 325% this week.
</p>
<focsd-stat
label="Revenue"
value="48200"
period="last month"
delta="0.124"
id="guide-spark-stat"></focsd-stat>
</div>
<script type="module">
document.getElementById('guide-spark').values = [4, 8, 6, 12, 19, 17];
document.getElementById('guide-spark-stat').format = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };
</script>

The sparkline is an image whose name is the label plus a sentence: “Signups per day: 6 values, from 4 to 19, last 17, up 325% from the first”. It has no tab stops. A stat says its change in words (“Up 12.4% from last month, good”), so the arrow and color are never the only signal.

A gauge shows one value against a range. Bands name the ranges, so the state is in words and a list, not only in color.

HTML
<div class="stack">
<focsd-gauge id="guide-gauge" label="Disk used" show-value></focsd-gauge>
</div>
<script type="module">
const gauge = document.getElementById('guide-gauge');
gauge.value = 72;
gauge.format = { style: 'unit', unit: 'percent' };
gauge.bands = [
{ to: 60, label: 'Normal', tone: 'positive' },
{ to: 85, label: 'Warning', tone: 'caution' },
{ to: 100, label: 'Critical', tone: 'critical' },
];
</script>

The gauge is a meter. Its value text includes the current band (“72%, Warning”), and the bands are also a visible list.

  • Use a chart when people need the values: it has the table and the keyboard.
  • Use a sparkline for a trend next to a number or in a table row, never alone for important data.
  • Use a gauge for one value against known limits, and give the bands names.
  • Use a stat for a headline figure and its change.

Charts draw with SVG attributes, not inline style, so they run under a strict policy (style-src 'self').