← All 40 libraries

SugarCharts

📊 SugarCharts

Terminal charts for SugarCraft

inspired by NimbleMarkets/ntcharts charts sparkline heatmap sixel kitty iterm2

SugarCharts code coverage

Sparkline, Bar, Line, Heatmap, Scatter, TimeSeries, Streamline, Waveline, OHLC, plus inline image rendering via Sixel / Kitty / iTerm2 protocols. Auto-detects terminal capability.

Install

composer require sugarcraft/sugar-charts

Quickstart

use SugarCraft\Charts\Sparkline\Sparkline;
use SugarCraft\Charts\BarChart\BarChart;
use SugarCraft\Charts\LineChart\LineChart;

echo Sparkline::new([1, 3, 2, 8, 5, 4, 7, 6], 8)->view() . PHP_EOL;
// " ▂▁█▅▃▇▆" (scaled to the window's min..max; the minimum is a blank cell)

echo BarChart::new(
    [['cpu', 0.7], ['mem', 0.4], ['disk', 0.9]], 20, 5
)->view() . PHP_EOL;

echo LineChart::new([1, 4, 2, 8, 6, 3, 7], 30, 6)->view();

What's in the box

CanvasFixed cell grid with rune + Sprinkles Style per cell. Foundation under every chart type.
SparklineSingle-row series via 8 Unicode bar glyphs. Perfect for log dashboards.
Bar / LineAuto-scaling y-axis, configurable axis labels, optional grid.
HeatmapDensity visualisation with palette gradients.
OHLCCandlestick chart for financial time-series.
TimeSeriesStreaming series with bucket aggregation.
Picture (Sixel/Kitty/iTerm2)Inline raster image rendering. Auto-detects via terminal-version query.
Per-point / per-bar colorbtop-style value heat: LineChart::withSeriesColorFn() and BarChart::withBarColor() resolvers.

Per-point and per-bar colors

btop tints every graph point and meter by its value through the theme gradient. LineChart::withSeriesColorFn(fn(string $dataset, int $x, float $value): ?Color) colors each sample's dot, fill run and outgoing connector ($dataset is LineChart::PRIMARY_SERIES for the unnamed series; $x is the index in the full series; null keeps the series color). BarChart::withBarColor(fn(Bar $bar, int $i): ?Color) colors each bar's body and eighth-block cap. Passing null restores byte-identical default output; a non-Color return throws UnexpectedValueException.

use SugarCraft\Charts\BarChart\{Bar, BarChart};
use SugarCraft\Charts\LineChart\LineChart;
use SugarCraft\Core\Util\Color;

$heat = static fn (float $v): Color => match (true) {
    $v >= 7 => Color::hex('#ff5555'),
    $v >= 4 => Color::hex('#f1fa8c'),
    default => Color::hex('#50fa7b'),
};

echo LineChart::new([1, 4, 2, 8, 6, 3, 7], 30, 6)
    ->withSeriesColorFn(
        static fn (string $dataset, int $x, float $v): ?Color =>
            $dataset === LineChart::PRIMARY_SERIES ? $heat($v) : null,
    )
    ->view() . PHP_EOL;

echo BarChart::new([['cpu', 0.7], ['mem', 0.4], ['disk', 0.9]], 20, 5)
    ->withFractionalHeights()
    ->withBarColor(static fn (Bar $bar, int $i): ?Color => $heat($bar->value * 10))
    ->view() . PHP_EOL;

Source & demos

Try the quickstart →

API

ClassMethodDescription
Sparklinenew(data, width)Create sparkline chart
Sparklineview()Render sparkline
BarChartnew(data, width, height)Create bar chart
BarChartview()Render bar chart
BarChartwithBarColor(?Closure)Per-bar color resolver fn(Bar, i): ?Color
LineChartnew(data, width, height)Create line chart
LineChartview()Render line chart
LineChartwithSeriesColorFn(?Closure)Per-point color resolver fn(dataset, x, value): ?Color
Heatmapnew(data, width, height)Create heatmap
Canvasnew(width, height)Create chart canvas

Demos.

VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.

Sparkline

Sparkline

Single-row series — eight Unicode glyphs.
Bar chart

Bar chart

Auto-scaling labelled vertical bars.
Line chart

Line chart

Single-series ASCII plot with axes.
Heatmap

Heatmap

Density visualisation with palette gradient.
Scatter

Scatter

Cell-quantised XY plot.
TimeSeries

TimeSeries

Streaming series with bucket aggregation.
OHLC

OHLC

Candlestick chart for financial data.
Picture

Picture

Inline image via Sixel / Kitty / iTerm2.