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 * CategoryLabelPosition.java
029 * --------------------------
030 * (C) Copyright 2003-2016, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 31-Oct-2003 : Version 1 (DG);
038 * 17-Feb-2004 : Added new constructor (DG);
039 * 23-Mar-2004 : Added width calculation parameters (DG);
040 * 07-Jan-2005 : Fixed bug in equals() method (DG);
041 * 11-Jan-2005 : Removed deprecated constructor in preparation for the 1.0.0
042 *               release (DG);
043 * 02-Jul-2013 : Use ParamChecks (DG);
044 *
045 */
046
047package org.jfree.chart.axis;
048
049import java.io.Serializable;
050import org.jfree.chart.text.TextBlockAnchor;
051import org.jfree.chart.ui.RectangleAnchor;
052import org.jfree.chart.ui.TextAnchor;
053import org.jfree.chart.util.Args;
054
055/**
056 * The attributes that control the position of the labels for the categories on
057 * a {@link CategoryAxis}. Instances of this class are immutable and other
058 * JFreeChart classes rely upon this.
059 */
060public class CategoryLabelPosition implements Serializable {
061
062    /** For serialization. */
063    private static final long serialVersionUID = 5168681143844183864L;
064
065    /** The category anchor point. */
066    private RectangleAnchor categoryAnchor;
067
068    /** The text block anchor. */
069    private TextBlockAnchor labelAnchor;
070
071    /** The rotation anchor. */
072    private TextAnchor rotationAnchor;
073
074    /** The rotation angle (in radians). */
075    private double angle;
076
077    /** The width calculation type. */
078    private CategoryLabelWidthType widthType;
079
080    /**
081     * The maximum label width as a percentage of the category space or the
082     * range space.
083     */
084    private float widthRatio;
085
086    /**
087     * Creates a new position record with default settings.
088     */
089    public CategoryLabelPosition() {
090        this(RectangleAnchor.CENTER, TextBlockAnchor.BOTTOM_CENTER,
091                TextAnchor.CENTER, 0.0, CategoryLabelWidthType.CATEGORY, 0.95f);
092    }
093
094    /**
095     * Creates a new category label position record.
096     *
097     * @param categoryAnchor  the category anchor ({@code null} not
098     *                        permitted).
099     * @param labelAnchor  the label anchor ({@code null} not permitted).
100     */
101    public CategoryLabelPosition(RectangleAnchor categoryAnchor,
102                                 TextBlockAnchor labelAnchor) {
103        // argument checking delegated...
104        this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0,
105                CategoryLabelWidthType.CATEGORY, 0.95f);
106    }
107
108    /**
109     * Creates a new category label position record.
110     *
111     * @param categoryAnchor  the category anchor ({@code null} not
112     *                        permitted).
113     * @param labelAnchor  the label anchor ({@code null} not permitted).
114     * @param widthType  the width type ({@code null} not permitted).
115     * @param widthRatio  the maximum label width as a percentage (of the
116     *                    category space or the range space).
117     */
118    public CategoryLabelPosition(RectangleAnchor categoryAnchor,
119            TextBlockAnchor labelAnchor, CategoryLabelWidthType widthType,
120            float widthRatio) {
121        // argument checking delegated...
122        this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0, widthType,
123                widthRatio);
124    }
125
126    /**
127     * Creates a new position record.  The item label anchor is a point
128     * relative to the data item (dot, bar or other visual item) on a chart.
129     * The item label is aligned by aligning the text anchor with the item
130     * label anchor.
131     *
132     * @param categoryAnchor  the category anchor ({@code null} not
133     *                        permitted).
134     * @param labelAnchor  the label anchor ({@code null} not permitted).
135     * @param rotationAnchor  the rotation anchor ({@code null} not
136     *                        permitted).
137     * @param angle  the rotation angle ({@code null} not permitted).
138     * @param widthType  the width type ({@code null} not permitted).
139     * @param widthRatio  the maximum label width as a percentage (of the
140     *                    category space or the range space).
141     */
142    public CategoryLabelPosition(RectangleAnchor categoryAnchor,
143            TextBlockAnchor labelAnchor, TextAnchor rotationAnchor, 
144            double angle, CategoryLabelWidthType widthType, float widthRatio) {
145
146        Args.nullNotPermitted(categoryAnchor, "categoryAnchor");
147        Args.nullNotPermitted(labelAnchor, "labelAnchor");
148        Args.nullNotPermitted(rotationAnchor, "rotationAnchor");
149        Args.nullNotPermitted(widthType, "widthType");
150
151        this.categoryAnchor = categoryAnchor;
152        this.labelAnchor = labelAnchor;
153        this.rotationAnchor = rotationAnchor;
154        this.angle = angle;
155        this.widthType = widthType;
156        this.widthRatio = widthRatio;
157
158    }
159
160    /**
161     * Returns the item label anchor.
162     *
163     * @return The item label anchor (never {@code null}).
164     */
165    public RectangleAnchor getCategoryAnchor() {
166        return this.categoryAnchor;
167    }
168
169    /**
170     * Returns the text block anchor.
171     *
172     * @return The text block anchor (never {@code null}).
173     */
174    public TextBlockAnchor getLabelAnchor() {
175        return this.labelAnchor;
176    }
177
178    /**
179     * Returns the rotation anchor point.
180     *
181     * @return The rotation anchor point (never {@code null}).
182     */
183    public TextAnchor getRotationAnchor() {
184        return this.rotationAnchor;
185    }
186
187    /**
188     * Returns the angle of rotation for the label.
189     *
190     * @return The angle (in radians).
191     */
192    public double getAngle() {
193        return this.angle;
194    }
195
196    /**
197     * Returns the width calculation type.
198     *
199     * @return The width calculation type (never {@code null}).
200     */
201    public CategoryLabelWidthType getWidthType() {
202        return this.widthType;
203    }
204
205    /**
206     * Returns the ratio used to calculate the maximum category label width.
207     *
208     * @return The ratio.
209     */
210    public float getWidthRatio() {
211        return this.widthRatio;
212    }
213
214    /**
215     * Tests this instance for equality with an arbitrary object.
216     *
217     * @param obj  the object ({@code null} permitted).
218     *
219     * @return A boolean.
220     */
221    @Override
222    public boolean equals(Object obj) {
223        if (obj == this) {
224            return true;
225        }
226        if (!(obj instanceof CategoryLabelPosition)) {
227            return false;
228        }
229        CategoryLabelPosition that = (CategoryLabelPosition) obj;
230        if (!this.categoryAnchor.equals(that.categoryAnchor)) {
231            return false;
232        }
233        if (!this.labelAnchor.equals(that.labelAnchor)) {
234            return false;
235        }
236        if (!this.rotationAnchor.equals(that.rotationAnchor)) {
237            return false;
238        }
239        if (this.angle != that.angle) {
240            return false;
241        }
242        if (this.widthType != that.widthType) {
243            return false;
244        }
245        if (this.widthRatio != that.widthRatio) {
246            return false;
247        }
248        return true;
249    }
250
251    /**
252     * Returns a hash code for this object.
253     *
254     * @return A hash code.
255     */
256    @Override
257    public int hashCode() {
258        int result = 19;
259        result = 37 * result + this.categoryAnchor.hashCode();
260        result = 37 * result + this.labelAnchor.hashCode();
261        result = 37 * result + this.rotationAnchor.hashCode();
262        return result;
263    }
264
265}