Chartjs-Chart.js error: Bar is not a function

3👍

This is because you are using old syntax (used in ChartJS 1.x) for creating your chart, and you might be using the latest version (2.7.0) of ChartJS library.

In latest version of ChartJS, you should create your chart as follows :

var mychart = document.getElementById("chart").getContext("2d");

new Chart(mychart, {
   type: 'bar',
   data: {
      labels: [{% for item in labels %}
                    "{{item}}",
               {% endfor %}],
      datasets: [{
         data: [{% for item in values %}
                    {{item}}, 
                {% endfor %}],
         backgroundColor: 'rgba(151,187,205,0.2)',
         borderColor: 'rgba(151,187,205,1)',
         pointBackgroundColor: 'rgba(151,187,205,1)'
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 10
            }
         }]
      }
   }
});

Refer to the official documentation to learn more.

Leave a comment