001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2008, 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-2008, 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 * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG); 061 * 062 */ 063 064 package org.jfree.chart.renderer.xy; 065 066 import java.awt.Graphics2D; 067 import java.awt.Paint; 068 import java.awt.Shape; 069 import java.awt.Stroke; 070 import java.awt.geom.Ellipse2D; 071 import java.awt.geom.Rectangle2D; 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}. The example shown here 089 * is generated by the <code>XYBubbleChartDemo1.java</code> program 090 * included in the JFreeChart demo collection: 091 * <br><br> 092 * <img src="../../../../../images/XYBubbleRendererSample.png" 093 * alt="XYBubbleRendererSample.png" /> 094 */ 095 public class XYBubbleRenderer extends AbstractXYItemRenderer 096 implements XYItemRenderer, PublicCloneable { 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 setBaseLegendShape(new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0)); 143 } 144 145 /** 146 * Returns the scale type that was set when the renderer was constructed. 147 * 148 * @return The scale type (one of: {@link #SCALE_ON_BOTH_AXES}, 149 * {@link #SCALE_ON_DOMAIN_AXIS}, {@link #SCALE_ON_RANGE_AXIS}). 150 */ 151 public int getScaleType() { 152 return this.scaleType; 153 } 154 155 /** 156 * Draws the visual representation of a single data item. 157 * 158 * @param g2 the graphics device. 159 * @param state the renderer state. 160 * @param dataArea the area within which the data is being drawn. 161 * @param info collects information about the drawing. 162 * @param plot the plot (can be used to obtain standard color 163 * information etc). 164 * @param domainAxis the domain (horizontal) axis. 165 * @param rangeAxis the range (vertical) axis. 166 * @param dataset the dataset (an {@link XYZDataset} is expected). 167 * @param series the series index (zero-based). 168 * @param item the item index (zero-based). 169 * @param crosshairState crosshair information for the plot 170 * (<code>null</code> permitted). 171 * @param pass the pass index. 172 */ 173 public void drawItem(Graphics2D g2, XYItemRendererState state, 174 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 175 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 176 int series, int item, CrosshairState crosshairState, int pass) { 177 178 // return straight away if the item is not visible 179 if (!getItemVisible(series, item)) { 180 return; 181 } 182 183 PlotOrientation orientation = plot.getOrientation(); 184 185 // get the data point... 186 double x = dataset.getXValue(series, item); 187 double y = dataset.getYValue(series, item); 188 double z = Double.NaN; 189 if (dataset instanceof XYZDataset) { 190 XYZDataset xyzData = (XYZDataset) dataset; 191 z = xyzData.getZValue(series, item); 192 } 193 if (!Double.isNaN(z)) { 194 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); 195 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); 196 double transX = domainAxis.valueToJava2D(x, dataArea, 197 domainAxisLocation); 198 double transY = rangeAxis.valueToJava2D(y, dataArea, 199 rangeAxisLocation); 200 201 double transDomain = 0.0; 202 double transRange = 0.0; 203 double zero; 204 205 switch(getScaleType()) { 206 case SCALE_ON_DOMAIN_AXIS: 207 zero = domainAxis.valueToJava2D(0.0, dataArea, 208 domainAxisLocation); 209 transDomain = domainAxis.valueToJava2D(z, dataArea, 210 domainAxisLocation) - zero; 211 transRange = transDomain; 212 break; 213 case SCALE_ON_RANGE_AXIS: 214 zero = rangeAxis.valueToJava2D(0.0, dataArea, 215 rangeAxisLocation); 216 transRange = zero - rangeAxis.valueToJava2D(z, dataArea, 217 rangeAxisLocation); 218 transDomain = transRange; 219 break; 220 default: 221 double zero1 = domainAxis.valueToJava2D(0.0, dataArea, 222 domainAxisLocation); 223 double zero2 = rangeAxis.valueToJava2D(0.0, dataArea, 224 rangeAxisLocation); 225 transDomain = domainAxis.valueToJava2D(z, dataArea, 226 domainAxisLocation) - zero1; 227 transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea, 228 rangeAxisLocation); 229 } 230 transDomain = Math.abs(transDomain); 231 transRange = Math.abs(transRange); 232 Ellipse2D circle = null; 233 if (orientation == PlotOrientation.VERTICAL) { 234 circle = new Ellipse2D.Double(transX - transDomain / 2.0, 235 transY - transRange / 2.0, transDomain, transRange); 236 } 237 else if (orientation == PlotOrientation.HORIZONTAL) { 238 circle = new Ellipse2D.Double(transY - transRange / 2.0, 239 transX - transDomain / 2.0, transRange, transDomain); 240 } 241 g2.setPaint(getItemPaint(series, item)); 242 g2.fill(circle); 243 g2.setStroke(getItemOutlineStroke(series, item)); 244 g2.setPaint(getItemOutlinePaint(series, item)); 245 g2.draw(circle); 246 247 if (isItemLabelVisible(series, item)) { 248 if (orientation == PlotOrientation.VERTICAL) { 249 drawItemLabel(g2, orientation, dataset, series, item, 250 transX, transY, false); 251 } 252 else if (orientation == PlotOrientation.HORIZONTAL) { 253 drawItemLabel(g2, orientation, dataset, series, item, 254 transY, transX, false); 255 } 256 } 257 258 // add an entity if this info is being collected 259 EntityCollection entities = null; 260 if (info != null) { 261 entities = info.getOwner().getEntityCollection(); 262 if (entities != null && circle.intersects(dataArea)) { 263 addEntity(entities, circle, dataset, series, item, 264 circle.getCenterX(), circle.getCenterY()); 265 } 266 } 267 268 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis); 269 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis); 270 updateCrosshairValues(crosshairState, x, y, domainAxisIndex, 271 rangeAxisIndex, transX, transY, orientation); 272 } 273 274 } 275 276 /** 277 * Returns a legend item for the specified series. The default method 278 * is overridden so that the legend displays circles for all series. 279 * 280 * @param datasetIndex the dataset index (zero-based). 281 * @param series the series index (zero-based). 282 * 283 * @return A legend item for the series. 284 */ 285 public LegendItem getLegendItem(int datasetIndex, int series) { 286 LegendItem result = null; 287 XYPlot plot = getPlot(); 288 if (plot == null) { 289 return null; 290 } 291 292 XYDataset dataset = plot.getDataset(datasetIndex); 293 if (dataset != null) { 294 if (getItemVisible(series, 0)) { 295 String label = getLegendItemLabelGenerator().generateLabel( 296 dataset, series); 297 String description = label; 298 String toolTipText = null; 299 if (getLegendItemToolTipGenerator() != null) { 300 toolTipText = getLegendItemToolTipGenerator().generateLabel( 301 dataset, series); 302 } 303 String urlText = null; 304 if (getLegendItemURLGenerator() != null) { 305 urlText = getLegendItemURLGenerator().generateLabel( 306 dataset, series); 307 } 308 Shape shape = lookupLegendShape(series); 309 Paint paint = lookupSeriesPaint(series); 310 Paint outlinePaint = lookupSeriesOutlinePaint(series); 311 Stroke outlineStroke = lookupSeriesOutlineStroke(series); 312 result = new LegendItem(label, description, toolTipText, 313 urlText, shape, paint, outlineStroke, outlinePaint); 314 result.setLabelFont(lookupLegendTextFont(series)); 315 Paint labelPaint = lookupLegendTextPaint(series); 316 if (labelPaint != null) { 317 result.setLabelPaint(labelPaint); 318 } 319 result.setDataset(dataset); 320 result.setDatasetIndex(datasetIndex); 321 result.setSeriesKey(dataset.getSeriesKey(series)); 322 result.setSeriesIndex(series); 323 } 324 } 325 return result; 326 } 327 328 /** 329 * Tests this renderer for equality with an arbitrary object. 330 * 331 * @param obj the object (<code>null</code> permitted). 332 * 333 * @return A boolean. 334 */ 335 public boolean equals(Object obj) { 336 if (obj == this) { 337 return true; 338 } 339 if (!(obj instanceof XYBubbleRenderer)) { 340 return false; 341 } 342 XYBubbleRenderer that = (XYBubbleRenderer) obj; 343 if (this.scaleType != that.scaleType) { 344 return false; 345 } 346 return super.equals(obj); 347 } 348 349 /** 350 * Returns a clone of the renderer. 351 * 352 * @return A clone. 353 * 354 * @throws CloneNotSupportedException if the renderer cannot be cloned. 355 */ 356 public Object clone() throws CloneNotSupportedException { 357 return super.clone(); 358 } 359 360 }