001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------------
028 * XYBubbleRenderer.java
029 * ---------------------
030 * (C) Copyright 2003-2007, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Christian W. Zuckschwerdt;
034 *
035 * $Id: XYBubbleRenderer.java,v 1.8.2.13 2007/06/13 09:57:14 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 28-Jan-2003 : Version 1 (DG);
040 * 25-Mar-2003 : Implemented Serializable (DG);
041 * 01-May-2003 : Modified drawItem() method signature (DG);
042 * 30-Jul-2003 : Modified entity constructor (CZ);
043 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
044 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
045 * 10-Feb-2004 : Small change to drawItem() method to make cut-and-paste
046 * overriding easier (DG);
047 * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
048 * 19-Jan-2005 : Now accesses only primitives from dataset (DG);
049 * 28-Feb-2005 : Modify renderer to use circles in legend (DG);
050 * 17-Mar-2005 : Fixed bug in bubble bounds calculation (DG);
051 * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
052 * ------------- JFREECHART 1.0.x ---------------------------------------------
053 * 13-Dec-2005 : Added support for item labels (bug 1373371) (DG);
054 * 20-Jan-2006 : Check flag for drawing item labels (DG);
055 * 21-Sep-2006 : Respect the outline paint and stroke settings (DG);
056 * 24-Jan-2007 : Added new equals() override (DG);
057 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG);
058 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
059 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
060 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
061 * 13-Jun-2007 : Fixed seriesVisibility bug (DG);
062 *
063 */
064
065 package org.jfree.chart.renderer.xy;
066
067 import java.awt.Graphics2D;
068 import java.awt.Paint;
069 import java.awt.Shape;
070 import java.awt.Stroke;
071 import java.awt.geom.Ellipse2D;
072 import java.awt.geom.Rectangle2D;
073 import java.io.Serializable;
074
075 import org.jfree.chart.LegendItem;
076 import org.jfree.chart.axis.ValueAxis;
077 import org.jfree.chart.entity.EntityCollection;
078 import org.jfree.chart.plot.CrosshairState;
079 import org.jfree.chart.plot.PlotOrientation;
080 import org.jfree.chart.plot.PlotRenderingInfo;
081 import org.jfree.chart.plot.XYPlot;
082 import org.jfree.data.xy.XYDataset;
083 import org.jfree.data.xy.XYZDataset;
084 import org.jfree.ui.RectangleEdge;
085 import org.jfree.util.PublicCloneable;
086
087 /**
088 * A renderer that draws a circle at each data point with a diameter that is
089 * determined by the z-value in the dataset (the renderer requires the dataset
090 * to be an instance of {@link XYZDataset}.
091 */
092 public class XYBubbleRenderer extends AbstractXYItemRenderer
093 implements XYItemRenderer,
094 Cloneable,
095 PublicCloneable,
096 Serializable {
097
098 /** For serialization. */
099 public static final long serialVersionUID = -5221991598674249125L;
100
101 /**
102 * A constant to specify that the bubbles drawn by this renderer should be
103 * scaled on both axes (see {@link #XYBubbleRenderer(int)}).
104 */
105 public static final int SCALE_ON_BOTH_AXES = 0;
106
107 /**
108 * A constant to specify that the bubbles drawn by this renderer should be
109 * scaled on the domain axis (see {@link #XYBubbleRenderer(int)}).
110 */
111 public static final int SCALE_ON_DOMAIN_AXIS = 1;
112
113 /**
114 * A constant to specify that the bubbles drawn by this renderer should be
115 * scaled on the range axis (see {@link #XYBubbleRenderer(int)}).
116 */
117 public static final int SCALE_ON_RANGE_AXIS = 2;
118
119 /** Controls how the width and height of the bubble are scaled. */
120 private int scaleType;
121
122 /**
123 * Constructs a new renderer.
124 */
125 public XYBubbleRenderer() {
126 this(SCALE_ON_BOTH_AXES);
127 }
128
129 /**
130 * Constructs a new renderer with the specified type of scaling.
131 *
132 * @param scaleType the type of scaling (must be one of:
133 * {@link #SCALE_ON_BOTH_AXES}, {@link #SCALE_ON_DOMAIN_AXIS},
134 * {@link #SCALE_ON_RANGE_AXIS}).
135 */
136 public XYBubbleRenderer(int scaleType) {
137 super();
138 if (scaleType < 0 || scaleType > 2) {
139 throw new IllegalArgumentException("Invalid 'scaleType'.");
140 }
141 this.scaleType = scaleType;
142 }
143
144 /**
145 * Returns the scale type that was set when the renderer was constructed.
146 *
147 * @return The scale type (one of: {@link #SCALE_ON_BOTH_AXES},
148 * {@link #SCALE_ON_DOMAIN_AXIS}, {@link #SCALE_ON_RANGE_AXIS}).
149 */
150 public int getScaleType() {
151 return this.scaleType;
152 }
153
154 /**
155 * Draws the visual representation of a single data item.
156 *
157 * @param g2 the graphics device.
158 * @param state the renderer state.
159 * @param dataArea the area within which the data is being drawn.
160 * @param info collects information about the drawing.
161 * @param plot the plot (can be used to obtain standard color
162 * information etc).
163 * @param domainAxis the domain (horizontal) axis.
164 * @param rangeAxis the range (vertical) axis.
165 * @param dataset the dataset (an {@link XYZDataset} is expected).
166 * @param series the series index (zero-based).
167 * @param item the item index (zero-based).
168 * @param crosshairState crosshair information for the plot
169 * (<code>null</code> permitted).
170 * @param pass the pass index.
171 */
172 public void drawItem(Graphics2D g2, XYItemRendererState state,
173 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
174 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
175 int series, int item, CrosshairState crosshairState, int pass) {
176
177 // return straight away if the item is not visible
178 if (!getItemVisible(series, item)) {
179 return;
180 }
181
182 PlotOrientation orientation = plot.getOrientation();
183
184 // get the data point...
185 double x = dataset.getXValue(series, item);
186 double y = dataset.getYValue(series, item);
187 double z = Double.NaN;
188 if (dataset instanceof XYZDataset) {
189 XYZDataset xyzData = (XYZDataset) dataset;
190 z = xyzData.getZValue(series, item);
191 }
192 if (!Double.isNaN(z)) {
193 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
194 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
195 double transX = domainAxis.valueToJava2D(x, dataArea,
196 domainAxisLocation);
197 double transY = rangeAxis.valueToJava2D(y, dataArea,
198 rangeAxisLocation);
199
200 double transDomain = 0.0;
201 double transRange = 0.0;
202 double zero;
203
204 switch(getScaleType()) {
205 case SCALE_ON_DOMAIN_AXIS:
206 zero = domainAxis.valueToJava2D(0.0, dataArea,
207 domainAxisLocation);
208 transDomain = domainAxis.valueToJava2D(z, dataArea,
209 domainAxisLocation) - zero;
210 transRange = transDomain;
211 break;
212 case SCALE_ON_RANGE_AXIS:
213 zero = rangeAxis.valueToJava2D(0.0, dataArea,
214 rangeAxisLocation);
215 transRange = zero - rangeAxis.valueToJava2D(z, dataArea,
216 rangeAxisLocation);
217 transDomain = transRange;
218 break;
219 default:
220 double zero1 = domainAxis.valueToJava2D(0.0, dataArea,
221 domainAxisLocation);
222 double zero2 = rangeAxis.valueToJava2D(0.0, dataArea,
223 rangeAxisLocation);
224 transDomain = domainAxis.valueToJava2D(z, dataArea,
225 domainAxisLocation) - zero1;
226 transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea,
227 rangeAxisLocation);
228 }
229 transDomain = Math.abs(transDomain);
230 transRange = Math.abs(transRange);
231 Ellipse2D circle = null;
232 if (orientation == PlotOrientation.VERTICAL) {
233 circle = new Ellipse2D.Double(transX - transDomain / 2.0,
234 transY - transRange / 2.0, transDomain, transRange);
235 }
236 else if (orientation == PlotOrientation.HORIZONTAL) {
237 circle = new Ellipse2D.Double(transY - transRange / 2.0,
238 transX - transDomain / 2.0, transRange, transDomain);
239 }
240 g2.setPaint(getItemPaint(series, item));
241 g2.fill(circle);
242 g2.setStroke(getItemOutlineStroke(series, item));
243 g2.setPaint(getItemOutlinePaint(series, item));
244 g2.draw(circle);
245
246 if (isItemLabelVisible(series, item)) {
247 if (orientation == PlotOrientation.VERTICAL) {
248 drawItemLabel(g2, orientation, dataset, series, item,
249 transX, transY, false);
250 }
251 else if (orientation == PlotOrientation.HORIZONTAL) {
252 drawItemLabel(g2, orientation, dataset, series, item,
253 transY, transX, false);
254 }
255 }
256
257 // add an entity if this info is being collected
258 EntityCollection entities = null;
259 if (info != null) {
260 entities = info.getOwner().getEntityCollection();
261 if (entities != null && circle.intersects(dataArea)) {
262 addEntity(entities, circle, dataset, series, item,
263 circle.getCenterX(), circle.getCenterY());
264 }
265 }
266
267 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
268 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
269 updateCrosshairValues(crosshairState, x, y, domainAxisIndex,
270 rangeAxisIndex, transX, transY, orientation);
271 }
272
273 }
274
275 /**
276 * Returns a legend item for the specified series. The default method
277 * is overridden so that the legend displays circles for all series.
278 *
279 * @param datasetIndex the dataset index (zero-based).
280 * @param series the series index (zero-based).
281 *
282 * @return A legend item for the series.
283 */
284 public LegendItem getLegendItem(int datasetIndex, int series) {
285 LegendItem result = null;
286 XYPlot plot = getPlot();
287 if (plot == null) {
288 return null;
289 }
290
291 XYDataset dataset = plot.getDataset(datasetIndex);
292 if (dataset != null) {
293 if (getItemVisible(series, 0)) {
294 String label = getLegendItemLabelGenerator().generateLabel(
295 dataset, series);
296 String description = label;
297 String toolTipText = null;
298 if (getLegendItemToolTipGenerator() != null) {
299 toolTipText = getLegendItemToolTipGenerator().generateLabel(
300 dataset, series);
301 }
302 String urlText = null;
303 if (getLegendItemURLGenerator() != null) {
304 urlText = getLegendItemURLGenerator().generateLabel(
305 dataset, series);
306 }
307 Shape shape = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
308 Paint paint = lookupSeriesPaint(series);
309 Paint outlinePaint = lookupSeriesOutlinePaint(series);
310 Stroke outlineStroke = lookupSeriesOutlineStroke(series);
311 result = new LegendItem(label, description, toolTipText,
312 urlText, shape, paint, outlineStroke, outlinePaint);
313 result.setDataset(dataset);
314 result.setDatasetIndex(datasetIndex);
315 result.setSeriesKey(dataset.getSeriesKey(series));
316 result.setSeriesIndex(series);
317 }
318 }
319 return result;
320 }
321
322 /**
323 * Tests this renderer for equality with an arbitrary object.
324 *
325 * @param obj the object (<code>null</code> permitted).
326 *
327 * @return A boolean.
328 */
329 public boolean equals(Object obj) {
330 if (obj == this) {
331 return true;
332 }
333 if (!(obj instanceof XYBubbleRenderer)) {
334 return false;
335 }
336 XYBubbleRenderer that = (XYBubbleRenderer) obj;
337 if (this.scaleType != that.scaleType) {
338 return false;
339 }
340 return super.equals(obj);
341 }
342
343 /**
344 * Returns a clone of the renderer.
345 *
346 * @return A clone.
347 *
348 * @throws CloneNotSupportedException if the renderer cannot be cloned.
349 */
350 public Object clone() throws CloneNotSupportedException {
351 return super.clone();
352 }
353
354 }