1 48 49 package org.jfree.chart.renderer; 50 51 import java.awt.BasicStroke ; 52 import java.awt.Color ; 53 import java.awt.Component ; 54 import java.awt.Graphics ; 55 import java.awt.Graphics2D ; 56 import java.awt.Paint ; 57 import java.awt.Shape ; 58 import java.awt.Stroke ; 59 import java.awt.geom.AffineTransform ; 60 import java.awt.geom.Arc2D ; 61 import java.awt.geom.GeneralPath ; 62 import java.awt.geom.Line2D ; 63 import java.awt.geom.Rectangle2D ; 64 import java.io.IOException ; 65 import java.io.ObjectInputStream ; 66 import java.io.ObjectOutputStream ; 67 68 import javax.swing.Icon ; 69 70 import org.jfree.chart.Marker; 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.data.CategoryDataset; 78 import org.jfree.data.Range; 79 import org.jfree.io.SerialUtilities; 80 81 90 public class MinMaxCategoryRenderer extends AbstractCategoryItemRenderer { 91 92 93 private boolean plotLines = false; 94 95 96 private transient Paint groupPaint = Color.black; 97 98 99 private transient Stroke groupStroke = new BasicStroke (1.0f); 100 101 102 private transient Icon minIcon = getIcon(new Arc2D.Double (-4, -4, 8, 8, 0, 360, Arc2D.OPEN), 103 null, Color.black); 104 105 106 private transient Icon maxIcon = getIcon(new Arc2D.Double (-4, -4, 8, 8, 0, 360, Arc2D.OPEN), 107 null, Color.black); 108 109 110 private transient Icon objectIcon = getIcon(new Line2D.Double (-4, 0, 4, 0), false, true); 111 112 113 private int lastCategory = -1; 114 115 116 private double min; 117 118 119 private double max; 120 121 122 private Number minValue; 123 124 125 private Number maxValue; 126 127 130 public MinMaxCategoryRenderer () { 131 } 132 133 146 public void drawItem (Graphics2D g2, 147 CategoryItemRendererState state, 148 Rectangle2D dataArea, 149 CategoryPlot plot, 150 CategoryAxis domainAxis, 151 ValueAxis rangeAxis, 152 CategoryDataset dataset, 153 int row, 154 int column) { 155 156 Number value = dataset.getValue(row, column); 158 if (value != null) { 159 double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, 161 plot.getDomainAxisEdge()); 162 double y1 = rangeAxis.translateValueToJava2D(value.doubleValue(), dataArea, 163 plot.getRangeAxisEdge()); 164 g2.setPaint(getItemPaint(row, column)); 165 g2.setStroke(getItemStroke(row, column)); 166 Shape shape = null; 167 shape = new Rectangle2D.Double (x1 - 4, y1 - 4, 8.0, 8.0); 168 objectIcon.paintIcon(null, g2, (int) x1, (int) y1); 169 if (lastCategory == column) { 170 if (minValue.doubleValue() > value.doubleValue()) { 171 min = y1; 172 minValue = value; 173 } 174 if (maxValue.doubleValue() < value.doubleValue()) { 175 max = y1; 176 maxValue = value; 177 } 178 if (dataset.getRowCount() - 1 == row) { 179 g2.setPaint(groupPaint); 180 g2.setStroke(groupStroke); 181 g2.draw(new Line2D.Double (x1, min, x1, max)); 182 minIcon.paintIcon(null, g2, (int) x1, (int) min); 183 maxIcon.paintIcon(null, g2, (int) x1, (int) max); 184 } 197 } 198 else { 199 lastCategory = column; 200 min = y1; 201 max = y1; 202 minValue = value; 203 maxValue = value; 204 } 205 if (this.plotLines) { 207 if (column != 0) { 208 Number previousValue = dataset.getValue(row, column - 1); 209 if (previousValue != null) { 210 double previous = previousValue.doubleValue(); 212 double x0 = domainAxis.getCategoryStart(column - 1, getColumnCount(), 213 dataArea, 214 plot.getDomainAxisEdge()); 215 double y0 = rangeAxis.translateValueToJava2D(previous, dataArea, 216 plot.getRangeAxisEdge()); 217 g2.setPaint(getItemPaint(row, column)); 218 g2.setStroke(getItemStroke(row, column)); 219 Line2D line = new Line2D.Double (x0, y0, x1, y1); 220 g2.draw(line); 221 } 222 } 223 } 224 225 if (state.getInfo() != null) { 227 EntityCollection entities = state.getInfo().getOwner().getEntityCollection(); 228 if (entities != null && shape != null) { 229 String tip = null; 230 CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); 231 if (generator != null) { 232 tip = generator.generateToolTip(dataset, row, column); 233 } 234 CategoryItemEntity entity = new CategoryItemEntity( 235 shape, tip, null, dataset, row, dataset.getColumnKey(column), column); 236 entities.addEntity(entity); 237 } 238 } 239 } 240 } 241 242 305 315 public void drawRangeMarker (Graphics2D g2, CategoryPlot plot, ValueAxis axis, 316 Marker marker, Rectangle2D axisDataArea, Shape dataClipRegion) { 317 double value = marker.getValue(); 318 Range range = axis.getRange(); 319 if (!range.contains(value)) { 320 return; 321 } 322 double y = axis.translateValueToJava2D(marker.getValue(), axisDataArea, 323 plot.getRangeAxisEdge()); 324 Line2D line = new Line2D.Double (axisDataArea.getMinX(), y, axisDataArea.getMaxX(), y); 325 g2.setPaint(marker.getOutlinePaint()); 326 g2.draw(line); 327 } 328 329 334 public void setDrawLines (boolean drawLines) { 335 this.plotLines = drawLines; 336 } 337 338 343 public boolean isDrawLines () { 344 return plotLines; 345 } 346 347 352 public void setGroupPaint (Paint groupPaint) { 353 this.groupPaint = groupPaint; 354 } 355 356 361 public Paint getGroupPaint () { 362 return groupPaint; 363 } 364 365 370 public void setGroupStroke (Stroke groupStroke) { 371 this.groupStroke = groupStroke; 372 } 373 374 379 public Stroke getGroupStroke () { 380 return groupStroke; 381 } 382 383 388 public void setObjectIcon (Icon objectIcon) { 389 this.objectIcon = objectIcon; 390 } 391 392 397 public Icon getObjectIcon () { 398 return objectIcon; 399 } 400 401 406 public void setMaxIcon (Icon maxIcon) { 407 this.maxIcon = maxIcon; 408 } 409 410 415 public Icon getMaxIcone () { 416 return maxIcon; 417 } 418 419 424 public void setMinIcon (Icon minIcon) { 425 this.minIcon = minIcon; 426 } 427 428 433 public Icon getMinIcon () { 434 return minIcon; 435 } 436 437 446 private Icon getIcon(Shape shape, final Paint fillPaint, final Paint outlinePaint) { 447 448 final int width = shape.getBounds().width; 449 final int height = shape.getBounds().height; 450 final GeneralPath path = new GeneralPath (shape); 451 return new Icon () { 452 public void paintIcon(Component c, Graphics g, int x, int y) { 453 Graphics2D g2 = (Graphics2D ) g; 454 path.transform(AffineTransform.getTranslateInstance(x, y)); 455 if (fillPaint != null) { 456 g2.setPaint(fillPaint); 457 g2.fill(path); 458 } 459 if (outlinePaint != null) { 460 g2.setPaint(outlinePaint); 461 g2.draw(path); 462 } 463 path.transform(AffineTransform.getTranslateInstance(-x, -y)); 464 } 465 466 public int getIconWidth() { 467 return width; 468 } 469 470 public int getIconHeight() { 471 return height; 472 } 473 474 }; 475 } 476 477 486 private Icon getIcon(Shape shape, final boolean fill, final boolean outline) { 487 final int width = shape.getBounds().width; 488 final int height = shape.getBounds().height; 489 final GeneralPath path = new GeneralPath (shape); 490 return new Icon () { 491 public void paintIcon(Component c, Graphics g, int x, int y) { 492 Graphics2D g2 = (Graphics2D ) g; 493 path.transform(AffineTransform.getTranslateInstance(x, y)); 494 if (fill) { 495 g2.fill(path); 496 } 497 if (outline) { 498 g2.draw(path); 499 } 500 path.transform(AffineTransform.getTranslateInstance(-x, -y)); 501 } 502 503 public int getIconWidth() { 504 return width; 505 } 506 507 public int getIconHeight() { 508 return height; 509 } 510 }; 511 } 512 513 520 private void writeObject(ObjectOutputStream stream) throws IOException { 521 stream.defaultWriteObject(); 522 SerialUtilities.writeStroke(this.groupStroke, stream); 523 SerialUtilities.writePaint(this.groupPaint, stream); 524 } 525 526 534 private void readObject(ObjectInputStream stream) throws IOException , ClassNotFoundException { 535 stream.defaultReadObject(); 536 this.groupStroke = SerialUtilities.readStroke(stream); 537 this.groupPaint = SerialUtilities.readPaint(stream); 538 539 minIcon = getIcon(new Arc2D.Double (-4, -4, 8, 8, 0, 360, Arc2D.OPEN), null, Color.black); 540 maxIcon = getIcon(new Arc2D.Double (-4, -4, 8, 8, 0, 360, Arc2D.OPEN), null, Color.black); 541 objectIcon = getIcon(new Line2D.Double (-4, 0, 4, 0), false, true); 542 } 543 544 } 545 | Popular Tags |