KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > xy > XYAreaRenderer2


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  * XYAreaRenderer2.java
29  * --------------------
30  * (C) Copyright 2004-2006, by Hari and Contributors.
31  *
32  * Original Author: Hari (ourhari@hotmail.com);
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  * Richard Atkinson;
35  * Christian W. Zuckschwerdt;
36  *
37  * $Id: XYAreaRenderer2.java,v 1.12.2.5 2006/09/22 08:53:48 mungady Exp $
38  *
39  * Changes:
40  * --------
41  * 03-Apr-2002 : Version 1, contributed by Hari. This class is based on the
42  * StandardXYItemRenderer class (DG);
43  * 09-Apr-2002 : Removed the translated zero from the drawItem method -
44  * overridden the initialise() method to calculate it (DG);
45  * 30-May-2002 : Added tool tip generator to constructor to match super
46  * class (DG);
47  * 25-Jun-2002 : Removed unnecessary local variable (DG);
48  * 05-Aug-2002 : Small modification to drawItem method to support URLs for
49  * HTML image maps (RA);
50  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
51  * 07-Nov-2002 : Renamed AreaXYItemRenderer --> XYAreaRenderer (DG);
52  * 25-Mar-2003 : Implemented Serializable (DG);
53  * 01-May-2003 : Modified drawItem() method signature (DG);
54  * 27-Jul-2003 : Made line and polygon properties protected rather than
55  * private (RA);
56  * 30-Jul-2003 : Modified entity constructor (CZ);
57  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
58  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
59  * 07-Oct-2003 : Added renderer state (DG);
60  * 08-Dec-2003 : Modified hotspot for chart entity (DG);
61  * 10-Feb-2004 : Changed the drawItem() method to make cut-and-paste
62  * overriding easier. Also moved state class into this
63  * class (DG);
64  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed
65  * XYToolTipGenerator --> XYItemLabelGenerator (DG);
66  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
67  * getYValue() (DG);
68  * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
69  * 19-Jan-2005 : Now accesses only primitives from the dataset (DG);
70  * 21-Mar-2005 : Override getLegendItem() (DG);
71  * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
72  *
73  */

74
75 package org.jfree.chart.renderer.xy;
76
77 import java.awt.Graphics2D JavaDoc;
78 import java.awt.Paint JavaDoc;
79 import java.awt.Polygon JavaDoc;
80 import java.awt.Shape JavaDoc;
81 import java.awt.Stroke JavaDoc;
82 import java.awt.geom.GeneralPath JavaDoc;
83 import java.awt.geom.Rectangle2D JavaDoc;
84 import java.io.IOException JavaDoc;
85 import java.io.ObjectInputStream JavaDoc;
86 import java.io.ObjectOutputStream JavaDoc;
87 import java.io.Serializable JavaDoc;
88
89 import org.jfree.chart.LegendItem;
90 import org.jfree.chart.axis.ValueAxis;
91 import org.jfree.chart.entity.EntityCollection;
92 import org.jfree.chart.entity.XYItemEntity;
93 import org.jfree.chart.event.RendererChangeEvent;
94 import org.jfree.chart.labels.XYSeriesLabelGenerator;
95 import org.jfree.chart.labels.XYToolTipGenerator;
96 import org.jfree.chart.plot.CrosshairState;
97 import org.jfree.chart.plot.PlotOrientation;
98 import org.jfree.chart.plot.PlotRenderingInfo;
99 import org.jfree.chart.plot.XYPlot;
100 import org.jfree.chart.urls.XYURLGenerator;
101 import org.jfree.data.xy.XYDataset;
102 import org.jfree.io.SerialUtilities;
103 import org.jfree.util.PublicCloneable;
104
105 /**
106  * Area item renderer for an {@link XYPlot}.
107  */

