1 62 63 package org.jfree.chart.renderer; 64 65 import java.awt.Graphics2D ; 66 import java.awt.Shape ; 67 import java.awt.geom.Line2D ; 68 import java.awt.geom.Rectangle2D ; 69 import java.io.Serializable ; 70 71 import org.jfree.chart.axis.CategoryAxis; 72 import org.jfree.chart.axis.ValueAxis; 73 import org.jfree.chart.entity.CategoryItemEntity; 74 import org.jfree.chart.entity.EntityCollection; 75 import org.jfree.chart.labels.CategoryItemLabelGenerator; 76 import org.jfree.chart.plot.CategoryPlot; 77 import org.jfree.chart.plot.PlotOrientation; 78 import org.jfree.data.CategoryDataset; 79 import org.jfree.util.BooleanList; 80 import org.jfree.util.ObjectUtils; 81 import org.jfree.util.PublicCloneable; 82 83 90 public class LineAndShapeRenderer extends AbstractCategoryItemRenderer 91 implements Cloneable , PublicCloneable, Serializable { 92 93 94 public static final int SHAPES = 1; 95 96 97 public static final int LINES = 2; 98 99 100 public static final int SHAPES_AND_LINES = 3; 101 102 103 public static final int TOP = 1; 104 105 106 public static final int BOTTOM = 2; 107 108 109 public static final int LEFT = 3; 110 111 112 public static final int RIGHT = 4; 113 114 115 private boolean drawShapes; 116 117 118 private boolean drawLines; 119 120 121 private Boolean shapesFilled; 122 123 124 private BooleanList seriesShapesFilled; 125 126 127 private Boolean defaultShapesFilled; 128 129 132 public LineAndShapeRenderer() { 133 this(SHAPES_AND_LINES); 134 } 135 136 144 public LineAndShapeRenderer(int type) { 145 super(); 146 if (type == SHAPES) { 147 this.drawShapes = true; 148 } 149 if (type == LINES) { 150 this.drawLines = true; 151 } 152 if (type == SHAPES_AND_LINES) { 153 this.drawShapes = true; 154 this.drawLines = true; 155 } 156 157 this.shapesFilled = null; 158 this.seriesShapesFilled = new BooleanList(); 159 this.defaultShapesFilled = Boolean.TRUE; 160 } 161 162 168 public boolean isDrawShapes() { 169 return this.drawShapes; 170 } 171 172 178 public void setDrawShapes(boolean draw) { 179 if (draw != this.drawShapes) { 180 this.drawShapes = draw; 181 this.firePropertyChanged("Shapes", new Boolean (!draw), new Boolean (draw)); 182 } 183 } 184 185 191 public boolean isDrawLines() { 192 return this.drawLines; 193 } 194 195 200 public void setDrawLines(boolean draw) { 201 if (draw != this.drawLines) { 202 this.drawLines = draw; 203 this.firePropertyChanged("Lines", new Boolean (!draw), new Boolean (draw)); 204 } 205 } 206 207 209 220 public boolean getItemShapeFilled(int series, int item) { 221 return getSeriesShapesFilled(series); 222 } 223 224 231 public boolean getSeriesShapesFilled(int series) { 232 233 if (this.shapesFilled != null) { 235 return this.shapesFilled.booleanValue(); 236 } 237 238 Boolean flag = this.seriesShapesFilled.getBoolean(series); 240 if (flag != null) { 241 return flag.booleanValue(); 242 } 243 else { 244 return this.defaultShapesFilled.booleanValue(); 245 } 246 247 } 248 249 254 public Boolean getShapesFilled() { 255 return this.shapesFilled; 256 } 257 258 263 public void setShapesFilled(boolean filled) { 264 if (filled) { 265 setShapesFilled(Boolean.TRUE); 266 } 267 else { 268 setShapesFilled(Boolean.FALSE); 269 } 270 } 271 272 277 public void setShapesFilled(Boolean filled) { 278 this.shapesFilled = filled; 279 } 280 281 287 public void setSeriesShapesFilled(int series, Boolean filled) { 288 this.seriesShapesFilled.setBoolean(series, filled); 289 } 290 291 297 public void setSeriesShapesFilled(int series, boolean filled) { 298 this.seriesShapesFilled.setBoolean(series, new Boolean (filled)); 299 } 300 301 306 public Boolean getDefaultShapesFilled() { 307 return this.defaultShapesFilled; 308 } 309 310 315 public void setDefaultShapesFilled(Boolean flag) { 316 this.defaultShapesFilled = flag; 317 } 318 319 324 public void setDefaultShapesFilled(boolean flag) { 325 setDefaultShapesFilled(new Boolean (flag)); 326 } 327 328 341 public void drawItem(Graphics2D g2, 342 CategoryItemRendererState state, 343 Rectangle2D dataArea, 344 CategoryPlot plot, 345 CategoryAxis domainAxis, 346 ValueAxis rangeAxis, 347 CategoryDataset dataset, 348 int row, 349 int column) { 350 351 Number value = dataset.getValue(row, column); 353 if (value == null) { 354 return; 355 } 356 357 PlotOrientation orientation = plot.getOrientation(); 358 359 double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, 361 plot.getDomainAxisEdge()); 362 double y1 = rangeAxis.translateValueToJava2D(value.doubleValue(), dataArea, 363 plot.getRangeAxisEdge()); 364 365 g2.setPaint(getItemPaint(row, column)); 366 g2.setStroke(getItemStroke(row, column)); 367 368 Shape shape = getItemShape(row, column); 369 if (orientation == PlotOrientation.HORIZONTAL) { 370 shape = createTransformedShape(shape, y1, x1); 371 } 372 else if (orientation == PlotOrientation.VERTICAL) { 373 shape = createTransformedShape(shape, x1, y1); 374 } 375 if (this.drawShapes) { 376 377 if (getItemShapeFilled(row, column)) { 378 g2.fill(shape); 379 } 380 else { 381 g2.draw(shape); 382 } 383 } 384 385 if (this.drawLines) { 386 if (column != 0) { 387 388 Number previousValue = dataset.getValue(row, column - 1); 389 if (previousValue != null) { 390 391 double previous = previousValue.doubleValue(); 393 double x0 = domainAxis.getCategoryMiddle(column - 1, 394 getColumnCount(), dataArea, 395 plot.getDomainAxisEdge()); 396 double y0 = rangeAxis.translateValueToJava2D(previous, dataArea, 397 plot.getRangeAxisEdge()); 398 399 g2.setPaint(getItemPaint(row, column)); 400 g2.setStroke(getItemStroke(row, column)); 401 Line2D line = null; 402 if (orientation == PlotOrientation.HORIZONTAL) { 403 line = new Line2D.Double (y0, x0, y1, x1); 404 } 405 else if (orientation == PlotOrientation.VERTICAL) { 406 line = new Line2D.Double (x0, y0, x1, y1); 407 } 408 g2.draw(line); 409 } 410 } 411 } 412 413 if (isItemLabelVisible(row, column)) { 415 drawItemLabel(g2, orientation, 416 dataset, row, column, x1, y1, (value.doubleValue() < 0.0)); 417 } 418 419 if (state.getInfo() != null) { 421 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 422 if (entities != null && shape != null) { 423 String tip = null; 424 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); 425 if (generator != null) { 426 tip = generator.generateToolTip(dataset, row, column); 427 } 428 String url = null; 429 if (getItemURLGenerator(row, column) != null) { 430 url = getItemURLGenerator(row, column).generateURL(dataset, row, column); 431 } 432 CategoryItemEntity entity = new CategoryItemEntity( 433 shape, tip, url, dataset, row, dataset.getColumnKey(column), column 434 ); 435 entities.addEntity(entity); 436 437 } 438 439 } 440 441 } 442 443 450 public boolean equals(Object obj) { 451 452 boolean result = super.equals(obj); 453 454 if (obj instanceof LineAndShapeRenderer) { 455 LineAndShapeRenderer r = (LineAndShapeRenderer) obj; 456 boolean b0 = (r.drawLines == this.drawLines); 457 boolean b1 = (r.drawShapes == this.drawShapes); 458 boolean b2 = ObjectUtils.equal(r.shapesFilled, this.shapesFilled); 459 boolean b3 = ObjectUtils.equal(r.seriesShapesFilled, this.seriesShapesFilled); 460 boolean b4 = ObjectUtils.equal(r.defaultShapesFilled, this.defaultShapesFilled); 461 462 result = result && b0 && b1 && b2 && b3 && b4; 463 } 464 465 return result; 466 467 } 468 469 476 public Object clone() throws CloneNotSupportedException { 477 LineAndShapeRenderer clone = (LineAndShapeRenderer) super.clone(); 478 clone.seriesShapesFilled = (BooleanList) this.seriesShapesFilled.clone(); 479 return clone; 480 } 481 } 482 | Popular Tags |