2

我从以下示例开始:

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()");

这使所有标签成为我数组中的第一种颜色。但是我需要每个标签在数组中都是不同的颜色。我只是不确定如何访问标签,所以我可以循环并更改颜色。

4

1 回答 1

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); });
于 2013-09-18T14:30:15.850 回答