PHP code example of molbal / smolchart

1. Go to this page and download the library: Download molbal/smolchart library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

molbal / smolchart example snippets




use Molbal\Smolchart\Charts\ProgressBar;
use Molbal\Smolchart\Rendering\ASCII\AsciiRenderer;

$progress = (new ProgressBar())
    ->setData(['completed' => 45, 'total' => 100])
    ->setLabels(['title' => 'Build'])
    ->setAnnotations(['note' => 'Halfway there']);

echo $progress->render(new AsciiRenderer());



use Molbal\Smolchart\Charts\BarChart;
use Molbal\Smolchart\Rendering\HTML\HtmlRenderer;

$bar = (new BarChart())
    ->setData([
        'Apples' => 10,
        'Bananas' => 6,
        'Cherries' => 14,
    ])
    ->setLabels(['x' => 'Fruit', 'y' => 'Quantity'])
    ->setAnnotations(['peak' => 'Cherries lead']);

$markup = $bar->render(new HtmlRenderer());



use Molbal\Smolchart\Charts\LineChart;
use Molbal\Smolchart\Rendering\ASCII\AsciiRenderer;

$line = (new LineChart())
    ->setData([
        ['t' => '2025-09-01', 'value' => 10],
        ['t' => '2025-09-02', 'value' => 12],
        ['t' => '2025-09-03', 'value' => 7],
    ])
    ->setLabels(['x' => 'Date', 'y' => 'Value'])
    ->setAnnotations(['dip' => 'Anomaly on 09-03']);

echo $line->render(new AsciiRenderer());



use Molbal\Smolchart\Charts\BarChart;
use Molbal\Smolchart\Rendering\ASCII\AsciiRenderer;
use Molbal\Smolchart\Rendering\HTML\HtmlRenderer;

$chart = (new BarChart())
    ->setData(['A' => 1, 'B' => 2])
    ->setLabels(['x' => 'Key', 'y' => 'Value'])
    ->setAnnotations(['note' => 'Demo']);

$ascii = $chart->render(new AsciiRenderer());
$html  = $chart->render(new HtmlRenderer());


use Molbal\Smolchart\Rendering\ASCII\AsciiRenderer;

$renderer = (new AsciiRenderer())
    ->withCharacter('▓')  // custom fill character (default already ▓)
    ->withBorder(true)    // draw a border box around the content
    ->withTitle('Weekly Sales')
    ->withWidth(30)       // Bar & Progress: total width incl. borders; Line: drawing width. (default 20 for unspecified)
    ->withLabelWidth(12); // reserve 12 chars for labels; defaults to longest label