1 52 53 package org.jfree.chart.renderer.xy; 54 55 import java.awt.Graphics2D ; 56 import java.awt.Paint ; 57 import java.awt.Polygon ; 58 import java.awt.Shape ; 59 import java.awt.Stroke ; 60 import java.awt.geom.Rectangle2D ; 61 import java.io.Serializable ; 62 63 import org.jfree.chart.axis.ValueAxis; 64 import org.jfree.chart.entity.EntityCollection; 65 import org.jfree.chart.entity.XYItemEntity; 66 import org.jfree.chart.event.RendererChangeEvent; 67 import org.jfree.chart.labels.XYToolTipGenerator; 68 import org.jfree.chart.plot.CrosshairState; 69 import org.jfree.chart.plot.PlotOrientation; 70 import org.jfree.chart.plot.PlotRenderingInfo; 71 import org.jfree.chart.plot.XYPlot; 72 import org.jfree.chart.urls.XYURLGenerator; 73 import org.jfree.data.xy.XYDataset; 74 import org.jfree.util.PublicCloneable; 75 import org.jfree.util.ShapeUtilities; 76 77 80 public class XYStepAreaRenderer extends AbstractXYItemRenderer 81 implements XYItemRenderer, 82 Cloneable , 83 PublicCloneable, 84 Serializable { 85 86 87 private static final long serialVersionUID = -7311560779702649635L; 88 89 90 public static final int SHAPES = 1; 91 92 93 public static final int AREA = 2; 94 95 98 public static final int AREA_AND_SHAPES = 3; 99 100 101 private boolean shapesVisible; 102 103 104 private boolean shapesFilled; 105 106 107 private boolean plotArea; 108 109 110 private boolean showOutline; 111 112 113 protected transient Polygon pArea = null; 114 115 119 private double rangeBase; 120 121 124 public XYStepAreaRenderer() { 125 this(AREA); 126 } 127 128 133 public XYStepAreaRenderer(int type) { 134 this(type, null, null); 135 } 136 137 148 public XYStepAreaRenderer(int type, 149 XYToolTipGenerator toolTipGenerator, 150 XYURLGenerator urlGenerator) { 151 152 super(); 153 setBaseToolTipGenerator(toolTipGenerator); 154 setURLGenerator(urlGenerator); 155 156 if (type == AREA) { 157 this.plotArea = true; 158 } 159 else if (type == SHAPES) { 160 this.shapesVisible = true; 161 } 162 else if (type == AREA_AND_SHAPES) { 163 this.plotArea = true; 164 this.shapesVisible = true; 165 } 166 this.showOutline = false; 167 } 168 169 175 public boolean isOutline() { 176 return this.showOutline; 177 } 178 179 186 public void setOutline(boolean show) { 187 this.showOutline = show; 188 notifyListeners(new RendererChangeEvent(this)); 189 } 190 191 196 public boolean getShapesVisible() { 197 return this.shapesVisible; 198 } 199 200 207 public void setShapesVisible(boolean flag) { 208 this.shapesVisible = flag; 209 notifyListeners(new RendererChangeEvent(this)); 210 } 211 212 217 public boolean isShapesFilled() { 218 return this.shapesFilled; 219 } 220 221 226 public void setShapesFilled(boolean filled) { 227 this.shapesFilled = filled; 228 notifyListeners(new RendererChangeEvent(this)); 229 } 230 231 236 public boolean getPlotArea() { 237 return this.plotArea; 238 } 239 240 246 public void setPlotArea(boolean flag) { 247 this.plotArea = flag; 248 notifyListeners(new RendererChangeEvent(this)); 249 } 250 251 258 public double getRangeBase() { 259 return this.rangeBase; 260 } 261 262 270 public void setRangeBase(double val) { 271 this.rangeBase = val; 272 notifyListeners(new RendererChangeEvent(this)); 273 } 274 275 288 public XYItemRendererState initialise(Graphics2D g2, 289 Rectangle2D dataArea, 290 XYPlot plot, 291 XYDataset data, 292 PlotRenderingInfo info) { 293 294 return super.initialise(g2, dataArea, plot, data, info); 295 296 } 297 298 299 317 public void drawItem(Graphics2D g2, 318 XYItemRendererState state, 319 Rectangle2D dataArea, 320 PlotRenderingInfo info, 321 XYPlot plot, 322 ValueAxis domainAxis, 323 ValueAxis rangeAxis, 324 XYDataset dataset, 325 int series, 326 int item, 327 CrosshairState crosshairState, 328 int pass) { 329 330 PlotOrientation orientation = plot.getOrientation(); 331 332 int itemCount = dataset.getItemCount(series); 335 336 Paint paint = getItemPaint(series, item); 337 Stroke seriesStroke = getItemStroke(series, item); 338 g2.setPaint(paint); 339 g2.setStroke(seriesStroke); 340 341 double x1 = dataset.getXValue(series, item); 343 double y1 = dataset.getYValue(series, item); 344 double x = x1; 345 double y = Double.isNaN(y1) ? getRangeBase() : y1; 346 double transX1 = domainAxis.valueToJava2D(x, dataArea, 347 plot.getDomainAxisEdge()); 348 double transY1 = rangeAxis.valueToJava2D(y, dataArea, 349 plot.getRangeAxisEdge()); 350 351 transY1 = restrictValueToDataArea(transY1, plot, dataArea); 353 354 if (this.pArea == null && !Double.isNaN(y1)) { 355 356 this.pArea = new Polygon (); 358 359 double transY2 = rangeAxis.valueToJava2D(getRangeBase(), dataArea, 361 plot.getRangeAxisEdge()); 362 363 transY2 = restrictValueToDataArea(transY2, plot, dataArea); 365 366 if (orientation == PlotOrientation.VERTICAL) { 368 this.pArea.addPoint((int) transX1, (int) transY2); 369 } 370 else if (orientation == PlotOrientation.HORIZONTAL) { 371 this.pArea.addPoint((int) transY2, (int) transX1); 372 } 373 } 374 375 double transX0 = 0; 376 double transY0 = restrictValueToDataArea( 377 getRangeBase(), plot, dataArea 378 ); 379 380 double x0; 381 double y0; 382 if (item > 0) { 383 x0 = dataset.getXValue(series, item - 1); 385 y0 = Double.isNaN(y1) ? y1 : dataset.getYValue(series, item - 1); 386 387 x = x0; 388 y = Double.isNaN(y0) ? getRangeBase() : y0; 389 transX0 = domainAxis.valueToJava2D(x, dataArea, 390 plot.getDomainAxisEdge()); 391 transY0 = rangeAxis.valueToJava2D(y, dataArea, 392 plot.getRangeAxisEdge()); 393 394 transY0 = restrictValueToDataArea(transY0, plot, dataArea); 396 397 if (Double.isNaN(y1)) { 398 transX1 = transX0; 401 transY0 = transY1; 402 } 403 if (transY0 != transY1) { 404 if (orientation == PlotOrientation.VERTICAL) { 406 this.pArea.addPoint((int) transX1, (int) transY0); 407 } 408 else if (orientation == PlotOrientation.HORIZONTAL) { 409 this.pArea.addPoint((int) transY0, (int) transX1); 410 } 411 } 412 } 413 414 Shape shape = null; 415 if (!Double.isNaN(y1)) { 416 if (orientation == PlotOrientation.VERTICAL) { 418 this.pArea.addPoint((int) transX1, (int) transY1); 419 } 420 else if (orientation == PlotOrientation.HORIZONTAL) { 421 this.pArea.addPoint((int) transY1, (int) transX1); 422 } 423 424 if (getShapesVisible()) { 425 shape = getItemShape(series, item); 426 if (orientation == PlotOrientation.VERTICAL) { 427 shape = ShapeUtilities.createTranslatedShape(shape, 428 transX1, transY1); 429 } 430 else if (orientation == PlotOrientation.HORIZONTAL) { 431 shape = ShapeUtilities.createTranslatedShape(shape, 432 transY1, transX1); 433 } 434 if (isShapesFilled()) { 435 g2.fill(shape); 436 } 437 else { 438 g2.draw(shape); 439 } 440 } 441 else { 442 if (orientation == PlotOrientation.VERTICAL) { 443 shape = new Rectangle2D.Double (transX1 - 2, transY1 - 2, 444 4.0, 4.0); 445 } 446 else if (orientation == PlotOrientation.HORIZONTAL) { 447 shape = new Rectangle2D.Double (transY1 - 2, transX1 - 2, 448 4.0, 4.0); 449 } 450 } 451 } 452 453 if (getPlotArea() && item > 0 && this.pArea != null 457 && (item == (itemCount - 1) || Double.isNaN(y1))) { 458 459 double transY2 = rangeAxis.valueToJava2D(getRangeBase(), dataArea, 460 plot.getRangeAxisEdge()); 461 462 transY2 = restrictValueToDataArea(transY2, plot, dataArea); 464 465 if (orientation == PlotOrientation.VERTICAL) { 466 this.pArea.addPoint((int) transX1, (int) transY2); 468 } 469 else if (orientation == PlotOrientation.HORIZONTAL) { 470 this.pArea.addPoint((int) transY2, (int) transX1); 472 } 473 474 g2.fill(this.pArea); 476 477 if (isOutline()) { 479 g2.setStroke(plot.getOutlineStroke()); 480 g2.setPaint(plot.getOutlinePaint()); 481 g2.draw(this.pArea); 482 } 483 484 this.pArea = null; 486 } 487 488 if (!Double.isNaN(y1)) { 490 updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, 491 orientation); 492 } 493 494 if (state.getInfo() != null) { 496 EntityCollection entities = state.getEntityCollection(); 497 if (entities != null && shape != null) { 498 String tip = null; 499 XYToolTipGenerator generator 500 = getToolTipGenerator(series, item); 501 if (generator != null) { 502 tip = generator.generateToolTip(dataset, series, item); 503 } 504 String url = null; 505 if (getURLGenerator() != null) { 506 url = getURLGenerator().generateURL(dataset, series, item); 507 } 508 XYItemEntity entity = new XYItemEntity(shape, dataset, series, 509 item, tip, url); 510 entities.add(entity); 511 } 512 } 513 } 514 515 522 public Object clone() throws CloneNotSupportedException { 523 return super.clone(); 524 } 525 526 541 protected static double restrictValueToDataArea(double value, 542 XYPlot plot, 543 Rectangle2D dataArea) { 544 double min = 0; 545 double max = 0; 546 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 547 min = dataArea.getMinY(); 548 max = dataArea.getMaxY(); 549 } 550 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 551 min = dataArea.getMinX(); 552 max = dataArea.getMaxX(); 553 } 554 if (value < min) { 555 value = min; 556 } 557 else if (value > max) { 558 value = max; 559 } 560 return value; 561 } 562 563 } 564 | Popular Tags |