KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > annotations > CategoryTextAnnotation


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------------
27  * CategoryTextAnnotation.java
28  * ---------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: CategoryTextAnnotation.java,v 1.6 2005/05/19 15:41:53 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 02-Apr-2003 : Version 1 (DG);
39  * 02-Jul-2003 : Added new text alignment and rotation options (DG);
40  * 04-Jul-2003 : Added a category anchor option (DG);
41  * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
42  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
43  * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities
44  * --> TextUtilities (DG);
45  *
46  */

47
48 package org.jfree.chart.annotations;
49
50 import java.awt.Graphics2D JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.Serializable JavaDoc;
53
54 import org.jfree.chart.axis.CategoryAnchor;
55 import org.jfree.chart.axis.CategoryAxis;
56 import org.jfree.chart.axis.ValueAxis;
57 import org.jfree.chart.plot.CategoryPlot;
58 import org.jfree.chart.plot.Plot;
59 import org.jfree.chart.plot.PlotOrientation;
60 import org.jfree.data.category.CategoryDataset;
61 import org.jfree.text.TextUtilities;
62 import org.jfree.ui.RectangleEdge;
63
64 /**
65  * A text annotation that can be placed on a
66  * {@link org.jfree.chart.plot.CategoryPlot}.
67  */

68 public class CategoryTextAnnotation extends TextAnnotation
69                                     implements CategoryAnnotation,
70                                                Cloneable JavaDoc, Serializable JavaDoc {
71
72     /** For serialization. */
73     private static final long serialVersionUID = 3333360090781320147L;
74     
75     /** The category. */
76     private Comparable JavaDoc category;
77
78     /** The category anchor (START, MIDDLE, or END). */
79     private CategoryAnchor categoryAnchor;
80      
81     /** The value. */
82     private double value;
83
84     /**
85      * Creates a new annotation to be displayed at the given location.
86      *
87      * @param text the text (<code>null</code> not permitted).
88      * @param category the category (<code>null</code> not permitted).
89      * @param value the value.
90      */

91     public CategoryTextAnnotation(String JavaDoc text, Comparable JavaDoc category,
92                                   double value) {
93         super(text);
94         if (category == null) {
95             throw new IllegalArgumentException JavaDoc("Null 'category' argument.");
96         }
97         this.category = category;
98         this.value = value;
99         this.categoryAnchor = CategoryAnchor.MIDDLE;
100     }
101
102     /**
103      * Returns the category.
104      *
105      * @return The category (never <code>null</code>).
106      */

107     public Comparable JavaDoc getCategory() {
108         return this.category;
109     }
110     
111     /**
112      * Sets the category that the annotation attaches to.
113      *
114      * @param category the category (<code>null</code> not permitted).
115      */

116     public void setCategory(Comparable JavaDoc category) {
117         if (category == null) {
118             throw new IllegalArgumentException JavaDoc("Null 'category' argument.");
119         }
120         this.category = category;
121     }
122     
123     /**
124      * Returns the category anchor point.
125      *
126      * @return The category anchor point.
127      */

128     public CategoryAnchor getCategoryAnchor() {
129         return this.categoryAnchor;
130     }
131     
132     /**
133      * Sets the category anchor point.
134      *
135      * @param anchor the anchor point (<code>null</code> not permitted).
136      */

137     public void setCategoryAnchor(CategoryAnchor anchor) {
138         if (anchor == null) {
139             throw new IllegalArgumentException JavaDoc("Null 'anchor' argument.");
140         }
141         this.categoryAnchor = anchor;
142     }
143     
144     /**
145      * Returns the value that the annotation attaches to.
146      *
147      * @return The value.
148      */

149     public double getValue() {
150         return this.value;
151     }
152     
153     /**
154      * Sets the value.
155      *
156      * @param value the value.
157      */

158     public void setValue(double value) {
159         this.value = value;
160     }
161     
162     /**
163      * Draws the annotation.
164      *
165      * @param g2 the graphics device.
166      * @param plot the plot.
167      * @param dataArea the data area.
168      * @param domainAxis the domain axis.
169      * @param rangeAxis the range axis.
170      */

171     public void draw(Graphics2D JavaDoc g2, CategoryPlot plot, Rectangle2D JavaDoc dataArea,
172                      CategoryAxis domainAxis, ValueAxis rangeAxis) {
173
174         CategoryDataset dataset = plot.getDataset();
175         int catIndex = dataset.getColumnIndex(this.category);
176         int catCount = dataset.getColumnCount();
177
178         float anchorX = 0.0f;
179         float anchorY = 0.0f;
180         PlotOrientation orientation = plot.getOrientation();
181         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
182             plot.getDomainAxisLocation(), orientation
183         );
184         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
185             plot.getRangeAxisLocation(), orientation
186         );
187         
188         if (orientation == PlotOrientation.HORIZONTAL) {
189             anchorY = (float) domainAxis.getCategoryJava2DCoordinate(
190                 this.categoryAnchor, catIndex, catCount, dataArea, domainEdge
191             );
192             anchorX = (float) rangeAxis.valueToJava2D(
193                 this.value, dataArea, rangeEdge
194             );
195         }
196         else if (orientation == PlotOrientation.VERTICAL) {
197             anchorX = (float) domainAxis.getCategoryJava2DCoordinate(
198                 this.categoryAnchor, catIndex, catCount, dataArea, domainEdge
199             );
200             anchorY = (float) rangeAxis.valueToJava2D(
201                 this.value, dataArea, rangeEdge
202             );
203         }
204         g2.setFont(getFont());
205         g2.setPaint(getPaint());
206         TextUtilities.drawRotatedString(
207             getText(),
208             g2,
209             anchorX,
210             anchorY,
211             getTextAnchor(),
212             getRotationAngle(),
213             getRotationAnchor()
214         );
215
216     }
217
218     /**
219      * Tests this object for equality with another.
220      *
221      * @param obj the object (<code>null</code> permitted).
222      *
223      * @return <code>true</code> or <code>false</code>.
224      */

225     public boolean equals(Object JavaDoc obj) {
226         if (obj == this) {
227             return true;
228         }
229         if (!(obj instanceof CategoryTextAnnotation)) {
230             return false;
231         }
232         CategoryTextAnnotation that = (CategoryTextAnnotation) obj;
233         if (!super.equals(obj)) {
234             return false;
235         }
236         if (!this.category.equals(that.getCategory())) {
237             return false;
238         }
239         if (!this.categoryAnchor.equals(that.getCategoryAnchor())) {
240             return false;
241         }
242         if (this.value != that.getValue()) {
243             return false;
244         }
245         return true;
246     }
247     
248     /**
249      * Returns a clone of the annotation.
250      *
251      * @return A clone.
252      *
253      * @throws CloneNotSupportedException this class will not throw this
254      * exception, but subclasses (if any) might.
255      */

256     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
257         return super.clone();
258     }
259     
260 }
261
Popular Tags