1 42 43 package org.jfree.experimental.chart.renderer.xy; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Graphics2D ; 47 import java.awt.Paint ; 48 import java.awt.geom.Rectangle2D ; 49 import java.io.Serializable ; 50 51 import org.jfree.chart.axis.ValueAxis; 52 import org.jfree.chart.event.RendererChangeEvent; 53 import org.jfree.chart.plot.CrosshairState; 54 import org.jfree.chart.plot.PlotOrientation; 55 import org.jfree.chart.plot.PlotRenderingInfo; 56 import org.jfree.chart.plot.XYPlot; 57 import org.jfree.chart.renderer.xy.AbstractXYItemRenderer; 58 import org.jfree.chart.renderer.xy.XYItemRenderer; 59 import org.jfree.chart.renderer.xy.XYItemRendererState; 60 import org.jfree.data.Range; 61 import org.jfree.data.general.DatasetUtilities; 62 import org.jfree.data.xy.XYDataset; 63 import org.jfree.data.xy.XYZDataset; 64 import org.jfree.experimental.chart.renderer.LookupPaintScale; 65 import org.jfree.experimental.chart.renderer.PaintScale; 66 import org.jfree.ui.RectangleAnchor; 67 68 77 public class XYBlockRenderer extends AbstractXYItemRenderer 78 implements XYItemRenderer, Cloneable , Serializable { 79 80 83 private double blockWidth = 1.0; 84 85 88 private double blockHeight = 1.0; 89 90 94 private RectangleAnchor blockAnchor = RectangleAnchor.CENTER; 95 96 private double xOffset; 97 98 private double yOffset; 99 100 private PaintScale paintScale; 101 102 106 public XYBlockRenderer() { 107 updateOffsets(); 108 this.paintScale = new LookupPaintScale(); 109 } 110 111 118 public double getBlockWidth() { 119 return this.blockWidth; 120 } 121 122 129 public void setBlockWidth(double width) { 130 if (width <= 0.0) { 131 throw new IllegalArgumentException ( 132 "The 'width' argument must be > 0.0"); 133 } 134 this.blockWidth = width; 135 updateOffsets(); 136 this.notifyListeners(new RendererChangeEvent(this)); 137 } 138 139 146 public double getBlockHeight() { 147 return this.blockHeight; 148 } 149 150 157 public void setBlockHeight(double height) { 158 if (height <= 0.0) { 159 throw new IllegalArgumentException ( 160 "The 'height' argument must be > 0.0"); 161 } 162 this.blockHeight = height; 163 updateOffsets(); 164 this.notifyListeners(new RendererChangeEvent(this)); 165 } 166 167 175 public RectangleAnchor getBlockAnchor() { 176 return this.blockAnchor; 177 } 178 179 187 public void setBlockAnchor(RectangleAnchor anchor) { 188 if (anchor == null) { 189 throw new IllegalArgumentException ("Null 'anchor' argument."); 190 } 191 if (this.blockAnchor.equals(anchor)) { 192 return; } 194 this.blockAnchor = anchor; 195 updateOffsets(); 196 notifyListeners(new RendererChangeEvent(this)); 197 } 198 199 204 public void setPaintScale(PaintScale scale) { 205 if (scale == null) { 206 throw new IllegalArgumentException ("Null 'scale' argument."); 207 } 208 this.paintScale = scale; 209 notifyListeners(new RendererChangeEvent(this)); 210 } 211 212 216 private void updateOffsets() { 217 if (this.blockAnchor.equals(RectangleAnchor.BOTTOM_LEFT)) { 218 xOffset = 0.0; 219 yOffset = 0.0; 220 } 221 else if (this.blockAnchor.equals(RectangleAnchor.BOTTOM)) { 222 xOffset = -this.blockWidth / 2.0; 223 yOffset = 0.0; 224 } 225 else if (this.blockAnchor.equals(RectangleAnchor.BOTTOM_RIGHT)) { 226 xOffset = -this.blockWidth; 227 yOffset = 0.0; 228 } 229 else if (this.blockAnchor.equals(RectangleAnchor.LEFT)) { 230 xOffset = 0.0; 231 yOffset = -this.blockHeight / 2.0; 232 } 233 else if (this.blockAnchor.equals(RectangleAnchor.CENTER)) { 234 xOffset = -this.blockWidth / 2.0; 235 yOffset = -this.blockHeight / 2.0; 236 } 237 else if (this.blockAnchor.equals(RectangleAnchor.RIGHT)) { 238 xOffset = -this.blockWidth; 239 yOffset = -this.blockHeight / 2.0; 240 } 241 else if (this.blockAnchor.equals(RectangleAnchor.TOP_LEFT)) { 242 xOffset = 0.0; 243 yOffset = -this.blockHeight; 244 } 245 else if (this.blockAnchor.equals(RectangleAnchor.TOP)) { 246 xOffset = -this.blockWidth / 2.0; 247 yOffset = -this.blockHeight; 248 } 249 else if (this.blockAnchor.equals(RectangleAnchor.TOP_RIGHT)) { 250 xOffset = -this.blockWidth; 251 yOffset = -this.blockHeight; 252 } 253 } 254 255 264 public Range findDomainBounds(XYDataset dataset) { 265 if (dataset != null) { 266 Range r = DatasetUtilities.findDomainBounds(dataset, false); 267 return new Range(r.getLowerBound() + xOffset, r.getUpperBound() 268 + blockWidth + xOffset); 269 } 270 else { 271 return null; 272 } 273 } 274 275 284 public Range findRangeBounds(XYDataset dataset) { 285 if (dataset != null) { 286 Range r = DatasetUtilities.findRangeBounds(dataset, false); 287 return new Range(r.getLowerBound() + yOffset, r.getUpperBound() 288 + blockHeight + yOffset); 289 } 290 else { 291 return null; 292 } 293 } 294 295 311 public void drawItem(Graphics2D g2, XYItemRendererState state, 312 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 313 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 314 int series, int item, CrosshairState crosshairState, int pass) { 315 316 double x = dataset.getXValue(series, item); 317 double y = dataset.getYValue(series, item); 318 double z = 0.0; 319 if (dataset instanceof XYZDataset) { 320 z = ((XYZDataset) dataset).getZValue(series, item); 321 } 322 Paint p = this.paintScale.getPaint(z); 323 double xx0 = domainAxis.valueToJava2D(x + this.xOffset, dataArea, 324 plot.getDomainAxisEdge()); 325 double yy0 = rangeAxis.valueToJava2D(y + this.yOffset, dataArea, 326 plot.getRangeAxisEdge()); 327 double xx1 = domainAxis.valueToJava2D(x + this.blockWidth 328 + this.xOffset, dataArea, plot.getDomainAxisEdge()); 329 double yy1 = rangeAxis.valueToJava2D(y + this.blockHeight 330 + this.yOffset, dataArea, plot.getRangeAxisEdge()); 331 Rectangle2D block; 332 PlotOrientation orientation = plot.getOrientation(); 333 if (orientation.equals(PlotOrientation.HORIZONTAL)) { 334 block = new Rectangle2D.Double (Math.min(yy0, yy1), 335 Math.min(xx0, xx1), Math.abs(yy1 - yy0), 336 Math.abs(xx0 - xx1)); 337 } 338 else { 339 block = new Rectangle2D.Double (Math.min(xx0, xx1), 340 Math.min(yy0, yy1), Math.abs(xx1 - xx0), 341 Math.abs(yy1 - yy0)); 342 } 343 g2.setPaint(p); 344 g2.fill(block); 345 g2.setStroke(new BasicStroke (1.0f)); 346 g2.draw(block); 347 } 348 349 363 public boolean equals(Object obj) { 364 if (obj == this) { 365 return true; 366 } 367 if (!(obj instanceof XYBlockRenderer)) { 368 return false; 369 } 370 XYBlockRenderer that = (XYBlockRenderer) obj; 371 if (this.blockHeight != that.blockHeight) { 372 return false; 373 } 374 if (this.blockWidth != that.blockWidth) { 375 return false; 376 } 377 if (!this.blockAnchor.equals(that.blockAnchor)) { 378 return false; 379 } 380 if (!this.paintScale.equals(that.paintScale)) { 381 return false; 382 } 383 return super.equals(obj); 384 } 385 386 394 public Object clone() throws CloneNotSupportedException { 395 return super.clone(); 396 } 397 398 } 399 | Popular Tags |