Chart.js无法创建图表

时间:2018-05-03 10:54:56

标签: javascript html chart.js

我试图使用Chart.js创建一个简单的条形图,但每次我尝试渲染它时,都会给我以下错误:无法创建图表:无法从中获取上下文给定项目。



  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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您必须添加一个canvas元素作为图表的基础。将html更改为:

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

答案 1 :(得分:1)

请使用下面的画布代替div。

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