108 public class XYAreaRenderer2 extends AbstractXYItemRenderer
109                              implements XYItemRenderer,
110                                         Cloneable JavaDoc,
111                                         PublicCloneable,
112                                         Serializable JavaDoc {
113
114     /** For serialization. */
115     private static final long serialVersionUID = -7378069681579984133L;
116
117     /** A flag indicating whether or not lines are drawn between XY points. */
118     private boolean plotLines;
119
120     /** A flag that controls whether or not the outline is shown. */
121     private boolean showOutline;
122
123     /**
124      * The shape used to represent an area in each legend item (this should
125      * never be <code>null</code>).
126      */

127     private transient Shape JavaDoc legendArea;
128
129     /**
130      * Constructs a new renderer.
131      */

132     public XYAreaRenderer2() {
133         this(null, null);
134     }
135
136     /**
137      * Constructs a new renderer.
138      * <p>
139      * To specify the type of renderer, use one of the constants: SHAPES, LINES,
140      * SHAPES_AND_LINES, AREA or AREA_AND_SHAPES.
141      *
142      * @param labelGenerator the tool tip generator to use. <code>null</code>
143      * is none.
144      * @param urlGenerator the URL generator (null permitted).
145      */

146     public XYAreaRenderer2(XYToolTipGenerator labelGenerator,
147                            XYURLGenerator urlGenerator) {
148         super();
149         this.plotLines = false;
150         this.showOutline = false;
151         setBaseToolTipGenerator(labelGenerator);
152         setURLGenerator(urlGenerator);
153         GeneralPath JavaDoc area = new GeneralPath JavaDoc();
154         area.moveTo(0.0f, -4.0f);
155         area.lineTo(3.0f, -2.0f);
156         area.lineTo(4.0f, 4.0f);
157         area.lineTo(-4.0f, 4.0f);
158         area.lineTo(-3.0f, -2.0f);
159         area.closePath();
160         this.legendArea = area;
161     }
162
163     /**
164      * Returns a flag that controls whether or not outlines of the areas are
165      * drawn.
166      *
167      * @return The flag.
168      */

169     public boolean isOutline() {
170         return this.showOutline;
171     }
172
173     /**
174      * Sets a flag that controls whether or not outlines of the areas are drawn.
175      *
176      * @param show the flag.
177      */

178     public void setOutline(boolean show) {
179         this.showOutline = show;
180     }
181
182     /**
183      * Returns true if lines are being plotted by the renderer.
184      *
185      * @return <code>true</code> if lines are being plotted by the renderer.
186      */

187     public boolean getPlotLines() {
188         return this.plotLines;
189     }
190
191     /**
192      * Returns the shape used to represent an area in the legend.
193      *
194      * @return The legend area (never <code>null</code>).
195      */

196     public Shape JavaDoc getLegendArea() {
197         return this.legendArea;
198     }
199     
200     /**
201      * Sets the shape used as an area in each legend item and sends a
202      * {@link RendererChangeEvent} to all registered listeners.
203      *
204      * @param area the area (<code>null</code> not permitted).
205      */

206     public void setLegendArea(Shape JavaDoc area) {
207         if (area == null) {
208             throw new IllegalArgumentException JavaDoc("Null 'area' argument.");
209         }
210         this.legendArea = area;
211         notifyListeners(new RendererChangeEvent(this));
212     }
213
214     /**
215      * Returns a default legend item for the specified series. Subclasses
216      * should override this method to generate customised items.
217      *
218      * @param datasetIndex the dataset index (zero-based).
219      * @param series the series index (zero-based).
220      *
221      * @return A legend item for the series.
222      */

223     public LegendItem getLegendItem(int datasetIndex, int series) {
224         LegendItem result = null;
225         XYPlot xyplot = getPlot();
226         if (xyplot != null) {
227             XYDataset dataset = xyplot.getDataset(datasetIndex);
228             if (dataset != null) {
229                 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator();
230                 String JavaDoc label = lg.generateLabel(dataset, series);
231                 String JavaDoc description = label;
232                 String JavaDoc toolTipText = null;
233                 if (getLegendItemToolTipGenerator() != null) {
234                     toolTipText = getLegendItemToolTipGenerator().generateLabel(
235                             dataset, series);
236                 }
237                 String JavaDoc urlText = null;
238                 if (getLegendItemURLGenerator() != null) {
239                     urlText = getLegendItemURLGenerator().generateLabel(
240                             dataset, series);
241                 }
242                 Paint JavaDoc paint = getSeriesPaint(series);
243                 result = new LegendItem(label, description, toolTipText,
244                         urlText, this.legendArea, paint);
245             }
246         }
247         return result;
248     }
249     
250     /**
251      * Draws the visual representation of a single data item.
252      *
253      * @param g2 the graphics device.
254      * @param state the renderer state.
255      * @param dataArea the area within which the data is being drawn.
256      * @param info collects information about the drawing.
257      * @param plot the plot (can be used to obtain standard color
258      * information etc).
259      * @param domainAxis the domain axis.
260      * @param rangeAxis the range axis.
261      * @param dataset the dataset.
262      * @param series the series index (zero-based).
263      * @param item the item index (zero-based).
264      * @param crosshairState crosshair information for the plot
265      * (<code>null</code> permitted).
266      * @param pass the pass index.
267      */

268     public void drawItem(Graphics2D JavaDoc g2,
269                          XYItemRendererState state,
270                          Rectangle2D JavaDoc dataArea,
271                          PlotRenderingInfo info,
272                          XYPlot plot,
273                          ValueAxis domainAxis,
274                          ValueAxis rangeAxis,
275                          XYDataset dataset,
276                          int series,
277                          int item,
278                          CrosshairState crosshairState,
279                          int pass) {
280         
281         if (!getItemVisible(series, item)) {
282             return;
283         }
284         // get the data point...
285
double x1 = dataset.getXValue(series, item);
286         double y1 = dataset.getYValue(series, item);
287         if (Double.isNaN(y1)) {
288             y1 = 0.0;
289         }
290         
291         double transX1 = domainAxis.valueToJava2D(x1, dataArea,
292                 plot.getDomainAxisEdge());
293         double transY1 = rangeAxis.valueToJava2D(y1, dataArea,
294                 plot.getRangeAxisEdge());
295         
296         // get the previous point and the next point so we can calculate a
297
// "hot spot" for the area (used by the chart entity)...
298
double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
299         double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
300         if (Double.isNaN(y0)) {
301             y0 = 0.0;
302         }
303         double transX0 = domainAxis.valueToJava2D(x0, dataArea,
304                 plot.getDomainAxisEdge());
305         double transY0 = rangeAxis.valueToJava2D(y0, dataArea,
306                 plot.getRangeAxisEdge());
307         
308         int itemCount = dataset.getItemCount(series);
309         double x2 = dataset.getXValue(series, Math.min(item + 1,
310                 itemCount - 1));
311         double y2 = dataset.getYValue(series, Math.min(item + 1,
312                 itemCount - 1));
313         if (Double.isNaN(y2)) {
314             y2 = 0.0;
315         }
316         double transX2 = domainAxis.valueToJava2D(x2, dataArea,
317                 plot.getDomainAxisEdge());
318         double transY2 = rangeAxis.valueToJava2D(y2, dataArea,
319                 plot.getRangeAxisEdge());
320         
321         double transZero = rangeAxis.valueToJava2D(0.0, dataArea,
322                 plot.getRangeAxisEdge());
323         Polygon JavaDoc hotspot = null;
324         if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
325             hotspot = new Polygon JavaDoc();
326             hotspot.addPoint((int) transZero,
327                     (int) ((transX0 + transX1) / 2.0));
328             hotspot.addPoint((int) ((transY0 + transY1) / 2.0),
329                     (int) ((transX0 + transX1) / 2.0));
330             hotspot.addPoint((int) transY1, (int) transX1);
331             hotspot.addPoint((int) ((transY1 + transY2) / 2.0),
332                     (int) ((transX1 + transX2) / 2.0));
333             hotspot.addPoint((int) transZero,
334                     (int) ((transX1 + transX2) / 2.0));
335         }
336         else { // vertical orientation
337
hotspot = new Polygon JavaDoc();
338             hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
339                     (int) transZero);
340             hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
341                     (int) ((transY0 + transY1) / 2.0));
342             hotspot.addPoint((int) transX1, (int) transY1);
343             hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
344                     (int) ((transY1 + transY2) / 2.0));
345             hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
346                     (int) transZero);
347         }
348                 
349         PlotOrientation orientation = plot.getOrientation();
350         Paint JavaDoc paint = getItemPaint(series, item);
351         Stroke JavaDoc stroke = getItemStroke(series, item);
352         g2.setPaint(paint);
353         g2.setStroke(stroke);
354
355         if (getPlotLines()) {
356             if (item > 0) {
357                 if (plot.getOrientation() == PlotOrientation.VERTICAL) {
358                     state.workingLine.setLine(transX0, transY0, transX1,
359                             transY1);
360                 }
361                 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
362                     state.workingLine.setLine(transY0, transX0, transY1,
363                             transX1);
364                 }
365                 g2.draw(state.workingLine);
366             }
367         }
368
369         // Check if the item is the last item for the series.
370
// and number of items > 0. We can't draw an area for a single point.
371
g2.fill(hotspot);
372
373         // draw an outline around the Area.
374
if (isOutline()) {
375             g2.setStroke(getSeriesOutlineStroke(series));
376             g2.setPaint(getSeriesOutlinePaint(series));
377             g2.draw(hotspot);
378         }
379         updateCrosshairValues(
380             crosshairState, x1, y1, transX1, transY1, orientation
381         );
382         
383         // collect entity and tool tip information...
384
if (state.getInfo() != null) {
385             EntityCollection entities = state.getEntityCollection();
386             if (entities != null && hotspot != null) {
387                 String JavaDoc tip = null;
388                 XYToolTipGenerator generator = getToolTipGenerator(
389                     series, item
390                 );
391                 if (generator != null) {
392                     tip = generator.generateToolTip(dataset, series, item);
393                 }
394                 String JavaDoc url = null;
395                 if (getURLGenerator() != null) {
396                     url = getURLGenerator().generateURL(dataset, series, item);
397                 }
398                 XYItemEntity entity = new XYItemEntity(hotspot, dataset,
399                         series, item, tip, url);
400                 entities.add(entity);
401             }
402         }
403
404     }
405
406     /**
407      * Returns a clone of the renderer.
408      *
409      * @return A clone.
410      *
411      * @throws CloneNotSupportedException if the renderer cannot be cloned.
412      */

413     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
414         return super.clone();
415     }
416     
417     /**
418      * Provides serialization support.
419      *
420      * @param stream the input stream.
421      *
422      * @throws IOException if there is an I/O error.
423      * @throws ClassNotFoundException if there is a classpath problem.
424      */

425     private void readObject(ObjectInputStream JavaDoc stream)
426             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
427         stream.defaultReadObject();
428         this.legendArea = SerialUtilities.readShape(stream);
429     }
430     
431     /**
432      * Provides serialization support.
433      *
434      * @param stream the output stream.
435      *
436      * @throws IOException if there is an I/O error.
437      */

438     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
439         stream.defaultWriteObject();
440         SerialUtilities.writeShape(this.legendArea, stream);
441     }
442
443 }
444
445
Popular Tags