[英]How to show custom label on pie chart using c3js?
I want to show custom label on pie chart as shown in image by using C3.js
. 我想通过使用
C3.js
在饼图上显示自定义标签,如图所示。
I tried to change pie chart label with format: {...}
function. 我尝试使用以下
format: {...}
更改饼图标签format: {...}
函数。 But it doesn't work. 但这是行不通的。
Here is what I tried, 这是我尝试过的
var charThree = c3.generate({
bindto: "#chartThree",
size: {
width: 500,
height: 300
},
data: {
colors: {
A: 'yellow',
B: 'red',
C: 'green',
D: 'orange',
E: 'blue'
},
columns: [
['A',20],
['B',40],
['C',20],
['D',10],
['E',9]
],
type: 'pie'
},
pie: {
labels: {
show: true,
threshold: 0.1,
format: {
A: function (value, ratio, id) {
if(value=20) {
return "A<br/>9item<br/>20.2%";
}
}
}
}
}
});
I think, I need to used some of d3js
code. 我认为,我需要使用一些
d3js
代码。 But I'm not familiar with d3js
. 但是我对
d3js
不熟悉。
I'm very appreciate for any suggestion. 我非常感谢您的任何建议。
This is a little quick and dirty
but it gets the job done... 这有点
quick and dirty
但是可以完成工作...
I'm saving the data as a comma separated list in the pie.label.format
function, since it's not possible to display html here (to my knowledge): 我将数据保存为
pie.label.format
函数中以逗号分隔的列表,因为据我所知无法在此处显示html:
pie: {
label: {
threshold: 0.1,
format: function(value, ratio, id) {
ratio = d3.format("%")(ratio); // format ratio
return [id, value, ratio].join(); // used to pass values to the onrender function
}
}
},
And the actual formatting in onrendered
: 并在
onrendered
的实际格式为:
onrendered: function() {
d3.selectAll(".c3-chart-arc text").each(function(v) {
var label = d3.select(this);
var data = label[0][0].innerHTML.split(',');
var id = data[0];
var value = data[1];
var perc = data[2];
d3.select(this).text("")
.append("tspan")
.text(id)
.attr("dy", 0)
.attr("x", 0)
.attr("text-anchor", "middle").append("tspan")
.text(parseInt(value) / 4 + " item")
.attr("dy", "1.2em")
.attr("x", 0)
.attr("text-anchor", "middle")
.append("tspan")
.text(perc)
.attr("dy", "1.2em")
.attr("x", 0)
.attr("text-anchor", "middle");
});
}
});
var chart = c3.generate({ bindto: "#chart", size: { width: 500, height: 300 }, data: { colors: { A: 'yellow', B: 'red', C: 'green', D: 'orange', E: 'blue' }, columns: [ ['A', 20], ['B', 40], ['C', 20], ['D', 10], ['E', 10] ], type: 'pie' }, pie: { label: { threshold: 0.1, format: function(value, ratio, id) { ratio = d3.format("%")(ratio); // format ratio return [id, value, ratio].join(); // used to pass values to the onrender function } } }, onrendered: function() { d3.selectAll(".c3-chart-arc text").each(function(v) { var label = d3.select(this); var data = label[0][0].innerHTML.split(','); var id = data[0]; var value = data[1]; var perc = data[2]; d3.select(this).text("") .append("tspan") .text(id) .attr("dy", 0) .attr("x", 0) .attr("text-anchor", "middle").append("tspan") .text(parseInt(value) / 4 + " item") .attr("dy", "1.2em") .attr("x", 0) .attr("text-anchor", "middle") .append("tspan") .text(perc) .attr("dy", "1.2em") .attr("x", 0) .attr("text-anchor", "middle"); }); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.12/d3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.min.css"/> <div id="chart"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.