0👍
Your script is running before the dom is fully build.
This means that the canvas elements don’t exist yet so chart.js tries to get the context of something that is not available.
To resolve this you need to make sure the script runs after your dom is fully loaded.
There are multiple ways to do this as described here in this so answer.
From linked answer:
These solutions will work:
As mentioned in comments use defer:
<script src="deferMe.js" defer></script>
or
<body onload="script();">
or
document.onload = function ...
or even
window.onload = function ...
- Chartjs-Trying to change background color in chart js filter
- Chartjs-Chart.js – creating time series freqency chart with JSON data
Source:stackexchange.com