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: * CategoryTextAnnotation.java 29: * --------------------------- 30: * (C) Copyright 2003-2005, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: CategoryTextAnnotation.java,v 1.6.2.1 2005/10/25 16:51:15 mungady Exp $ 36: * 37: * Changes: 38: * -------- 39: * 02-Apr-2003 : Version 1 (DG); 40: * 02-Jul-2003 : Added new text alignment and rotation options (DG); 41: * 04-Jul-2003 : Added a category anchor option (DG); 42: * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG); 43: * 21-Jan-2004 : Update for renamed method in ValueAxis (DG); 44: * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities 45: * --> TextUtilities (DG); 46: * 47: */ 48: 49: package org.jfree.chart.annotations; 50: 51: import java.awt.Graphics2D; 52: import java.awt.geom.Rectangle2D; 53: import java.io.Serializable; 54: 55: import org.jfree.chart.axis.CategoryAnchor; 56: import org.jfree.chart.axis.CategoryAxis; 57: import org.jfree.chart.axis.ValueAxis; 58: import org.jfree.chart.plot.CategoryPlot; 59: import org.jfree.chart.plot.Plot; 60: import org.jfree.chart.plot.PlotOrientation; 61: import org.jfree.data.category.CategoryDataset; 62: import org.jfree.text.TextUtilities; 63: import org.jfree.ui.RectangleEdge; 64: 65: /** 66: * A text annotation that can be placed on a 67: * {@link org.jfree.chart.plot.CategoryPlot}. 68: */ 69: public class CategoryTextAnnotation extends TextAnnotation 70: implements CategoryAnnotation, 71: Cloneable, Serializable { 72: 73: /** For serialization. */ 74: private static final long serialVersionUID = 3333360090781320147L; 75: 76: /** The category. */ 77: private Comparable category; 78: 79: /** The category anchor (START, MIDDLE, or END). */ 80: private CategoryAnchor categoryAnchor; 81: 82: /** The value. */ 83: private double value; 84: 85: /** 86: * Creates a new annotation to be displayed at the given location. 87: * 88: * @param text the text (<code>null</code> not permitted). 89: * @param category the category (<code>null</code> not permitted). 90: * @param value the value. 91: */ 92: public CategoryTextAnnotation(String text, Comparable category, 93: double value) { 94: super(text); 95: if (category == null) { 96: throw new IllegalArgumentException("Null 'category' argument."); 97: } 98: this.category = category; 99: this.value = value; 100: this.categoryAnchor = CategoryAnchor.MIDDLE; 101: } 102: 103: /** 104: * Returns the category. 105: * 106: * @return The category (never <code>null</code>). 107: */ 108: public Comparable getCategory() { 109: return this.category; 110: } 111: 112: /** 113: * Sets the category that the annotation attaches to. 114: * 115: * @param category the category (<code>null</code> not permitted). 116: */ 117: public void setCategory(Comparable category) { 118: if (category == null) { 119: throw new IllegalArgumentException("Null 'category' argument."); 120: } 121: this.category = category; 122: } 123: 124: /** 125: * Returns the category anchor point. 126: * 127: * @return The category anchor point. 128: */ 129: public CategoryAnchor getCategoryAnchor() { 130: return this.categoryAnchor; 131: } 132: 133: /** 134: * Sets the category anchor point. 135: * 136: * @param anchor the anchor point (<code>null</code> not permitted). 137: */ 138: public void setCategoryAnchor(CategoryAnchor anchor) { 139: if (anchor == null) { 140: throw new IllegalArgumentException("Null 'anchor' argument."); 141: } 142: this.categoryAnchor = anchor; 143: } 144: 145: /** 146: * Returns the value that the annotation attaches to. 147: * 148: * @return The value. 149: */ 150: public double getValue() { 151: return this.value; 152: } 153: 154: /** 155: * Sets the value. 156: * 157: * @param value the value. 158: */ 159: public void setValue(double value) { 160: this.value = value; 161: } 162: 163: /** 164: * Draws the annotation. 165: * 166: * @param g2 the graphics device. 167: * @param plot the plot. 168: * @param dataArea the data area. 169: * @param domainAxis the domain axis. 170: * @param rangeAxis the range axis. 171: */ 172: public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea, 173: CategoryAxis domainAxis, ValueAxis rangeAxis) { 174: 175: CategoryDataset dataset = plot.getDataset(); 176: int catIndex = dataset.getColumnIndex(this.category); 177: int catCount = dataset.getColumnCount(); 178: 179: float anchorX = 0.0f; 180: float anchorY = 0.0f; 181: PlotOrientation orientation = plot.getOrientation(); 182: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 183: plot.getDomainAxisLocation(), orientation 184: ); 185: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 186: plot.getRangeAxisLocation(), orientation 187: ); 188: 189: if (orientation == PlotOrientation.HORIZONTAL) { 190: anchorY = (float) domainAxis.getCategoryJava2DCoordinate( 191: this.categoryAnchor, catIndex, catCount, dataArea, domainEdge 192: ); 193: anchorX = (float) rangeAxis.valueToJava2D( 194: this.value, dataArea, rangeEdge 195: ); 196: } 197: else if (orientation == PlotOrientation.VERTICAL) { 198: anchorX = (float) domainAxis.getCategoryJava2DCoordinate( 199: this.categoryAnchor, catIndex, catCount, dataArea, domainEdge 200: ); 201: anchorY = (float) rangeAxis.valueToJava2D( 202: this.value, dataArea, rangeEdge 203: ); 204: } 205: g2.setFont(getFont()); 206: g2.setPaint(getPaint()); 207: TextUtilities.drawRotatedString( 208: getText(), 209: g2, 210: anchorX, 211: anchorY, 212: getTextAnchor(), 213: getRotationAngle(), 214: getRotationAnchor() 215: ); 216: 217: } 218: 219: /** 220: * Tests this object for equality with another. 221: * 222: * @param obj the object (<code>null</code> permitted). 223: * 224: * @return <code>true</code> or <code>false</code>. 225: */ 226: public boolean equals(Object obj) { 227: if (obj == this) { 228: return true; 229: } 230: if (!(obj instanceof CategoryTextAnnotation)) { 231: return false; 232: } 233: CategoryTextAnnotation that = (CategoryTextAnnotation) obj; 234: if (!super.equals(obj)) { 235: return false; 236: } 237: if (!this.category.equals(that.getCategory())) { 238: return false; 239: } 240: if (!this.categoryAnchor.equals(that.getCategoryAnchor())) { 241: return false; 242: } 243: if (this.value != that.getValue()) { 244: return false; 245: } 246: return true; 247: } 248: 249: /** 250: * Returns a clone of the annotation. 251: * 252: * @return A clone. 253: * 254: * @throws CloneNotSupportedException this class will not throw this 255: * exception, but subclasses (if any) might. 256: */ 257: public Object clone() throws CloneNotSupportedException { 258: return super.clone(); 259: } 260: 261: }