1 58 59 package org.jfree.chart.renderer.xy; 60 61 import java.awt.Graphics2D ; 62 import java.awt.Paint ; 63 import java.awt.Shape ; 64 import java.awt.Stroke ; 65 import java.awt.geom.Ellipse2D ; 66 import java.awt.geom.Rectangle2D ; 67 import java.io.Serializable ; 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 88 public class XYBubbleRenderer extends AbstractXYItemRenderer 89 implements XYItemRenderer, 90 Cloneable , 91 PublicCloneable, 92 Serializable { 93 94 95 public static final long serialVersionUID = -5221991598674249125L; 96 97 98 public static final int SCALE_ON_BOTH_AXES = 0; 99 100 101 public static final int SCALE_ON_DOMAIN_AXIS = 1; 102 103 104 public static final int SCALE_ON_RANGE_AXIS = 2; 105 106 107 private int scaleType; 108 109 112 public XYBubbleRenderer() { 113 this(SCALE_ON_BOTH_AXES); 114 } 115 116 123 public XYBubbleRenderer(int scaleType) { 124 super(); 125 if (scaleType < 0 || scaleType > 2) { 126 throw new IllegalArgumentException ("Invalid 'scaleType'."); 127 } 128 this.scaleType = scaleType; 129 } 130 131 137 public int getScaleType() { 138 return this.scaleType; 139 } 140 141 159 public void drawItem(Graphics2D g2, XYItemRendererState state, 160 Rectangle2D 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 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 circle = null; 214 if (orientation == PlotOrientation.VERTICAL) { 215 circle = new Ellipse2D.Double (transX - transDomain / 2.0, 216 transY - transRange / 2.0, transDomain, transRange); 217 } 218 else if (orientation == PlotOrientation.HORIZONTAL) { 219 circle = new Ellipse2D.Double (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 EntityCollection entities = null; 241 if (info != null) { 242 entities = info.getOwner().getEntityCollection(); 243 } 244 245 if (entities != null) { 247 String tip = null; 248 XYToolTipGenerator generator 249 = getToolTipGenerator(series, item); 250 if (generator != null) { 251 tip = generator.generateToolTip(dataset, series, item); 252 } 253 String 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 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 label = getLegendItemLabelGenerator().generateLabel( 285 dataset, series); 286 String description = label; 287 String toolTipText = null; 288 if (getLegendItemToolTipGenerator() != null) { 289 toolTipText 290 = getLegendItemToolTipGenerator().generateLabel( 291 dataset, series); 292 } 293 String urlText = null; 294 if (getLegendItemURLGenerator() != null) { 295 urlText = getLegendItemURLGenerator().generateLabel( 296 dataset, series); 297 } 298 Shape shape = new Ellipse2D.Double (-4.0, -4.0, 8.0, 8.0); 299 Paint paint = getSeriesPaint(series); 300 Paint outlinePaint = getSeriesOutlinePaint(series); 301 Stroke 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 319 public Object clone() throws CloneNotSupportedException { 320 return super.clone(); 321 } 322 323 } 324 | Popular Tags |