KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYDotRenderer.java
29  * ------------------
30  * (C) Copyright 2002-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Christian W. Zuckschwerdt;
34  *
35  * $Id: XYDotRenderer.java,v 1.5.2.3 2006/10/25 09:44:04 mungady Exp $
36  *
37  * Changes (from 29-Oct-2002)
38  * --------------------------
39  * 29-Oct-2002 : Added standard header (DG);
40  * 25-Mar-2003 : Implemented Serializable (DG);
41  * 01-May-2003 : Modified drawItem() method signature (DG);
42  * 30-Jul-2003 : Modified entity constructor (CZ);
43  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
44  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
45  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
46  * 19-Jan-2005 : Now uses only primitives from dataset (DG);
47  * ------------- JFREECHART 1.0.x ---------------------------------------------
48  * 10-Jul-2006 : Added dotWidth and dotHeight attributes (DG);
49  *
50  */

51
52 package org.jfree.chart.renderer.xy;
53
54 import java.awt.Graphics2D JavaDoc;
55 import java.awt.geom.Rectangle2D JavaDoc;
56 import java.io.Serializable JavaDoc;
57
58 import org.jfree.chart.axis.ValueAxis;
59 import org.jfree.chart.event.RendererChangeEvent;
60 import org.jfree.chart.plot.CrosshairState;
61 import org.jfree.chart.plot.PlotOrientation;
62 import org.jfree.chart.plot.PlotRenderingInfo;
63 import org.jfree.chart.plot.XYPlot;
64 import org.jfree.data.xy.XYDataset;
65 import org.jfree.ui.RectangleEdge;
66 import org.jfree.util.PublicCloneable;
67
68 /**
69  * A renderer that draws a small dot at each data point for an {@link XYPlot}.
70  */

71 public class XYDotRenderer extends AbstractXYItemRenderer
72                            implements XYItemRenderer,
73                                       Cloneable JavaDoc,
74                                       PublicCloneable,
75                                       Serializable JavaDoc {
76
77     /** For serialization. */
78     private static final long serialVersionUID = -2764344339073566425L;
79     
80     /** The dot width. */
81     private int dotWidth;
82     
83     /** The dot height. */
84     private int dotHeight;
85     
86     /**
87      * Constructs a new renderer.
88      */

89     public XYDotRenderer() {
90         super();
91         this.dotWidth = 1;
92         this.dotHeight = 1;
93     }
94
95     /**
96      * Returns the dot width (the default value is 1).
97      *
98      * @return The dot width.
99      *
100      * @since 1.0.2
101      * @see #setDotWidth(int)
102      */

103     public int getDotWidth() {
104         return this.dotWidth;
105     }
106     
107     /**
108      * Sets the dot width and sends a {@link RendererChangeEvent} to all
109      * registered listeners.
110      *
111      * @param w the new width (must be greater than zero).
112      *
113      * @throws IllegalArgumentException if <code>w</code> is less than one.
114      *
115      * @since 1.0.2
116      * @see #getDotWidth()
117      */

118     public void setDotWidth(int w) {
119         if (w < 1) {
120             throw new IllegalArgumentException JavaDoc("Requires w > 0.");
121         }
122         this.dotWidth = w;
123         notifyListeners(new RendererChangeEvent(this));
124     }
125     
126     /**
127      * Returns the dot height (the default value is 1).
128      *
129      * @return The dot height.
130      *
131      * @since 1.0.2
132      * @see #setDotHeight(int)
133      */

134     public int getDotHeight() {
135         return this.dotHeight;
136     }
137     
138     /**
139      * Sets the dot height and sends a {@link RendererChangeEvent} to all
140      * registered listeners.
141      *
142      * @param h the new height (must be greater than zero).
143      *
144      * @throws IllegalArgumentException if <code>h</code> is less than one.
145      *
146      * @since 1.0.2
147      * @see #getDotHeight()
148      */

149     public void setDotHeight(int h) {
150         if (h < 1) {
151             throw new IllegalArgumentException JavaDoc("Requires h > 0.");
152         }
153         this.dotHeight = h;
154         notifyListeners(new RendererChangeEvent(this));
155     }
156     
157     /**
158      * Draws the visual representation of a single data item.
159      *
160      * @param g2 the graphics device.
161      * @param state the renderer state.
162      * @param dataArea the area within which the data is being drawn.
163      * @param info collects information about the drawing.
164      * @param plot the plot (can be used to obtain standard color
165      * information etc).
166      * @param domainAxis the domain (horizontal) axis.
167      * @param rangeAxis the range (vertical) axis.
168      * @param dataset the dataset.
169      * @param series the series index (zero-based).
170      * @param item the item index (zero-based).
171      * @param crosshairState crosshair information for the plot
172      * (<code>null</code> permitted).
173      * @param pass the pass index.
174      */

175     public void drawItem(Graphics2D JavaDoc g2,
176                          XYItemRendererState state,
177                          Rectangle2D JavaDoc dataArea,
178                          PlotRenderingInfo info,
179                          XYPlot plot,
180                          ValueAxis domainAxis,
181                          ValueAxis rangeAxis,
182                          XYDataset dataset,
183                          int series,
184                          int item,
185                          CrosshairState crosshairState,
186                          int pass) {
187
188         // get the data point...
189
double x = dataset.getXValue(series, item);
190         double y = dataset.getYValue(series, item);
191         double adjx = (this.dotWidth - 1) / 2.0;
192         double adjy = (this.dotHeight - 1) / 2.0;
193         if (!Double.isNaN(y)) {
194             RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
195             RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
196             double transX = domainAxis.valueToJava2D(x, dataArea,
197                     xAxisLocation) - adjx;
198             double transY = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation)
199                     - adjy;
200
201             g2.setPaint(getItemPaint(series, item));
202             PlotOrientation orientation = plot.getOrientation();
203             if (orientation == PlotOrientation.HORIZONTAL) {
204                 g2.fillRect((int) transY, (int) transX, this.dotHeight,
205                         this.dotWidth);
206             }
207             else if (orientation == PlotOrientation.VERTICAL) {
208                 g2.fillRect((int) transX, (int) transY, this.dotWidth,
209                         this.dotHeight);
210             }
211
212             updateCrosshairValues(crosshairState, x, y, transX, transY,
213                     orientation);
214         }
215
216     }
217
218     /**
219      * Tests this renderer for equality with an arbitrary object. This method
220      * returns <code>true</code> if and only if:
221      *
222      * <ul>
223      * <li><code>obj</code> is not <code>null</code>;</li>
224      * <li><code>obj</code> is an instance of <code>XYDotRenderer</code>;</li>
225      * <li>both renderers have the same attribute values.
226      * </ul>
227      *
228      * @param obj the object (<code>null</code> permitted).
229      *
230      * @return A boolean.
231      */

232     public boolean equals(Object JavaDoc obj) {
233         if (obj == this) {
234             return true;
235         }
236         if (!(obj instanceof XYDotRenderer)) {
237             return false;
238         }
239         XYDotRenderer that = (XYDotRenderer) obj;
240         if (this.dotWidth != that.dotWidth) {
241             return false;
242         }
243         if (this.dotHeight != that.dotHeight) {
244             return false;
245         }
246         return super.equals(obj);
247     }
248     
249     /**
250      * Returns a clone of the renderer.
251      *
252      * @return A clone.
253      *
254      * @throws CloneNotSupportedException if the renderer cannot be cloned.
255      */

256     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
257         return super.clone();
258     }
259
260 }
261
Popular Tags