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!
25.
Extending a UX Controller
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
}
}
13 Comments
hello,
For chartjs 3, 'beginAtZero' have to be at the same level as 'ticks', and not in ticks.
For example, this code is OK :
Thanks to the author for this tutorial. the explanations are clear and detailed. very good work.
Hey Yanubik,
Thanks for sharing this top with others regarding the new v3 version. I also see that the latest ChartJS version is v4 now, but seems it should work the same way as in v3.
Cheers!
Hello there,
I want to update a chart with a select field value. Therefore, I'd like to dispatch a custom event.
But unfortunately the event isn't recognized by the controller.
Here is some code:
The controller that fires the event doesn't surround the chart controller, but I guess this isn't needed.
Can you please help me? Thanks in advance.
Hey @Klaus-M!
Hmm. So, when a
chart:updateevent is dispatched, this will execute theupdateChart()method on your customdashboard-chartcontroller. Question: who/what dispatches thischart:updateevent? It sounds like it's a different custom controller?Whatever fires the
chart:updateevent must be inside the chart element... which actually isn't possible (because it's not possible to put anything inside of the element thatrender_chart()renders). This is because events "bubble up": you trigger an event (e.g.chart:update) on some element, and it goes up the element tree one-by-one looking for listeners. In this case, when it bubbles "up", this chart element is not a parent/ancestor, so that event never reaches this element.Here's what I would do:
A) Remove the
data-controllerstuff fromrender_chart(). Instead, find some HTML element that surrounds both your chart and wherever yourchart:updateis fired from. Add thedashboard-chartcontroller to that element via{{ stimulus_controller() }}(and also move thedata-actionthere to viastimulus_action().B) And... that should be it! My guess is that you are already listening to the
chartjs:connectevent from inside ofdashboard-chartcontroller so that you can gain access to theChartinstance. Even though yourdashboard-chartcontroller is now a parent/ancestor of the "chart" element, yourchartjs:connectlistener will still be called ondashboad-chart(the core chartjs UX controller dispatches this event, then it will "bubble up" eventually to the element that holds your custom controller).Let me know if this helps!
Cheers!
Not sure what changed in the configuration of Chart.js or SymfonyUX/ChartJS, but 'yAxes' params are now simple array only. Otherwise they are not working and throwing error 'Invalid scale configuration for scale: yAxes' in console . But the chart itself will still work.
For example to see if it is working
['yAxes' => ['ticks' => ['color' => 'blue']]]Hey Peter L.!
Sorry for the slow reply. Yes, that changed in chart.js 3... and you're right that it looks like I completely forgot to add a note about that. I'll do that!
Thanks!
Do you have example how we can add Chart to Modal and this chart will have datetime setting - range ?
Hey Mepcuk!
Hmm. Let me answer that in 2 parts:
For this part, you need to find the exact Chart.js config that you need to tweak from their docs... then update that data when you create the chart (similar to what we do here): https://symfonycasts.com/screencast/stimulus/extend-chartjs#codeblock-f48ad9bdb1
For this part, later in this tutorial, we discuss opening a modal and AJAX-loading in an HTML form - https://symfonycasts.com/screencast/stimulus/modal-form
In your situation, you would do much of the same thing. The difference would be that instead of returning an HTML form from your AJAX endpoint, you would return a template that basically has this in it:
The magic of Stimulus takes over from there: as soon as this HTML appears on the modal, the chart UX controller would take care of the rest.
If you wanted the "datetime" from the first part of your question to be dynamic (e.g. maybe the user selects a datetime from a field and then click "show chart"), then when you make the AJAX call to the "chart" endpoint, you would send that up as an extra field - e.g. to an endpoint like /sales/chart?startDate=2020-01-01 (were the 2020-01-01 is a value you read from some date field).
Let me know if that helps!
Cheers!
Hi Ryan!
I'm very interested in this point (dynamic date range on a chart) but still struggle to do it. I built a stimulus controller that handles the chart, I'm abble to get the dates set by the user from this controller but still have to get datas from my database in ordrer to rebuilt the chart. In first place from my symfony controller, I call a service that get back datas but from my stimulus controller I'm definitively lost!
Could you give some help please?
Cheers!
Hey Dimitri,
I think you have two options:
A) Pass the dates to your Stimulus controller in the traditional way. Fetch the data from the database from a Symfony controller action, and pass it to a Twig template, and the template will pass the dates to Stimulus as a value https://stimulus.hotwired.dev/reference/values
B) Fetch the dates from your Stimulus controller by making an API call
I hope it helps. Cheers!
Hi MolloKhan!
Thanks very much for your time and your tips.
I'm gonna dig around the API call!
Cheers!
I think I found an easter egg for those lazy copy&pasters...
In the video you set the new value to 30 while in the code block below it is set to 3000 making the transition somewhat "shocking" ;)
Hey elkuku
Lol - that's hilarious! That's why you should not copy-paste blindly... but seriously, that's our fault. I'll fix that
Thanks for letting us know. Cheers!
"Houston: no signs of life"
Start the conversation!