user6004404
user6004404

Reputation:

Chart.js unable to create chart

I'm trying to create a simple bar chart using Chart.js, but every time I try to render it, it give me the following error: Failed to create chart: can't acquire context from the given item.

  var hashtags = ["activewear", "adidas", "aloyoga", "batterseapark", "outdoors", "park", "training", "winter", "workout", "workoutwednesday"]
var avg_likes = [1185, 5311, 5521, 1713, 949, 321, 2860, 2661, 18899, 8108]

var chart = document.getElementById("chart");
var myBarChart = new Chart(chart, {
  type: 'bar',
  data: {
    labels: hashtags,
    datasets: [{
      data: avg_likes
    }]
  },
  options: {}
}); 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

<div id="chart"></div>

Upvotes: 1

Views: 4183

Answers (2)

PPL
PPL

Reputation: 6565

Instead of div please use canvas like below.

<canvas id="chart"></canvas>

Upvotes: 1

Aniko Litvanyi
Aniko Litvanyi

Reputation: 2149

You have to add a canvas element as the base of the chart. Change the html to:

<div class="chart-container">
   <canvas id="chart"></canvas>
</div>

Upvotes: 1

Related Questions