使用Chart.js显示饼图



我在显示和操纵条形图方面取得了成功,但我不知道如何工作饼图。当我使用检查工具时,我可以看到那里有东西。我正在使用以下代码:Chart.js CDN:

图形的画布容器:从文档中获取的JavaScript:

var ctx = document.getElementById("myChart");       
var myChart = new Chart(ctx,{
    type:"pie",
    data: [3, 4],
    options: {
        animation:{
            animateScale:true
        }
    }
});

Amy建议将受到欢迎。

尝试一下。有关更多信息,请参见饼图的使用。评论是否有混乱

var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
    datasets: [{
      backgroundColor: [
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
      ],
      data: [12, 19, 3, 17, 28, 24, 7]
    }]
  }
});
.container {
  width: 80%;
  margin: 15px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js"></script>
<div class="container">
  <h2>Chart.js — Pie Chart Demo</h2>
  <div>
    <canvas id="myChart"></canvas>
  </div>
</div>

根据文档(Chart.js -pie&amp; Donut图表(,data变量应具有以下结构:

data: {
    labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
    datasets: [{
    backgroundColor: [ /* backgroundColor is optional */
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
    ], 
    data: [12, 19, 3, 17, 28, 24, 7]
}]

}

正如我所看到的,这是您的情况。

相关内容

  • 没有找到相关文章

最新更新