javascript - How to position text labels on a Sunburst chart with d3.js -


i facing problem trying position text inside wedges of sunburst chart based on d3.js.the text elements seem not positioned desired on zooming..

here brief snippet of code tried, 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 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 fiddle of chart

fiddle

what can possible problem ? , can please tell me or guide me how position <text> inside svg <path>.looks solution minor tweak this, not able after trying long time..

any help/comment in direction of solution appreciated...thanks in advance..

i think comes close aimed achieve: http://jsfiddle.net/4ps53/3/

the changes needed following:

function getangle(d) {     // offset angle 90 deg since '0' degree axis arc y axis, while     // text x axis.     var thetadeg = (180 / math.pi * (arc.startangle()(d) + arc.endangle()(d)) / 2 - 90);     // if rotating text more 90 deg, "flip" it.     // why "text-anchor", "middle" important, otherwise, "flip"     // little harder.     return (thetadeg > 90) ? thetadeg - 180 : thetadeg; }  slices.append("text")     .style("font-size", "10px")     .attr("x", function(d) { return d[1]; })      // rotate around center of text, not bottom left corner     .attr("text-anchor", "middle")      // first translate desired point , set rotation      // not sure intent of using this.parentnode.getbbox().width here (?)     .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")" + "rotate(" + getangle(d) + ")"; })                                         .attr("dx", "6") // margin     .attr("dy", ".35em") // vertical-align     .text(function(d){return d[2]})     .attr("pointer-events","none"); 

now make work zooming, reference point changes, need bit more infrastructure.

  1. make g.form-container , not path.form visible/invisible. means not have worry making labels disappear separately. (i have added form-container class.)
  2. calculate new point , calculate centroid , rotation it. bit more tricky, not difficult:

    function change_ref(data_point, reference_point) {     return [         get_start_angle(data_point, reference_point),         get_stop_angle (data_point, reference_point),         data_point[2],         get_level      (data_point, reference_point)     ]; }  // , while doing transitioning `text.label`:  svg.selectall('.label') .filter(     function (b)     {         return b[0] >= new_ref[0] && b[1] <= new_ref[1] && b[3] >= new_ref[3];     } ).transition().duration(1000)  .attr("transform", function(b) {      var b_prime = change_ref(b, d);      return "translate(" + arc.centroid(b_prime) + ")" +              "rotate("    + getangle(b_prime) +     ")";   }) 

    i have added class label text.

updated demo: http://jsfiddle.net/4ps53/6/


however, have argued there might better ways of presenting data, esp. if allowing zooming , panning: d3 put arc labels in pie chart if there enough space


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -