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