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