Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#350 Better Pie Label Layout

open
nobody
None
5
2012-11-27
2008-02-11
Anonymous
No

I played around with PiePlot to get a better result than screenshot0 (label link overlapping). Rotating the by by -90 degrees didn't help much (screenshot1). Then I subclassed PieLabelDistributor and tried using spreadEvenly() first:

--------------------
public void distributeLabels(double minY, double height) {
sort(); // sorts the label records into ascending order by baseY

if (isOverlap()) {
spreadEvenly(minY, height);
}

if (isOverlap()) {
adjustInwards();
}

// if still overlapping, do something else...
if (isOverlap()) {
adjustDownwards(minY, height);
}

if (isOverlap()) {
adjustUpwards(minY, height);
}

}
--------------------

as this "sounded" good, but the labels just had much too big spaces between the label boxes. So I changed it a bit:

--------------------------------
/**
* Labels are spaced evenly in the available space in an attempt to
* eliminate the overlaps.
*
* @param minY the minimum y value (in Java2D coordinate space).
* @param height the height available for all labels.
*/
protected void spreadEvenly2(double minY, double height) {
double y = minY;
double sumOfLabelHeights = 0.0;
for (int i = 0; i < this.labels.size(); i++) {
sumOfLabelHeights += getPieLabelRecord(i).getLabelHeight();
}
double gap = Math.max(0, height - sumOfLabelHeights);
y += gap / 2;

if (this.labels.size() > 1) {
gap = gap / (this.labels.size() - 1);
}

gap = Math.min(5, gap);

for (int i = 0; i < this.labels.size(); i++) {
PieLabelRecord record = getPieLabelRecord(i);
y = y + record.getLabelHeight() / 2.0;
record.setAllocatedY(y);
y = y + record.getLabelHeight() / 2.0 + gap;
}
}
----------------------------------------

and got a much better result as the default one (screenshot2). Could you please

a) use this as the default distribution or make this as an alternative and offer a method to explicitaly choose between the misc alternatives?

Discussion