KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYBubbleRenderer.java
29  * ---------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Christian W. Zuckschwerdt;
34  *
35  * $Id: XYBubbleRenderer.java,v 1.8.2.6 2006/09/21 15:04:04 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 28-Jan-2003 : Version 1 (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  * 10-Feb-2004 : Small change to drawItem() method to make cut-and-paste
46  * overriding easier (DG);
47  * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
48  * 19-Jan-2005 : Now accesses only primitives from dataset (DG);
49  * 28-Feb-2005 : Modify renderer to use circles in legend (DG);
50  * 17-Mar-2005 : Fixed bug in bubble bounds calculation (DG);
51  * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
52  * ------------- JFREECHART 1.0.0 ---------------------------------------------
53  * 13-Dec-2005 : Added support for item labels (bug 1373371) (DG);
54  * 20-Jan-2006 : Check flag for drawing item labels (DG);
55  * 21-Sep-2006 : Respect the outline paint and stroke settings (DG);
56  *
57  */

58
59 package org.jfree.chart.renderer.xy;
60
61 import java.awt.Graphics2D JavaDoc;
62 import java.awt.Paint JavaDoc;
63 import java.awt.Shape JavaDoc;
64 import java.awt.Stroke JavaDoc;
65 import java.awt.geom.Ellipse2D JavaDoc;
66 import java.awt.geom.Rectangle2D JavaDoc;
67 import java.io.Serializable JavaDoc;
68
69 import org.jfree.chart.LegendItem;
70 import org.jfree.chart.axis.ValueAxis;
71 import org.jfree.chart.entity.EntityCollection;
72 import org.jfree.chart.entity.XYItemEntity;
73 import org.jfree.chart.labels.XYToolTipGenerator;
74 import org.jfree.chart.plot.CrosshairState;
75 import org.jfree.chart.plot.PlotOrientation;
76 import org.jfree.chart.plot.PlotRenderingInfo;
77 import org.jfree.chart.plot.XYPlot;
78 import org.jfree.data.xy.XYDataset;
79 import org.jfree.data.xy.XYZDataset;
80 import org.jfree.ui.RectangleEdge;
81 import org.jfree.util.PublicCloneable;
82
83 /**
84  * A renderer that draws a circle at each data point with a diameter that is
85  * determined by the z-value in the dataset (the renderer requires the dataset
86  * to be an instance of {@link XYZDataset}.
87  */

88 public class XYBubbleRenderer extends AbstractXYItemRenderer
89                               implements XYItemRenderer,
90                                          Cloneable JavaDoc,
91                                          PublicCloneable,
92                                          Serializable JavaDoc {
93
94     /** For serialization. */
95     public static final long serialVersionUID = -5221991598674249125L;
96     
97     /** A useful constant. */
98     public static final int SCALE_ON_BOTH_AXES = 0;
99
100     /** A useful constant. */
101     public static final int SCALE_ON_DOMAIN_AXIS = 1;
102
103     /** A useful constant. */
104     public static final int SCALE_ON_RANGE_AXIS = 2;
105
106     /** Controls how the width and height of the bubble are scaled. */
107     private int scaleType;
108
109     /**
110      * Constructs a new renderer.
111      */

112     public XYBubbleRenderer() {
113         this(SCALE_ON_BOTH_AXES);
114     }
115
116     /**
117      * Constructs a new renderer with the specified type of scaling.
118      *
119      * @param scaleType the type of scaling (must be one of:
120      * {@link #SCALE_ON_BOTH_AXES}, {@link #SCALE_ON_DOMAIN_AXIS},
121      * {@link #SCALE_ON_RANGE_AXIS}).
122      */

123     public XYBubbleRenderer(int scaleType) {
124         super();
125         if (scaleType < 0 || scaleType > 2) {
126             throw new IllegalArgumentException JavaDoc("Invalid 'scaleType'.");
127         }
128         this.scaleType = scaleType;
129     }
130
131     /**
132      * Returns the scale type that was set when the renderer was constructed.
133      *
134      * @return The scale type (one of: {@link #SCALE_ON_BOTH_AXES},
135      * {@link #SCALE_ON_DOMAIN_AXIS}, {@link #SCALE_ON_RANGE_AXIS}).
136      */

137     public int getScaleType() {
138         return this.scaleType;
139     }
140
141     /**
142      * Draws the visual representation of a single data item.
143      *
144      * @param g2 the graphics device.
145      * @param state the renderer state.
146      * @param dataArea the area within which the data is being drawn.
147      * @param info collects information about the drawing.
148      * @param plot the plot (can be used to obtain standard color
149      * information etc).
150      * @param domainAxis the domain (horizontal) axis.
151      * @param rangeAxis the range (vertical) axis.
152      * @param dataset the dataset.
153      * @param series the series index (zero-based).
154      * @param item the item index (zero-based).
155      * @param crosshairState crosshair information for the plot
156      * (<code>null</code> permitted).
157      * @param pass the pass index.
158      */

159     public void drawItem(Graphics2D JavaDoc g2, XYItemRendererState state,
160             Rectangle2D JavaDoc dataArea, PlotRenderingInfo info, XYPlot plot,
161             ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
162             int series, int item, CrosshairState crosshairState, int pass) {
163
164         PlotOrientation orientation = plot.getOrientation();
165         
166         // get the data point...
167
double x = dataset.getXValue(series, item);
168         double y = dataset.getYValue(series, item);
169         double z = Double.NaN;
170         if (dataset instanceof XYZDataset) {
171             XYZDataset xyzData = (XYZDataset) dataset;
172             z = xyzData.getZValue(series, item);
173         }
174         if (!Double.isNaN(z)) {
175             RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
176             RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
177             double transX = domainAxis.valueToJava2D(x, dataArea,
178                     domainAxisLocation);
179             double transY = rangeAxis.valueToJava2D(y, dataArea,
180                     rangeAxisLocation);
181
182             double transDomain = 0.0;
183             double transRange = 0.0;
184             double zero;
185
186             switch(getScaleType()) {
187                 case SCALE_ON_DOMAIN_AXIS:
188                     zero = domainAxis.valueToJava2D(0.0, dataArea,
189                             domainAxisLocation);
190                     transDomain = domainAxis.valueToJava2D(z, dataArea,
191                             domainAxisLocation) - zero;
192                     transRange = transDomain;
193                     break;
194                 case SCALE_ON_RANGE_AXIS:
195                     zero = rangeAxis.valueToJava2D(0.0, dataArea,
196                             rangeAxisLocation);
197                     transRange = zero - rangeAxis.valueToJava2D(z, dataArea,
198                             rangeAxisLocation);
199                     transDomain = transRange;
200                     break;
201                 default:
202                     double zero1 = domainAxis.valueToJava2D(0.0, dataArea,
203                             domainAxisLocation);
204                     double zero2 = rangeAxis.valueToJava2D(0.0, dataArea,
205                             rangeAxisLocation);
206                     transDomain = domainAxis.valueToJava2D(z, dataArea,
207                             domainAxisLocation) - zero1;
208                     transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea,
209                             rangeAxisLocation);
210             }
211             transDomain = Math.abs(transDomain);
212             transRange = Math.abs(transRange);
213             Ellipse2D JavaDoc circle = null;
214             if (orientation == PlotOrientation.VERTICAL) {
215                 circle = new Ellipse2D.Double JavaDoc(transX - transDomain / 2.0,
216                         transY - transRange / 2.0, transDomain, transRange);
217             }
218             else if (orientation == PlotOrientation.HORIZONTAL) {
219                 circle = new Ellipse2D.Double JavaDoc(transY - transRange / 2.0,
220                         transX - transDomain / 2.0, transRange, transDomain);
221             }
222             g2.setPaint(getItemPaint(series, item));
223             g2.fill(circle);
224             g2.setStroke(getItemOutlineStroke(series, item));
225             g2.setPaint(getItemOutlinePaint(series, item));
226             g2.draw(circle);
227
228             if (isItemLabelVisible(series, item)) {
229                 if (orientation == PlotOrientation.VERTICAL) {
230                     drawItemLabel(g2, orientation, dataset, series, item,
231                             transX, transY, false);
232                 }
233                 else if (orientation == PlotOrientation.HORIZONTAL) {
234                     drawItemLabel(g2, orientation, dataset, series, item,
235                             transY, transX, false);
236                 }
237             }
238             
239             // setup for collecting optional entity info...
240
EntityCollection entities = null;
241             if (info != null) {
242                 entities = info.getOwner().getEntityCollection();
243             }
244
245             // add an entity for the item...
246
if (entities != null) {
247                 String JavaDoc tip = null;
248                 XYToolTipGenerator generator
249                     = getToolTipGenerator(series, item);
250                 if (generator != null) {
251                     tip = generator.generateToolTip(dataset, series, item);
252                 }
253                 String JavaDoc url = null;
254                 if (getURLGenerator() != null) {
255                     url = getURLGenerator().generateURL(dataset, series, item);
256                 }
257                 XYItemEntity entity = new XYItemEntity(circle, dataset, series,
258                         item, tip, url);
259                 entities.add(entity);
260             }
261
262             updateCrosshairValues(crosshairState, x, y, transX, transY,
263                     orientation);
264         }
265
266     }
267
268     /**
269      * Returns a legend item for the specified series. The default method
270      * is overridden so that the legend displays circles for all series.
271      *
272      * @param datasetIndex the dataset index (zero-based).
273      * @param series the series index (zero-based).
274      *
275      * @return A legend item for the series.
276      */

277     public LegendItem getLegendItem(int datasetIndex, int series) {
278         LegendItem result = null;
279         XYPlot xyplot = getPlot();
280         if (xyplot != null) {
281             XYDataset dataset = xyplot.getDataset(datasetIndex);
282             if (dataset != null) {
283                 if (getItemVisible(series, 0)) {
284                     String JavaDoc label = getLegendItemLabelGenerator().generateLabel(
285                         dataset, series);
286                     String JavaDoc description = label;
287                     String JavaDoc toolTipText = null;
288                     if (getLegendItemToolTipGenerator() != null) {
289                         toolTipText
290                             = getLegendItemToolTipGenerator().generateLabel(
291                                 dataset, series);
292                     }
293                     String JavaDoc urlText = null;
294                     if (getLegendItemURLGenerator() != null) {
295                         urlText = getLegendItemURLGenerator().generateLabel(
296                             dataset, series);
297                     }
298                     Shape JavaDoc shape = new Ellipse2D.Double JavaDoc(-4.0, -4.0, 8.0, 8.0);
299                     Paint JavaDoc paint = getSeriesPaint(series);
300                     Paint JavaDoc outlinePaint = getSeriesOutlinePaint(series);
301                     Stroke JavaDoc outlineStroke = getSeriesOutlineStroke(series);
302                     result = new LegendItem(label, description,
303                             toolTipText, urlText, shape, paint,
304                             outlineStroke, outlinePaint);
305                 }
306             }
307
308         }
309         return result;
310     }
311     
312     /**
313      * Returns a clone of the renderer.
314      *
315      * @return A clone.
316      *
317      * @throws CloneNotSupportedException if the renderer cannot be cloned.
318      */

319     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
320         return super.clone();
321     }
322
323 }
324
Popular Tags