6

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="{&quot;type&quot;:&quot;line&quot;,&quot;data&quot;:{&quot;labels&quot;:[&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;],&quot;datasets&quot;:[{&quot;label&quot;:&quot;My First dataset&quot;,&quot;backgroundColor&quot;:&quot;rgb(255, 99, 132)&quot;,&quot;borderColor&quot;:&quot;rgb(255, 99, 132)&quot;,&quot;data&quot;:[0,10,5,2,20,30,45]}]},&quot;options&quot;:{&quot;scales&quot;:{&quot;y&quot;:{&quot;suggestedMin&quot;:0,&quot;suggestedMax&quot;:100}}}}"></canvas>

But it just display nothing.

enter image description here

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

2
  • The chart configuration is OK, this fiddle is made from the value in your data-symfony--ux-chartjs--chart-view-value. I see that A and B are at a distance, but can you make sure in Chrome inspect that there's actually a canvas element inside the div with the same height? Commented Jun 15, 2023 at 14:07
  • yes, there is one. I have pasted it in my question Commented Jun 16, 2023 at 7:35

4 Answers 4

1

This happens when the page does not include scripts that will process the canvas. Try this code for graph.html.twig:

<!DOCTYPE html>
<html>
<head>
    {% block stylesheets %}
        {{ encore_entry_link_tags('app') }}
    {% endblock %}

    {% block javascripts %}
        {{ encore_entry_script_tags('app') }}
    {% endblock %}
</head>
<body>
{{ render_chart(chart) }}
</body>
</html>

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:

composer require symfony/ux-chartjs
composer require symfony/webpack-encore-bundle
yarn install
yarn encore dev
Sign up to request clarification or add additional context in comments.

2 Comments

OK so actually the issue was in the webpack. config.js The path in setPublicPath was absolute .setPublicPath('/public/build') I changed to the full path .setPublicPath('/symfonyapp/public/build') But this is ridiculous. I should not have to do that. It should get the full path by itself..
From my webpack.config.js: ``` .setOutputPath('public/build/') .setPublicPath('/build')```
1

I Had the same problem.

  1. Install Stimulus : composer require symfony/stimulus-bundle
  2. Enable Stimulus in webpack.config.js
// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
.enableStimulusBridge('./assets/controllers.json')
  1. Import Stimulus in assets/app.js
// start the Stimulus application
import './bootstrap';
  1. Import Stimulus in assets/bootstrap.js
import { startStimulusApp } from '@symfony/stimulus-bridge';

// Registers Stimulus controllers from controllers.json and in the controllers/ directory
export const app = startStimulusApp(require.context(
    '@symfony/stimulus-bridge/lazy-controller-loader!./controllers',
    true,
    /\.[jt]sx?$/
));
  1. npm run watch

Let me know if it works.

Comments

0

I had the exact same issue.

I tried every comments on this page and it was still not working. So in addition I run yarn add chartjs in my terminal and it worked.

I am not a pro so I don't know if it is really clean but at least my problem is gone. Maybe yours will be too

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

The problem is apparently @hotwired/stimulus ^3.0. It may be required directly in package.json or e.g. by the new version of stimulus-use. I removed it from package.json and updated the stimulus-use: ^0.24.0-1. After yarn install --force everything works as it should.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.