我试图通过在同一 Canvas 上多次初始化 Chart.js 来模拟多层圆环图。当时只有一张图表可见。当您将鼠标悬停在其位置上时,另一个将可见……
有人知道如何同时显示两者吗?
这是我的代码:
<!doctype html>
<html>
<head>
<title>Doughnut Chart</title>
<script src="../Chart.js"></script>
<style>
body{
padding: 0;
margin: 0;
}
#canvas-holder-1{
position: fixed;
top: 50%;
left: 50%;
margin-top: -250px;
margin-left: -250px;
}
</style>
</head>
<body>
<div id="canvas-holder-1">
<canvas id="chart-area" width="500" height="500"/>
</div>
<script>
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"
}
];
window.onload = function(){
var ctx = document.getElementById("chart-area").getContext("2d");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {
responsive : false,
animateScale: false,
animateRotate:false,
animationEasing : "easeOutSine",
animationSteps : 80,
percentageInnerCutout : 90,
});
myDoughnut.outerRadius= 200;
window.myDoughnut2 = new Chart(ctx).Doughnut(doughnutData2, {
responsive : false,
animateScale: false,
animateRotate:false,
animationEasing : "easeOutSine",
animationSteps : 80,
percentageInnerCutout : 90
});
};
</script>
</body>
</html>
谢谢, 乔臣
最佳答案
有 2 个 Canvas 元素并使用 CSS 将它们一个放在另一个上。内部 Canvas 有内部 donut 和圆形边框,因此它尽可能少地覆盖外部 donut 。
预览
HTML
<div id="w">
<canvas id="d1" height="400" width="400"></canvas>
<canvas id="d2" height="320" width="320"></canvas>
</div>
CSS
#w {
position: relative;
height: 400px;
width: 400px;
}
#d1, #d2 {
position: absolute;
}
#d1 {
top: 0px;
left: 0px;
}
#d2 {
border-radius: 150px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
fiddle - https://jsfiddle.net/rhLxawt5/
在这一点上,我们有一个看起来相当不错的饼图,其中没有重叠的 donut ,但是我们的工具提示将被剪掉,因为它们仍然是各自 Canvas 的一部分。
我们可以通过编写一个 customTooltips 函数来使用 div 而不是在 Canvas 中绘制一个矩形来解决这个问题。
fiddle - https://jsfiddle.net/rca0ync3/
关于javascript - Chart.js - 同一 Canvas 上的多个圆环图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30866844/