KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SignalRenderer.java
24  * -------------------
25  * (C) Copyright 2001-2003, by Sylvain Viuejot and Contributors.
26  *
27  * Original Author: Sylvain Vieujot;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Richard Atkinson;
30  * Christian W. Zuckschwerdt;
31  *
32  * $Id: SignalRenderer.java,v 1.13 2003/11/03 14:21:27 mungady Exp $
33  *
34  * Changes
35  * -------
36  * 08-Jan-2002 : Version 1. Based on code in the SignalsPlot class, written by Sylvain
37  * Vieujot (DG);
38  * 23-Jan-2002 : Added DrawInfo parameter to drawItem(...) method (DG);
39  * 14-Feb-2002 : Added small fix from Sylvain (DG);
40  * 28-Mar-2002 : Added a property change listener mechanism so that renderers no longer need to be
41  * immutable (DG);
42  * 09-Apr-2002 : Removed translatedRangeZero from the drawItem(...) method, and changed the return
43  * type of the drawItem method to void, reflecting a change in the XYItemRenderer
44  * interface. Added tooltip code to drawItem(...) method (DG);
45  * 25-Jun-2002 : Removed redundant code (DG);
46  * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML image maps (RA);
47  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
48  * 25-Mar-2003 : Implemented Serializable (DG);
49  * 01-May-2003 : Modified drawItem(...) method signature (DG);
50  * 30-Jul-2003 : Modified entity constructor (CZ);
51  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
52  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
53  *
54  */

55
56 package org.jfree.chart.renderer;
57
58 import java.awt.Color JavaDoc;
59 import java.awt.Graphics2D JavaDoc;
60 import java.awt.Paint JavaDoc;
61 import java.awt.Stroke JavaDoc;
62 import java.awt.geom.Ellipse2D JavaDoc;
63 import java.awt.geom.GeneralPath JavaDoc;
64 import java.awt.geom.Rectangle2D JavaDoc;
65 import java.io.Serializable JavaDoc;
66
67 import org.jfree.chart.CrosshairInfo;
68 import org.jfree.chart.axis.ValueAxis;
69 import org.jfree.chart.entity.EntityCollection;
70 import org.jfree.chart.entity.XYItemEntity;
71 import org.jfree.chart.plot.PlotRenderingInfo;
72 import org.jfree.chart.plot.XYPlot;
73 import org.jfree.data.SignalsDataset;
74 import org.jfree.data.XYDataset;
75 import org.jfree.util.PublicCloneable;
76
77 /**
78  * A renderer that draws signals on an {@link XYPlot}.
79  *
80  * @author Sylvain Vieujot
81  */

