我正在尝试使用 chart.js 制作分层圆环图。我可以使用 2 层,但我无法弄清楚为什么没有显示第三层。
为什么第三个 donut 没有显示?
谢谢你的帮助
<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
});
关于javascript - charts.js 分层 donut 饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787793/