charts.js分层甜甜圈饼图

charts.js layered donut pie chart

本文关键字:甜甜圈 分层 js charts      更新时间:2023-09-26

我正在尝试使用chart.js制作一个分层的圆环图。我已经用了两层,但我不明白为什么第三层没有显示。

为什么第三个甜甜圈没有显示?

感谢您的帮助

<div id="w">
<canvas id="d1" height="600" width="600"></canvas>
<canvas id="d2" height="500" width="500"></canvas>
<canvas id="d3" height="400" width="400" ></canvas>

#w {
    position: relative;
    height: 600px;
    width: 600px;
}
#d1, #d2, #d3 {
    position: absolute;
}
#d3 {
  position: absolute;
}
#d1 {
    top: 0px;
    left: 0px;
}
#d2 {
    top: 8%;
    left: 8%;
}
#d3 {
    top: 11%;
    left: 11%;
}
var doughnutData = [
                {
                    value: 20,
                    color:"#F7464A",
                    highlight: "#FF5A5E",
                    label: "Red",
                },
                {
                    value: 50,
                    color: "#46BFBD",
                    highlight: "#5AD3D1",
                    label: "Green"
                },
                {
                    value: 30,
                    color: "#FDB45C",
                    highlight: "#FFC870",
                    label: "Yellow"
                },
                {
                    value: 40,
                    color: "#949FB1",
                    highlight: "#A8B3C5",
                    label: "Grey"
                },
                {
                    value: 120,
                    color: "#4D5360",
                    highlight: "#616774",
                    label: "Dark Grey"
                }
            ];
var doughnutData2 = [
                {
                    value: 10,
                    color:"#F7464A",
                    highlight: "#FF5A5E",
                    label: "Red",
                },
                {
                    value: 100,
                    color: "#46BFBD",
                    highlight: "#5AD3D1",
                    label: "Green"
                },
                {
                    value: 20,
                    color: "#FDB45C",
                    highlight: "#FFC870",
                    label: "Yellow"
                },
                {
                    value: 60,
                    color: "#949FB1",
                    highlight: "#A8B3C5",
                    label: "Grey"
                },
                {
                    value: 120,
                    color: "#4D5360",
                    highlight: "#616774",
                    label: "Dark Grey"
                }
            ];
var doughnutData3 = [
                {
                    value: 20,
                    color:"#F7464A",
                    highlight: "#FF5A5E",
                    label: "Red",
                },
                {
                    value: 90,
                    color: "#46BFBD",
                    highlight: "#5AD3D1",
                    label: "Green"
                },
                {
                    value: 20,
                    color: "#FDB45C",
                    highlight: "#FFC870",
                    label: "Yellow"
                },
                {
                    value: 60,
                    color: "#949FB1",
                    highlight: "#A8B3C5",
                    label: "Grey"
                },
                {
                    value: 120,
                    color: "#4D5360",
                    highlight: "#616774",
                    label: "Dark Grey"
                }
            ];

var ctx1 = $("#d1").get(0).getContext("2d");
var myChart1 = new Chart(ctx1).Doughnut(doughnutData, {
    percentageInnerCutout: 90
});
var ctx2 = $("#d2").get(0).getContext("2d");
var myChart2 = new Chart(ctx2).Doughnut(doughnutData2,  {
    percentageInnerCutout: 90
});
var ctx3 = $("#d3").get(0).getContext("3d");
var myChart3 = new Chart(ctx3).Doughnut(doughnutData3,  {
    percentageInnerCutout: 90
});

请参阅此处了解CodePen

您请求的是3d上下文,而不是最后一个图表中的2d上下文。所以改变这个

var ctx3 = $("#d3").get(0).getContext("3d");
var myChart3 = new Chart(ctx3).Doughnut(doughnutData3,  {
    percentageInnerCutout: 90
});  

到这个

var ctx3 = $("#d3").get(0).getContext("2d");
var myChart3 = new Chart(ctx3).Doughnut(doughnutData3,  {
    percentageInnerCutout: 90
});