我开始使用以下示例:
http://jsfiddle.net/nrabinowitz/GQDUS/
我试图让每个弧的标签成为弧的颜色。
我已经把它弄到了所有标签颜色相同的地方。但我现在知道如何访问每个标签并更改颜色。
在我的代码中,我在最后一行完成了以下操作:
arcs.append("svg:text").attr("transform", function (d){var c = arc.centroid(d); x = c[0]; y = c[1]; h = Math.sqrt(x*x + y*y); return "translate(" + (x/h * 100) + ',' + (y/h * 90) + ")";}).text(function(d){return Math.round((d.data/total)*100)+"%";}).attr("text-anchor","middle").attr("fill","color_data.pop()");
这使得所有标签成为我阵列中的第一种颜色。但是我需要每个标签在数组中是不同的颜色。我只是不确定如何访问标签,以便我可以循环并更改颜色。
答案 0 :(得分:5)
只需添加用于弧的相同填充参数,例如
arcs.append("svg:text")
.attr("transform", function(d) {
var c = arc.centroid(d),
x = c[0],
y = c[1],
// pythagorean theorem for hypotenuse
h = Math.sqrt(x*x + y*y);
return "translate(" + (x/h * labelr) + ',' +
(y/h * labelr) + ")";
})
.attr("dy", ".35em")
.attr("fill", function(d, i) { return color(i); })
.attr("text-anchor", function(d) {
// are we past the center?
return (d.endAngle + d.startAngle)/2 > Math.PI ?
"end" : "start";
})
.text(function(d, i) { return d.value.toFixed(2); });