KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYStepRenderer.java
29  * -------------------
30  * (C) Copyright 2002-2006, by Roger Studner and Contributors.
31  *
32  * Original Author: Roger Studner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  * Matthias Rose;
35  * Gerald Struck (fix for bug 1569094);
36  *
37  * $Id: XYStepRenderer.java,v 1.7.2.5 2006/10/11 09:04:53 mungady Exp $
38  *
39  * Changes
40  * -------
41  * 13-May-2002 : Version 1, contributed by Roger Studner (DG);
42  * 25-Jun-2002 : Updated import statements (DG);
43  * 22-Jul-2002 : Added check for null data items (DG);
44  * 25-Mar-2003 : Implemented Serializable (DG);
45  * 01-May-2003 : Modified drawItem() method signature (DG);
46  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
47  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
48  * 28-Oct-2003 : Added tooltips, code contributed by Matthias Rose
49  * (RFE 824857) (DG);
50  * 10-Feb-2004 : Removed working line (use line from state object instead) (DG);
51  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed
52  * XYToolTipGenerator --> XYItemLabelGenerator (DG);
53  * 19-Jan-2005 : Now accesses only primitives from dataset (DG);
54  * 15-Mar-2005 : Fix silly bug in drawItem() method (DG);
55  * 19-Sep-2005 : Extend XYLineAndShapeRenderer (fixes legend shapes), added
56  * support for series visibility, and use getDefaultEntityRadius()
57  * for entity hotspot size (DG);
58  * ------------- JFREECHART 1.0.x ---------------------------------------------
59  * 15-Jun-2006 : Added basic support for item labels (DG);
60  * 11-Oct-2006 : Fixed rendering with horizontal orientation (see bug 1569094),
61  * thanks to Gerald Struck (DG);
62  *
63  */

64
65 package org.jfree.chart.renderer.xy;
66
67 import java.awt.Graphics2D JavaDoc;
68 import java.awt.Paint JavaDoc;
69 import java.awt.Shape JavaDoc;
70 import java.awt.Stroke JavaDoc;
71 import java.awt.geom.Line2D JavaDoc;
72 import java.awt.geom.Rectangle2D JavaDoc;
73 import java.io.Serializable JavaDoc;
74
75 import org.jfree.chart.axis.ValueAxis;
76 import org.jfree.chart.entity.EntityCollection;
77 import org.jfree.chart.entity.XYItemEntity;
78 import org.jfree.chart.labels.XYToolTipGenerator;
79 import org.jfree.chart.plot.CrosshairState;
80 import org.jfree.chart.plot.PlotOrientation;
81 import org.jfree.chart.plot.PlotRenderingInfo;
82 import org.jfree.chart.plot.XYPlot;
83 import org.jfree.chart.urls.XYURLGenerator;
84 import org.jfree.data.xy.XYDataset;
85 import org.jfree.ui.RectangleEdge;
86 import org.jfree.util.PublicCloneable;
87
88 /**
89  * Line/Step item renderer for an {@link XYPlot}. This class draws lines
90  * between data points, only allowing horizontal or vertical lines (steps).
91  */

