I have followed the official tutorial to get my chart on symfony : https://symfony.com/bundles/ux-chartjs/current/index.html
But the chart does not display, it is just blank. I head that it may be caused by an old version of Encore or Symfony UX Chart.js but they are all up to date :
Encore is up to date : Using version ^2.0 for symfony/webpack-encore-bundle
UX Chart.js is up to date :Using version ^2.9 for symfony/ux-chartjs
Stimulus is up to date :Using version ^2.9 for symfony/stimulus-bundle
I Ran npm run watch :
> watch> encore dev --watch
Running webpack ...
DONE Compiled successfully in 3085ms 7:43:59 PM
5 files written to public/buildEntrypoint app [big] 1.75 MiB = runtime.js 14.6 KiB vendors-node_modules_symfony_stimulus-bridge_dist_index_js-node_modules_symfony_ux-chartjs_di-bb5774.js 1.71 MiB app.css 741 bytes app.js 23.2 KiBwebpack compiled successfully
The code of my control is basically the same that the code on the tutorial :
$chart = $chartBuilder->createChart(Chart::TYPE_LINE);
$chart->setData([
'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
'datasets' => [
[
'label' => 'My First dataset',
'backgroundColor' => 'rgb(255, 99, 132)',
'borderColor' => 'rgb(255, 99, 132)',
'data' => [0, 10, 5, 2, 20, 30, 45],
],
],
]);
$chart->setOptions([
'scales' => [
'y' => [
'suggestedMin' => 0,
'suggestedMax' => 100,
],
],
]);
return $this->render('graph.html.twig', ['chart' => $chart]);
And here is the content of my twig :
{% extends 'armature.html.twig' %}
{% block contenu %}
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Graphiques</h1>
A
<div>
{{ render_chart(chart) }}
</div>
B
Here is the HTML generated :
<canvas data-controller="symfony--ux-chartjs--chart" data-symfony--ux-chartjs--chart-view-value="{"type":"line","data":{"labels":["January","February","March","April","May","June","July"],"datasets":[{"label":"My First dataset","backgroundColor":"rgb(255, 99, 132)","borderColor":"rgb(255, 99, 132)","data":[0,10,5,2,20,30,45]}]},"options":{"scales":{"y":{"suggestedMin":0,"suggestedMax":100}}}}"></canvas>
But it just display nothing.
I added on my twig but it is the same.
I don't see any errors or warning in the Chrome console.
Any ideas? I'm a bit lost.
Thanks
This happens when the page does not include scripts that will process the canvas. Try this code for graph.html.twig:
If that doesn't work, you can try remove the packages and require them again, making sure that you don't get errors when applying their recipes. Something like this: