1 42 43 package org.jfree.chart.renderer.xy; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Graphics2D ; 47 import java.awt.Paint ; 48 import java.awt.geom.Line2D ; 49 import java.awt.geom.Rectangle2D ; 50 import java.io.IOException ; 51 import java.io.ObjectInputStream ; 52 import java.io.ObjectOutputStream ; 53 54 import org.jfree.chart.axis.ValueAxis; 55 import org.jfree.chart.event.RendererChangeEvent; 56 import org.jfree.chart.plot.CrosshairState; 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.data.Range; 61 import org.jfree.data.general.DatasetUtilities; 62 import org.jfree.data.xy.IntervalXYDataset; 63 import org.jfree.data.xy.XYDataset; 64 import org.jfree.io.SerialUtilities; 65 import org.jfree.ui.RectangleEdge; 66 import org.jfree.util.PaintUtilities; 67 68 75 public class XYErrorRenderer extends XYLineAndShapeRenderer { 76 77 78 private boolean drawXError; 79 80 81 private boolean drawYError; 82 83 84 private double capLength; 85 86 90 private transient Paint errorPaint; 91 92 95 public XYErrorRenderer() { 96 super(false, true); 97 this.drawXError = true; 98 this.drawYError = true; 99 this.errorPaint = null; 100 this.capLength = 4.0; 101 } 102 103 111 public boolean getDrawXError() { 112 return this.drawXError; 113 } 114 115 124 public void setDrawXError(boolean draw) { 125 if (this.drawXError != draw) { 126 this.drawXError = draw; 127 this.notifyListeners(new RendererChangeEvent(this)); 128 } 129 } 130 131 139 public boolean getDrawYError() { 140 return this.drawYError; 141 } 142 143 152 public void setDrawYError(boolean draw) { 153 if (this.drawYError != draw) { 154 this.drawYError = draw; 155 notifyListeners(new RendererChangeEvent(this)); 156 } 157 } 158 159 167 public double getCapLength() { 168 return this.capLength; 169 } 170 171 179 public void setCapLength(double length) { 180 this.capLength = length; 181 notifyListeners(new RendererChangeEvent(this)); 182 } 183 184 192 public Paint getErrorPaint() { 193 return this.errorPaint; 194 } 195 196 203 public void setErrorPaint(Paint paint) { 204 this.errorPaint = paint; 205 notifyListeners(new RendererChangeEvent(this)); 206 } 207 208 217 public Range findDomainBounds(XYDataset dataset) { 218 if (dataset != null) { 219 return DatasetUtilities.findDomainBounds(dataset, true); 220 } 221 else { 222 return null; 223 } 224 } 225 226 235 public Range findRangeBounds(XYDataset dataset) { 236 if (dataset != null) { 237 return DatasetUtilities.findRangeBounds(dataset, true); 238 } 239 else { 240 return null; 241 } 242 } 243 244 260 public void drawItem(Graphics2D g2, XYItemRendererState state, 261 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, 262 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, 263 int series, int item, CrosshairState crosshairState, int pass) { 264 265 if (pass == 0 && dataset instanceof IntervalXYDataset) { 266 IntervalXYDataset ixyd = (IntervalXYDataset) dataset; 267 PlotOrientation orientation = plot.getOrientation(); 268 if (drawXError) { 269 double x0 = ixyd.getStartXValue(series, item); 271 double x1 = ixyd.getEndXValue(series, item); 272 double y = ixyd.getYValue(series, item); 273 RectangleEdge edge = plot.getDomainAxisEdge(); 274 double xx0 = domainAxis.valueToJava2D(x0, dataArea, edge); 275 double xx1 = domainAxis.valueToJava2D(x1, dataArea, edge); 276 double yy = rangeAxis.valueToJava2D(y, dataArea, 277 plot.getRangeAxisEdge()); 278 Line2D line; 279 Line2D cap1 = null; 280 Line2D cap2 = null; 281 double adj = this.capLength / 2.0; 282 if (orientation == PlotOrientation.VERTICAL) { 283 line = new Line2D.Double (xx0, yy, xx1, yy); 284 cap1 = new Line2D.Double (xx0, yy - adj, xx0, yy + adj); 285 cap2 = new Line2D.Double (xx1, yy - adj, xx1, yy + adj); 286 } 287 else { line = new Line2D.Double (yy, xx0, yy, xx1); 289 cap1 = new Line2D.Double (yy - adj, xx0, yy + adj, xx0); 290 cap2 = new Line2D.Double (yy - adj, xx1, yy + adj, xx1); 291 } 292 g2.setStroke(new BasicStroke (1.0f)); 293 if (this.errorPaint != null) { 294 g2.setPaint(this.errorPaint); 295 } 296 else { 297 g2.setPaint(getItemPaint(series, item)); 298 } 299 g2.draw(line); 300 g2.draw(cap1); 301 g2.draw(cap2); 302 } 303 if (drawYError) { 304 double y0 = ixyd.getStartYValue(series, item); 306 double y1 = ixyd.getEndYValue(series, item); 307 double x = ixyd.getXValue(series, item); 308 RectangleEdge edge = plot.getRangeAxisEdge(); 309 double yy0 = rangeAxis.valueToJava2D(y0, dataArea, edge); 310 double yy1 = rangeAxis.valueToJava2D(y1, dataArea, edge); 311 double xx = domainAxis.valueToJava2D(x, dataArea, 312 plot.getDomainAxisEdge()); 313 Line2D line; 314 Line2D cap1 = null; 315 Line2D cap2 = null; 316 double adj = this.capLength / 2.0; 317 if (orientation == PlotOrientation.VERTICAL) { 318 line = new Line2D.Double (xx, yy0, xx, yy1); 319 cap1 = new Line2D.Double (xx - adj, yy0, xx + adj, yy0); 320 cap2 = new Line2D.Double (xx - adj, yy1, xx + adj, yy1); 321 } 322 else { line = new Line2D.Double (yy0, xx, yy1, xx); 324 cap1 = new Line2D.Double (yy0, xx - adj, yy0, xx + adj); 325 cap2 = new Line2D.Double (yy1, xx - adj, yy1, xx + adj); 326 } 327 g2.setStroke(new BasicStroke (1.0f)); 328 if (this.errorPaint != null) { 329 g2.setPaint(this.errorPaint); 330 } 331 else { 332 g2.setPaint(getItemPaint(series, item)); 333 } 334 g2.draw(line); 335 g2.draw(cap1); 336 g2.draw(cap2); 337 } 338 } 339 super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, 340 dataset, series, item, crosshairState, pass); 341 } 342 343 350 public boolean equals(Object obj) { 351 if (obj == this) { 352 return true; 353 } 354 if (! (obj instanceof XYErrorRenderer)) { 355 return false; 356 } 357 XYErrorRenderer that = (XYErrorRenderer) obj; 358 if (this.drawXError != that.drawXError) { 359 return false; 360 } 361 if (this.drawYError != that.drawYError) { 362 return false; 363 } 364 if (this.capLength != that.capLength) { 365 return false; 366 } 367 if (!PaintUtilities.equal(this.errorPaint, that.errorPaint)) { 368 return false; 369 } 370 return super.equals(obj); 371 } 372 373 381 private void readObject(ObjectInputStream stream) 382 throws IOException , ClassNotFoundException { 383 stream.defaultReadObject(); 384 this.errorPaint = SerialUtilities.readPaint(stream); 385 } 386 387 394 private void writeObject(ObjectOutputStream stream) throws IOException { 395 stream.defaultWriteObject(); 396 SerialUtilities.writePaint(this.errorPaint, stream); 397 } 398 399 } 400 | Popular Tags |