Skip to content

Usage Meter

A progress bar of how much of something is used, such as disk space, with a label and an "X of Y used" line. It turns orange past a warning share and red past a danger share, and splits into colored parts with a legend.

Decision

When Use
A used amount out of a known total, such as a cache, a drive, or a quota Usage Meter
A task's progress Progress Ring
Comparing amounts with no total Bar Chart

Usage

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

var meter = createUsageMeter({ label: 'Cache', used: 12.4e9, total: 50e9, bytes: true });
view.querySelector('#meterHost').appendChild(meter.element);
meter.setValue({ used: 20e9 });

Classes: stolon-usage-meter, with the stolon-warning and stolon-danger modifiers, and the parts stolon-usage-meter-head, stolon-usage-meter-label, stolon-usage-meter-text, stolon-usage-meter-bar, stolon-usage-meter-fill, and stolon-usage-meter-legend. The handle is { element, getValue, setValue, destroy }. setValue merges { used, total, parts } and draws again.

States and Modifiers

  • The bar is the Progress Bar track, stolon-progress, a narrow gap tall, with one stolon-progress-bar fill per part.
  • bytes: true writes the amounts as sizes, such as 12.4 GB, through the shared size format. Otherwise unit follows a plain number, and format(value) replaces both.
  • usedText is "{used} of {total} used" by default, and also takes {percent}.
  • The meter takes stolon-warning at 80 percent and stolon-danger at 95 percent by default. Its line takes the same color.
  • parts: [{ label, value, color }] splits the used amount into segments, each in its color, a status color name or a hex code. A part with no color takes the chart series colors in turn. With two or more parts a legend lists each part and its amount. A segmented meter keeps its part colors past a threshold, and only its line turns orange or red.
  • used defaults to the sum of the parts. A total of 0 shows an empty bar.
  • The bar is a meter to a screen reader, named by the label, with the line as its value text.

Reference

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

createUsageMeter

function createUsageMeter(options?: UsageMeterOptions): UsageMeterHandle

A progress bar that shows how much of something is used, such as disk space, with a label and an "X of Y used" line. The bar turns orange past the warning share and red past the danger share. With parts it is segmented, one colored segment per part, with a legend of the parts and their amounts. It is a meter to a screen reader, with the same line as its value text.

Option Type What it does
label string the name over the bar, such as 'Storage' [optional]
bytes boolean true writes the numbers as sizes, such as 2.4 GB (default false)
unit string text after plain numbers, such as 'items' [optional]
warning number percent of the total from which the bar turns orange (default 80)
danger number percent of the total from which the bar turns red (default 95)
usedText string the line, where {used}, {total}, and {percent} are filled in (default '{used} of {total} used')
format (value: number): string function(value) returning the text of an amount, which wins over bytes and unit [optional]
Handle member Type
getValue (): UsageMeterValue
setValue (next: UsageMeterValue | null): void
destroy (): void
Type What it is
UsageMeterPart One part of a segmented usage meter, such as the space movies take. color is a status color name or a hex code.
UsageMeterValue What a usage meter shows: how much is used out of a total, or the parts that add up to what is used.
Class What it looks like
stolon-usage-meter A progress bar of how much is used, with a label and an "X of Y used" line over it, and a legend under it when it holds several parts
stolon-progress.stolon-usage-meter-bar The bar, the progress bar's track a narrow gap tall, whose fills sit side by side, each as wide as its share
stolon-usage-meter.stolon-warning A meter past its warning share turns orange, and past its danger share turns red, with its line in the same color. A segmented meter keeps its part colors and colors only the line
stolon-usage-meter-legend The legend of a segmented meter: a swatch, a name, and an amount per part, wrapping as a row

Example

Example Code

<!-- title: Usage Meter -->
<div id="mount" style="display: grid; gap: 24px;"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
const GB = 1024 * 1024 * 1024;
mount.appendChild(base.createUsageMeter({ label: 'Cache', used: 12.4 * GB, total: 50 * GB, bytes: true }).element);
mount.appendChild(base.createUsageMeter({ label: 'Transcodes', used: 42, total: 50, unit: 'files' }).element);
mount.appendChild(base.createUsageMeter({ label: 'Backups', used: 97 * GB, total: 100 * GB, bytes: true }).element);
mount.appendChild(base.createUsageMeter({
    label: 'Media Drive', total: 4096 * GB, bytes: true,
    parts: [{ label: 'Movies', value: 1800 * GB }, { label: 'Shows', value: 1100 * GB }, { label: 'Music', value: 210 * GB, color: 'orange' }]
}).element);
</script>