I'm currently working on finding a way to display additional text on a pie chart using mouseover
in addition to just the data bound to the pie. Here is the code I have implemented:
function Pie(value,names){
svg.selectAll("g.arc").remove()
var outerRadius = 100;
var innerRadius = 0;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie();
var color = d3.scale.category10();
var arcs = svg.selectAll("g.arc")
.data(pie(value))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(950,80)");
arcs.append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc)
.on("mouseover",function(d,i) {
arcs.append("text")
.attr("dy", ".5em")
.style("text-anchor", "middle")
.style("fill", function(d,i){return "black";})
.text(d.data)
})
.on("mouseout", function(d) {
arcs.select("text").remove();
});}
The names
array has the same length as the value
array, which is provided to the pie. I was hoping that replacing the initial mouseover
with something like this would solve it:
.on("mouseover",function(d,i) {
arcs.append("text")
.attr("dy", ".5em")
.style("text-anchor", "middle")
.style("fill", function(d,i){return "black";})
.text(function(d,i){return (d.data +" " + names[i]);)
})
However, instead of displaying the desired text, it simply stacks all elements of the values
array and shows the last element of the names
array. It appears that i
always corresponds to the last index in this scenario. How can I address this issue? Is there an alternate method for achieving the intended text display? Your assistance is greatly appreciated.