001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ------------------------
028 * PieLabelDistributor.java
029 * ------------------------
030 * (C) Copyright 2004-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.plot;
038
039import java.util.Collections;
040
041/**
042 * This class distributes the section labels for one side of a pie chart so
043 * that they do not overlap.
044 */
045public class PieLabelDistributor extends AbstractPieLabelDistributor {
046
047    /** The minimum gap. */
048    private double minGap = 4.0;
049
050    /**
051     * Creates a new distributor.
052     *
053     * @param labelCount  the number of labels (ignored).
054     */
055    public PieLabelDistributor(int labelCount) {
056        super();
057    }
058
059    /**
060     * Distributes the labels.
061     *
062     * @param minY  the minimum y-coordinate in Java2D-space.
063     * @param height  the available height (in Java2D units).
064     */
065    @Override
066    public void distributeLabels(double minY, double height) {
067        sort();  // sorts the label records into ascending order by baseY
068//        if (isOverlap()) {
069//            adjustInwards();
070//        }
071        // if still overlapping, do something else...
072        if (isOverlap()) {
073            adjustDownwards(minY, height);
074        }
075
076        if (isOverlap()) {
077            adjustUpwards(minY, height);
078        }
079
080        if (isOverlap()) {
081            spreadEvenly(minY, height);
082        }
083    }
084
085    /**
086     * Returns {@code true} if there are overlapping labels in the list,
087     * and {@code false} otherwise.
088     *
089     * @return A boolean.
090     */
091    private boolean isOverlap() {
092        double y = 0.0;
093        for (int i = 0; i < this.labels.size(); i++) {
094            PieLabelRecord plr = getPieLabelRecord(i);
095            if (y > plr.getLowerY()) {
096                return true;
097            }
098            y = plr.getUpperY();
099        }
100        return false;
101    }
102
103    /**
104     * Adjusts the y-coordinate for the labels in towards the center in an
105     * attempt to fix overlapping.
106     */
107    protected void adjustInwards() {
108        int lower = 0;
109        int upper = this.labels.size() - 1;
110        while (upper > lower) {
111            if (lower < upper - 1) {
112                PieLabelRecord r0 = getPieLabelRecord(lower);
113                PieLabelRecord r1 = getPieLabelRecord(lower + 1);
114                if (r1.getLowerY() < r0.getUpperY()) {
115                    double adjust = r0.getUpperY() - r1.getLowerY()
116                                    + this.minGap;
117                    r1.setAllocatedY(r1.getAllocatedY() + adjust);
118                }
119            }
120            PieLabelRecord r2 = getPieLabelRecord(upper - 1);
121            PieLabelRecord r3 = getPieLabelRecord(upper);
122            if (r2.getUpperY() > r3.getLowerY()) {
123                double adjust = (r2.getUpperY() - r3.getLowerY()) + this.minGap;
124                r3.setAllocatedY(r3.getAllocatedY() + adjust);
125            }
126            lower++;
127            upper--;
128        }
129    }
130
131    /**
132     * Any labels that are overlapping are moved down in an attempt to
133     * eliminate the overlaps.
134     *
135     * @param minY  the minimum y value (in Java2D coordinate space).
136     * @param height  the height available for all labels.
137     */
138    protected void adjustDownwards(double minY, double height) {
139        for (int i = 0; i < this.labels.size() - 1; i++) {
140            PieLabelRecord record0 = getPieLabelRecord(i);
141            PieLabelRecord record1 = getPieLabelRecord(i + 1);
142            if (record1.getLowerY() < record0.getUpperY()) {
143                record1.setAllocatedY(Math.min(minY + height
144                        - record1.getLabelHeight() / 2.0,
145                        record0.getUpperY() + this.minGap
146                        + record1.getLabelHeight() / 2.0));
147            }
148        }
149    }
150
151    /**
152     * Any labels that are overlapping are moved up in an attempt to eliminate
153     * the overlaps.
154     *
155     * @param minY  the minimum y value (in Java2D coordinate space).
156     * @param height  the height available for all labels.
157     */
158    protected void adjustUpwards(double minY, double height) {
159        for (int i = this.labels.size() - 1; i > 0; i--) {
160            PieLabelRecord record0 = getPieLabelRecord(i);
161            PieLabelRecord record1 = getPieLabelRecord(i - 1);
162            if (record1.getUpperY() > record0.getLowerY()) {
163                record1.setAllocatedY(Math.max(minY
164                        + record1.getLabelHeight() / 2.0, record0.getLowerY()
165                        - this.minGap - record1.getLabelHeight() / 2.0));
166            }
167        }
168    }
169
170    /**
171     * Labels are spaced evenly in the available space in an attempt to
172     * eliminate the overlaps.
173     *
174     * @param minY  the minimum y value (in Java2D coordinate space).
175     * @param height  the height available for all labels.
176     */
177    protected void spreadEvenly(double minY, double height) {
178        double y = minY;
179        double sumOfLabelHeights = 0.0;
180        for (int i = 0; i < this.labels.size(); i++) {
181            sumOfLabelHeights += getPieLabelRecord(i).getLabelHeight();
182        }
183        double gap = height - sumOfLabelHeights;
184        if (this.labels.size() > 1) {
185            gap = gap / (this.labels.size() - 1);
186        }
187        for (int i = 0; i < this.labels.size(); i++) {
188            PieLabelRecord record = getPieLabelRecord(i);
189            y = y + record.getLabelHeight() / 2.0;
190            record.setAllocatedY(y);
191            y = y + record.getLabelHeight() / 2.0 + gap;
192        }
193    }
194
195    /**
196     * Sorts the label records into ascending order by y-value.
197     */
198    public void sort() {
199        Collections.sort(this.labels);
200    }
201
202    /**
203     * Returns a string containing a description of the object for
204     * debugging purposes.
205     *
206     * @return A string.
207     */
208    @Override
209    public String toString() {
210        StringBuilder result = new StringBuilder();
211        for (int i = 0; i < this.labels.size(); i++) {
212            result.append(getPieLabelRecord(i).toString()).append("\n");
213        }
214        return result.toString();
215    }
216
217}