001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * [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 * PieLabelRecord.java
029 * -------------------
030 * (C) Copyright 2004-2008, 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 * 21-Nov-2007 : Implemented equals() to shut up FindBugs (DG);
040 *
041 */
042
043package org.jfree.chart.plot;
044
045import java.io.Serializable;
046import org.jfree.chart.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    @Override
220    public int compareTo(Object obj) {
221        int result = 0;
222        if (obj instanceof PieLabelRecord) {
223            PieLabelRecord plr = (PieLabelRecord) obj;
224            if (this.baseY < plr.baseY) {
225                result = -1;
226            }
227            else if (this.baseY > plr.baseY) {
228                result = 1;
229            }
230        }
231        return result;
232    }
233
234    /**
235     * Tests this record for equality with an arbitrary object.
236     *
237     * @param obj  the object ({@code null} permitted).
238     *
239     * @return A boolean.
240     */
241    @Override
242    public boolean equals(Object obj) {
243        if (obj == this) {
244            return true;
245        }
246        if (!(obj instanceof PieLabelRecord)) {
247            return false;
248        }
249        PieLabelRecord that = (PieLabelRecord) obj;
250        if (!this.key.equals(that.key)) {
251            return false;
252        }
253        if (this.angle != that.angle) {
254            return false;
255        }
256        if (this.gap != that.gap) {
257            return false;
258        }
259        if (this.allocatedY != that.allocatedY) {
260            return false;
261        }
262        if (this.baseY != that.baseY) {
263            return false;
264        }
265        if (this.labelHeight != that.labelHeight) {
266            return false;
267        }
268        if (this.linkPercent != that.linkPercent) {
269            return false;
270        }
271        if (!this.label.equals(that.label)) {
272            return false;
273        }
274        return true;
275    }
276
277    /**
278     * Returns a string describing the object.  This is used for debugging only.
279     *
280     * @return A string.
281     */
282    @Override
283    public String toString() {
284        return this.baseY + ", " + this.key.toString();
285    }
286}