gpt4 book ai didi

d3.js - C3 - 如何在圆环图中居中文本?

转载 作者:行者123 更新时间:2023-12-04 16:12:13 26 4
gpt4 key购买 nike

我有 C3 圆环图的问题 - 它没有在图表内居中文本:
http://jsfiddle.net/aspirisen/q8h39/83/

c3.generate({
legend: {
show: false,
position: 'right'
},
data: {
columns: [
['data1', 50],
['data2', 50],
],
type: 'donut',
onclick: function (d, i) { console.log("onclick", d, i); },
onmouseover: function (d, i) { console.log("onmouseover", d, i); },
onmouseout: function (d, i) { console.log("onmouseout", d, i); }
},
tooltip: {
show: false
},
donut: {
width: 26,
label: {
format: function (value, ratio, id) {
return (ratio * 100).toFixed(0).toString()
}
}
}
});

enter image description here

怎么解决这个问题?

谢谢!

最佳答案

咆哮警告 -- 这就是为什么“助手”库建立在 d3 之上的原因太烦我了。直接创建此圆环图 d3大约是相同数量的代码,你可以绝对控制这样的事情。

无论如何,您可以通过在 c3 绘制并重新定位文本后重新计算质心来解决此问题(调整半径 r 以移动标签):

// find all the labels
d3.selectAll(".c3-chart-arc>text")
.attr("transform", function(d,i){
var r = 30, //<-- adjust this to move the labels
a = (d.startAngle + d.endAngle) / 2 - (Math.PI / 2);
// compute the new centroid
return "translate(" + (Math.cos(a) * r) + "," + (Math.sin(a) * r) + ")";
});

完整代码:

c3.generate({
legend: {
show: false,
position: 'right'
},
data: {
columns: [
['data1', 32],
['data2', 50],
],
type: 'donut',
onclick: function (d, i) { console.log("onclick", d, i); },
onmouseover: function (d, i) { console.log("onmouseover", d, i); },
onmouseout: function (d, i) { console.log("onmouseout", d, i); }
},
tooltip: {
show: false
},
donut: {
width: 26,
label: {
format: function (value, ratio, id) {
return (ratio * 100).toFixed(0).toString()
}
}
}
});

setTimeout(function(){
d3.selectAll(".c3-chart-arc>text")
.attr("transform", function(d,i){
var r = 30,
a = (d.startAngle + d.endAngle) / 2 - (Math.PI / 2);
console.log("translate(" + (Math.cos(a) * r) + "," + (Math.sin(a) * r) + ")")
return "translate(" + (Math.cos(a) * r) + "," + (Math.sin(a) * r) + ")";
});
}, 100);
#chart {
width: 100px;
height: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.js"></script>
<div id="chart"></div>

关于d3.js - C3 - 如何在圆环图中居中文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34964887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com