Article Image

A Zero-dependency SVG Chart Library for PHP

Published on: Oct 22 2024
By: Paul Redmond

The maantje/charts package by Jamie Schouten is a zero-dependency PHP library for generating SVG charts. Charts has a straightforward API, which you can use to create SVGs directly in PHP without any additional dependencies required:

use Maantje\Charts\Bar\Bar;
use Maantje\Charts\Bar\Bars;
use Maantje\Charts\Chart;
 
$chart = new Chart(
    series: [
        new Bars(
            bars: [
                new Bar(name: 'Jan', value: 222301),
                new Bar(name: 'Feb', value: 189242),
                new Bar(name: 'Mar', value: 144922),
            ],
        ),
    ],
);
 
echo $chart->render();

The example bar chart renders the following SVG:

Simple bar chart example

The library seemed very performant to me while running the examples. You can generate the example SVG files by cloning the repo and running the examples composer script:

git checkout https://github.com/maantje/charts
cd charts
composer install
composer run examples
open examples/output/

#Package Features

  • Simple, intuitive API for chart creation
  • Lightweight, with no external dependencies
  • Supports various chart types: line charts, bar charts, stacked charts, and mixed charts
  • Fully customizable and extendable
  • Outputs pure SVG, allowing for: 
    • Embedding in PDFs (ideal for reports)

You can learn more about this package, get full installation instructions, and view the source code on GitHub at maantje/charts.