Chartjs-Destroy ChartJS rendered with Symfony

0👍

symfny-ux controller creates its own instance of ChartJS inside an appropriate stimulus-controller, and this variable isn’t directly accessible. But, symfony/ux developers are smart, and they created events (e.g. chartjs:connect) which you can listen to (link)

If you do so, you will end up with a JS native CustomEvent object and in the details property of this event you will find passed chart instance previously created in symfiny-ux-controller for chart.js
So in theory you would be able just to

document.addEventListener("chartjs:connect",(chartEv) => { 
console.log(chartEv.details); // chartEv.details.chart.destroy()
});

I do recommend you to watch Ryan’s tutorial on Symfony UX, especially "Extending a UX Controller" chapter where he tries to work with previously created instance of new Chart() like in your case.

0👍

You can use the getChart api method chart.js exposes on itself.

So when you know the ID of your canvas you can use this js to get the chart object:

const chart = Chart.getChart('my-chart');

Leave a comment