82 public class SignalRenderer extends AbstractXYItemRenderer implements XYItemRenderer,
83                                                                       Cloneable JavaDoc,
84                                                                       PublicCloneable,
85                                                                       Serializable JavaDoc {
86
87     /** The mark offset. */
88     private double markOffset = 5;
89
90     /** The shape width. */
91     private double shapeWidth = 15;
92
93     /** The shape height. */
94     private double shapeHeight = 25;
95
96     /**
97      * Creates a new renderer.
98      */

99     public SignalRenderer() {
100     }
101
102     /**
103      * Returns the mark offset.
104      *
105      * @return the mark offset.
106      */

107     public double getMarkOffset() {
108         return this.markOffset;
109     }
110
111     /**
112      * Sets the mark offset.
113      *
114      * @param offset the mark offset.
115      */

116     public void setMarkOffset(double offset) {
117         this.markOffset = offset;
118     }
119
120     /**
121      * Returns the shape width.
122      *
123      * @return the shape width.
124      */

125     public double getShapeWidth() {
126         return this.shapeWidth;
127     }
128
129     /**
130      * Sets the shape width.
131      *
132      * @param width the shape width.
133      */

134     public void setShapeWidth(double width) {
135         this.shapeWidth = width;
136     }
137
138     /**
139      * Returns the shape height.
140      *
141      * @return the shape height.
142      */

143     public double getShapeHeight() {
144         return this.shapeHeight;
145     }
146
147     /**
148      * Sets the shape height.
149      *
150      * @param height the shape height.
151      */

152     public void setShapeHeight(double height) {
153         this.shapeHeight = height;
154     }
155
156     /**
157      * Draws the visual representation of a single data item.
158      *
159      * @param g2 the graphics device.
160      * @param state the renderer state.
161      * @param dataArea the area within which the plot is being drawn.
162      * @param info collects information about the drawing.
163      * @param plot the plot (can be used to obtain standard color information etc).
164      * @param horizontalAxis the horizontal axis.
165      * @param verticalAxis the vertical axis.
166      * @param dataset the dataset.
167      * @param series the series index (zero-based).
168      * @param item the item index (zero-based).
169      * @param crosshairInfo information about crosshairs on a plot.
170      * @param pass the pass index.
171      */

172     public void drawItem(Graphics2D JavaDoc g2,
173                          XYItemRendererState state,
174                          Rectangle2D JavaDoc dataArea,
175                          PlotRenderingInfo info,
176                          XYPlot plot,
177                          ValueAxis horizontalAxis,
178                          ValueAxis verticalAxis,
179                          XYDataset dataset,
180                          int series,
181                          int item,
182                          CrosshairInfo crosshairInfo,
183                          int pass) {
184
185         // setup for collecting optional entity info...
186
EntityCollection entities = null;
187         if (info != null) {
188             entities = info.getOwner().getEntityCollection();
189         }
190
191         SignalsDataset signalData = (SignalsDataset) dataset;
192
193         Number JavaDoc x = signalData.getXValue(series, item);
194         Number JavaDoc y = signalData.getYValue(series, item);
195         int type = signalData.getType(series, item);
196         //double level = signalData.getLevel(series, item);
197

198         double xx = horizontalAxis.translateValueToJava2D(x.doubleValue(), dataArea,
199                                                           plot.getDomainAxisEdge());
200         double yy = verticalAxis.translateValueToJava2D(y.doubleValue(), dataArea,
201                                                         plot.getRangeAxisEdge());
202
203         Paint JavaDoc p = getItemPaint(series, item);
204         Stroke JavaDoc s = getItemStroke(series, item);
205         g2.setPaint(p);
206         g2.setStroke(s);
207
208         int direction = 1;
209         if ((type == SignalsDataset.ENTER_LONG) || (type == SignalsDataset.EXIT_SHORT)) {
210             yy = yy + markOffset;
211             direction = -1;
212         }
213         else {
214             yy = yy - markOffset;
215         }
216
217         GeneralPath JavaDoc path = new GeneralPath JavaDoc();
218         if ((type == SignalsDataset.ENTER_LONG) || (type == SignalsDataset.ENTER_SHORT)) {
219             path.moveTo((float) xx, (float) yy);
220             path.lineTo((float) (xx + shapeWidth / 2), (float) (yy - direction * shapeHeight / 3));
221             path.lineTo((float) (xx + shapeWidth / 6), (float) (yy - direction * shapeHeight / 3));
222             path.lineTo((float) (xx + shapeWidth / 6), (float) (yy - direction * shapeHeight));
223             path.lineTo((float) (xx - shapeWidth / 6), (float) (yy - direction * shapeHeight));
224             path.lineTo((float) (xx - shapeWidth / 6), (float) (yy - direction * shapeHeight / 3));
225             path.lineTo((float) (xx - shapeWidth / 2), (float) (yy - direction * shapeHeight / 3));
226             path.lineTo((float) xx, (float) yy);
227         }
228         else {
229             path.moveTo((float) xx, (float) yy);
230             path.lineTo((float) xx, (float) (yy - direction * shapeHeight));
231             Ellipse2D.Double JavaDoc ellipse = new Ellipse2D.Double JavaDoc(
232                 xx - shapeWidth / 2,
233                 yy + (direction == 1 ? -shapeHeight : shapeHeight - shapeWidth),
234                 shapeWidth,
235                 shapeWidth
236             );
237             path.append(ellipse, false);
238         }
239
240         g2.fill(path);
241         g2.setPaint(Color.black);
242         g2.draw(path);
243
244         // add an entity for the item...
245
if (entities != null) {
246             String JavaDoc tip = null;
247             if (getToolTipGenerator() != null) {
248                 tip = getToolTipGenerator().generateToolTip(dataset, series, item);
249             }
250             String JavaDoc url = null;
251             if (getURLGenerator() != null) {
252                 url = getURLGenerator().generateURL(dataset, series, item);
253             }
254             XYItemEntity entity = new XYItemEntity(path, dataset, series, item, tip, url);
255             entities.addEntity(entity);
256         }
257
258     }
259
260     /**
261      * Returns a clone of the renderer.
262      *
263      * @return A clone.
264      *
265      * @throws CloneNotSupportedException if the renderer cannot be cloned.
266      */

267     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
268         return super.clone();
269     }
270
271 }
272
Popular Tags