Charts from PHP

Render beautiful graphs with chart.js

Leverage the power of Chart.js entirely from PHP.

 

src/Controller/ChartController.php

#[Route('/chartjs', name: 'app_chartjs')]
public function chartjs(ChartBuilderInterface $chartBuilder): Response
{
    $chart = $chartBuilder->createChart(Chart::TYPE_LINE);

    $chart->setData([
        'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        'datasets' => [
            ['label' => 'Cookies eaten 🍪', 'data' => [2, 10, 5, 18, 20, 30, 45]],
            ['label' => 'Km walked 🏃‍♀️', 'data' => [10, 15, 4, 3, 25, 41, 25]],
        ],
    ]);

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

templates/chart/chartjs.html.twig

{#
    Renders the canvas element with all of the data and
    options already attached to it!
#}

{{ render_chart(chart) }}
Symfony logo

UX Chart.js

Install It

$ composer require ux symfony/ux-chartjs
$ npm install --force
$ npm run watch