KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYStepRenderer.java
24  * -------------------
25  * (C) Copyright 2002, 2003, by Roger Studner and Contributors.
26  *
27  * Original Author: Roger Studner;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Matthias Rose;
30  *
31  * $Id: XYStepRenderer.java,v 1.18 2003/11/03 14:21:28 mungady Exp $
32  *
33  * Changes
34  * -------
35  * 13-May-2002 : Version 1, contributed by Roger Studner (DG);
36  * 25-Jun-2002 : Updated import statements (DG);
37  * 22-Jul-2002 : Added check for null data items (DG);
38  * 25-Mar-2003 : Implemented Serializable (DG);
39  * 01-May-2003 : Modified drawItem(...) method signature (DG);
40  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
41  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
42  * 28-Oct-2003 : Added tooltips, code contributed by Matthias Rose (RFE 824857) (DG);
43  *
44  */

45 package org.jfree.chart.renderer;
46
47 import java.awt.Graphics2D JavaDoc;
48 import java.awt.Paint JavaDoc;
49 import java.awt.Shape JavaDoc;
50 import java.awt.Stroke JavaDoc;
51 import java.awt.geom.Line2D JavaDoc;
52 import java.awt.geom.Rectangle2D JavaDoc;
53 import java.io.Serializable JavaDoc;
54
55 import org.jfree.chart.CrosshairInfo;
56 import org.jfree.chart.axis.ValueAxis;
57 import org.jfree.chart.entity.EntityCollection;
58 import org.jfree.chart.entity.XYItemEntity;
59 import org.jfree.chart.labels.XYToolTipGenerator;
60 import org.jfree.chart.plot.PlotOrientation;
61 import org.jfree.chart.plot.PlotRenderingInfo;
62 import org.jfree.chart.plot.XYPlot;
63 import org.jfree.chart.urls.XYURLGenerator;
64 import org.jfree.data.XYDataset;
65 import org.jfree.ui.RectangleEdge;
66 import org.jfree.util.PublicCloneable;
67
68 /**
69  * Line/Step item renderer for an {@link XYPlot}. This class draws lines between data
70  * points, only allowing horizontal or vertical lines (steps).
71  *
72  * @author Roger Studner
73  */

