KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------
27  * SignalRenderer.java
28  * -------------------
29  * (C) Copyright 2001-2004, by Sylvain Viuejot and Contributors.
30  *
31  * Original Author: Sylvain Vieujot;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  * Richard Atkinson;
34  * Christian W. Zuckschwerdt;
35  *
36  * $Id: SignalRenderer.java,v 1.5 2005/05/19 14:01:52 mungady Exp $
37  *
38  * Changes
39  * -------
40  * 08-Jan-2002 : Version 1. Based on code in the SignalsPlot class, written by
41  * Sylvain Vieujot (DG);
42  * 23-Jan-2002 : Added DrawInfo parameter to drawItem() method (DG);
43  * 14-Feb-2002 : Added small fix from Sylvain (DG);
44  * 28-Mar-2002 : Added a property change listener mechanism so that renderers
45  * no longer need to be immutable (DG);
46  * 09-Apr-2002 : Removed translatedRangeZero from the drawItem() method, and
47  * changed the return type of the drawItem method to void,
48  * reflecting a change in the XYItemRenderer interface. Added
49  * tooltip code to drawItem() method (DG);
50  * 25-Jun-2002 : Removed redundant code (DG);
51  * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML
52  * image maps (RA);
53  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
54  * 25-Mar-2003 : Implemented Serializable (DG);
55  * 01-May-2003 : Modified drawItem() method signature (DG);
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  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
60  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
61  * getYValue() (DG);
62  *
63  */

64
65 package org.jfree.chart.renderer.xy;
66
67 import java.awt.Color JavaDoc;
68 import java.awt.Graphics2D JavaDoc;
69 import java.awt.Paint JavaDoc;
70 import java.awt.Stroke JavaDoc;
71 import java.awt.geom.Ellipse2D JavaDoc;
72 import java.awt.geom.GeneralPath JavaDoc;
73 import java.awt.geom.Rectangle2D JavaDoc;
74 import java.io.Serializable JavaDoc;
75
76 import org.jfree.chart.axis.ValueAxis;
77 import org.jfree.chart.entity.EntityCollection;
78 import org.jfree.chart.entity.XYItemEntity;
79 import org.jfree.chart.labels.XYToolTipGenerator;
80 import org.jfree.chart.plot.CrosshairState;
81 import org.jfree.chart.plot.PlotRenderingInfo;
82 import org.jfree.chart.plot.XYPlot;
83 import org.jfree.data.xy.SignalsDataset;
84 import org.jfree.data.xy.XYDataset;
85 import org.jfree.util.PublicCloneable;
86
87 /**
88  * A renderer that draws signals on an {@link XYPlot}.
89  *
90  * @author Sylvain Vieujot
91  */

