KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > category > AreaRenderer


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * AreaRenderer.java
29  * -----------------
30  * (C) Copyright 2002-2006, by Jon Iles and Contributors.
31  *
32  * Original Author: Jon Iles;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  * Christian W. Zuckschwerdt;
35  *
36  * $Id: AreaRenderer.java,v 1.6.2.5 2006/10/11 10:25:51 mungady Exp $
37  *
38  * Changes:
39  * --------
40  * 21-May-2002 : Version 1, contributed by John Iles (DG);
41  * 29-May-2002 : Now extends AbstractCategoryItemRenderer (DG);
42  * 11-Jun-2002 : Updated Javadoc comments (DG);
43  * 25-Jun-2002 : Removed unnecessary imports (DG);
44  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
45  * 10-Oct-2002 : Added constructors and basic entity support (DG);
46  * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and
47  * CategoryToolTipGenerator interface (DG);
48  * 05-Nov-2002 : Replaced references to CategoryDataset with TableDataset (DG);
49  * 06-Nov-2002 : Renamed drawCategoryItem() --> drawItem() and now using axis
50  * for category spacing. Renamed AreaCategoryItemRenderer
51  * --> AreaRenderer (DG);
52  * 17-Jan-2003 : Moved plot classes into a separate package (DG);
53  * 25-Mar-2003 : Implemented Serializable (DG);
54  * 10-Apr-2003 : Changed CategoryDataset to KeyedValues2DDataset in
55  * drawItem() method (DG);
56  * 12-May-2003 : Modified to take into account the plot orientation (DG);
57  * 30-Jul-2003 : Modified entity constructor (CZ);
58  * 13-Aug-2003 : Implemented Cloneable (DG);
59  * 07-Oct-2003 : Added renderer state (DG);
60  * 05-Nov-2004 : Modified drawItem() signature (DG);
61  * 20-Apr-2005 : Apply tooltips and URLs to legend items (DG);
62  * 09-Jun-2005 : Use addItemEntity() method from superclass (DG);
63  * ------------- JFREECHART 1.0.x ---------------------------------------------
64  * 11-Oct-2006 : Fixed bug in equals() method (DG);
65  *
66  */

67
68 package org.jfree.chart.renderer.category;
69
70 import java.awt.Graphics2D JavaDoc;
71 import java.awt.Paint JavaDoc;
72 import java.awt.Shape JavaDoc;
73 import java.awt.Stroke JavaDoc;
74 import java.awt.geom.GeneralPath JavaDoc;
75 import java.awt.geom.Rectangle2D JavaDoc;
76 import java.io.Serializable JavaDoc;
77
78 import org.jfree.chart.LegendItem;
79 import org.jfree.chart.axis.CategoryAxis;
80 import org.jfree.chart.axis.ValueAxis;
81 import org.jfree.chart.entity.EntityCollection;
82 import org.jfree.chart.event.RendererChangeEvent;
83 import org.jfree.chart.plot.CategoryPlot;
84 import org.jfree.chart.plot.PlotOrientation;
85 import org.jfree.chart.renderer.AreaRendererEndType;
86 import org.jfree.data.category.CategoryDataset;
87 import org.jfree.ui.RectangleEdge;
88 import org.jfree.util.PublicCloneable;
89
90 /**
91  * A category item renderer that draws area charts. You can use this renderer
92  * with the {@link org.jfree.chart.plot.CategoryPlot} class.
93  */

94 public class AreaRenderer extends AbstractCategoryItemRenderer
95                           implements Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
96
97     /** For serialization. */
98     private static final long serialVersionUID = -4231878281385812757L;
99     
100     /** A flag that controls how the ends of the areas are drawn. */
101     private AreaRendererEndType endType;
102     
103     /**
104      * Creates a new renderer.
105      */

106     public AreaRenderer() {
107         super();
108         this.endType = AreaRendererEndType.TAPER;
109     }
110
111     /**
112      * Returns a token that controls how the renderer draws the end points.
113      * The default value is {@link AreaRendererEndType#TAPER}.
114      *
115      * @return The end type (never <code>null</code>).
116      *
117      * @see #setEndType
118      */

119     public AreaRendererEndType getEndType() {
120         return this.endType;
121     }
122     
123     /**
124      * Sets a token that controls how the renderer draws the end points, and
125      * sends a {@link RendererChangeEvent} to all registered listeners.
126      *
127      * @param type the end type (<code>null</code> not permitted).
128      *
129      * @see #getEndType()
130      */

131     public void setEndType(AreaRendererEndType type) {
132         if (type == null) {
133             throw new IllegalArgumentException JavaDoc("Null 'type' argument.");
134         }
135         this.endType = type;
136         notifyListeners(new RendererChangeEvent(this));
137     }
138     
139     /**
140      * Returns a legend item for a series.
141      *
142      * @param datasetIndex the dataset index (zero-based).
143      * @param series the series index (zero-based).
144      *
145      * @return The legend item.
146      */

