1 42 43 package org.jfree.chart.renderer; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Graphics2D ; 48 import java.awt.geom.Ellipse2D ; 49 import java.awt.geom.Rectangle2D ; 50 import java.io.Serializable ; 51 52 import org.jfree.chart.CrosshairInfo; 53 import org.jfree.chart.axis.ValueAxis; 54 import org.jfree.chart.entity.EntityCollection; 55 import org.jfree.chart.entity.XYItemEntity; 56 import org.jfree.chart.labels.XYZToolTipGenerator; 57 import org.jfree.chart.plot.PlotOrientation; 58 import org.jfree.chart.plot.PlotRenderingInfo; 59 import org.jfree.chart.plot.XYPlot; 60 import org.jfree.chart.urls.XYZURLGenerator; 61 import org.jfree.data.XYDataset; 62 import org.jfree.data.XYZDataset; 63 import org.jfree.ui.RectangleEdge; 64 import org.jfree.util.PublicCloneable; 65 66 72 public class XYBubbleRenderer extends AbstractXYItemRenderer implements XYItemRenderer, 73 Cloneable , 74 PublicCloneable, 75 Serializable { 76 77 78 public static final int SCALE_ON_BOTH_AXES = 0; 79 80 81 public static final int SCALE_ON_DOMAIN_AXIS = 1; 82 83 84 public static final int SCALE_ON_RANGE_AXIS = 2; 85 86 87 private int scaleType; 88 89 92 public XYBubbleRenderer() { 93 this(SCALE_ON_BOTH_AXES); 94 } 95 96 101 public XYBubbleRenderer(int scaleType) { 102 super(); 103 this.scaleType = scaleType; 104 } 105 106 115 public XYBubbleRenderer(int scaleType, 116 XYZToolTipGenerator toolTipGenerator, XYZURLGenerator urlGenerator) { 117 super(); 118 this.scaleType = scaleType; 119 setToolTipGenerator(toolTipGenerator); 120 setURLGenerator(urlGenerator); 121 } 122 123 128 public int getScaleType() { 129 return this.scaleType; 130 } 131 132 148 public void drawItem(Graphics2D g2, 149 XYItemRendererState state, 150 Rectangle2D dataArea, 151 PlotRenderingInfo info, 152 XYPlot plot, 153 ValueAxis domainAxis, 154 ValueAxis rangeAxis, 155 XYDataset dataset, 156 int series, 157 int item, 158 CrosshairInfo crosshairInfo, 159 int pass) { 160 161 Number xn = dataset.getXValue(series, item); 163 Number yn = dataset.getYValue(series, item); 164 Number zn = null; 165 if (dataset instanceof XYZDataset) { 166 XYZDataset xyzData = (XYZDataset) dataset; 167 zn = xyzData.getZValue(series, item); 168 } 169 if (zn != null) { 170 double x = xn.doubleValue(); 171 double y = yn.doubleValue(); 172 double z = zn.doubleValue(); 173 174 RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); 175 RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); 176 double transX = domainAxis.translateValueToJava2D(x, dataArea, domainAxisLocation); 177 double transY = rangeAxis.translateValueToJava2D(y, dataArea, rangeAxisLocation); 178 179 double transDomain = 0.0; 180 double transRange = 0.0; 181 double zero; 182 183 switch(this.scaleType) { 184 case SCALE_ON_DOMAIN_AXIS: 185 zero = domainAxis.translateValueToJava2D(0.0, dataArea, domainAxisLocation); 186 transDomain = domainAxis.translateValueToJava2D(z, dataArea, rangeAxisLocation) 187 - zero; 188 transRange = transDomain; 189 break; 190 case SCALE_ON_RANGE_AXIS: 191 zero = rangeAxis.translateValueToJava2D(0.0, dataArea, rangeAxisLocation); 192 transRange = zero 193 - rangeAxis.translateValueToJava2D(z, dataArea, rangeAxisLocation); 194 transDomain = transRange; 195 break; 196 default: 197 double zero1 = domainAxis.translateValueToJava2D(0.0, dataArea, 198 domainAxisLocation); 199 double zero2 = rangeAxis.translateValueToJava2D(0.0, dataArea, 200 rangeAxisLocation); 201 transDomain = domainAxis.translateValueToJava2D(z, dataArea, domainAxisLocation) 202 - zero1; 203 transRange = zero2 - rangeAxis.translateValueToJava2D(z, dataArea, 204 rangeAxisLocation); 205 } 206 double transZ = -rangeAxis.translateValueToJava2D(z, dataArea, rangeAxisLocation) 207 + rangeAxis.translateValueToJava2D(0.0, dataArea, rangeAxisLocation); 208 transZ = Math.abs(transZ); 209 transDomain = Math.abs(transDomain); 210 transRange = Math.abs(transRange); 211 Ellipse2D circle = null; 212 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 213 circle = new Ellipse2D.Double (transX - transZ / 2.0, 214 transY - transZ / 2.0, 215 transDomain, transRange); 216 } 217 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 218 circle = new Ellipse2D.Double (transY - transZ / 2.0, 219 transX - transZ / 2.0, 220 transRange, transDomain); 221 } 222 g2.setPaint(getItemPaint(series, item)); 223 g2.fill(circle); 224 g2.setStroke(new BasicStroke (1.0f)); 225 g2.setPaint(Color.lightGray); 226 g2.draw(circle); 227 228 EntityCollection entities = null; 230 if (info != null) { 231 entities = info.getOwner().getEntityCollection(); 232 } 233 234 if (entities != null) { 236 String tip = null; 237 if (getToolTipGenerator() != null) { 238 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 239 } 240 String url = null; 241 if (getURLGenerator() != null) { 242 url = getURLGenerator().generateURL(dataset, series, item); 243 } 244 XYItemEntity entity = new XYItemEntity(circle, dataset, series, item, tip, url); 245 entities.addEntity(entity); 246 } 247 248 if (plot.isDomainCrosshairLockedOnData()) { 250 if (plot.isRangeCrosshairLockedOnData()) { 251 crosshairInfo.updateCrosshairPoint(x, y, transX, transY); 253 } 254 else { 255 crosshairInfo.updateCrosshairX(x); 257 } 258 } 259 else { 260 if (plot.isRangeCrosshairLockedOnData()) { 261 crosshairInfo.updateCrosshairY(y); 263 } 264 } 265 } 266 267 } 268 269 276 public Object clone() throws CloneNotSupportedException { 277 return super.clone(); 278 } 279 280 } 281 | Popular Tags |