Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * -------------------------- 28: * CategoryLabelPosition.java 29: * -------------------------- 30: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: CategoryLabelPosition.java,v 1.7.2.1 2005/10/25 20:37:34 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 31-Oct-2003 : Version 1 (DG); 40: * 17-Feb-2004 : Added new constructor (DG); 41: * 23-Mar-2004 : Added width calculation parameters (DG); 42: * 07-Jan-2005 : Fixed bug in equals() method (DG); 43: * 11-Jan-2005 : Removed deprecated constructor in preparation for the 1.0.0 44: * release (DG); 45: * 46: */ 47: 48: package org.jfree.chart.axis; 49: 50: import java.io.Serializable; 51: 52: import org.jfree.text.TextBlockAnchor; 53: import org.jfree.ui.RectangleAnchor; 54: import org.jfree.ui.TextAnchor; 55: 56: /** 57: * The attributes that control the position of the labels for the categories on 58: * a {@link CategoryAxis}. Instances of this class are immutable and other 59: * JFreeChart classes rely upon this. 60: */ 61: public class CategoryLabelPosition implements Serializable { 62: 63: /** For serialization. */ 64: private static final long serialVersionUID = 5168681143844183864L; 65: 66: /** The category anchor point. */ 67: private RectangleAnchor categoryAnchor; 68: 69: /** The text block anchor. */ 70: private TextBlockAnchor labelAnchor; 71: 72: /** The rotation anchor. */ 73: private TextAnchor rotationAnchor; 74: 75: /** The rotation angle (in radians). */ 76: private double angle; 77: 78: /** The width calculation type. */ 79: private CategoryLabelWidthType widthType; 80: 81: /** 82: * The maximum label width as a percentage of the category space or the 83: * range space. 84: */ 85: private float widthRatio; 86: 87: /** 88: * Creates a new position record with default settings. 89: */ 90: public CategoryLabelPosition() { 91: this( 92: RectangleAnchor.CENTER, TextBlockAnchor.BOTTOM_CENTER, 93: TextAnchor.CENTER, 0.0, CategoryLabelWidthType.CATEGORY, 0.95f 94: ); 95: } 96: 97: /** 98: * Creates a new category label position record. 99: * 100: * @param categoryAnchor the category anchor (<code>null</code> not 101: * permitted). 102: * @param labelAnchor the label anchor (<code>null</code> not permitted). 103: */ 104: public CategoryLabelPosition(RectangleAnchor categoryAnchor, 105: TextBlockAnchor labelAnchor) { 106: // argument checking delegated... 107: this( 108: categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0, 109: CategoryLabelWidthType.CATEGORY, 0.95f 110: ); 111: } 112: 113: /** 114: * Creates a new category label position record. 115: * 116: * @param categoryAnchor the category anchor (<code>null</code> not 117: * permitted). 118: * @param labelAnchor the label anchor (<code>null</code> not permitted). 119: * @param widthType the width type (<code>null</code> not permitted). 120: * @param widthRatio the maximum label width as a percentage (of the 121: * category space or the range space). 122: */ 123: public CategoryLabelPosition(RectangleAnchor categoryAnchor, 124: TextBlockAnchor labelAnchor, 125: CategoryLabelWidthType widthType, 126: float widthRatio) { 127: // argument checking delegated... 128: this( 129: categoryAnchor, labelAnchor, TextAnchor.CENTER, 130: 0.0, widthType, widthRatio 131: ); 132: } 133: 134: /** 135: * Creates a new position record. The item label anchor is a point 136: * relative to the data item (dot, bar or other visual item) on a chart. 137: * The item label is aligned by aligning the text anchor with the item 138: * label anchor. 139: * 140: * @param categoryAnchor the category anchor (<code>null</code> not 141: * permitted). 142: * @param labelAnchor the label anchor (<code>null</code> not permitted). 143: * @param rotationAnchor the rotation anchor (<code>null</code> not 144: * permitted). 145: * @param angle the rotation angle (<code>null</code> not permitted). 146: * @param widthType the width type (<code>null</code> not permitted). 147: * @param widthRatio the maximum label width as a percentage (of the 148: * category space or the range space). 149: */ 150: public CategoryLabelPosition(RectangleAnchor categoryAnchor, 151: TextBlockAnchor labelAnchor, 152: TextAnchor rotationAnchor, 153: double angle, 154: CategoryLabelWidthType widthType, 155: float widthRatio) { 156: 157: if (categoryAnchor == null) { 158: throw new IllegalArgumentException( 159: "Null 'categoryAnchor' argument." 160: ); 161: } 162: if (labelAnchor == null) { 163: throw new IllegalArgumentException( 164: "Null 'labelAnchor' argument." 165: ); 166: } 167: if (rotationAnchor == null) { 168: throw new IllegalArgumentException( 169: "Null 'rotationAnchor' argument." 170: ); 171: } 172: if (widthType == null) { 173: throw new IllegalArgumentException("Null 'widthType' argument."); 174: } 175: 176: this.categoryAnchor = categoryAnchor; 177: this.labelAnchor = labelAnchor; 178: this.rotationAnchor = rotationAnchor; 179: this.angle = angle; 180: this.widthType = widthType; 181: this.widthRatio = widthRatio; 182: 183: } 184: 185: /** 186: * Returns the item label anchor. 187: * 188: * @return The item label anchor (never <code>null</code>). 189: */ 190: public RectangleAnchor getCategoryAnchor() { 191: return this.categoryAnchor; 192: } 193: 194: /** 195: * Returns the text block anchor. 196: * 197: * @return The text block anchor (never <code>null</code>). 198: */ 199: public TextBlockAnchor getLabelAnchor() { 200: return this.labelAnchor; 201: } 202: 203: /** 204: * Returns the rotation anchor point. 205: * 206: * @return The rotation anchor point (never <code>null</code>). 207: */ 208: public TextAnchor getRotationAnchor() { 209: return this.rotationAnchor; 210: } 211: 212: /** 213: * Returns the angle of rotation for the label. 214: * 215: * @return The angle (in radians). 216: */ 217: public double getAngle() { 218: return this.angle; 219: } 220: 221: /** 222: * Returns the width calculation type. 223: * 224: * @return The width calculation type. 225: */ 226: public CategoryLabelWidthType getWidthType() { 227: return this.widthType; 228: } 229: 230: /** 231: * Returns the ratio used to calculate the maximum category label width. 232: * 233: * @return The ratio. 234: */ 235: public float getWidthRatio() { 236: return this.widthRatio; 237: } 238: 239: /** 240: * Tests this instance for equality with an arbitrary object. 241: * 242: * @param obj the object (<code>null</code> permitted). 243: * 244: * @return A boolean. 245: */ 246: public boolean equals(Object obj) { 247: if (obj == this) { 248: return true; 249: } 250: if (!(obj instanceof CategoryLabelPosition)) { 251: return false; 252: } 253: CategoryLabelPosition that = (CategoryLabelPosition) obj; 254: if (!this.categoryAnchor.equals(that.categoryAnchor)) { 255: return false; 256: } 257: if (!this.labelAnchor.equals(that.labelAnchor)) { 258: return false; 259: } 260: if (!this.rotationAnchor.equals(that.rotationAnchor)) { 261: return false; 262: } 263: if (this.angle != that.angle) { 264: return false; 265: } 266: if (this.widthType != that.widthType) { 267: return false; 268: } 269: if (this.widthRatio != that.widthRatio) { 270: return false; 271: } 272: return true; 273: } 274: 275: /** 276: * Returns a hash code for this object. 277: * 278: * @return A hash code. 279: */ 280: public int hashCode() { 281: int result = 19; 282: result = 37 * result + this.categoryAnchor.hashCode(); 283: result = 37 * result + this.labelAnchor.hashCode(); 284: result = 37 * result + this.rotationAnchor.hashCode(); 285: return result; 286: } 287: 288: }