Matthew Keys
Matthew Keys

Reputation: 47

Chart.js chart not being displayed

I am trying to play around with and test a simple chart.js chart, but no matter what I do I cannot seem to get the chart to display.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <title>My Chart.js Chart</title>
</head>
<body>
  <div class="container">
    <canvas id="myChart" width="600" height="400"></canvas>
  </div>

  <script>
    var myChart = document.getElementByID('myChart').getContext('2d');

    var spiderChart = new Chart(myChart, {
      type: 'bar',
      data: {
        labels:['Happiness', 'Organization', 'Communication', 'Leader'],
        datasets:[{
            label: 'Statistics',
            data: [10, 20, 55, 30]
          }]
      }
    });
  </script>

</body>
</html>

Upvotes: 0

Views: 47

Answers (1)

pouyan
pouyan

Reputation: 3439

Simple Typeo problem:). you should use getElementById instead of getElementByID on document.

working example:

var myChart = document.getElementById('myChart').getContext('2d');  // The problem was here

    var spiderChart = new Chart(myChart, {
      type: 'bar',
      data: {
        labels:['Happiness', 'Organization', 'Communication', 'Leader'],
        datasets:[{
            label: 'Statistics',
            data: [10, 20, 55, 30]
          }]
      }
    });
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <title>My Chart.js Chart</title>
</head>
<body>
  <div class="container">
    <canvas id="myChart" width="600" height="400"></canvas>
  </div>


</body>
</html>

Upvotes: 1

Related Questions