KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------
23  * AreaRenderer.java
24  * -----------------
25  * (C) Copyright 2002, 2003, by Jon Iles and Contributors.
26  *
27  * Original Author: Jon Iles;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Christian W. Zuckschwerdt;
30  *
31  * $Id: AreaRenderer.java,v 1.23 2003/11/03 14:21:27 mungady Exp $
32  *
33  * Changes:
34  * --------
35  * 21-May-2002 : Version 1, contributed by John Iles (DG);
36  * 29-May-2002 : Now extends AbstractCategoryItemRenderer (DG);
37  * 11-Jun-2002 : Updated Javadoc comments (DG);
38  * 25-Jun-2002 : Removed unnecessary imports (DG);
39  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 10-Oct-2002 : Added constructors and basic entity support (DG);
41  * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and CategoryToolTipGenerator
42  * interface (DG);
43  * 05-Nov-2002 : Replaced references to CategoryDataset with TableDataset (DG);
44  * 06-Nov-2002 : Renamed drawCategoryItem(...) --> drawItem(...) and now using axis for
45  * category spacing. Renamed AreaCategoryItemRenderer --> AreaRenderer (DG);
46  * 17-Jan-2003 : Moved plot classes into a separate package (DG);
47  * 25-Mar-2003 : Implemented Serializable (DG);
48  * 10-Apr-2003 : Changed CategoryDataset to KeyedValues2DDataset in drawItem(...) method (DG);
49  * 12-May-2003 : Modified to take into account the plot orientation (DG);
50  * 30-Jul-2003 : Modified entity constructor (CZ);
51  * 13-Aug-2003 : Implemented Cloneable (DG);
52  * 07-Oct-2003 : Added renderer state (DG);
53  *
54  */

55
56 package org.jfree.chart.renderer;
57
58 import java.awt.Graphics2D JavaDoc;
59 import java.awt.geom.GeneralPath JavaDoc;
60 import java.awt.geom.Rectangle2D JavaDoc;
61 import java.io.Serializable JavaDoc;
62
63 import org.jfree.chart.axis.CategoryAxis;
64 import org.jfree.chart.axis.ValueAxis;
65 import org.jfree.chart.entity.CategoryItemEntity;
66 import org.jfree.chart.entity.EntityCollection;
67 import org.jfree.chart.labels.CategoryItemLabelGenerator;
68 import org.jfree.chart.plot.CategoryPlot;
69 import org.jfree.chart.plot.PlotOrientation;
70 import org.jfree.data.CategoryDataset;
71 import org.jfree.ui.RectangleEdge;
72 import org.jfree.util.PublicCloneable;
73
74 /**
75  * A category item renderer that draws area charts.
76  * <p>
77  * You can use this renderer with the {@link org.jfree.chart.plot.CategoryPlot} class.
78  *
79  * @author Jon Iles
80  */

81 public class AreaRenderer extends AbstractCategoryItemRenderer
82                           implements Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
83
84     /**
85      * Creates a new renderer.
86      */

87     public AreaRenderer() {
88         super();
89     }
90
91     /**
92      * Draw a single data item.
93      *
94      * @param g2 the graphics device.
95      * @param state the renderer state.
96      * @param dataArea the data plot area.
97      * @param plot the plot.
98      * @param domainAxis the domain axis.
99      * @param rangeAxis the range axis.
100      * @param dataset the dataset.
101      * @param row the row index (zero-based).
102      * @param column the column index (zero-based).
103      */

