1

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>

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of div please use canvas like below.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.