Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing overlap of text in D3 pie chart

I've been googling around, but I can't seem to grasp this.

My situation is that the countries overlap when presented on the pie chart:

This is an example of what is happening:

jsfiddle

enter image description here

I am a total beginner to D3 and am trying to prevent text overlap. Is there like a text margin attribute that I can add such that my labels don't overlap each other?

like image 630
user1431282 Avatar asked Jan 26 '13 04:01

user1431282


People also ask

How do you prevent data labels overlapping in a pie chart?

To prevent overlapping labels displayed outside a pie chart On the 3D Options tab, select Enable 3D. If you want the chart to have more room for labels but still appear two-dimensional, set the Rotation and Inclination properties to 0.

Do pie charts overlap?

A common problem related to Pie Charts is the overlapping of the labels that represent data points with relatively small values, adjacent to each other. By default, the layout engine will try to arrange the data labels so they do not overlap.


1 Answers

Update: See the answer to D3 put arc labels in a Pie Chart if there is enough space for a more comprehensive solution.


I do not know any generic method of laying text elements such that they do not overlap.

However, there is a workaround for your problem by rotating the labels and scaling the graph such that they do not overlap: http://jsfiddle.net/2uT7F/

// Get the angle on the arc and then rotate by -90 degrees
var getAngle = function (d) {
    return (180 / Math.PI * (d.startAngle + d.endAngle) / 2 - 90);
};

g.append("text")
    .attr("transform", function(d) { 
            return "translate(" + pos.centroid(d) + ") " +
                    "rotate(" + getAngle(d) + ")"; }) 
    .attr("dy", 5) 
    .style("text-anchor", "start")
    .text(function(d) { return d.data.label; });
like image 188
musically_ut Avatar answered Oct 11 '22 04:10

musically_ut