charts.js分层甜甜饼饼图

时间:2016-08-05 11:10:17

标签: javascript chart.js

我正在尝试使用chart.js制作分层圆环图。我有2层工作,但我无法弄清楚为什么第三层没有显示。

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

感谢您的帮助

<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

1 个答案:

答案 0 :(得分:3)

您在最后一个图表中请求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
});