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.9 2007/02/06 16:29:11 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 * 059 */ 060 061 package org.jfree.chart.renderer.xy; 062 063 import java.awt.Graphics2D; 064 import java.awt.Paint; 065 import java.awt.Shape; 066 import java.awt.Stroke; 067 import java.awt.geom.Ellipse2D; 068 import java.awt.geom.Rectangle2D; 069 import java.io.Serializable; 070 071 import org.jfree.chart.LegendItem; 072 import org.jfree.chart.axis.ValueAxis; 073 import org.jfree.chart.entity.EntityCollection; 074 import org.jfree.chart.entity.XYItemEntity; 075 import org.jfree.chart.labels.XYToolTipGenerator; 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 PlotOrientation orientation = plot.getOrientation(); 176 177 // get the data point... 178 double x = dataset.getXValue(series, item); 179 double y = dataset.getYValue(series, item); 180 double z = Double.NaN; 181 if (dataset instanceof XYZDataset) { 182 XYZDataset xyzData = (XYZDataset) dataset; 183 z = xyzData.getZValue(series, item); 184 } 185 if (!Double.isNaN(z)) { 186 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); 187 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); 188 double transX = domainAxis.valueToJava2D(x, dataArea, 189 domainAxisLocation); 190 double transY = rangeAxis.valueToJava2D(y, dataArea, 191 rangeAxisLocation); 192 193 double transDomain = 0.0; 194 double transRange = 0.0; 195 double zero; 196 197 switch(getScaleType()) { 198 case SCALE_ON_DOMAIN_AXIS: 199 zero = domainAxis.valueToJava2D(0.0, dataArea, 200 domainAxisLocation); 201 transDomain = domainAxis.valueToJava2D(z, dataArea, 202 domainAxisLocation) - zero; 203 transRange = transDomain; 204 break; 205 case SCALE_ON_RANGE_AXIS: 206 zero = rangeAxis.valueToJava2D(0.0, dataArea, 207 rangeAxisLocation); 208 transRange = zero - rangeAxis.valueToJava2D(z, dataArea, 209 rangeAxisLocation); 210 transDomain = transRange; 211 break; 212 default: 213 double zero1 = domainAxis.valueToJava2D(0.0, dataArea, 214 domainAxisLocation); 215 double zero2 = rangeAxis.valueToJava2D(0.0, dataArea, 216 rangeAxisLocation); 217 transDomain = domainAxis.valueToJava2D(z, dataArea, 218 domainAxisLocation) - zero1; 219 transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea, 220 rangeAxisLocation); 221 } 222 transDomain = Math.abs(transDomain); 223 transRange = Math.abs(transRange); 224 Ellipse2D circle = null; 225 if (orientation == PlotOrientation.VERTICAL) { 226 circle = new Ellipse2D.Double(transX - transDomain / 2.0, 227 transY - transRange / 2.0, transDomain, transRange); 228 } 229 else if (orientation == PlotOrientation.HORIZONTAL) { 230 circle = new Ellipse2D.Double(transY - transRange / 2.0, 231 transX - transDomain / 2.0, transRange, transDomain); 232 } 233 g2.setPaint(getItemPaint(series, item)); 234 g2.fill(circle); 235 g2.setStroke(getItemOutlineStroke(series, item)); 236 g2.setPaint(getItemOutlinePaint(series, item)); 237 g2.draw(circle); 238 239 if (isItemLabelVisible(series, item)) { 240 if (orientation == PlotOrientation.VERTICAL) { 241 drawItemLabel(g2, orientation, dataset, series, item, 242 transX, transY, false); 243 } 244 else if (orientation == PlotOrientation.HORIZONTAL) { 245 drawItemLabel(g2, orientation, dataset, series, item, 246 transY, transX, false); 247 } 248 } 249 250 // setup for collecting optional entity info... 251 EntityCollection entities = null; 252 if (info != null) { 253 entities = info.getOwner().getEntityCollection(); 254 } 255 256 // add an entity for the item... 257 if (entities != null) { 258 String tip = null; 259 XYToolTipGenerator generator 260 = getToolTipGenerator(series, item); 261 if (generator != null) { 262 tip = generator.generateToolTip(dataset, series, item); 263 } 264 String url = null; 265 if (getURLGenerator() != null) { 266 url = getURLGenerator().generateURL(dataset, series, item); 267 } 268 XYItemEntity entity = new XYItemEntity(circle, dataset, series, 269 item, tip, url); 270 entities.add(entity); 271 } 272 273 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis); 274 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis); 275 updateCrosshairValues(crosshairState, x, y, domainAxisIndex, 276 rangeAxisIndex, transX, transY, orientation); 277 } 278 279 } 280 281 /** 282 * Returns a legend item for the specified series. The default method 283 * is overridden so that the legend displays circles for all series. 284 * 285 * @param datasetIndex the dataset index (zero-based). 286 * @param series the series index (zero-based). 287 * 288 * @return A legend item for the series. 289 */ 290 public LegendItem getLegendItem(int datasetIndex, int series) { 291 LegendItem result = null; 292 XYPlot xyplot = getPlot(); 293 if (xyplot != null) { 294 XYDataset dataset = xyplot.getDataset(datasetIndex); 295 if (dataset != null) { 296 if (getItemVisible(series, 0)) { 297 String label = getLegendItemLabelGenerator().generateLabel( 298 dataset, series); 299 String description = label; 300 String toolTipText = null; 301 if (getLegendItemToolTipGenerator() != null) { 302 toolTipText 303 = getLegendItemToolTipGenerator().generateLabel( 304 dataset, series); 305 } 306 String urlText = null; 307 if (getLegendItemURLGenerator() != null) { 308 urlText = getLegendItemURLGenerator().generateLabel( 309 dataset, series); 310 } 311 Shape shape = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0); 312 Paint paint = getSeriesPaint(series); 313 Paint outlinePaint = getSeriesOutlinePaint(series); 314 Stroke outlineStroke = getSeriesOutlineStroke(series); 315 result = new LegendItem(label, description, 316 toolTipText, urlText, shape, paint, 317 outlineStroke, outlinePaint); 318 } 319 } 320 321 } 322 return result; 323 } 324 325 /** 326 * Tests this renderer for equality with an arbitrary object. 327 * 328 * @param obj the object (<code>null</code> permitted). 329 * 330 * @return A boolean. 331 */ 332 public boolean equals(Object obj) { 333 if (obj == this) { 334 return true; 335 } 336 if (!(obj instanceof XYBubbleRenderer)) { 337 return false; 338 } 339 XYBubbleRenderer that = (XYBubbleRenderer) obj; 340 if (this.scaleType != that.scaleType) { 341 return false; 342 } 343 return super.equals(obj); 344 } 345 346 /** 347 * Returns a clone of the renderer. 348 * 349 * @return A clone. 350 * 351 * @throws CloneNotSupportedException if the renderer cannot be cloned. 352 */ 353 public Object clone() throws CloneNotSupportedException { 354 return super.clone(); 355 } 356 357 }