Skip to content

Gauges and sparklines ​

A value on a scale, and small charts the size of a word. Both are SVGs drawn by PHP: no JavaScript, no inline styles.

Gauge demo. Data is embedded and nothing is saved.
php
<?php

use Caelrik\Ui\Components\Gauge;
use Caelrik\Ui\Components\Sparkline;
use Caelrik\Ui\Support\Html;

$load = static fn (Gauge $gauge): Gauge => $gauge
    ->format('{value}%')
    ->band(0, 60, 'success', 'Normal')
    ->band(60, 85, 'warning', 'High')
    ->band(85, 100, 'danger', 'Critical');

return [
    Html::raw('<div class="d-flex flex-wrap gap-4 align-items-end mb-3">'),
    $load(Gauge::make('cpu')->label('CPU')->value(72)->target(90)),
    $load(Gauge::make('memory')->label('Memory')->value(41)->size('lg')),
    $load(Gauge::make('disk')->label('Disk')->value(93)->size('sm')),
    Gauge::make('latency')->label('Response time')->value(180)->range(0, 500)->format('{value} ms')->color('info'),
    Html::raw('</div><div class="col-md-8">'),
    Gauge::make('quota')->label('Storage used')->value(1250)->range(0, 2000)->format('{value} GB')->linear()
        ->band(0, 1500, 'success', 'OK')->band(1500, 2000, 'danger', 'Almost full')->target(1500),
    Gauge::make('sales')->label('Sales vs goal')->value(84000)->range(0, 120000)->format('${value}')->linear()
        ->target(100000),
    Html::raw('</div><table class="table w-auto"><caption>Weekly figures</caption><thead><tr>'
        . '<th scope="col">Figure</th><th scope="col">Last 7 weeks</th><th scope="col">Now</th></tr></thead><tbody>'
        . '<tr><th scope="row">Revenue</th><td>'
        . Sparkline::make()->label('Revenue')->values([8, 9, 12, 11, 14, 13, 16])->type('area')->markLast()
        . '</td><td>16</td></tr><tr><th scope="row">Profit</th><td>'
        . Sparkline::make()->label('Profit')->values([3, -2, 0, 5, 4, -1, 6])->type('bar')->color('success')
        . '</td><td>6</td></tr><tr><th scope="row">Errors</th><td>'
        . Sparkline::make()->label('Errors')->values([5, 4, 6, 3, 2, 2, 1])->color('danger')
        . '</td><td>1</td></tr></tbody></table>'),
];

Gauge ​

php
use Caelrik\Ui\Components\Gauge;

Gauge::make('cpu')
    ->label('CPU')
    ->value(72)
    ->format('{value}%')                       // "{value} ms", "${value}"
    ->band(0, 60, 'success', 'Normal')
    ->band(60, 85, 'warning', 'High')
    ->band(85, 100, 'danger', 'Critical')
    ->target(90);

Gauge::make('quota')->label('Storage used')->value(1250)->range(0, 2000)->format('{value} GB')->linear();
  • range($min, $max): the scale, 0 to 100 by default. A value outside it fills the gauge to its end, and its text still gives the real value.
  • band($from, $to, $color, $label): colored ranges, drawn along the scale. The value takes its band's color, and the band's name is written under it.
  • target($value): a mark across the scale.
  • linear(): a bar with the label and the value above it, instead of a semicircle.
  • format(), decimals(): how numbers are written; by default up to 2 decimals, with thousands separators.
  • size() (sm, md, lg), color() (the value's color outside the bands), scale(false) hides the minimum and maximum.
  • labels(['target' => 'objectif {target}']) translates the target's text.

Accessibility ​

  • A meter (role="meter") named by its visible label, with aria-valuemin, aria-valuemax, aria-valuenow and a value text that says it all: "72%, High, target 90%".
  • Not by color alone: the band's name is written next to the value.
  • Colors keep 3:1 against the page and the track (warning and info use darker shades), and the gauge shows in forced-colors mode with system colors.

Sparkline ​

php
use Caelrik\Ui\Components\Sparkline;

Sparkline::make()->label('Revenue')->values([8, 9, 12, 11, 14, 13, 16])->type('area')->markLast();
Sparkline::make()->label('Profit')->values([3, -2, 0, 5])->type('bar')->color('success');
Sparkline::make()->values([120, 140, 90])->decorative();   // next to the written values
  • Types: line (default), area, bar (from zero: negative values are outlined bars below the line).
  • Sizes: sm, md, lg. It scales with the text around it, so it fits in a sentence, a table cell or a record view.
  • An image named after its values' range: "Revenue, last 7 values: from 8 to 16, lowest 8, highest 16." labels(['name' => …]) translates it ({label}, {count}, {first}, {last}, {min}, {max}).
  • decorative() hides it from screen readers when the values are written next to it.
  • The stat tile's sparkline (Stat::sparkline()) is drawn the same way.

Released under the MIT License.