Charts from PHP

Render beautiful graphs with chart.js

Leverage the power of Chart.js entirely from PHP.

composer require symfony/ux-chartjs

What Chart.js gives you

  • Charts from PHP

    Build the whole chart server-side in a class.

  • Full Chart.js options

    Every option the library exposes, set from PHP.

  • Updatable data

    Swap datasets without rebuilding the canvas.

  • Stimulus events

    Hook into the chart before it is drawn.

  • Any chart type

    Line, bar, radar, doughnut and the rest.

Interactive charts with Chart.js

Add Chart.js to your app.

Easy charts with Chart.js

// ... use statements hidden - click to show
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;

class ChartjsController extends AbstractController
{
    #[Route('/chartjs', name: 'app_chartjs')]
    public function __invoke(ChartBuilderInterface $chartBuilder): Response
    {
        $chart = $chartBuilder->createChart(Chart::TYPE_LINE);
        $chart->setData([
            'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
            'datasets' => [
                [
                    'label' => 'Cookies eaten ๐Ÿช',
                    'backgroundColor' => 'rgb(255, 99, 132, .4)',
                    'borderColor' => 'rgb(255, 99, 132)',
                    'data' => [2, 10, 5, 18, 20, 30, 45],
                    'tension' => 0.4,
                ],
                [
                    'label' => 'Km walked ๐Ÿƒโ€โ™€๏ธ',
                    'backgroundColor' => 'rgba(45, 220, 126, .4)',
                    'borderColor' => 'rgba(45, 220, 126)',
                    'data' => [10, 15, 4, 3, 25, 41, 25],
                    'tension' => 0.4,
                ],
            ],
        ]);
        $chart->setOptions([
            'maintainAspectRatio' => false,
        ]);

        return $this->render('ux_packages/chartjs.html.twig', [
            'chart' => $chart,
        ]);
    }
}
{% extends 'base.html.twig' %}

{% block body %}
    <div style="height: 300px;">
        {{ render_chart(chart) }}
    </div>
{% endblock %}
Symfony logo

UX Chart.js

Built in PHP, drawn by Chart.js

A chart is a PHP object.

You never write the JavaScript configuration by hand. A builder produces the chart, Twig renders the canvas, Chart.js draws it.

Data from your repository

Line, bar, radar, pie, doughnut and the rest are all the same object with a different type.

$chart = $chartBuilder
    ->createChart(Chart::TYPE_LINE);

$chart->setData([
    'labels' => ['January', 'February'],
    'datasets' => [[
        'label' => 'Visitors',
        'data' => [120, 240],
    ]],
]);

Every Chart.js option

setOptions takes the library configuration untouched, so the documentation you read still applies.

$chart->setOptions([
    'scales' => [
        'y' => ['suggestedMin' => 0],
    ],
]);

One Twig function

It prints the canvas and the Stimulus controller that boots the chart.

{{ render_chart(chart) }}

Install Chart.js.

$ composer require symfony/ux-chartjs

More from Symfony UX.

Chart.js is one package among many. They share the same Stimulus foundation and install the same way.

Image for the Icons UX package

Icons

Render SVG icons seamlessly from your Twig templates.

Image for the Map UX package

Map

Render interactive Maps in PHP with Leaflet or Google Maps.

Image for the Twig Components UX package

Twig Components

Create PHP classes that can render themselves

Browse every package

Keep Chart.js at hand.

Documentation, the people behind it, and where to ask when something does not behave.