1 54 55 package org.jfree.chart.renderer; 56 57 import java.awt.Graphics2D ; 58 import java.awt.Paint ; 59 import java.awt.Polygon ; 60 import java.awt.Shape ; 61 import java.awt.Stroke ; 62 import java.awt.geom.Line2D ; 63 import java.awt.geom.Rectangle2D ; 64 import java.io.Serializable ; 65 66 import org.jfree.chart.CrosshairInfo; 67 import org.jfree.chart.axis.ValueAxis; 68 import org.jfree.chart.entity.EntityCollection; 69 import org.jfree.chart.entity.XYItemEntity; 70 import org.jfree.chart.labels.XYToolTipGenerator; 71 import org.jfree.chart.plot.PlotOrientation; 72 import org.jfree.chart.plot.PlotRenderingInfo; 73 import org.jfree.chart.plot.XYPlot; 74 import org.jfree.chart.urls.XYURLGenerator; 75 import org.jfree.data.XYDataset; 76 import org.jfree.util.PublicCloneable; 77 78 85 public class AreaXYRenderer extends AbstractXYItemRenderer implements XYItemRenderer, 86 Cloneable , 87 PublicCloneable, 88 Serializable { 89 90 91 public static final int SHAPES = 1; 92 93 94 public static final int LINES = 2; 95 96 97 public static final int SHAPES_AND_LINES = 3; 98 99 100 public static final int AREA = 4; 101 102 103 public static final int AREA_AND_SHAPES = 5; 104 105 106 private boolean plotShapes; 107 108 109 private boolean plotLines; 110 111 112 private boolean plotArea; 113 114 115 private boolean showOutline; 116 117 118 protected transient Line2D line; 119 120 121 protected transient Polygon pArea = null; 122 123 126 public AreaXYRenderer() { 127 128 this(AREA); 129 130 } 131 132 137 public AreaXYRenderer(int type) { 138 this(type, null, null); 139 } 140 141 151 public AreaXYRenderer(int type, 152 XYToolTipGenerator toolTipGenerator, XYURLGenerator urlGenerator) { 153 154 super(); 155 setToolTipGenerator(toolTipGenerator); 156 setURLGenerator(urlGenerator); 157 158 if (type == SHAPES) { 159 this.plotShapes = true; 160 } 161 if (type == LINES) { 162 this.plotLines = true; 163 } 164 if (type == SHAPES_AND_LINES) { 165 this.plotShapes = true; 166 this.plotLines = true; 167 } 168 if (type == AREA) { 169 this.plotArea = true; 170 } 171 if (type == AREA_AND_SHAPES) { 172 this.plotArea = true; 173 this.plotShapes = true; 174 } 175 this.line = new Line2D.Double (0.0, 0.0, 0.0, 0.0); 176 showOutline = false; 177 178 } 179 180 185 public boolean isOutline() { 186 return showOutline; 187 } 188 189 194 public void setOutline(boolean show) { 195 showOutline = show; 196 } 197 198 203 public boolean getPlotShapes() { 204 return this.plotShapes; 205 } 206 207 212 public boolean getPlotLines() { 213 return this.plotLines; 214 } 215 216 221 public boolean getPlotArea() { 222 return this.plotArea; 223 } 224 225 237 public XYItemRendererState initialise(Graphics2D g2, 238 Rectangle2D dataArea, 239 XYPlot plot, 240 XYDataset data, 241 PlotRenderingInfo info) { 242 243 return super.initialise(g2, dataArea, plot, data, info); 244 245 } 246 247 263 public void drawItem(Graphics2D g2, 264 XYItemRendererState state, 265 Rectangle2D dataArea, 266 PlotRenderingInfo info, 267 XYPlot plot, 268 ValueAxis domainAxis, 269 ValueAxis rangeAxis, 270 XYDataset dataset, 271 int series, 272 int item, 273 CrosshairInfo crosshairInfo, 274 int pass) { 275 276 int itemCount = dataset.getItemCount(series); 278 279 Paint paint = getItemPaint(series, item); 280 Stroke seriesStroke = getItemStroke(series, item); 281 g2.setPaint(paint); 282 g2.setStroke(seriesStroke); 283 284 Number x1 = dataset.getXValue(series, item); 286 Number y1 = dataset.getYValue(series, item); 287 double transX1 = domainAxis.translateValueToJava2D(x1.doubleValue(), dataArea, 288 plot.getDomainAxisEdge()); 289 double transY1 = rangeAxis.translateValueToJava2D(y1.doubleValue(), dataArea, 290 plot.getRangeAxisEdge()); 291 292 if (item == 0) { 293 pArea = new Polygon (); 295 296 double transY2 = rangeAxis.translateValueToJava2D(0.0, dataArea, 298 plot.getRangeAxisEdge()); 299 300 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 302 pArea.addPoint((int) transX1, (int) transY2); 303 } 304 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 305 pArea.addPoint((int) transY2, (int) transX1); 306 } 307 } 308 309 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 311 pArea.addPoint((int) transX1, (int) transY1); 312 } 313 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 314 pArea.addPoint((int) transY1, (int) transX1); 315 } 316 Shape shape = null; 317 if (this.plotShapes) { 318 shape = getItemShape(series, item); 319 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 320 shape = createTransformedShape(shape, transX1, transY1); 321 } 322 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 323 shape = createTransformedShape(shape, transY1, transX1); 324 } 325 g2.draw(shape); 326 } 327 else { 328 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 329 shape = new Rectangle2D.Double (transX1 - 2, transY1 - 2, 4.0, 4.0); 330 } 331 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 332 shape = new Rectangle2D.Double (transY1 - 2, transX1 - 2, 4.0, 4.0); 333 } 334 } 335 336 if (this.plotLines) { 337 if (item > 0) { 338 Number x0 = dataset.getXValue(series, item - 1); 340 Number y0 = dataset.getYValue(series, item - 1); 341 double transX0 = domainAxis.translateValueToJava2D(x0.doubleValue(), dataArea, 342 plot.getDomainAxisEdge()); 343 double transY0 = rangeAxis.translateValueToJava2D(y0.doubleValue(), dataArea, 344 plot.getRangeAxisEdge()); 345 346 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 347 line.setLine(transX0, transY0, transX1, transY1); 348 } 349 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 350 line.setLine(transY0, transX0, transY1, transX1); 351 } 352 g2.draw(line); 353 } 354 } 355 356 if (this.plotArea && item > 0 && item == (itemCount - 1)) { 359 360 double transY2 = rangeAxis.translateValueToJava2D(0.0, dataArea, 361 plot.getRangeAxisEdge()); 362 363 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 364 pArea.addPoint((int) transX1, (int) transY2); 366 } 367 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 368 pArea.addPoint((int) transY2, (int) transX1); 370 } 371 372 g2.fill(pArea); 374 375 if (showOutline) { 377 g2.setStroke(plot.getOutlineStroke()); 378 g2.setPaint(plot.getOutlinePaint()); 379 g2.draw(pArea); 380 } 381 } 382 383 if (plot.isDomainCrosshairLockedOnData()) { 385 if (plot.isRangeCrosshairLockedOnData()) { 386 crosshairInfo.updateCrosshairPoint(x1.doubleValue(), y1.doubleValue(), 388 transX1, transY1); 389 } 390 else { 391 crosshairInfo.updateCrosshairX(x1.doubleValue()); 393 394 } 395 } 396 else { 397 if (plot.isRangeCrosshairLockedOnData()) { 398 crosshairInfo.updateCrosshairY(y1.doubleValue()); 400 } 401 } 402 403 if (state.getInfo() != null) { 405 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 406 if (entities != null && shape != null) { 407 String tip = null; 408 if (getToolTipGenerator() != null) { 409 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 410 } 411 String url = null; 412 if (getURLGenerator() != null) { 413 url = getURLGenerator().generateURL(dataset, series, item); 414 } 415 XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url); 416 entities.addEntity(entity); 417 } 418 } 419 420 } 421 422 429 public Object clone() throws CloneNotSupportedException { 430 return super.clone(); 431 } 432 433 } 434 | Popular Tags |