147     public LegendItem getLegendItem(int datasetIndex, int series) {
148
149         CategoryPlot cp = getPlot();
150         if (cp == null) {
151             return null;
152         }
153
154         CategoryDataset dataset;
155         dataset = cp.getDataset(datasetIndex);
156         String JavaDoc label = getLegendItemLabelGenerator().generateLabel(dataset,
157                 series);
158         String JavaDoc description = label;
159         String JavaDoc toolTipText = null;
160         if (getLegendItemToolTipGenerator() != null) {
161             toolTipText = getLegendItemToolTipGenerator().generateLabel(
162                     dataset, series);
163         }
164         String JavaDoc urlText = null;
165         if (getLegendItemURLGenerator() != null) {
166             urlText = getLegendItemURLGenerator().generateLabel(dataset,
167                     series);
168         }
169         Shape JavaDoc shape = new Rectangle2D.Double JavaDoc(-4.0, -4.0, 8.0, 8.0);
170         Paint JavaDoc paint = getSeriesPaint(series);
171         Paint JavaDoc outlinePaint = getSeriesOutlinePaint(series);
172         Stroke JavaDoc outlineStroke = getSeriesOutlineStroke(series);
173
174         return new LegendItem(label, description, toolTipText, urlText,
175                 shape, paint, outlineStroke, outlinePaint);
176
177     }
178
179     /**
180      * Draw a single data item.
181      *
182      * @param g2 the graphics device.
183      * @param state the renderer state.
184      * @param dataArea the data plot area.
185      * @param plot the plot.
186      * @param domainAxis the domain axis.
187      * @param rangeAxis the range axis.
188      * @param dataset the dataset.
189      * @param row the row index (zero-based).
190      * @param column the column index (zero-based).
191      * @param pass the pass index.
192      */

193     public void drawItem(Graphics2D JavaDoc g2,
194                          CategoryItemRendererState state,
195                          Rectangle2D JavaDoc dataArea,
196                          CategoryPlot plot,
197                          CategoryAxis domainAxis,
198                          ValueAxis rangeAxis,
199                          CategoryDataset dataset,
200                          int row,
201                          int column,
202                          int pass) {
203
204         // plot non-null values only...
205
Number JavaDoc value = dataset.getValue(row, column);
206         if (value != null) {
207             PlotOrientation orientation = plot.getOrientation();
208             RectangleEdge axisEdge = plot.getDomainAxisEdge();
209             int count = dataset.getColumnCount();
210             float x0 = (float) domainAxis.getCategoryStart(column, count,
211                     dataArea, axisEdge);
212             float x1 = (float) domainAxis.getCategoryMiddle(column, count,
213                     dataArea, axisEdge);
214             float x2 = (float) domainAxis.getCategoryEnd(column, count,
215                     dataArea, axisEdge);
216
217             x0 = Math.round(x0);
218             x1 = Math.round(x1);
219             x2 = Math.round(x2);
220
221             if (this.endType == AreaRendererEndType.TRUNCATE) {
222                 if (column == 0) {
223                     x0 = x1;
224                 }
225                 else if (column == getColumnCount() - 1) {
226                     x2 = x1;
227                 }
228             }
229             
230             double yy1 = value.doubleValue();
231
232             double yy0 = 0.0;
233             if (column > 0) {
234                 Number JavaDoc n0 = dataset.getValue(row, column - 1);
235                 if (n0 != null) {
236                     yy0 = (n0.doubleValue() + yy1) / 2.0;
237                 }
238             }
239
240             double yy2 = 0.0;
241             if (column < dataset.getColumnCount() - 1) {
242                 Number JavaDoc n2 = dataset.getValue(row, column + 1);
243                 if (n2 != null) {
244                     yy2 = (n2.doubleValue() + yy1) / 2.0;
245                 }
246             }
247
248             RectangleEdge edge = plot.getRangeAxisEdge();
249             float y0 = (float) rangeAxis.valueToJava2D(yy0, dataArea, edge);
250             float y1 = (float) rangeAxis.valueToJava2D(yy1, dataArea, edge);
251             float y2 = (float) rangeAxis.valueToJava2D(yy2, dataArea, edge);
252             float yz = (float) rangeAxis.valueToJava2D(0.0, dataArea, edge);
253
254             g2.setPaint(getItemPaint(row, column));
255             g2.setStroke(getItemStroke(row, column));
256
257             GeneralPath JavaDoc area = new GeneralPath JavaDoc();
258
259             if (orientation == PlotOrientation.VERTICAL) {
260                 area.moveTo(x0, yz);
261                 area.lineTo(x0, y0);
262                 area.lineTo(x1, y1);
263                 area.lineTo(x2, y2);
264                 area.lineTo(x2, yz);
265             }
266             else if (orientation == PlotOrientation.HORIZONTAL) {
267                 area.moveTo(yz, x0);
268                 area.lineTo(y0, x0);
269                 area.lineTo(y1, x1);
270                 area.lineTo(y2, x2);
271                 area.lineTo(yz, x2);
272             }
273             area.closePath();
274
275             g2.setPaint(getItemPaint(row, column));
276             g2.fill(area);
277
278             // draw the item labels if there are any...
279
if (isItemLabelVisible(row, column)) {
280                 drawItemLabel(g2, orientation, dataset, row, column, x1, y1,
281                         (value.doubleValue() < 0.0));
282             }
283
284             // add an item entity, if this information is being collected
285
EntityCollection entities = state.getEntityCollection();
286             if (entities != null) {
287                 addItemEntity(entities, dataset, row, column, area);
288             }
289         }
290
291     }
292     
293     /**
294      * Tests this instance for equality with an arbitrary object.
295      *
296      * @param obj the object to test (<code>null</code> permitted).
297      *
298      * @return A boolean.
299      */

300     public boolean equals(Object JavaDoc obj) {
301         if (obj == this) {
302             return true;
303         }
304         if (!(obj instanceof AreaRenderer)) {
305             return false;
306         }
307         AreaRenderer that = (AreaRenderer) obj;
308         if (!this.endType.equals(that.endType)) {
309             return false;
310         }
311         return super.equals(obj);
312     }
313     
314     /**
315      * Returns an independent copy of the renderer.
316      *
317      * @return A clone.
318      *
319      * @throws CloneNotSupportedException should not happen.
320      */

321     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
322         return super.clone();
323     }
324
325 }
326
Popular Tags