KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > CategoryAxis3D


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  * CategoryAxis3D.java
28  * -------------------
29  * (C) Copyright 2003-2005, by Klaus Rheinwald and Contributors.
30  *
31  * Original Author: Klaus Rheinwald;
32  * Contributor(s): Tin Luu,
33  * David Gilbert (for Object Refinery Limited);
34  *
35  * Changes
36  * -------
37  * 19-Feb-2003 : File creation;
38  * 21-Mar-2003 : Added to JFreeChart CVS, see bug id 685501 for code
39  * contribution from KR (DG);
40  * 26-Mar-2003 : Implemented Serializable (DG);
41  * 13-May-2003 : Renamed HorizontalCategoryAxis3D --> CategoryAxis3D, and
42  * modified to take into account the plot orientation (DG);
43  * 14-Aug-2003 : Implemented Cloneable (DG);
44  * 21-Aug-2003 : Fixed draw() method bugs (DG);
45  * 22-Mar-2004 : Added workaround for bug 920959 (null pointer exception with
46  * no renderer) (DG);
47  *
48  */

49
50 package org.jfree.chart.axis;
51
52 import java.awt.Graphics2D JavaDoc;
53 import java.awt.geom.Rectangle2D JavaDoc;
54 import java.io.Serializable JavaDoc;
55
56 import org.jfree.chart.Effect3D;
57 import org.jfree.chart.plot.CategoryPlot;
58 import org.jfree.chart.plot.PlotRenderingInfo;
59 import org.jfree.chart.renderer.category.CategoryItemRenderer;
60 import org.jfree.ui.RectangleEdge;
61
62 /**
63  * An axis that displays categories and has a 3D effect.
64  * Used for bar charts and line charts.
65  *
66  * @author Klaus Rheinwald
67  */

68 public class CategoryAxis3D extends CategoryAxis
69                             implements Cloneable JavaDoc, Serializable JavaDoc {
70
71     /** For serialization. */
72     private static final long serialVersionUID = 4114732251353700972L;
73     
74     /**
75      * Creates a new axis.
76      */

77     public CategoryAxis3D() {
78         this(null);
79     }
80     
81     /**
82      * Creates a new axis using default attribute values.
83      *
84      * @param label the axis label (<code>null</code> permitted).
85      */

86     public CategoryAxis3D(String JavaDoc label) {
87         super(label);
88     }
89
90     /**
91      * Draws the axis on a Java 2D graphics device (such as the screen or a
92      * printer).
93      *
94      * @param g2 the graphics device (<code>null</code> not permitted).
95      * @param cursor the cursor location.
96      * @param plotArea the area within which the axis should be drawn
97      * (<code>null</code> not permitted).
98      * @param dataArea the area within which the plot is being drawn
99      * (<code>null</code> not permitted).
100      * @param edge the location of the axis (<code>null</code> not permitted).
101      * @param plotState collects information about the plot (<code>null</code>
102      * permitted).
103      *
104      * @return The axis state (never <code>null</code>).
105      */

106     public AxisState draw(Graphics2D JavaDoc g2,
107                           double cursor,
108                           Rectangle2D JavaDoc plotArea,
109                           Rectangle2D JavaDoc dataArea,
110                           RectangleEdge edge,
111                           PlotRenderingInfo plotState) {
112
113         // if the axis is not visible, don't draw it...
114
if (!isVisible()) {
115             return new AxisState(cursor);
116         }
117
118         // calculate the adjusted data area taking into account the 3D effect...
119
// this assumes that there is a 3D renderer, all this 3D effect is a
120
// bit of an ugly hack...
121
CategoryPlot plot = (CategoryPlot) getPlot();
122
123         Rectangle2D JavaDoc adjustedDataArea = new Rectangle2D.Double JavaDoc();
124         if (plot.getRenderer() instanceof Effect3D) {
125             Effect3D e3D = (Effect3D) plot.getRenderer();
126             double adjustedX = dataArea.getMinX();
127             double adjustedY = dataArea.getMinY();
128             double adjustedW = dataArea.getWidth() - e3D.getXOffset();
129             double adjustedH = dataArea.getHeight() - e3D.getYOffset();
130
131             if (edge == RectangleEdge.LEFT || edge == RectangleEdge.BOTTOM) {
132                 adjustedY += e3D.getYOffset();
133             }
134             else if (edge == RectangleEdge.RIGHT || edge == RectangleEdge.TOP) {
135                 adjustedX += e3D.getXOffset();
136             }
137             adjustedDataArea.setRect(
138                 adjustedX, adjustedY, adjustedW, adjustedH
139             );
140         }
141         else {
142             adjustedDataArea.setRect(dataArea);
143         }
144
145         // draw the category labels and axis label
146
AxisState state = new AxisState(cursor);
147         state = drawCategoryLabels(
148             g2, adjustedDataArea, edge, state, plotState
149         );
150         state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state);
151
152         return state;
153         
154     }
155     
156     /**
157      * Returns the Java 2D coordinate for a category.
158      *
159      * @param anchor the anchor point.
160      * @param category the category index.
161      * @param categoryCount the category count.
162      * @param area the data area.
163      * @param edge the location of the axis.
164      *
165      * @return The coordinate.
166      */

167     public double getCategoryJava2DCoordinate(CategoryAnchor anchor,
168                                               int category,
169                                               int categoryCount,
170                                               Rectangle2D JavaDoc area,
171                                               RectangleEdge edge) {
172     
173         double result = 0.0;
174         Rectangle2D JavaDoc adjustedArea = area;
175         CategoryPlot plot = (CategoryPlot) getPlot();
176         CategoryItemRenderer renderer = plot.getRenderer();
177         if (renderer instanceof Effect3D) {
178             Effect3D e3D = (Effect3D) renderer;
179             double adjustedX = area.getMinX();
180             double adjustedY = area.getMinY();
181             double adjustedW = area.getWidth() - e3D.getXOffset();
182             double adjustedH = area.getHeight() - e3D.getYOffset();
183
184             if (edge == RectangleEdge.LEFT || edge == RectangleEdge.BOTTOM) {
185                 adjustedY += e3D.getYOffset();
186             }
187             else if (edge == RectangleEdge.RIGHT || edge == RectangleEdge.TOP) {
188                 adjustedX += e3D.getXOffset();
189             }
190             adjustedArea = new Rectangle2D.Double JavaDoc(
191                 adjustedX, adjustedY, adjustedW, adjustedH
192             );
193         }
194
195         if (anchor == CategoryAnchor.START) {
196             result = getCategoryStart(
197                 category, categoryCount, adjustedArea, edge
198             );
199         }
200         else if (anchor == CategoryAnchor.MIDDLE) {
201             result = getCategoryMiddle(
202                 category, categoryCount, adjustedArea, edge
203             );
204         }
205         else if (anchor == CategoryAnchor.END) {
206             result = getCategoryEnd(
207                 category, categoryCount, adjustedArea, edge
208             );
209         }
210         return result;
211                                                       
212     }
213                                               
214     /**
215      * Returns a clone of the axis.
216      *
217      * @return A clone.
218      *
219      * @throws CloneNotSupportedException If the axis is not cloneable for
220      * some reason.
221      */

222     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
223         return super.clone();
224     }
225     
226 }
227
Popular Tags