74 public class XYStepRenderer extends AbstractXYItemRenderer implements XYItemRenderer,
75                                                                       Cloneable JavaDoc,
76                                                                       PublicCloneable,
77                                                                       Serializable JavaDoc {
78
79     /** A working line (to save creating many instances). */
80     private transient Line2D JavaDoc line;
81
82     /**
83      * Constructs a new renderer with no tooltip or URL generation.
84      */

85     public XYStepRenderer() {
86         super();
87         this.line = new Line2D.Double JavaDoc(0.0, 0.0, 0.0, 0.0);
88     }
89
90     /**
91      * Constructs a new renderer.
92      *
93      * @param toolTipGenerator the tooltip generator.
94      * @param urlGenerator the URL generator.
95      */

96     public XYStepRenderer(XYToolTipGenerator toolTipGenerator,
97                           XYURLGenerator urlGenerator) {
98
99         
100         super();
101         setToolTipGenerator(toolTipGenerator);
102         setURLGenerator(urlGenerator);
103         this.line = new Line2D.Double JavaDoc(0.0, 0.0, 0.0, 0.0);
104
105     }
106
107     /**
108      * Draws the visual representation of a single data item.
109      *
110      * @param g2 the graphics device.
111      * @param state the renderer state.
112      * @param dataArea the area within which the data is being drawn.
113      * @param info collects information about the drawing.
114      * @param plot the plot (can be used to obtain standard color information etc).
115      * @param horizontalAxis the horizontal axis.
116      * @param verticalAxis the vertical axis.
117      * @param dataset the dataset.
118      * @param series the series index (zero-based).
119      * @param item the item index (zero-based).
120      * @param crosshairInfo collects information about the crosshairs.
121      * @param pass the pass index (ignored here).
122      */

123     public void drawItem(Graphics2D JavaDoc g2,
124                          XYItemRendererState state,
125                          Rectangle2D JavaDoc dataArea,
126                          PlotRenderingInfo info,
127                          XYPlot plot,
128                          ValueAxis horizontalAxis,
129                          ValueAxis verticalAxis,
130                          XYDataset dataset,
131                          int series,
132                          int item,
133                          CrosshairInfo crosshairInfo,
134                          int pass) {
135
136         Paint JavaDoc seriesPaint = getItemPaint(series, item);
137         Stroke JavaDoc seriesStroke = getItemStroke(series, item);
138         g2.setPaint(seriesPaint);
139         g2.setStroke(seriesStroke);
140
141         // get the data point...
142
Number JavaDoc x1 = dataset.getXValue(series, item);
143         Number JavaDoc y1 = dataset.getYValue(series, item);
144         if (y1 == null) {
145             return;
146         }
147
148         RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
149         RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
150         double transX1 = horizontalAxis.translateValueToJava2D(x1.doubleValue(), dataArea,
151                                                                xAxisLocation);
152         double transY1 = verticalAxis.translateValueToJava2D(y1.doubleValue(), dataArea,
153                                                              yAxisLocation);
154
155         if (item > 0) {
156             // get the previous data point...
157
Number JavaDoc x0 = dataset.getXValue(series, item - 1);
158             Number JavaDoc y0 = dataset.getYValue(series, item - 1);
159             if (y0 != null) {
160                 double transX0 = horizontalAxis.translateValueToJava2D(x0.doubleValue(), dataArea,
161                                                                        xAxisLocation);
162                 double transY0 = verticalAxis.translateValueToJava2D(y0.doubleValue(), dataArea,
163                                                                      yAxisLocation);
164
165                 PlotOrientation orientation = plot.getOrientation();
166                 if (orientation == PlotOrientation.HORIZONTAL) {
167                     if (transY0 == transY1) { //this represents the situation for drawing a
168
//horizontal bar.
169
line.setLine(transY0, transX0, transY1, transX1);
170                         g2.draw(line);
171                     }
172                     else { //this handles the need to perform a 'step'.
173
line.setLine(transY0, transX0, transY1, transX0);
174                         g2.draw(line);
175                         line.setLine(transY1, transX0, transY1, transX1);
176                         g2.draw(line);
177                     }
178                 }
179                 else if (orientation == PlotOrientation.VERTICAL) {
180                     if (transY0 == transY1) { //this represents the situation for drawing a
181
//horizontal bar.
182
line.setLine(transX0, transY0, transX1, transY1);
183                         g2.draw(line);
184                     }
185                     else { //this handles the need to perform a 'step'.
186
line.setLine(transX0, transY0, transX1, transY0);
187                         g2.draw(line);
188                         line.setLine(transX1, transY0, transX1, transY1);
189                         g2.draw(line);
190                     }
191                 }
192
193             }
194         }
195
196         // do we need to update the crosshair values?
197
if (plot.isDomainCrosshairLockedOnData()) {
198             if (plot.isRangeCrosshairLockedOnData()) {
199                 // both crosshairs
200
crosshairInfo.updateCrosshairPoint(x1.doubleValue(), y1.doubleValue(),
201                                                    transX1, transY1);
202             }
203             else {
204                 // just the horizontal axis...
205
crosshairInfo.updateCrosshairX(x1.doubleValue());
206
207             }
208         }
209         else {
210             if (plot.isRangeCrosshairLockedOnData()) {
211                 // just the vertical axis...
212
crosshairInfo.updateCrosshairY(y1.doubleValue());
213             }
214         }
215         // collect entity and tool tip information...
216

217         if (state.getInfo() != null) {
218             EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
219             if (entities != null) {
220                 Shape JavaDoc shape = plot.getOrientation() == PlotOrientation.VERTICAL
221                     ? new Rectangle2D.Double JavaDoc(transX1 - 2, transY1 - 2, 4.0, 4.0)
222                     : new Rectangle2D.Double JavaDoc(transY1 - 2, transX1 - 2, 4.0, 4.0);
223                 if (shape != null) {
224                     String JavaDoc tip = null;
225                     if (getToolTipGenerator() != null) {
226                         tip = getToolTipGenerator().generateToolTip(dataset, series, item);
227                     }
228                     String JavaDoc url = null;
229                     if (getURLGenerator() != null) {
230                         url = getURLGenerator().generateURL(dataset, series, item);
231                     }
232                     XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url);
233                     entities.addEntity(entity);
234                 }
235             }
236         }
237     }
238
239     /**
240      * Returns a clone of the renderer.
241      *
242      * @return A clone.
243      *
244      * @throws CloneNotSupportedException if the renderer cannot be cloned.
245      */

246     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
247         return super.clone();
248     }
249
250 }
251
Popular Tags