1 41 42 package org.jfree.chart.renderer; 43 44 import java.awt.Graphics2D ; 45 import java.awt.Paint ; 46 import java.awt.Polygon ; 47 import java.awt.Shape ; 48 import java.awt.Stroke ; 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.XYToolTipGenerator; 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.XYURLGenerator; 61 import org.jfree.data.XYDataset; 62 import org.jfree.util.PublicCloneable; 63 64 69 public class XYStepAreaRenderer extends AbstractXYItemRenderer 70 implements XYItemRenderer, 71 Cloneable , 72 PublicCloneable, 73 Serializable { 74 75 76 public static final int SHAPES = 1; 77 78 79 public static final int AREA = 2; 80 81 82 public static final int AREA_AND_SHAPES = 3; 83 84 85 private boolean plotShapes; 86 87 88 private boolean shapesFilled; 89 90 91 private boolean plotArea; 92 93 94 private boolean showOutline; 95 96 97 protected transient Polygon pArea = null; 98 99 100 private double rangeBase; 101 102 105 public XYStepAreaRenderer() { 106 this(AREA); 107 } 108 109 114 public XYStepAreaRenderer(int type) { 115 this(type, null, null); 116 } 117 118 128 public XYStepAreaRenderer(int type, 129 XYToolTipGenerator toolTipGenerator, XYURLGenerator urlGenerator) { 130 131 super(); 132 setToolTipGenerator(toolTipGenerator); 133 setURLGenerator(urlGenerator); 134 135 if (type == AREA) { 136 this.plotArea = true; 137 } 138 else if (type == SHAPES) { 139 this.plotShapes = true; 140 } 141 else if (type == AREA_AND_SHAPES) { 142 this.plotArea = true; 143 this.plotShapes = true; 144 } 145 showOutline = false; 146 } 147 148 153 public boolean isOutline() { 154 return showOutline; 155 } 156 157 162 public void setOutline(boolean show) { 163 showOutline = show; 164 } 165 166 171 public boolean getPlotShapes() { 172 return this.plotShapes; 173 } 174 175 180 public void setShapesFilled(boolean filled) { 181 this.shapesFilled = filled; 182 } 183 184 189 public boolean getPlotArea() { 190 return this.plotArea; 191 } 192 193 200 public void setRangeBase(double val) { 201 this.rangeBase = val; 202 } 203 204 210 public double getRangeBase() { 211 return this.rangeBase; 212 } 213 214 226 public XYItemRendererState initialise(Graphics2D g2, 227 Rectangle2D dataArea, 228 XYPlot plot, 229 XYDataset data, 230 PlotRenderingInfo info) { 231 232 return super.initialise(g2, dataArea, plot, data, info); 233 234 } 235 236 237 253 public void drawItem(Graphics2D g2, 254 XYItemRendererState state, 255 Rectangle2D dataArea, 256 PlotRenderingInfo info, 257 XYPlot plot, 258 ValueAxis domainAxis, 259 ValueAxis rangeAxis, 260 XYDataset dataset, 261 int series, 262 int item, 263 CrosshairInfo crosshairInfo, 264 int pass) { 265 266 int itemCount = dataset.getItemCount(series); 268 269 Paint paint = getItemPaint(series, item); 270 Stroke seriesStroke = getItemStroke(series, item); 271 g2.setPaint(paint); 272 g2.setStroke(seriesStroke); 273 274 Number x1 = dataset.getXValue(series, item); 276 Number y1 = dataset.getYValue(series, item); 277 double x = x1.doubleValue(); 278 double y = y1 == null ? this.rangeBase : y1.doubleValue(); 279 double transX1 = domainAxis.translateValueToJava2D(x, dataArea, 280 plot.getDomainAxisEdge()); 281 double transY1 = rangeAxis.translateValueToJava2D(y, dataArea, 282 plot.getRangeAxisEdge()); 283 284 transY1 = restrictValueToDataArea(transY1, plot, dataArea); 286 287 if (pArea == null && y1 != null) { 288 289 pArea = new Polygon (); 291 292 double transY2 = rangeAxis.translateValueToJava2D(this.rangeBase, dataArea, 294 plot.getRangeAxisEdge()); 295 296 transY2 = restrictValueToDataArea(transY2, plot, dataArea); 298 299 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 301 pArea.addPoint((int) transX1, (int) transY2); 302 } 303 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 304 pArea.addPoint((int) transY2, (int) transX1); 305 } 306 } 307 308 double transX0 = 0; 309 double transY0 = restrictValueToDataArea(this.rangeBase, plot, dataArea); 310 311 Number x0 = null; 312 Number y0 = null; 313 if (item > 0) { 314 x0 = dataset.getXValue(series, item - 1); 316 y0 = y1 == null ? null : dataset.getYValue(series, item - 1); 317 318 x = x0.doubleValue(); 319 y = y0 == null ? this.rangeBase : y0.doubleValue(); 320 transX0 = domainAxis.translateValueToJava2D(x, dataArea, plot.getDomainAxisEdge()); 321 transY0 = rangeAxis.translateValueToJava2D(y, dataArea, plot.getRangeAxisEdge()); 322 323 transY0 = restrictValueToDataArea(transY0, plot, dataArea); 325 326 if (y1 == null) { 327 transX1 = transX0; 330 transY0 = transY1; 331 } 332 if (transY0 != transY1) { 333 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 335 pArea.addPoint((int) transX1, (int) transY0); 336 } 337 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 338 pArea.addPoint((int) transY0, (int) transX1); 339 } 340 } 341 } 342 343 Shape shape = null; 344 if (y1 != null) { 345 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 347 pArea.addPoint((int) transX1, (int) transY1); 348 } 349 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 350 pArea.addPoint((int) transY1, (int) transX1); 351 } 352 353 if (this.plotShapes) { 354 shape = getItemShape(series, item); 355 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 356 shape = createTransformedShape(shape, transX1, transY1); 357 } 358 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 359 shape = createTransformedShape(shape, transY1, transX1); 360 } 361 if (shapesFilled) 362 g2.fill(shape); 363 else 364 g2.draw(shape); 365 } 366 else { 367 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 368 shape = new Rectangle2D.Double (transX1 - 2, transY1 - 2, 4.0, 4.0); 369 } 370 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 371 shape = new Rectangle2D.Double (transY1 - 2, transX1 - 2, 4.0, 4.0); 372 } 373 } 374 } 375 376 if (this.plotArea && item > 0 && pArea != null && (item == (itemCount - 1) || y1 == null)) { 379 380 double transY2 = rangeAxis.translateValueToJava2D(this.rangeBase, dataArea, 381 plot.getRangeAxisEdge()); 382 383 transY2 = restrictValueToDataArea(transY2, plot, dataArea); 385 386 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 387 pArea.addPoint((int) transX1, (int) transY2); 389 } 390 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 391 pArea.addPoint((int) transY2, (int) transX1); 393 } 394 395 g2.fill(pArea); 397 398 if (showOutline) { 400 g2.setStroke(plot.getOutlineStroke()); 401 g2.setPaint(plot.getOutlinePaint()); 402 g2.draw(pArea); 403 } 404 405 pArea = null; 407 } 408 409 if (y1 != null) { 411 if (plot.isDomainCrosshairLockedOnData()) { 412 if (plot.isRangeCrosshairLockedOnData()) { 413 crosshairInfo.updateCrosshairPoint(x1.doubleValue(), y1.doubleValue(), 415 transX1, transY1); 416 } 417 else { 418 crosshairInfo.updateCrosshairX(x1.doubleValue()); 420 421 } 422 } 423 else { 424 if (plot.isRangeCrosshairLockedOnData()) { 425 crosshairInfo.updateCrosshairY(y1.doubleValue()); 427 } 428 } 429 } 430 431 if (state.getInfo() != null) { 433 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 434 if (entities != null && shape != null) { 435 String tip = null; 436 if (getToolTipGenerator() != null) { 437 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 438 } 439 String url = null; 440 if (getURLGenerator() != null) { 441 url = getURLGenerator().generateURL(dataset, series, item); 442 } 443 XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url); 444 entities.addEntity(entity); 445 } 446 } 447 } 448 449 456 public Object clone() throws CloneNotSupportedException { 457 return super.clone(); 458 } 459 460 474 private static double restrictValueToDataArea(double value, XYPlot plot, Rectangle2D dataArea) { 475 double min = 0; 476 double max = 0; 477 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 478 min = dataArea.getMinY(); 479 max = dataArea.getMaxY(); 480 } 481 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 482 min = dataArea.getMinX(); 483 max = dataArea.getMaxX(); 484 } 485 if (value < min) { 486 value = min; 487 } 488 else if (value > max) { 489 value = max; 490 } 491 return value; 492 } 493 494 } 495 | Popular Tags |