1 43 44 package org.jfree.chart.renderer; 45 46 import java.awt.Graphics2D ; 47 import java.awt.Paint ; 48 import java.awt.Point ; 49 import java.awt.Polygon ; 50 import java.awt.Shape ; 51 import java.awt.Stroke ; 52 import java.awt.geom.Rectangle2D ; 53 import java.io.Serializable ; 54 import java.util.Stack ; 55 56 import org.jfree.chart.CrosshairInfo; 57 import org.jfree.chart.axis.ValueAxis; 58 import org.jfree.chart.entity.EntityCollection; 59 import org.jfree.chart.entity.XYItemEntity; 60 import org.jfree.chart.labels.XYToolTipGenerator; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.chart.plot.PlotRenderingInfo; 63 import org.jfree.chart.plot.XYPlot; 64 import org.jfree.chart.urls.XYURLGenerator; 65 import org.jfree.data.TableXYDataset; 66 import org.jfree.data.XYDataset; 67 import org.jfree.util.PublicCloneable; 68 69 74 public class StackedAreaXYRenderer extends AreaXYRenderer implements Cloneable , 75 PublicCloneable, 76 Serializable { 77 78 79 private transient Stack lastSeriesPoints = null; 80 81 82 private transient Stack thisSeriesPoints = null; 83 84 85 private Paint shapePaint = null; 86 87 88 private Stroke shapeStroke = null; 89 90 93 public StackedAreaXYRenderer() { 94 this(AREA); 95 } 96 101 public StackedAreaXYRenderer(int type) { 102 this(type, null, null); 103 } 104 105 115 public StackedAreaXYRenderer(int type, 116 XYToolTipGenerator toolTipGenerator, XYURLGenerator urlGenerator) { 117 118 super(type, toolTipGenerator, urlGenerator); 119 } 120 121 126 public RangeType getRangeType() { 127 return RangeType.STACKED; 128 } 129 130 145 public XYItemRendererState initialise(Graphics2D g2, 146 Rectangle2D dataArea, 147 XYPlot plot, 148 XYDataset data, 149 PlotRenderingInfo info) { 150 151 return super.initialise(g2, dataArea, plot, data, info); 152 153 } 154 155 160 public int getPassCount() { 161 return 2; 162 } 163 164 180 public void drawItem(Graphics2D g2, 181 XYItemRendererState state, 182 Rectangle2D dataArea, 183 PlotRenderingInfo info, 184 XYPlot plot, 185 ValueAxis domainAxis, 186 ValueAxis rangeAxis, 187 XYDataset dataset, 188 int series, 189 int item, 190 CrosshairInfo crosshairInfo, 191 int pass) { 192 193 TableXYDataset tableXYDataset = (TableXYDataset) dataset; 195 int itemCount = tableXYDataset.getItemCount(); 196 197 Number x1 = dataset.getXValue(series, item); 199 Number y1 = dataset.getYValue(series, item); 200 boolean nullPoint = false; 201 if (y1 == null) { 202 y1 = new Double (0); 203 nullPoint = true; 204 } 205 206 double ph1 = this.getPreviousHeight(dataset, series, item); 208 double transX1 = domainAxis.translateValueToJava2D(x1.doubleValue(), dataArea, 209 plot.getDomainAxisEdge()); 210 double transY1 = rangeAxis.translateValueToJava2D(y1.doubleValue() + ph1, dataArea, 211 plot.getRangeAxisEdge()); 212 213 Paint seriesPaint = getItemPaint(series, item); 215 Stroke seriesStroke = getItemStroke(series, item); 216 217 if (pass == 0) { 218 220 if (item == 0) { 221 pArea = new Polygon (); 223 this.lastSeriesPoints = this.thisSeriesPoints; 224 this.thisSeriesPoints = new Stack (); 225 226 double transY2 = rangeAxis.translateValueToJava2D(ph1, dataArea, 228 plot.getRangeAxisEdge()); 229 230 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 232 pArea.addPoint((int) transX1, (int) transY2); 233 } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 234 pArea.addPoint((int) transY2, (int) transX1); 235 } 236 } 237 238 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 240 Point point = new Point ((int) transX1, (int) transY1); 241 pArea.addPoint((int) point.getX(), (int) point.getY()); 242 this.thisSeriesPoints.push(point); 243 } 244 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 245 pArea.addPoint((int) transY1, (int) transX1); 246 } 247 248 if (this.getPlotLines()) { 249 if (item > 0) { 250 Number x0 = dataset.getXValue(series, item - 1); 252 Number y0 = dataset.getYValue(series, item - 1); 253 double ph0 = this.getPreviousHeight(dataset, series, item - 1); 254 double transX0 = domainAxis.translateValueToJava2D(x0.doubleValue(), dataArea, 255 plot.getDomainAxisEdge()); 256 double transY0 = rangeAxis.translateValueToJava2D(y0.doubleValue() + ph0, 257 dataArea, 258 plot.getRangeAxisEdge()); 259 260 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 261 line.setLine(transX0, transY0, transX1, transY1); 262 } 263 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 264 line.setLine(transY0, transX0, transY1, transX1); 265 } 266 g2.draw(line); 267 } 268 } 269 270 if (this.getPlotArea() && item > 0 && item == (itemCount - 1)) { 273 274 double transY2 = rangeAxis.translateValueToJava2D(ph1, dataArea, 275 plot.getRangeAxisEdge()); 276 277 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 278 pArea.addPoint((int) transX1, (int) transY2); 280 } 281 else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 282 pArea.addPoint((int) transY2, (int) transX1); 284 } 285 286 if (series != 0) { 288 while (!this.lastSeriesPoints.empty()) { 289 Point point = (Point ) this.lastSeriesPoints.pop(); 290 pArea.addPoint((int) point.getX(), (int) point.getY()); 291 } 292 } 293 294 g2.setPaint(seriesPaint); 296 g2.setStroke(seriesStroke); 297 g2.fill(pArea); 298 299 if (this.isOutline()) { 301 g2.setStroke(plot.getOutlineStroke()); 302 g2.setPaint(plot.getOutlinePaint()); 303 g2.draw(pArea); 304 } 305 } 306 307 if (plot.isDomainCrosshairLockedOnData()) { 309 if (plot.isRangeCrosshairLockedOnData()) { 310 crosshairInfo.updateCrosshairPoint(x1.doubleValue(), y1.doubleValue(), 312 transX1, transY1); 313 } else { 314 crosshairInfo.updateCrosshairX(x1.doubleValue()); 316 317 } 318 } else { 319 if (plot.isRangeCrosshairLockedOnData()) { 320 crosshairInfo.updateCrosshairY(y1.doubleValue()); 322 } 323 } 324 325 } else if (pass == 1) { 326 328 Shape shape = null; 329 if (this.getPlotShapes()) { 330 shape = getItemShape(series, item); 331 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 332 shape = createTransformedShape(shape, transX1, transY1); 333 } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 334 shape = createTransformedShape(shape, transY1, transX1); 335 } 336 if (!nullPoint) { 337 if (this.shapePaint != null) { 338 g2.setPaint(this.shapePaint); 339 } else { 340 g2.setPaint(seriesPaint); 341 } 342 if (this.shapeStroke != null) { 343 g2.setStroke(this.shapeStroke); 344 } else { 345 g2.setStroke(seriesStroke); 346 } 347 g2.draw(shape); 348 } 349 } else { 350 if (plot.getOrientation() == PlotOrientation.VERTICAL) { 351 shape = new Rectangle2D.Double (transX1 - 3, transY1 - 3, 6.0, 6.0); 352 } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { 353 shape = new Rectangle2D.Double (transY1 - 3, transX1 - 3, 6.0, 6.0); 354 } 355 } 356 357 if (state.getInfo() != null) { 359 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 360 if (entities != null && shape != null && !nullPoint) { 361 String tip = null; 362 if (getToolTipGenerator() != null) { 363 tip = getToolTipGenerator().generateToolTip(dataset, series, item); 364 } 365 String url = null; 366 if (getURLGenerator() != null) { 367 url = getURLGenerator().generateURL(dataset, series, item); 368 } 369 XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url); 370 entities.addEntity(entity); 371 } 372 } 373 374 } 375 } 376 377 378 390 protected double getPreviousHeight(XYDataset data, int series, int index) { 391 392 double result = 0.0; 393 394 Number tmp; 395 for (int i = 0; i < series; i++) { 396 tmp = data.getYValue(i, index); 397 if (tmp != null) { 398 result += tmp.doubleValue(); 399 } 400 } 401 402 return result; 403 404 } 405 406 413 public Object clone() throws CloneNotSupportedException { 414 return super.clone(); 415 } 416 417 422 public Paint getShapePaint() { 423 return this.shapePaint; 424 } 425 426 431 public Stroke getShapeStroke() { 432 return this.shapeStroke; 433 } 434 435 440 public void setShapePaint(Paint shapePaint) { 441 this.shapePaint = shapePaint; 442 } 443 444 449 public void setShapeStroke(Stroke shapeStroke) { 450 this.shapeStroke = shapeStroke; 451 } 452 453 } 454 | Popular Tags |