KickJava   Java API By Example, From Geeks To Geeks.

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


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
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  * YIntervalRenderer.java
29  * ----------------------
30  * (C) Copyright 2002-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: YIntervalRenderer.java,v 1.7.2.1 2005/10/25 20:56:21 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 05-Nov-2002 : Version 1 (DG);
40  * 25-Mar-2003 : Implemented Serializable (DG);
41  * 01-May-2003 : Modified drawItem() method signature (DG);
42  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
43  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
44  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
45  * 27-Sep-2004 : Access double values from dataset (DG);
46  * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
47  *
48  */

49
50 package org.jfree.chart.renderer.xy;
51
52 import java.awt.Graphics2D JavaDoc;
53 import java.awt.Paint JavaDoc;
54 import java.awt.Shape JavaDoc;
55 import java.awt.Stroke JavaDoc;
56 import java.awt.geom.Line2D JavaDoc;
57 import java.awt.geom.Rectangle2D JavaDoc;
58 import java.io.Serializable JavaDoc;
59
60 import org.jfree.chart.axis.ValueAxis;
61 import org.jfree.chart.entity.EntityCollection;
62 import org.jfree.chart.entity.XYItemEntity;
63 import org.jfree.chart.labels.XYToolTipGenerator;
64 import org.jfree.chart.plot.CrosshairState;
65 import org.jfree.chart.plot.PlotOrientation;
66 import org.jfree.chart.plot.PlotRenderingInfo;
67 import org.jfree.chart.plot.XYPlot;
68 import org.jfree.data.xy.IntervalXYDataset;
69 import org.jfree.data.xy.XYDataset;
70 import org.jfree.ui.RectangleEdge;
71 import org.jfree.util.PublicCloneable;
72 import org.jfree.util.ShapeUtilities;
73
74 /**
75  * A renderer that draws a line connecting the start and end Y values for an
76  * {@link XYPlot}.
77  */

78 public class YIntervalRenderer extends AbstractXYItemRenderer
79                                implements XYItemRenderer,
80                                           Cloneable JavaDoc,
81                                           PublicCloneable,
82                                           Serializable JavaDoc {
83
84     private static final long serialVersionUID = -2951586537224143260L;
85     
86     /**
87      * The default constructor.
88      */

89     public YIntervalRenderer() {
90         super();
91     }
92
93     /**
94      * Draws the visual representation of a single data item.
95      *
96      * @param g2 the graphics device.
97      * @param state the renderer state.
98      * @param dataArea the area within which the plot is being drawn.
99      * @param info collects information about the drawing.
100      * @param plot the plot (can be used to obtain standard color
101      * information etc).
102      * @param domainAxis the domain axis.
103      * @param rangeAxis the range axis.
104      * @param dataset the dataset.
105      * @param series the series index (zero-based).
106      * @param item the item index (zero-based).
107      * @param crosshairState crosshair information for the plot
108      * (<code>null</code> permitted).
109      * @param pass the pass index (ignored here).
110      */

111     public void drawItem(Graphics2D JavaDoc g2,
112                          XYItemRendererState state,
113                          Rectangle2D JavaDoc dataArea,
114                          PlotRenderingInfo info,
115                          XYPlot plot,
116                          ValueAxis domainAxis,
117                          ValueAxis rangeAxis,
118                          XYDataset dataset,
119                          int series,
120                          int item,
121                          CrosshairState crosshairState,
122                          int pass) {
123
124         // setup for collecting optional entity info...
125
Shape JavaDoc entityArea = null;
126         EntityCollection entities = null;
127         if (info != null) {
128             entities = info.getOwner().getEntityCollection();
129         }
130
131         IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
132
133         double x = intervalDataset.getXValue(series, item);
134         double yLow = intervalDataset.getStartYValue(series, item);
135         double yHigh = intervalDataset.getEndYValue(series, item);
136
137         RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
138         RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
139         
140         double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
141         double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation);
142         double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation);
143
144         Paint JavaDoc p = getItemPaint(series, item);
145         Stroke JavaDoc s = getItemStroke(series, item);
146         
147         Line2D JavaDoc line = null;
148         Shape JavaDoc shape = getItemShape(series, item);
149         Shape JavaDoc top = null;
150         Shape JavaDoc bottom = null;
151         PlotOrientation orientation = plot.getOrientation();
152         if (orientation == PlotOrientation.HORIZONTAL) {
153             line = new Line2D.Double JavaDoc(yyLow, xx, yyHigh, xx);
154             top = ShapeUtilities.createTranslatedShape(shape, yyHigh, xx);
155             bottom = ShapeUtilities.createTranslatedShape(shape, yyLow, xx);
156         }
157         else if (orientation == PlotOrientation.VERTICAL) {
158             line = new Line2D.Double JavaDoc(xx, yyLow, xx, yyHigh);
159             top = ShapeUtilities.createTranslatedShape(shape, xx, yyHigh);
160             bottom = ShapeUtilities.createTranslatedShape(shape, xx, yyLow);
161         }
162         g2.setPaint(p);
163         g2.setStroke(s);
164         g2.draw(line);
165
166         g2.fill(top);
167         g2.fill(bottom);
168
169         // add an entity for the item...
170
if (entities != null) {
171             if (entityArea == null) {
172                 entityArea = line.getBounds();
173             }
174             String JavaDoc tip = null;
175             XYToolTipGenerator generator = getToolTipGenerator(series, item);
176             if (generator != null) {
177                 tip = generator.generateToolTip(dataset, series, item);
178             }
179             String JavaDoc url = null;
180             if (getURLGenerator() != null) {
181                 url = getURLGenerator().generateURL(dataset, series, item);
182             }
183             XYItemEntity entity = new XYItemEntity(
184                 entityArea, dataset, series, item, tip, url
185             );
186             entities.add(entity);
187         }
188
189     }
190     
191     /**
192      * Returns a clone of the renderer.
193      *
194      * @return A clone.
195      *
196      * @throws CloneNotSupportedException if the renderer cannot be cloned.
197      */

198     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
199         return super.clone();
200     }
201
202 }
203
Popular Tags