92 public class XYStepRenderer extends XYLineAndShapeRenderer
93                             implements XYItemRenderer,
94                                        Cloneable JavaDoc,
95                                        PublicCloneable,
96                                        Serializable JavaDoc {
97
98     /** For serialization. */
99     private static final long serialVersionUID = -8918141928884796108L;
100     
101     /**
102      * Constructs a new renderer with no tooltip or URL generation.
103      */

104     public XYStepRenderer() {
105         this(null, null);
106     }
107
108     /**
109      * Constructs a new renderer with the specified tool tip and URL
110      * generators.
111      *
112      * @param toolTipGenerator the item label generator (<code>null</code>
113      * permitted).
114      * @param urlGenerator the URL generator (<code>null</code> permitted).
115      */

116     public XYStepRenderer(XYToolTipGenerator toolTipGenerator,
117                           XYURLGenerator urlGenerator) {
118         super();
119         setBaseToolTipGenerator(toolTipGenerator);
120         setURLGenerator(urlGenerator);
121         setShapesVisible(false);
122     }
123
124     /**
125      * Draws the visual representation of a single data item.
126      *
127      * @param g2 the graphics device.
128      * @param state the renderer state.
129      * @param dataArea the area within which the data is being drawn.
130      * @param info collects information about the drawing.
131      * @param plot the plot (can be used to obtain standard color
132      * information etc).
133      * @param domainAxis the domain axis.
134      * @param rangeAxis the vertical axis.
135      * @param dataset the dataset.
136      * @param series the series index (zero-based).
137      * @param item the item index (zero-based).
138      * @param crosshairState crosshair information for the plot
139      * (<code>null</code> permitted).
140      * @param pass the pass index (ignored here).
141      */

142     public void drawItem(Graphics2D JavaDoc g2,
143                          XYItemRendererState state,
144                          Rectangle2D JavaDoc dataArea,
145                          PlotRenderingInfo info,
146                          XYPlot plot,
147                          ValueAxis domainAxis,
148                          ValueAxis rangeAxis,
149                          XYDataset dataset,
150                          int series,
151                          int item,
152                          CrosshairState crosshairState,
153                          int pass) {
154
155         // do nothing if item is not visible
156
if (!getItemVisible(series, item)) {
157             return;
158         }
159
160         PlotOrientation orientation = plot.getOrientation();
161         
162         Paint JavaDoc seriesPaint = getItemPaint(series, item);
163         Stroke JavaDoc seriesStroke = getItemStroke(series, item);
164         g2.setPaint(seriesPaint);
165         g2.setStroke(seriesStroke);
166
167         // get the data point...
168
double x1 = dataset.getXValue(series, item);
169         double y1 = dataset.getYValue(series, item);
170         if (Double.isNaN(y1)) {
171             return;
172         }
173
174         RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
175         RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
176         double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
177         double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
178
179         if (item > 0) {
180             // get the previous data point...
181
double x0 = dataset.getXValue(series, item - 1);
182             double y0 = dataset.getYValue(series, item - 1);
183             if (!Double.isNaN(y0)) {
184                 double transX0 = domainAxis.valueToJava2D(x0, dataArea,
185                         xAxisLocation);
186                 double transY0 = rangeAxis.valueToJava2D(y0, dataArea,
187                         yAxisLocation);
188
189                 Line2D JavaDoc line = state.workingLine;
190                 if (orientation == PlotOrientation.HORIZONTAL) {
191                     if (transY0 == transY1) { //this represents the situation
192
// for drawing a horizontal bar.
193
line.setLine(transY0, transX0, transY1, transX1);
194                         g2.draw(line);
195                     }
196                     else { //this handles the need to perform a 'step'.
197
line.setLine(transY0, transX0, transY0, transX1);
198                         g2.draw(line);
199                         line.setLine(transY0, transX1, transY1, transX1);
200                         g2.draw(line);
201                     }
202                 }
203                 else if (orientation == PlotOrientation.VERTICAL) {
204                     if (transY0 == transY1) { // this represents the situation
205
// for drawing a horizontal bar.
206
line.setLine(transX0, transY0, transX1, transY1);
207                         g2.draw(line);
208                     }
209                     else { //this handles the need to perform a 'step'.
210
line.setLine(transX0, transY0, transX1, transY0);
211                         g2.draw(line);
212                         line.setLine(transX1, transY0, transX1, transY1);
213                         g2.draw(line);
214                     }
215                 }
216
217             }
218         }
219
220         // draw the item label if there is one...
221
if (isItemLabelVisible(series, item)) {
222             double xx = transX1;
223             double yy = transY1;
224             if (orientation == PlotOrientation.HORIZONTAL) {
225                 xx = transY1;
226                 yy = transX1;
227             }
228             drawItemLabel(g2, orientation, dataset, series, item, xx, yy,
229                     (y1 < 0.0));
230         }
231
232         updateCrosshairValues(crosshairState, x1, y1, transX1, transY1,
233                 orientation);
234         
235         // collect entity and tool tip information...
236
if (state.getInfo() != null) {
237             EntityCollection entities = state.getEntityCollection();
238             if (entities != null) {
239                 int r = getDefaultEntityRadius();
240                 Shape JavaDoc shape = orientation == PlotOrientation.VERTICAL
241                     ? new Rectangle2D.Double JavaDoc(transX1 - r, transY1 - r, 2 * r,
242                             2 * r)
243                     : new Rectangle2D.Double JavaDoc(transY1 - r, transX1 - r, 2 * r,
244                             2 * r);
245                 if (shape != null) {
246                     String JavaDoc tip = null;
247                     XYToolTipGenerator generator
248                         = getToolTipGenerator(series, item);
249                     if (generator != null) {
250                         tip = generator.generateToolTip(dataset, series, item);
251                     }
252                     String JavaDoc url = null;
253                     if (getURLGenerator() != null) {
254                         url = getURLGenerator().generateURL(dataset, series,
255                                 item);
256                     }
257                     XYItemEntity entity = new XYItemEntity(shape, dataset,
258                             series, item, tip, url);
259                     entities.add(entity);
260                 }
261             }
262         }
263     }
264
265     /**
266      * Returns a clone of the renderer.
267      *
268      * @return A clone.
269      *
270      * @throws CloneNotSupportedException if the renderer cannot be cloned.
271      */

272     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
273         return super.clone();
274     }
275
276 }
277
Popular Tags