Charts from PHP
Render beautiful graphs with chart.js
Leverage the power of Chart.js entirely from PHP.
composer require symfony/ux-chartjs
// ... use statements hidden - click to show
class ChartjsController extends AbstractController
{
#[Route('/chartjs', name: 'app_chartjs')]
public function __invoke(UxPackageRepository $packageRepository, ChartBuilderInterface $chartBuilder): Response
{
$package = $packageRepository->find('chartjs');
$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', [
'package' => $package,
'chart' => $chart,
]);
}
}
{% extends 'base.html.twig' %}
{% block body %}
<div style="height: 300px;">
{{ render_chart(chart) }}
</div>
{% endblock %}
UX Chart.js
Install It
$ composer require symfony/ux-chartjs