001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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-2022, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.plot.pie;
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        
069        if (isOverlap()) {
070            adjustDownwards(minY, height);
071        }
072
073        // if still overlapping, do something else...
074        if (isOverlap()) {
075            adjustUpwards(minY, height);
076        }
077
078        if (isOverlap()) {
079            spreadEvenly(minY, height);
080        }
081    }
082
083    /**
084     * Returns {@code true} if there are overlapping labels in the list,
085     * and {@code false} otherwise.
086     *
087     * @return A boolean.
088     */
089    private boolean isOverlap() {
090        double y = 0.0;
091        for (int i = 0; i < this.labels.size(); i++) {
092            PieLabelRecord plr = getPieLabelRecord(i);
093            if (y > plr.getLowerY()) {
094                return true;
095            }
096            y = plr.getUpperY();
097        }
098        return false;
099    }
100
101    /**
102     * Adjusts the y-coordinate for the labels in towards the center in an
103     * attempt to fix overlapping.
104     */
105    protected void adjustInwards() {
106        int lower = 0;
107        int upper = this.labels.size() - 1;
108        while (upper > lower) {
109            if (lower < upper - 1) {
110                PieLabelRecord r0 = getPieLabelRecord(lower);
111                PieLabelRecord r1 = getPieLabelRecord(lower + 1);
112                if (r1.getLowerY() < r0.getUpperY()) {
113                    double adjust = r0.getUpperY() - r1.getLowerY()
114                                    + this.minGap;
115                    r1.setAllocatedY(r1.getAllocatedY() + adjust);
116                }
117            }
118            PieLabelRecord r2 = getPieLabelRecord(upper - 1);
119            PieLabelRecord r3 = getPieLabelRecord(upper);
120            if (r2.getUpperY() > r3.getLowerY()) {
121                double adjust = (r2.getUpperY() - r3.getLowerY()) + this.minGap;
122                r3.setAllocatedY(r3.getAllocatedY() + adjust);
123            }
124            lower++;
125            upper--;
126        }
127    }
128
129    /**
130     * Any labels that are overlapping are moved down in an attempt to
131     * eliminate the overlaps.
132     *
133     * @param minY  the minimum y value (in Java2D coordinate space).
134     * @param height  the height available for all labels.
135     */
136    protected void adjustDownwards(double minY, double height) {
137        for (int i = 0; i < this.labels.size() - 1; i++) {
138            PieLabelRecord record0 = getPieLabelRecord(i);
139            PieLabelRecord record1 = getPieLabelRecord(i + 1);
140            if (record1.getLowerY() < record0.getUpperY()) {
141                record1.setAllocatedY(Math.min(minY + height
142                        - record1.getLabelHeight() / 2.0,
143                        record0.getUpperY() + this.minGap
144                        + record1.getLabelHeight() / 2.0));
145            }
146        }
147    }
148
149    /**
150     * Any labels that are overlapping are moved up in an attempt to eliminate
151     * the overlaps.
152     *
153     * @param minY  the minimum y value (in Java2D coordinate space).
154     * @param height  the height available for all labels.
155     */
156    protected void adjustUpwards(double minY, double height) {
157        for (int i = this.labels.size() - 1; i > 0; i--) {
158            PieLabelRecord record0 = getPieLabelRecord(i);
159            PieLabelRecord record1 = getPieLabelRecord(i - 1);
160            if (record1.getUpperY() > record0.getLowerY()) {
161                record1.setAllocatedY(Math.max(minY
162                        + record1.getLabelHeight() / 2.0, record0.getLowerY()
163                        - this.minGap - record1.getLabelHeight() / 2.0));
164            }
165        }
166    }
167
168    /**
169     * Labels are spaced evenly in the available space in an attempt to
170     * eliminate the overlaps.
171     *
172     * @param minY  the minimum y value (in Java2D coordinate space).
173     * @param height  the height available for all labels.
174     */
175    protected void spreadEvenly(double minY, double height) {
176        double y = minY;
177        double sumOfLabelHeights = 0.0;
178        for (int i = 0; i < this.labels.size(); i++) {
179            sumOfLabelHeights += getPieLabelRecord(i).getLabelHeight();
180        }
181        double gap = height - sumOfLabelHeights;
182        if (this.labels.size() > 1) {
183            gap = gap / (this.labels.size() - 1);
184        }
185        for (int i = 0; i < this.labels.size(); i++) {
186            PieLabelRecord record = getPieLabelRecord(i);
187            y = y + record.getLabelHeight() / 2.0;
188            record.setAllocatedY(y);
189            y = y + record.getLabelHeight() / 2.0 + gap;
190        }
191    }
192
193    /**
194     * Sorts the label records into ascending order by y-value.
195     */
196    public void sort() {
197        Collections.sort(this.labels);
198    }
199
200    /**
201     * Returns a string containing a description of the object for
202     * debugging purposes.
203     *
204     * @return A string.
205     */
206    @Override
207    public String toString() {
208        StringBuilder result = new StringBuilder();
209        for (int i = 0; i < this.labels.size(); i++) {
210            result.append(getPieLabelRecord(i).toString()).append("\n");
211        }
212        return result.toString();
213    }
214
215}