I’m trying to use chart.js on a wordpress site. I can get the simple bar chart to work from the documentation, but nothing else. I can’t get a pie or doughnut or line or radar chart to render… even if I’m just copying code from JSFiddle straight to my page.
Please see this page: http://www.sledgeweb.com/2016/05/27/chart-test/
There should be a pie chart and then a bar chart. The bar chart works but the pie chart canvas is blank. What’s going on?
The reason may be that you are using the latest chart.min.js but using the old code. For correct reference, follow the chartjs.org documentation.
The code configuration structure has changed in the latest release. (I guess from 2.3 onwards)
So, instead of
We are structuring like:
or
The problem is your
<script>
tag is pointing to GitHub’s raw text file of the code. Because these files are “raw” they are sent from the server with the headerContent-Type:text/plain; charset=utf-8
andX-Content-Type-Options:nosniff
(see this question) which tells the client-side browser that these are text files and they were not meant to be executable. Certain browsers, such as Chrome, will therefore choke and not allow the JavaScript to be executed. If you change your<script>
tag’ssrc
URL for ChartJS to point to a CDN or somewhere that doesn’t send those headers it should work correctly.You’ve got errors with your JavaScript, you can see them in the console by pressing F12 They are:
Based on that, and investigating those lines of code, your chart data and call do not match whats in the chart.js docs. You can get it working by following the format used in the developer docs. Something like this should work:
Hope that helps!