KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYBubbleRenderer.java
24  * ---------------------
25  * (C) Copyright 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Christian W. Zuckschwerdt;
29  *
30  * $Id: XYBubbleRenderer.java,v 1.18 2003/11/03 14:21:28 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 28-Jan-2003 : Version 1 (DG);
35  * 25-Mar-2003 : Implemented Serializable (DG);
36  * 01-May-2003 : Modified drawItem(...) method signature (DG);
37  * 30-Jul-2003 : Modified entity constructor (CZ);
38  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
39  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
40  *
41  */

42
43 package org.jfree.chart.renderer;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Graphics2D JavaDoc;
48 import java.awt.geom.Ellipse2D JavaDoc;
49 import java.awt.geom.Rectangle2D JavaDoc;
50 import java.io.Serializable JavaDoc;
51
52 import org.jfree.chart.CrosshairInfo;
53 import org.jfree.chart.axis.ValueAxis;
54 import org.jfree.chart.entity.EntityCollection;
55 import org.jfree.chart.entity.XYItemEntity;
56 import org.jfree.chart.labels.XYZToolTipGenerator;
57 import org.jfree.chart.plot.PlotOrientation;
58 import org.jfree.chart.plot.PlotRenderingInfo;
59 import org.jfree.chart.plot.XYPlot;
60 import org.jfree.chart.urls.XYZURLGenerator;
61 import org.jfree.data.XYDataset;
62 import org.jfree.data.XYZDataset;
63 import org.jfree.ui.RectangleEdge;
64 import org.jfree.util.PublicCloneable;
65
66 /**
67  * A renderer that draws a circle at each data point. The renderer expects the dataset to be an
68  * {@link XYZDataset}.
69  *
70  * @author David Gilbert
71  */

