I am facing a problem trying to position text inside the wedges of a Sunburst chart which is based on d3.js.The text
elements seem to be not positioned as desired even on zooming..
Here is the brief snippet of the code that i tried, but unsuccessfully :
var slices = svg.selectAll(".form")
.data(function(d) { return data_slices; })
.enter()
.append("g");
slices.append("path")
.attr("d", arc)
.attr("id",function(d,i){return d[2]+""+i;})
.style("fill", function(d) { return color(d[2]);})
.on("click",animate)
.attr("class","form")
.append("svg:title")
.text(function(d) { return Math.round(d[0]*100)/100 +" , "+ Math.round(d[1]*100)/100; });
//Something needs to change below....
slices.append("text")
.style("font-size", "10px")
.attr("x", function(d) { return y(d[1]); })
.attr("transform", function(d) { return "rotate(" + this.parentNode.getBBox().width + ")"; })
.attr("dx", "6") // margin
.attr("dy", ".35em") // vertical-align
.text(function(d){return d[2]})
.attr("pointer-events","none");
Here is the Fiddle of the chart
FiddleWhat can be possible problem ? and can anyone please tell me or guide me as to how to position the <text>
inside svg <path>
.Looks like the solution is a minor tweak to this, but i am not able to get to it even after trying for a long time..
Any help/comment in the direction of a solution would be greatly appreciated...Thanks in Advance..
I think this comes close to what you aimed to achieve: http://jsfiddle.net/4PS53/3/
The changes needed are the following:
Now to make it work with zooming, where the reference point changes, we need a bit more infrastructure.
g.form-container
and not only thepath.form
visible/invisible. This means that we do not have to worry about making the labels disappear separately. (I have added theform-container
class.)Calculate the new point and calculate the
centroid
androtation
for it. This is a bit more tricky, but not too difficult:I have added the class
label
to thetext
.Updated Demo: http://jsfiddle.net/4PS53/6/
However, I have argued that there might be better ways of presenting this data, esp. if you are allowing zooming and panning: D3 put arc labels in a Pie Chart if there is enough space
I've made a little improve in musically_ut code.
Now you can change from one data to another.
EDIT: http://jsfiddle.net/k1031ogo/3/
(code could be cleaner, too much copy/paste)