104     public void drawItem(Graphics2D JavaDoc g2,
105                          CategoryItemRendererState state,
106                          Rectangle2D JavaDoc dataArea,
107                          CategoryPlot plot,
108                          CategoryAxis domainAxis,
109                          ValueAxis rangeAxis,
110                          CategoryDataset dataset,
111                          int row,
112                          int column) {
113
114         // plot non-null values only...
115
Number JavaDoc value = dataset.getValue(row, column);
116         if (value != null) {
117             PlotOrientation orientation = plot.getOrientation();
118             RectangleEdge axisEdge = plot.getDomainAxisEdge();
119             int count = dataset.getColumnCount();
120             float x0 = (float) domainAxis.getCategoryStart(column, count, dataArea, axisEdge);
121             float x1 = (float) domainAxis.getCategoryMiddle(column, count, dataArea, axisEdge);
122             float x2 = (float) domainAxis.getCategoryEnd(column, count, dataArea, axisEdge);
123
124             x0 = Math.round(x0);
125             x1 = Math.round(x1);
126             x2 = Math.round(x2);
127
128             double yy1 = value.doubleValue();
129
130             double yy0 = 0.0;
131             if (column > 0) {
132                 Number JavaDoc n0 = dataset.getValue(row, column - 1);
133                 if (n0 != null) {
134                     yy0 = (n0.doubleValue() + yy1) / 2.0;
135                 }
136             }
137
138             double yy2 = 0.0;
139             if (column < dataset.getColumnCount() - 1) {
140                 Number JavaDoc n2 = dataset.getValue(row, column + 1);
141                 if (n2 != null) {
142                     yy2 = (n2.doubleValue() + yy1) / 2.0;
143                 }
144             }
145
146             RectangleEdge edge = plot.getRangeAxisEdge();
147             float y0 = (float) rangeAxis.translateValueToJava2D(yy0, dataArea, edge);
148             float y1 = (float) rangeAxis.translateValueToJava2D(yy1, dataArea, edge);
149             float y2 = (float) rangeAxis.translateValueToJava2D(yy2, dataArea, edge);
150             float yz = (float) rangeAxis.translateValueToJava2D(0.0, dataArea, edge);
151
152             g2.setPaint(getItemPaint(row, column));
153             g2.setStroke(getItemStroke(row, column));
154
155             GeneralPath JavaDoc area = new GeneralPath JavaDoc();
156
157             if (orientation == PlotOrientation.VERTICAL) {
158                 area.moveTo(x0, yz);
159                 area.lineTo(x0, y0);
160                 area.lineTo(x1, y1);
161                 area.lineTo(x2, y2);
162                 area.lineTo(x2, yz);
163             }
164             else if (orientation == PlotOrientation.HORIZONTAL) {
165                 area.moveTo(yz, x0);
166                 area.lineTo(y0, x0);
167                 area.lineTo(y1, x1);
168                 area.lineTo(y2, x2);
169                 area.lineTo(yz, x2);
170             }
171             area.closePath();
172
173             g2.setPaint(getItemPaint(row, column));
174             g2.fill(area);
175
176             // draw the item labels if there are any...
177
if (isItemLabelVisible(row, column)) {
178                 drawItemLabel(g2, orientation, dataset, row, column, x1, y1,
179                               (value.doubleValue() < 0.0));
180             }
181
182             // collect entity and tool tip information...
183
if (state.getInfo() != null) {
184                 EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
185                 if (entities != null) {
186                     String JavaDoc tip = null;
187                     CategoryItemLabelGenerator generator = getBaseItemLabelGenerator();
188                     if (generator != null) {
189                         tip = generator.generateToolTip(dataset, row, column);
190                     }
191                     String JavaDoc url = null;
192                     if (getItemURLGenerator(row, column) != null) {
193                         url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
194                     }
195                     Comparable JavaDoc columnKey = dataset.getColumnKey(column);
196                     CategoryItemEntity entity = new CategoryItemEntity(
197                         area, tip, url, dataset, row, columnKey, column
198                     );
199                     entities.addEntity(entity);
200                 }
201             }
202         }
203
204     }
205     
206     /**
207      * Returns an independent copy of the renderer.
208      *
209      * @return A clone.
210      *
211      * @throws CloneNotSupportedException should not happen.
212      */

213     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
214         return super.clone();
215     }
216
217 }
218
Popular Tags