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