001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2007, by Object Refinery Limited 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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * ------------------------
028     * PieLabelDistributor.java
029     * ------------------------
030     * (C) Copyright 2004-2007, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: PieLabelDistributor.java,v 1.5.2.2 2007/06/14 15:04:21 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 08-Mar-2004 : Version 1 (DG);
040     * 18-Apr-2005 : Use StringBuffer (DG);
041     * 14-Jun-2007 : Now extends AbstractPieLabelDistributor (DG);
042     *
043     */
044    
045    package org.jfree.chart.plot;
046    
047    import java.util.Collections;
048    
049    /**
050     * This class distributes the section labels for one side of a pie chart so 
051     * that they do not overlap.
052     */
053    public class PieLabelDistributor extends AbstractPieLabelDistributor {
054            
055        /** The minimum gap. */
056        private double minGap = 4.0;
057        
058        /**
059         * Creates a new distributor.
060         * 
061         * @param labelCount  the number of labels (ignored).
062         */
063        public PieLabelDistributor(int labelCount) {
064            super();
065        }
066        
067        /**
068         * Distributes the labels.
069         * 
070         * @param minY  the minimum y-coordinate in Java2D-space.
071         * @param height  the available height (in Java2D units).
072         */
073        public void distributeLabels(double minY, double height) {
074            sort();  // sorts the label records into ascending order by baseY
075            if (isOverlap()) {
076                adjustInwards();
077            }
078            
079            // if still overlapping, do something else...
080            if (isOverlap()) {
081                adjustDownwards(minY, height);
082            }
083            
084            if (isOverlap()) { 
085                adjustUpwards(minY, height);
086            }
087            
088            if (isOverlap()) {  
089                spreadEvenly(minY, height);
090            }
091    
092        }
093        
094        /**
095         * Returns <code>true</code> if there are overlapping labels in the list, 
096         * and <code>false</code> otherwise.
097         * 
098         * @return A boolean.
099         */
100        private boolean isOverlap() {
101            double y = 0.0;
102            for (int i = 0; i < this.labels.size(); i++) {
103                PieLabelRecord plr = getPieLabelRecord(i);
104                if (y > plr.getLowerY()) {
105                    return true;
106                }
107                y = plr.getUpperY();    
108            }
109            return false;
110        }
111        
112        /**
113         * Adjusts the y-coordinate for the labels in towards the center in an 
114         * attempt to fix overlapping.
115         */
116        protected void adjustInwards() {   
117            int lower = 0;
118            int upper = this.labels.size() - 1;
119            while (upper > lower) {
120                if (lower < upper - 1) {
121                    PieLabelRecord r0 = getPieLabelRecord(lower);
122                    PieLabelRecord r1 = getPieLabelRecord(lower + 1); 
123                    if (r1.getLowerY() < r0.getUpperY()) {
124                        double adjust = r0.getUpperY() - r1.getLowerY() 
125                                        + this.minGap;  
126                        r1.setAllocatedY(r1.getAllocatedY() + adjust);   
127                    }
128                }
129                PieLabelRecord r2 = getPieLabelRecord(upper - 1);
130                PieLabelRecord r3 = getPieLabelRecord(upper);  
131                if (r2.getUpperY() > r3.getLowerY()) {
132                    double adjust = (r2.getUpperY() - r3.getLowerY()) + this.minGap;
133                    r3.setAllocatedY(r3.getAllocatedY() + adjust);   
134                }                
135                lower++; 
136                upper--;
137            }
138        }
139        
140        /**
141         * Any labels that are overlapping are moved down in an attempt to 
142         * eliminate the overlaps.
143         * 
144         * @param minY  the minimum y value (in Java2D coordinate space).
145         * @param height  the height available for all labels.
146         */
147        protected void adjustDownwards(double minY, double height) {
148            for (int i = 0; i < this.labels.size() - 1; i++) {
149                PieLabelRecord record0 = getPieLabelRecord(i);
150                PieLabelRecord record1 = getPieLabelRecord(i + 1);
151                if (record1.getLowerY() < record0.getUpperY()) {
152                    record1.setAllocatedY(Math.min(minY + height, 
153                            record0.getUpperY() + this.minGap 
154                            + record1.getLabelHeight() / 2.0));   
155                }
156            }        
157        }
158    
159        /**
160         * Any labels that are overlapping are moved up in an attempt to eliminate 
161         * the overlaps.
162         * 
163         * @param minY  the minimum y value (in Java2D coordinate space).
164         * @param height  the height available for all labels.
165         */
166        protected void adjustUpwards(double minY, double height) {
167            for (int i = this.labels.size() - 1; i > 0; i--) {
168                PieLabelRecord record0 = getPieLabelRecord(i);
169                PieLabelRecord record1 = getPieLabelRecord(i - 1);
170                if (record1.getUpperY() > record0.getLowerY()) {
171                    record1.setAllocatedY(Math.max(minY, record0.getLowerY() 
172                            - this.minGap - record1.getLabelHeight() / 2.0));
173                }
174            }        
175        }
176    
177        /**
178         * Labels are spaced evenly in the available space in an attempt to 
179         * eliminate the overlaps.
180         * 
181         * @param minY  the minimum y value (in Java2D coordinate space).
182         * @param height  the height available for all labels.
183         */
184        protected void spreadEvenly(double minY, double height) {
185            double y = minY;
186            double sumOfLabelHeights = 0.0;
187            for (int i = 0; i < this.labels.size(); i++) {
188                sumOfLabelHeights += getPieLabelRecord(i).getLabelHeight();
189            }
190            double gap = Math.max(0, height - sumOfLabelHeights);
191            if (this.labels.size() > 1) {
192                gap = gap / (this.labels.size() - 1);   
193            }
194            for (int i = 0; i < this.labels.size(); i++) {
195                PieLabelRecord record = getPieLabelRecord(i);
196                y = y + record.getLabelHeight() / 2.0;
197                record.setAllocatedY(y);
198                y = y + record.getLabelHeight() / 2.0 + gap;
199            }        
200        }
201            
202        /**
203         * Sorts the label records into ascending order by y-value.
204         */
205        public void sort() {
206            Collections.sort(this.labels);  
207        }
208        
209        /**
210         * Returns a string containing a description of the object for 
211         * debugging purposes.
212         * 
213         * @return A string.
214         */
215        public String toString() {
216            StringBuffer result = new StringBuffer();
217            for (int i = 0; i < this.labels.size(); i++) {
218                result.append(getPieLabelRecord(i).toString()).append("\n");   
219            }
220            return result.toString();
221        }
222        
223    }