Chapters
46 Chapters
|
4:55:39
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
23.
Symfony UX & Chart.js
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial works perfectly with Stimulus 3!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "1.11.99.1", // 1.11.99.1
"doctrine/annotations": "^1.0", // 1.11.1
"doctrine/doctrine-bundle": "^2.2", // 2.2.3
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.8", // 2.8.1
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"sensio/framework-extra-bundle": "^5.6", // v5.6.1
"symfony/asset": "5.2.*", // v5.2.3
"symfony/console": "5.2.*", // v5.2.3
"symfony/dotenv": "5.2.*", // v5.2.3
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/form": "5.2.*", // v5.2.3
"symfony/framework-bundle": "5.2.*", // v5.2.3
"symfony/property-access": "5.2.*", // v5.2.3
"symfony/property-info": "5.2.*", // v5.2.3
"symfony/proxy-manager-bridge": "5.2.*", // v5.2.3
"symfony/security-bundle": "5.2.*", // v5.2.3
"symfony/serializer": "5.2.*", // v5.2.3
"symfony/twig-bundle": "5.2.*", // v5.2.3
"symfony/ux-chartjs": "^1.1", // v1.2.0
"symfony/validator": "5.2.*", // v5.2.3
"symfony/webpack-encore-bundle": "^1.9", // v1.11.1
"symfony/yaml": "5.2.*", // v5.2.3
"twig/extra-bundle": "^2.12|^3.0", // v3.2.1
"twig/intl-extra": "^3.2", // v3.2.1
"twig/twig": "^2.12|^3.0" // v3.2.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.0
"symfony/debug-bundle": "^5.2", // v5.2.3
"symfony/maker-bundle": "^1.27", // v1.30.0
"symfony/monolog-bundle": "^3.0", // v3.6.0
"symfony/stopwatch": "^5.2", // v5.2.3
"symfony/var-dumper": "^5.2", // v5.2.3
"symfony/web-profiler-bundle": "^5.2" // v5.2.3
}
}
What JavaScript libraries does this tutorial use?
// package.json
{
"devDependencies": {
"@babel/preset-react": "^7.0.0", // 7.12.13
"@popperjs/core": "^2.9.1", // 2.9.1
"@symfony/stimulus-bridge": "^2.0.0", // 2.1.0
"@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets", // 1.1.0
"@symfony/webpack-encore": "^1.0.0", // 1.0.4
"bootstrap": "^5.0.0-beta2", // 5.0.0-beta2
"core-js": "^3.0.0", // 3.8.3
"jquery": "^3.6.0", // 3.6.0
"react": "^17.0.1", // 17.0.1
"react-dom": "^17.0.1", // 17.0.1
"regenerator-runtime": "^0.13.2", // 0.13.7
"stimulus": "^2.0.0", // 2.0.0
"stimulus-autocomplete": "^2.0.1-phylor-6095f2a9", // 2.0.1-phylor-6095f2a9
"stimulus-use": "^0.24.0-1", // 0.24.0-1
"sweetalert2": "^10.13.0", // 10.14.0
"webpack-bundle-analyzer": "^4.4.0", // 4.4.0
"webpack-notifier": "^1.6.0" // 1.13.0
}
}
21 Comments
I like the idea of being able to set the chart up from PHP. However, I'm really not a fan of configuring colors from PHP. It is clearly not the place for that. In a perfect world, I would be able to configure that with CSS variables but I didn't find a way to do that. So I'm falling back to plugins, so I've found the chartjs-plugin-colorschemes but registering it seems to be next to impossible with ux-chartjs. Any idea?
cheers!
Hey @julien_bonnier!
That opinion makes sense to me :). For registering plugins, this part must be done in js. It is possible and not too bad though I wish it were even easier. We have a spot in the docs about this, hopefully it clears things up. But let me know one way or another https://symfony.com/bundles/ux-chartjs/current/index.html#using-plugins
Cheers!
Hi!
I am using yarn 3.6.3, and I got stuck when installing the chartjs package.
I used
npm install --forceand it went through, but it did not copy the chartjs package to node_modules. Instead, it added an empty file, that appears to be a link:What should I do? The chart actually works.
PS. I've edited this comment a couple times to provide more info
Hey @escobarcampos ,
That's OK that you have a link, that points to the chart JS version from the "symfony/ux-chartjs" PHP package, it's done this way to do not have mismatch version between the ChartJS lib and symfony/ux-chartjs PHP package, so don't worry to much about that internal symlink, as soon as the chart works for you - everything is great :)
What about
yarn install --force- yeah, seems the new version of Yarn does not have that option. Not sure what exactly you should use, probablyyarn installshould work well there even without--force... so you can keep using Yarn by simply runningyarn install... and if something is not working as expected - try torm -rf node_modulesand thenyarn installagain, it should do the trick :)Cheers!
Nube Question:
Symfony 6
Charts working
_onConnect(event)worksevent.detail.chart.options.onClick = (mouseEvent) => {}works when you click the chart areaHow do I listen to an input tag in the twig and update the chart when the value changes?
Hey Richard!
Nah, it's a pretty reasonable questions ;).
First, nice job setting up the JavaScript to hook into the chart!
Here is what I would do:
A) Add a
stimulus_action()to the input element - make it call, for example, someupdateChart()method on your controller.B) Inside of
_onConnect(event)(for reference for others, you correctly got that from the documentation - https://symfony.com/bundles/ux-chartjs/current/index.html#extend-the-default-behavior - set theChartobject on a property on your Stimulus controller:C) Finally, inside of the
updateChart()method, you can use thethis.chartobject and any other info you need to update the chart:I'm assuming that you want to update the Chart object in JavaScript by hand. Another thing you could do is make an Ajax call to some Symfony controller that returns a new, fully-rendered Chart, which you could then put onto the page to render a brand new chart.
Cheers!
Hi,
Despite installing version 1.2.0 I can't get rid of this error:
<br />"./node_modules/@symfony/ux-chartjs/dist/controller.js" contains a reference to the file "stimulus".<br />This file can not be found, please check it for typos or update it if the file got moved.<br />It is obvious that it originates on this line
var _stimulus = require("stimulus");from the dist version orimport { Controller } from 'stimulus';from the src one.Maybe I need to updated to
@hotwired/stimulusbut no sure how to get it rebuild<b>Edit</b>
Changed dist version to
var _stimulus = require("@hotwired/stimulus");and the error is gone... too hacky for my tasteHey danresmejia!
Hmm, well that's no fun! So, if you're using Version 3 of Stimulus (the one where the package name is "@hotwired/stimulus" and not "stimulus"), then you need version 2.0 (or greater) of symfony/ux-chartjs. Really, the only difference between the 1.x and 2.x of the UX packages is that 2.x starts importing @hotwired/stimulus (instead of stimulus).
Hope that clarifies!
Cheers!
If you have a problem with "has unmet peer dependency" or undefined package stimulus, it's because you are using stimulus v3 but by installing "composer require symfony/ux-chartjs:1.2.0" you get bundle with package reference to 'stimulus' (v2) inside vendors
So you need to install "symfony/ux-chartjs" v2+, and before it you must update symfony/flex package (composer update symfony/flex) otherwise composer will throw an error
Hey @Hainovsky,
Thanks for sharing detailed explanation. BTW exact this installation string is mentioned in course code and also there is video note ;)
Cheers!
Hi,
Does symfony ux Chartsjs work in easyadmin3 dashboard?
Hey Emilio,
Sure, it should work! With EasyAdminBundle v3 you can add Webpack Encore entries, that's exactly what you need instead of static addJsFile() method calls. You just need to import the ChartJS code in the Webpack Encore entry and write the implementation of it.
Cheers!
Sorry, I was referring to symfony / ux-chartjs. Can it be integrated into the EasyAdmin 3v dashboard?
Yes, i was integrate it yesterday
1) need rewrite layout.html.twig
1.1) create folder templates/bundles/Easyadmin/
1.2) create in this folder layout.html.twig
1.3) copy intire code from vendor/...../views/layout.html.twig yo your 1.2)
1.4) add stymulus to css and to js like a encore_entry_link_tags('app')
1.5) yarn watch
1.6) create own controller in admin/Dashboard with php code from symfony/ux and render 1.7
1.7) add your page with chart to template like in body {{ render_chart(chart) }}
1.8) enjoy your work
I do not know if it is allowed by symfonycasts rules, but i can create tutorial and make repository and put here link.
RYAN should allow me to do it.
Thanks,
What I did was add a new JS (called dashboard.js inside import './bootstrap';) in webpack.config.js and then in DashboardController.php (EasyAdmin) add the assets with -> addWebpackEncoreEntry ('dashboard'). [in configureAssets() method]
It worked!.
Not override easyadmin's layout.html, but I tried that method and it worked too.
Thanks! Also worked for me!
1) I've created <b>dashboard.js</b> in <b>/assets/scripts/</b> with content:
import '../bootstrap';Used ".." cause of my folder structure.
2) Injected <b>dashboard.js</b> into <b>webpack.config.js</b> file:
.addEntry('dashboard', './assets/scripts/dashboard.js')3) Overrided <b>configureAssets</b> method in DashboardController class:
Hey Dmitriy,
Thank you for sharing your solution and confirming it works for you too!
Cheers!
Hey Emilio,
Great! I'm happy it worked for you. Thanks for sharing your solution with others!
Cheers!
How to create transparent background on chart Chart::TYPE_LINE? Removed this backgroundColor, nothing happens, light grey background exist....
Solved - 'backgroundColor' => 'rgba(255, 255, 255, 0)', Last parameter is transparency. Thanks to all
Hey Maxim,
You're right, good catch! I'm glad you were able to solve this by yourself
Cheers!
"Houston: no signs of life"
Start the conversation!