Terminal charts for SugarCraft
Sparkline, Bar, Line, Heatmap, Scatter, TimeSeries, Streamline, Waveline, OHLC, plus inline image rendering via Sixel / Kitty / iTerm2 protocols. Auto-detects terminal capability.
composer require sugarcraft/sugar-charts
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();
LineChart::withSeriesColorFn() and BarChart::withBarColor() resolvers.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;
| Class | Method | Description |
|---|---|---|
| Sparkline | new(data, width) | Create sparkline chart |
| Sparkline | view() | Render sparkline |
| BarChart | new(data, width, height) | Create bar chart |
| BarChart | view() | Render bar chart |
| BarChart | withBarColor(?Closure) | Per-bar color resolver fn(Bar, i): ?Color |
| LineChart | new(data, width, height) | Create line chart |
| LineChart | view() | Render line chart |
| LineChart | withSeriesColorFn(?Closure) | Per-point color resolver fn(dataset, x, value): ?Color |
| Heatmap | new(data, width, height) | Create heatmap |
| Canvas | new(width, height) | Create chart canvas |
VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.