Skip to content

Bar Chart

A bar chart drawn as inline SVG on the shared chart core. Each label is a group with one bar per series, in the series' color. It draws at its own width and again when that width changes, so its text never scales.

Decision

When Use
Comparing a few amounts side by side, such as items synced per day or storage per library Bar Chart
A value over many points in time Line Chart
A shape beside a number Sparkline
How a whole splits into a few named parts Pie Chart
How much of one limit is used Usage Meter

Usage

import { createBarChart } from './configurationpage?name=myplugin_stolon.js';

var chart = createBarChart({
    label: 'Items synced per day',
    labels: ['Mon', 'Tue', 'Wed'],
    series: [{ name: 'Synced', values: [42, 58, 31] }]
});
view.querySelector('#chartHost').appendChild(chart.element);
chart.setValue({ series: [{ name: 'Synced', values: [12, 20, 9] }] });

Classes: stolon-chart and stolon-bar-chart, with the parts stolon-chart-plot, stolon-chart-mark, stolon-chart-value, stolon-chart-legend, and stolon-chart-tip. The handle is { element, getValue, setValue, redraw, destroy }. setValue takes { labels, series } and draws again. height is 200 pixels by default.

States and Modifiers

  • A series' color is a status color name or a hex code. A series without one takes blue, green, orange, purple, red, teal, pink, and yellow in turn.
  • The y axis shows about ticks round gridlines (default 4) from 0, or from the lowest value below it, to a round number above the highest. min and max fix the range.
  • Each value sits above its bar while it fits the bar's width. valueLabels: false hides them.
  • unit follows each value, such as "GB", and format(value) replaces the text of every value.
  • Every bar keeps its label, shortened with an ellipsis when it does not fit its band.
  • A legend shows only with two or more series.
  • Hovering a bar shows its label and value on the shared tooltip surface. The plot takes focus, and the arrow keys move the tooltip from bar to bar. Escape hides it.
  • A visually hidden table holds every number, captioned by label, so a screen reader reads the data.
  • Bars grow up as they draw, except under prefers-reduced-motion: reduce.

Gotchas

  • Call destroy() when the page hides. It stops the resize watch.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createBarChart

function createBarChart(options?: BarChartOptions): BarChartHandle

A bar chart on the shared chart core, drawn as inline SVG at its own width. Each label is a group with one bar per series, in the series' color, with its value above it when there is room. It draws again when its width changes, shows a tooltip on hover and focus, adds a legend only with several series, and keeps a visually hidden table of every number.

Option Type What it does
valueLabels boolean false hides the value above each bar (default true)
Handle member Type
destroy (): void
Class What it looks like
stolon-chart A chart on the shared chart core: an inline SVG plot that fills its width, a legend under it when there are several series, and a hidden table of the numbers
stolon-chart-grid The gridlines, the ticks, and the labels, in the text color and the small font, so they never scale with the plot
stolon-chart-series One series of marks in its status color, or a hex color through --stolon-custom, the accent when it names none
stolon-chart-mark A mark under the pointer or the arrow keys, drawn stronger than the rest
stolon-bar-chart Bars grow up from the axis as they draw, except under reduced motion
stolon-chart-legend The legend under a chart with several series: a swatch and a name per series, wrapping as a row
stolon-chart-swatch A small square in a series color, in the legend and the tooltip
stolon-chart-tip The tooltip of a mark, on the shared tooltip surface: the label over the value
stolon-chart-empty The empty sentence that stands in for the plot and the legend when a chart has nothing to draw, in the List's empty look

Example

Example Code

<!-- title: Bar Chart -->
<div id="mount" style="display: grid; gap: 24px;"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
mount.appendChild(base.createBarChart({
    label: 'Items synced per day',
    labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    series: [{ name: 'Synced', values: [42, 58, 31, 77, 64, 12, 25] }]
}).element);
mount.appendChild(base.createBarChart({
    label: 'Storage by library',
    labels: ['Movies', 'Shows', 'Music', 'Photos'],
    unit: 'GB',
    series: [
        { name: 'Den', color: 'blue', values: [820, 1240, 96, 40] },
        { name: 'Cabin', color: 'green', values: [610, 980, 120, 12] },
        { name: 'Office', color: '#e91e63', values: [140, 320, 18, 0] }
    ]
}).element);
</script>