条形图未与chart.js一起显示。有人可以帮忙吗?代码贴在下面

时间:2017-05-21 03:30:59

标签: javascript

<!DOCTYPE html>
<html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Chart.js demo</title>
  <canvas id="myChart" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.min.js" type="text/javascript"></script>
</head>
<body>
  <h1>Chart.js Sample</h1>
  <script>
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
          type: 'bar',
          data: {
            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
            datasets: [{
              label: '# of Votes',
              data: [12, 19, 3, 5, 2, 3],
              backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
              ],
              borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
              ],
              borderWidth: 1
            }]
          },
  </script>
</body>
</html>

无法使用上述代码显示条形图。有人能帮助解决它的问题吗? 我想使用chart.js库作为显示图表的源 无法使用上面的代码显示条形图。有人能帮助解决它的问题吗? 我正在尝试使用chart.js库作为显示图表的来源

1 个答案:

答案 0 :(得分:0)

您的脚本中没有关闭某些大括号,并且引用的chart.js版本非常旧。当我更正脚本并将chart.js更新为当前版本2.5.0时显示图表。

<head>
  <meta charset="utf-8" />
  <title>Chart.js demo</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js" type="text/javascript"></script>
</head>

<body>
  <h1>Chart.js Sample</h1>
<canvas id="myChart" width="600" height="400"></canvas>

  <script>
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
      type: "bar",
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [
          {
            label: "# of Votes",
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
              "rgba(255, 99, 132, 0.2)",
              "rgba(54, 162, 235, 0.2)",
              "rgba(255, 206, 86, 0.2)",
              "rgba(75, 192, 192, 0.2)",
              "rgba(153, 102, 255, 0.2)",
              "rgba(255, 159, 64, 0.2)"
            ],
            borderColor: [
              "rgba(255,99,132,1)",
              "rgba(54, 162, 235, 1)",
              "rgba(255, 206, 86, 1)",
              "rgba(75, 192, 192, 1)",
              "rgba(153, 102, 255, 1)",
              "rgba(255, 159, 64, 1)"
            ],
            borderWidth: 1
          }
        ]
      }
    });
  </script>
</body>