Search by

maantje / charts

moijamie

SVG Charts in PHP

Package info

github.com/maantje/charts

pkg:composer/maantje/charts

Fund package maintenance!

maantje

Statistics

Installs: 162 936

Dependents: 2

Suggesters: 0

Stars: 337

Open Issues: 1

v0.10.0 2026-10-20 00:07 UTC

This package is auto-updated.

Last update: 2026-10-20 00:07:53 UTC


README

Charts is a zero-dependency PHP library for generating SVG charts. It enables easy creation of SVG-based charts directly from PHP, with no additional dependencies required.

Features

  • Simple, intuitive API for chart creation
  • Lightweight, with no external dependencies
  • Supports various chart types: line charts, bar charts, stacked charts, boxplots, and mixed charts
  • Fully customizable and extendable
  • Outputs pure SVG, allowing for:

Installation

To get started, install the package via composer:

composer require maantje/charts

Usage Examples

Below are some examples of the types of charts you can create using this library. Click on the links to view the source code for each example.

Example Usage With mPDF

📄 View PDF document
View source

Simple Line Chart

Simple Line Chart
View source

Curved Line Chart

Curved Line Chart
View source

Step line chart

Step line chart
View source

Area line chart

Area line chart
View source

Bar Chart

Bar Chart
View source

Stacked Bar Chart

Stacked Bar Chart
View source

Grouped Bar Chart

Grouped Bar Chart
View source

Advanced Line Chart

Advanced Line Chart
View source

Advanced Bar Chart

Advanced Bar Chart
View source

Mixed chart

Mixed chart
View source

Negative Values Chart

Negative Values Chart
View source

Simple boxplot chart

Boxplot chart View source

Boxplot chart with a line

Boxplot chart with a line View source

Pie chart

Pie chart
View source

Usage

Creating a Chart

You can create different types of charts using the provided classes. Below are examples of how to create a simple bar chart and a line chart.

Simple Bar Chart

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();

Simple Line Chart

use Maantje\Charts\Chart;
use Maantje\Charts\Line\Line;
use Maantje\Charts\Line\Lines;
use Maantje\Charts\Line\Point;

$chart = new Chart(
    series: [
        new Lines(
            lines: [
                new Line(
                    points: [
                        new Point(x: 0, y: 0),
                        new Point(x: 100, y: 4),
                        new Point(x: 200, y: 12),
                        new Point(x: 300, y: 8),
                    ],
                ),
                new Line(
                    points: [
                        [0, 0],
                        [100, 4],
                        [200, 12],
                        [300, 8],
                    ],
                ),
            ],
        ),
    ],
);

echo $chart->render();

Simple Boxplot Chart

use Maantje\Charts\Boxplot\Boxplot;
use Maantje\Charts\Boxplot\Boxplots;
use Maantje\Charts\Chart;

$chart = new Chart(
    series: [
        new Boxplots(
            boxplots: [
                new Boxplot(name: 'Jan', values: [12, 30, 45, 60, 88]),
                new Boxplot(name: 'Feb', values: [20, 35, 50, 70, 95, 130, 4]),
            ],
        ),
    ],
);

echo $chart->render();

The values of a boxplot are given in order: min, Q1, median, Q3, max. Any value after the fifth is an outlier and is drawn as a point. The library does not compute quartiles: pass the five statistics already calculated.

A Boxplot is a bar like any other, so it can also be placed in Bars, next to Bar, StackedBar or BarGroup. To draw boxplots on a secondary Y axis, pass the same yAxis name to Boxplots (it drives the axis range) and to each Boxplot (it drives the drawing), as with Bars and Bar.

Line over bars

A BarLine draws a line through the middle of each bar of a Bars series. It works with every kind of bar: Bar, StackedBar, BarGroup and Boxplot.

use Maantje\Charts\Bar\Bar;
use Maantje\Charts\Bar\BarLine;
use Maantje\Charts\Bar\Bars;

$bars = new Bars(bars: [
    new Bar(name: 'Jan', value: 42000),
    new Bar(name: 'Feb', value: 51000),
    new Bar(name: 'Mar', value: 47000),
]);

$chart = new Chart(
    series: [
        $bars,
        new BarLine(bars: $bars, values: [45000, null, 50000], color: 'red', pointColor: 'red'),
    ],
);
  • values holds one value per bar, in order. null leaves a gap, so a line can cover only some of the bars.
  • Without values, the line goes through the value of each bar: its height, the total of a stacked bar, the highest bar of a group or the median of a boxplot.
  • The line uses the Y axis of its bars; pass yAxis to draw it on another one.
  • BarLine accepts the same size, color, areaColor, curve and stepLine options as Line; points are drawn only when pointColor is set.

List the BarLine after its bars so the line is drawn on top.

Annotations

You can add annotations to your charts for better visualization.

use Maantje\Charts\Annotations\PointAnnotation;
use Maantje\Charts\YAxis;

$chart = new Chart(
    yAxis: new YAxis(
        annotations: [
            new PointAnnotation(x: 200, y: 120, label: 'Important Point'),
        ],
    ),
    // ...
);

License

The MIT License (MIT). Please see License File for more information.