92 public class SignalRenderer extends AbstractXYItemRenderer
93                             implements XYItemRenderer,
94                                        Cloneable JavaDoc,
95                                        PublicCloneable,
96                                        Serializable JavaDoc {
97
98     /** For serialization. */
99     private static final long serialVersionUID = 1161092564576638268L;
100     
101     /** The mark offset. */
102     private double markOffset = 5;
103
104     /** The shape width. */
105     private double shapeWidth = 15;
106
107     /** The shape height. */
108     private double shapeHeight = 25;
109
110     /**
111      * Creates a new renderer.
112      */

113     public SignalRenderer() {
114         super();
115     }
116
117     /**
118      * Returns the mark offset.
119      *
120      * @return The mark offset.
121      */

122     public double getMarkOffset() {
123         return this.markOffset;
124     }
125
126     /**
127      * Sets the mark offset.
128      *
129      * @param offset the mark offset.
130      */

131     public void setMarkOffset(double offset) {
132         this.markOffset = offset;
133     }
134
135     /**
136      * Returns the shape width.
137      *
138      * @return The shape width.
139      */

140     public double getShapeWidth() {
141         return this.shapeWidth;
142     }
143
144     /**
145      * Sets the shape width.
146      *
147      * @param width the shape width.
148      */

149     public void setShapeWidth(double width) {
150         this.shapeWidth = width;
151     }
152
153     /**
154      * Returns the shape height.
155      *
156      * @return The shape height.
157      */

158     public double getShapeHeight() {
159         return this.shapeHeight;
160     }
161
162     /**
163      * Sets the shape height.
164      *
165      * @param height the shape height.
166      */

167     public void setShapeHeight(double height) {
168         this.shapeHeight = height;
169     }
170
171     /**
172      * Draws the visual representation of a single data item.
173      *
174      * @param g2 the graphics device.
175      * @param state the renderer state.
176      * @param dataArea the area within which the plot is being drawn.
177      * @param info collects information about the drawing.
178      * @param plot the plot (can be used to obtain standard color
179      * information etc).
180      * @param horizontalAxis the horizontal axis.
181      * @param verticalAxis the vertical axis.
182      * @param dataset the dataset.
183      * @param series the series index (zero-based).
184      * @param item the item index (zero-based).
185      * @param crosshairState crosshair information for the plot
186      * (<code>null</code> permitted).
187      * @param pass the pass index.
188      */

189     public void drawItem(Graphics2D JavaDoc g2,
190                          XYItemRendererState state,
191                          Rectangle2D JavaDoc dataArea,
192                          PlotRenderingInfo info,
193                          XYPlot plot,
194                          ValueAxis horizontalAxis,
195                          ValueAxis verticalAxis,
196                          XYDataset dataset,
197                          int series,
198                          int item,
199                          CrosshairState crosshairState,
200                          int pass) {
201
202         // setup for collecting optional entity info...
203
EntityCollection entities = null;
204         if (info != null) {
205             entities = info.getOwner().getEntityCollection();
206         }
207
208         SignalsDataset signalData = (SignalsDataset) dataset;
209
210         Number JavaDoc x = signalData.getX(series, item);
211         Number JavaDoc y = signalData.getY(series, item);
212         int type = signalData.getType(series, item);
213         //double level = signalData.getLevel(series, item);
214

215         double xx = horizontalAxis.valueToJava2D(
216             x.doubleValue(), dataArea, plot.getDomainAxisEdge()
217         );
218         double yy = verticalAxis.valueToJava2D(
219             y.doubleValue(), dataArea, plot.getRangeAxisEdge()
220         );
221
222         Paint JavaDoc p = getItemPaint(series, item);
223         Stroke JavaDoc s = getItemStroke(series, item);
224         g2.setPaint(p);
225         g2.setStroke(s);
226
227         int direction = 1;
228         if ((type == SignalsDataset.ENTER_LONG)
229                 || (type == SignalsDataset.EXIT_SHORT)) {
230             yy = yy + this.markOffset;
231             direction = -1;
232         }
233         else {
234             yy = yy - this.markOffset;
235         }
236
237         GeneralPath JavaDoc path = new GeneralPath JavaDoc();
238         if ((type == SignalsDataset.ENTER_LONG)
239                 || (type == SignalsDataset.ENTER_SHORT)) {
240             path.moveTo((float) xx, (float) yy);
241             path.lineTo(
242                 (float) (xx + this.shapeWidth / 2),
243                 (float) (yy - direction * this.shapeHeight / 3)
244             );
245             path.lineTo(
246                 (float) (xx + this.shapeWidth / 6),
247                 (float) (yy - direction * this.shapeHeight / 3)
248             );
249             path.lineTo(
250                 (float) (xx + this.shapeWidth / 6),
251                 (float) (yy - direction * this.shapeHeight)
252             );
253             path.lineTo(
254                 (float) (xx - this.shapeWidth / 6),
255                 (float) (yy - direction * this.shapeHeight)
256             );
257             path.lineTo(
258                 (float) (xx - this.shapeWidth / 6),
259                 (float) (yy - direction * this.shapeHeight / 3)
260             );
261             path.lineTo(
262                 (float) (xx - this.shapeWidth / 2),
263                 (float) (yy - direction * this.shapeHeight / 3)
264             );
265             path.lineTo((float) xx, (float) yy);
266         }
267         else {
268             path.moveTo((float) xx, (float) yy);
269             path.lineTo(
270                 (float) xx, (float) (yy - direction * this.shapeHeight)
271             );
272             Ellipse2D.Double JavaDoc ellipse = new Ellipse2D.Double JavaDoc(
273                 xx - this.shapeWidth / 2,
274                 yy + (direction == 1 ? -this.shapeHeight
275                         : this.shapeHeight - this.shapeWidth),
276                 this.shapeWidth,
277                 this.shapeWidth
278             );
279             path.append(ellipse, false);
280         }
281
282         g2.fill(path);
283         g2.setPaint(Color.black);
284         g2.draw(path);
285
286         // add an entity for the item...
287
if (entities != null) {
288             String JavaDoc tip = null;
289             XYToolTipGenerator generator = getToolTipGenerator(series, item);
290             if (generator != null) {
291                 tip = generator.generateToolTip(dataset, series, item);
292             }
293             String JavaDoc url = null;
294             if (getURLGenerator() != null) {
295                 url = getURLGenerator().generateURL(dataset, series, item);
296             }
297             XYItemEntity entity = new XYItemEntity(
298                 path, dataset, series, item, tip, url
299             );
300             entities.add(entity);
301         }
302
303     }
304
305     /**
306      * Returns a clone of the renderer.
307      *
308      * @return A clone.
309      *
310      * @throws CloneNotSupportedException if the renderer cannot be cloned.
311      */

312     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
313         return super.clone();
314     }
315
316 }
317
Popular Tags