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