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 * PieLabelRecord.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 * Changes
036 * -------
037 * 08-Mar-2004 : Version 1 (DG);
038 * 14-Jun-2007 : Implemented Serializable, updated API docs (DG);
039 *
040 */
041
042package org.jfree.chart.plot;
043
044import java.io.Serializable;
045
046import org.jfree.text.TextBox;
047
048/**
049 * A structure that retains information about the label for a section in a pie 
050 * chart.
051 */
052public class PieLabelRecord implements Comparable, Serializable {
053    
054    /** The section key. */
055    private Comparable key;
056    
057    /** The angle of the centre of the section (in radians). */
058    private double angle;
059    
060    /** The base y-coordinate. */
061    private double baseY;
062    
063    /** The allocated y-coordinate. */
064    private double allocatedY;
065
066    /** The label. */
067    private TextBox label;
068    
069    /** The label height. */
070    private double labelHeight;
071    
072    /** The gap. */
073    private double gap;
074    
075    /** The link percent. */
076    private double linkPercent;
077    
078    /**
079     * Creates a new record.
080     * 
081     * @param key  the section key.
082     * @param angle  the angle to the middle of the section (in radians).
083     * @param baseY  the base y-coordinate.
084     * @param label  the section label.
085     * @param labelHeight  the label height (in Java2D units).
086     * @param gap  the offset to the left.
087     * @param linkPercent  the link percent.
088     */
089    public PieLabelRecord(Comparable key, double angle, double baseY, 
090                          TextBox label, double labelHeight, double gap, 
091                          double linkPercent) {
092        this.key = key;
093        this.angle = angle;
094        this.baseY = baseY;
095        this.allocatedY = baseY;
096        this.label = label;
097        this.labelHeight = labelHeight;
098        this.gap = gap;
099        this.linkPercent = linkPercent;
100    }
101    
102    /**
103     * Returns the base y-coordinate.  This is where the label will appear if 
104     * there is no overlapping of labels.
105     * 
106     * @return The base y-coordinate.
107     */
108    public double getBaseY() {
109        return this.baseY;   
110    }
111    
112    /**
113     * Sets the base y-coordinate.
114     * 
115     * @param base  the base y-coordinate.
116     */
117    public void setBaseY(double base) {
118        this.baseY = base;   
119    }
120    
121    /**
122     * Returns the lower bound of the label.
123     * 
124     * @return The lower bound.
125     */
126    public double getLowerY() {
127        return this.allocatedY - this.labelHeight / 2.0;   
128    }
129    
130    /**
131     * Returns the upper bound of the label.
132     * 
133     * @return The upper bound.
134     */
135    public double getUpperY() {
136        return this.allocatedY + this.labelHeight / 2.0;   
137    }
138    
139    /**
140     * Returns the angle of the middle of the section, in radians.
141     * 
142     * @return The angle, in radians.
143     */
144    public double getAngle() {
145        return this.angle;   
146    }
147    
148    /**
149     * Returns the key for the section that the label applies to.
150     * 
151     * @return The key.
152     */
153    public Comparable getKey() {
154        return this.key;   
155    }
156    
157    /**
158     * Returns the label.
159     * 
160     * @return The label.
161     */
162    public TextBox getLabel() {
163        return this.label;   
164    }
165    
166    /**
167     * Returns the label height (you could derive this from the label itself,
168     * but we cache the value so it can be retrieved quickly).
169     * 
170     * @return The label height (in Java2D units).
171     */
172    public double getLabelHeight() {
173        return this.labelHeight;   
174    }
175    
176    /**
177     * Returns the allocated y-coordinate.
178     * 
179     * @return The allocated y-coordinate.
180     */
181    public double getAllocatedY() {
182        return this.allocatedY;   
183    }
184    
185    /**
186     * Sets the allocated y-coordinate.
187     * 
188     * @param y  the y-coordinate.
189     */
190    public void setAllocatedY(double y) {
191        this.allocatedY = y;   
192    }
193    
194    /**
195     * Returns the gap.
196     * 
197     * @return The gap.
198     */
199    public double getGap() {
200        return this.gap;   
201    }
202    
203    /**
204     * Returns the link percent.
205     * 
206     * @return The link percent.
207     */
208    public double getLinkPercent() {
209        return this.linkPercent;   
210    }
211    
212    /**
213     * Compares this object to an arbitrary object.
214     * 
215     * @param obj  the object to compare against.
216     * 
217     * @return An integer that specifies the relative order of the two objects.
218     */
219    public int compareTo(Object obj) {
220        int result = 0;
221        if (obj instanceof PieLabelRecord) {
222            PieLabelRecord plr = (PieLabelRecord) obj;
223            if (this.baseY < plr.baseY) {
224                result = -1;   
225            }
226            else if (this.baseY > plr.baseY) {
227                result = 1;   
228            }
229        }
230        return result;
231    }
232    
233    /**
234     * Returns a string describing the object.  This is used for debugging only.
235     * 
236     * @return A string.
237     */
238    public String toString() {
239        return this.baseY + ", " + this.key.toString();   
240    }
241}