72 public class XYBubbleRenderer extends AbstractXYItemRenderer implements XYItemRenderer,
73                                                                         Cloneable JavaDoc,
74                                                                         PublicCloneable,
75                                                                         Serializable JavaDoc {
76
77     /** A useful constant. */
78     public static final int SCALE_ON_BOTH_AXES = 0;
79
80     /** A useful constant. */
81     public static final int SCALE_ON_DOMAIN_AXIS = 1;
82
83     /** A useful constant. */
84     public static final int SCALE_ON_RANGE_AXIS = 2;
85
86     /** Controls how the width and height of the bubble are scaled. */
87     private int scaleType;
88
89     /**
90      * Constructs a new renderer.
91      */

92     public XYBubbleRenderer() {
93         this(SCALE_ON_BOTH_AXES);
94     }
95
96     /**
97      * Constructs a new renderer.
98      *
99      * @param scaleType the type of scaling.
100      */

101     public XYBubbleRenderer(int scaleType) {
102         super();
103         this.scaleType = scaleType;
104     }
105
106     /**
107      * Constructs a new renderer.
108      *
109      * @param scaleType the type of scaling.
110      * @param toolTipGenerator the tool-tip generator (<code>null</code> permitted).
111      * @param urlGenerator the URL generator (<code>null</code> permitted).
112      *
113      * @deprecated Use default constructor.
114      */

115     public XYBubbleRenderer(int scaleType,
116                             XYZToolTipGenerator toolTipGenerator, XYZURLGenerator urlGenerator) {
117         super();
118         this.scaleType = scaleType;
119         setToolTipGenerator(toolTipGenerator);
120         setURLGenerator(urlGenerator);
121     }
122
123     /**
124      * Returns the scale type.
125      *
126      * @return the scale type.
127      */

128     public int getScaleType() {
129         return this.scaleType;
130     }
131
132     /**
133      * Draws the visual representation of a single data item.
134      *
135      * @param g2 the graphics device.
136      * @param state the renderer state.
137      * @param dataArea the area within which the data is being drawn.
138      * @param info collects information about the drawing.
139      * @param plot the plot (can be used to obtain standard color information etc).
140      * @param domainAxis the domain (horizontal) axis.
141      * @param rangeAxis the range (vertical) axis.
142      * @param dataset the dataset.
143      * @param series the series index (zero-based).
144      * @param item the item index (zero-based).
145      * @param crosshairInfo information about crosshairs on a plot.
146      * @param pass the pass index.
147      */

148     public void drawItem(Graphics2D JavaDoc g2,
149                          XYItemRendererState state,
150                          Rectangle2D JavaDoc dataArea,
151                          PlotRenderingInfo info,
152                          XYPlot plot,
153                          ValueAxis domainAxis,
154                          ValueAxis rangeAxis,
155                          XYDataset dataset,
156                          int series,
157                          int item,
158                          CrosshairInfo crosshairInfo,
159                          int pass) {
160
161         // get the data point...
162
Number JavaDoc xn = dataset.getXValue(series, item);
163         Number JavaDoc yn = dataset.getYValue(series, item);
164         Number JavaDoc zn = null;
165         if (dataset instanceof XYZDataset) {
166             XYZDataset xyzData = (XYZDataset) dataset;
167             zn = xyzData.getZValue(series, item);
168         }
169         if (zn != null) {
170             double x = xn.doubleValue();
171             double y = yn.doubleValue();
172             double z = zn.doubleValue();
173
174             RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
175             RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
176             double transX = domainAxis.translateValueToJava2D(x, dataArea, domainAxisLocation);
177             double transY = rangeAxis.translateValueToJava2D(y, dataArea, rangeAxisLocation);
178
179             double transDomain = 0.0;
180             double transRange = 0.0;
181             double zero;
182
183             switch(this.scaleType) {
184                 case SCALE_ON_DOMAIN_AXIS:
185                     zero = domainAxis.translateValueToJava2D(0.0, dataArea, domainAxisLocation);
186                     transDomain = domainAxis.translateValueToJava2D(z, dataArea, rangeAxisLocation)
187                                   - zero;
188                     transRange = transDomain;
189                     break;
190                 case SCALE_ON_RANGE_AXIS:
191                     zero = rangeAxis.translateValueToJava2D(0.0, dataArea, rangeAxisLocation);
192                     transRange = zero
193                                  - rangeAxis.translateValueToJava2D(z, dataArea, rangeAxisLocation);
194                     transDomain = transRange;
195                     break;
196                 default:
197                     double zero1 = domainAxis.translateValueToJava2D(0.0, dataArea,
198                                                                      domainAxisLocation);
199                     double zero2 = rangeAxis.translateValueToJava2D(0.0, dataArea,
200                                                                     rangeAxisLocation);
201                     transDomain = domainAxis.translateValueToJava2D(z, dataArea, domainAxisLocation)
202                                                                     - zero1;
203                     transRange = zero2 - rangeAxis.translateValueToJava2D(z, dataArea,
204                                                                           rangeAxisLocation);
205             }
206             double transZ = -rangeAxis.translateValueToJava2D(z, dataArea, rangeAxisLocation)
207                             + rangeAxis.translateValueToJava2D(0.0, dataArea, rangeAxisLocation);
208             transZ = Math.abs(transZ);
209             transDomain = Math.abs(transDomain);
210             transRange = Math.abs(transRange);
211             Ellipse2D JavaDoc circle = null;
212             if (plot.getOrientation() == PlotOrientation.VERTICAL) {
213                 circle = new Ellipse2D.Double JavaDoc(transX - transZ / 2.0,
214                                               transY - transZ / 2.0,
215                                               transDomain, transRange);
216             }
217             else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
218                 circle = new Ellipse2D.Double JavaDoc(transY - transZ / 2.0,
219                                               transX - transZ / 2.0,
220                                               transRange, transDomain);
221             }
222             g2.setPaint(getItemPaint(series, item));
223             g2.fill(circle);
224             g2.setStroke(new BasicStroke JavaDoc(1.0f));
225             g2.setPaint(Color.lightGray);
226             g2.draw(circle);
227
228             // setup for collecting optional entity info...
229
EntityCollection entities = null;
230             if (info != null) {
231                 entities = info.getOwner().getEntityCollection();
232             }
233
234             // add an entity for the item...
235
if (entities != null) {
236                 String JavaDoc tip = null;
237                 if (getToolTipGenerator() != null) {
238                     tip = getToolTipGenerator().generateToolTip(dataset, series, item);
239                 }
240                 String JavaDoc url = null;
241                 if (getURLGenerator() != null) {
242                     url = getURLGenerator().generateURL(dataset, series, item);
243                 }
244                 XYItemEntity entity = new XYItemEntity(circle, dataset, series, item, tip, url);
245                 entities.addEntity(entity);
246             }
247
248             // do we need to update the crosshair values?
249
if (plot.isDomainCrosshairLockedOnData()) {
250                 if (plot.isRangeCrosshairLockedOnData()) {
251                     // both axes
252
crosshairInfo.updateCrosshairPoint(x, y, transX, transY);
253                 }
254                 else {
255                     // just the horizontal axis...
256
crosshairInfo.updateCrosshairX(x);
257                 }
258             }
259             else {
260                 if (plot.isRangeCrosshairLockedOnData()) {
261                     // just the vertical axis...
262
crosshairInfo.updateCrosshairY(y);
263                 }
264             }
265         }
266
267     }
268
269     /**
270      * Returns a clone of the renderer.
271      *
272      * @return A clone.
273      *
274      * @throws CloneNotSupportedException if the renderer cannot be cloned.
275      */

276     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
277         return super.clone();
278     }
279
280 }
281
Popular Tags