How to avoid labels overlapping in a D3.js pie chart?

后端 未结 4 730
再見小時候
再見小時候 2020-12-29 15:42

I\'m drawing a pie chart using D3.js with a quite simple script. The problem is that when slices are small, their labels overlap.

What options do I have to prevent t

4条回答
  •  天涯浪人
    2020-12-29 16:26

    For small angles(less than 5% of the Pie Chart), I have changed the centroid value for the respective labels. I have used this code:

        arcs.append("text") 
            .attr("transform", function(d,i) {
                var centroid_value = arc.centroid(d);
    
                var pieValue = ((d.endAngle - d.startAngle)*100)/(2*Math.PI);                
                var accuratePieValue = pieValue.toFixed(0);
                if(accuratePieValue <= 5){
                    var pieLableArc = d3.svg.arc().innerRadius(i*20).outerRadius(outer_radius + i*20);
                    centroid_value = pieLableArc.centroid(d);
                }
    
                return "translate(" + centroid_value + ")";
            })
            .text(function(d, i) { ..... });
    

提交回复
热议问题