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
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?
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.
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.
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With