Chartjs-Chart.js – Uncaught ReferenceError: chart is not defined

0👍

There are two things that are wrong:

  1. classes should have a capital letter.
    new chart(mychart, { -> new Chart

  2. The library from the CDN doesn’t seem to return Chart. Try the one they recommend in their documentation:
    https://www.chartjs.org/docs/latest/getting-started/

Here is a working example:

<div id="billed_sample_today">1</div>
<div id="bleeded_sample_today">2</div>
<div id="completed_sample_today">3</div>
<div id="pending_sample_today">4</div>

<canvas id="myPieChart"></canvas>

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3"></script>

<script>
  var mychart = document.getElementById("myPieChart").getContext('2d');
  let round_graph = new Chart(mychart, {
    type: 'doughnut',
    data: {
      labels: ['Billed Samples (Today)', 'Collected Samples (Today)', 'Completed Test (Today)', 'Pending For Validation'],
      datasets: [{
        lable: 'Samples',
        data: [
          document.getElementById('billed_sample_today').innerHTML,
          document.getElementById('bleeded_sample_today').innerHTML,
          document.getElementById('completed_sample_today').innerHTML,
          document.getElementById('pending_sample_today').innerHTML
        ],
        backgroundColor: ['#4e73df', '#1cc88a', '#36b9cc'],
        hoverBackgroundColor: ['#2e59d9', '#17a673', '#2c9faf'],
        hoverBorderColor: "rgba(234, 236, 244, 1)",
      }]
    }

  })

</script>

0👍

There is issue with version 2.9.3. Version 2.7.3 is working.

Same time you have to call new Chart not new chart

window.onload = () => {
var mychart = document.getElementById("myPieChart").getContext('2d');
let round_graph = new Chart(mychart, {
type:'doughnut',
data:{
  labels:['Billed Samples (Today)','Collected Samples (Today)','Completed Test (Today)','Pending For Validation'],
  datasets:[{
    lable:'Samples',
    data :[
      document.getElementById('billed_sample_today').innerHTML,
      document.getElementById('bleeded_sample_today').innerHTML,
      document.getElementById('completed_sample_today').innerHTML,
      document.getElementById('pending_sample_today').innerHTML
    ],
    backgroundColor: ['#4e73df', '#1cc88a', '#36b9cc'],
      hoverBackgroundColor: ['#2e59d9', '#17a673', '#2c9faf'],
      hoverBorderColor: "rgba(234, 236, 244, 1)",
  }]
}

})

}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<canvas id="myPieChart" ></canvas>

Leave a comment