Chartjs-How to add Chart.js to Wagtail Homepage panels using construct_homepage_panels hook?

1๐Ÿ‘

โœ…

I have not run this code locally but just a quick suggestion โ€“ you may need to wait until the DOM is loaded before running the getElementById and the new Chart part.

This is because the document query will return nothing until the page is fully rendered.

<script tyle="text/javascript">
// note: you can set up your stars/frameworks and other data outside of the callback below
// the key thing is that the DOM writing and dom query (getElementById) are inside the callback

document.addEventListener('DOMContentLoaded', function() {

   var ctx = document.getElementById("pie_chart");
   var myChart = new Chart( /* ... code here */);

}, false);

</script>

You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event

Here is a SO question also https://stackoverflow.com/a/11936919/8070948

Leave a comment