001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * --------------------
028 * XYErrorRenderer.java
029 * --------------------
030 * (C) Copyright 2006-2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes
036 * -------
037 * 25-Oct-2006 : Version 1 (DG);
038 * 23-Mar-2007 : Check item visibility before drawing error bars - see bug
039 * 1686178 (DG);
040 *
041 */
042
043 package org.jfree.chart.renderer.xy;
044
045 import java.awt.BasicStroke;
046 import java.awt.Graphics2D;
047 import java.awt.Paint;
048 import java.awt.geom.Line2D;
049 import java.awt.geom.Rectangle2D;
050 import java.io.IOException;
051 import java.io.ObjectInputStream;
052 import java.io.ObjectOutputStream;
053
054 import org.jfree.chart.axis.ValueAxis;
055 import org.jfree.chart.event.RendererChangeEvent;
056 import org.jfree.chart.plot.CrosshairState;
057 import org.jfree.chart.plot.PlotOrientation;
058 import org.jfree.chart.plot.PlotRenderingInfo;
059 import org.jfree.chart.plot.XYPlot;
060 import org.jfree.data.Range;
061 import org.jfree.data.general.DatasetUtilities;
062 import org.jfree.data.xy.IntervalXYDataset;
063 import org.jfree.data.xy.XYDataset;
064 import org.jfree.io.SerialUtilities;
065 import org.jfree.ui.RectangleEdge;
066 import org.jfree.util.PaintUtilities;
067
068 /**
069 * A line and shape renderer that can also display x and/or y-error values.
070 * This renderer expects an {@link IntervalXYDataset}, otherwise it reverts
071 * to the behaviour of the super class. The example shown here is generated by
072 * the <code>XYErrorRendererDemo1.java</code> program included in the
073 * JFreeChart demo collection:
074 * <br><br>
075 * <img src="../../../../../images/XYErrorRendererSample.png"
076 * alt="XYErrorRendererSample.png" />
077 *
078 * @since 1.0.3
079 */
080 public class XYErrorRenderer extends XYLineAndShapeRenderer {
081
082 /** For serialization. */
083 static final long serialVersionUID = 5162283570955172424L;
084
085 /** A flag that controls whether or not the x-error bars are drawn. */
086 private boolean drawXError;
087
088 /** A flag that controls whether or not the y-error bars are drawn. */
089 private boolean drawYError;
090
091 /** The length of the cap at the end of the error bars. */
092 private double capLength;
093
094 /**
095 * The paint used to draw the error bars (if <code>null</code> we use the
096 * series paint).
097 */
098 private transient Paint errorPaint;
099
100 /**
101 * Creates a new <code>XYErrorRenderer</code> instance.
102 */
103 public XYErrorRenderer() {
104 super(false, true);
105 this.drawXError = true;
106 this.drawYError = true;
107 this.errorPaint = null;
108 this.capLength = 4.0;
109 }
110
111 /**
112 * Returns the flag that controls whether or not the renderer draws error
113 * bars for the x-values.
114 *
115 * @return A boolean.
116 *
117 * @see #setDrawXError(boolean)
118 */
119 public boolean getDrawXError() {
120 return this.drawXError;
121 }
122
123 /**
124 * Sets the flag that controls whether or not the renderer draws error
125 * bars for the x-values and, if the flag changes, sends a
126 * {@link RendererChangeEvent} to all registered listeners.
127 *
128 * @param draw the flag value.
129 *
130 * @see #getDrawXError()
131 */
132 public void setDrawXError(boolean draw) {
133 if (this.drawXError != draw) {
134 this.drawXError = draw;
135 fireChangeEvent();
136 }
137 }
138
139 /**
140 * Returns the flag that controls whether or not the renderer draws error
141 * bars for the y-values.
142 *
143 * @return A boolean.
144 *
145 * @see #setDrawYError(boolean)
146 */
147 public boolean getDrawYError() {
148 return this.drawYError;
149 }
150
151 /**
152 * Sets the flag that controls whether or not the renderer draws error
153 * bars for the y-values and, if the flag changes, sends a
154 * {@link RendererChangeEvent} to all registered listeners.
155 *
156 * @param draw the flag value.
157 *
158 * @see #getDrawYError()
159 */
160 public void setDrawYError(boolean draw) {
161 if (this.drawYError != draw) {
162 this.drawYError = draw;
163 fireChangeEvent();
164 }
165 }
166
167 /**
168 * Returns the length (in Java2D units) of the cap at the end of the error
169 * bars.
170 *
171 * @return The cap length.
172 *
173 * @see #setCapLength(double)
174 */
175 public double getCapLength() {
176 return this.capLength;
177 }
178
179 /**
180 * Sets the length of the cap at the end of the error bars, and sends a
181 * {@link RendererChangeEvent} to all registered listeners.
182 *
183 * @param length the length (in Java2D units).
184 *
185 * @see #getCapLength()
186 */
187 public void setCapLength(double length) {
188 this.capLength = length;
189 fireChangeEvent();
190 }
191
192 /**
193 * Returns the paint used to draw the error bars. If this is
194 * <code>null</code> (the default), the item paint is used instead.
195 *
196 * @return The paint (possibly <code>null</code>).
197 *
198 * @see #setErrorPaint(Paint)
199 */
200 public Paint getErrorPaint() {
201 return this.errorPaint;
202 }
203
204 /**
205 * Sets the paint used to draw the error bars and sends a
206 * {@link RendererChangeEvent} to all registered listeners.
207 *
208 * @param paint the paint (<code>null</code> permitted).
209 *
210 * @see #getErrorPaint()
211 */
212 public void setErrorPaint(Paint paint) {
213 this.errorPaint = paint;
214 fireChangeEvent();
215 }
216
217 /**
218 * Returns the range required by this renderer to display all the domain
219 * values in the specified dataset.
220 *
221 * @param dataset the dataset (<code>null</code> permitted).
222 *
223 * @return The range, or <code>null</code> if the dataset is
224 * <code>null</code>.
225 */
226 public Range findDomainBounds(XYDataset dataset) {
227 if (dataset != null) {
228 return DatasetUtilities.findDomainBounds(dataset, true);
229 }
230 else {
231 return null;
232 }
233 }
234
235 /**
236 * Returns the range required by this renderer to display all the range
237 * values in the specified dataset.
238 *
239 * @param dataset the dataset (<code>null</code> permitted).
240 *
241 * @return The range, or <code>null</code> if the dataset is
242 * <code>null</code>.
243 */
244 public Range findRangeBounds(XYDataset dataset) {
245 if (dataset != null) {
246 return DatasetUtilities.findRangeBounds(dataset, true);
247 }
248 else {
249 return null;
250 }
251 }
252
253 /**
254 * Draws the visual representation for one data item.
255 *
256 * @param g2 the graphics output target.
257 * @param state the renderer state.
258 * @param dataArea the data area.
259 * @param info the plot rendering info.
260 * @param plot the plot.
261 * @param domainAxis the domain axis.
262 * @param rangeAxis the range axis.
263 * @param dataset the dataset.
264 * @param series the series index.
265 * @param item the item index.
266 * @param crosshairState the crosshair state.
267 * @param pass the pass index.
268 */
269 public void drawItem(Graphics2D g2, XYItemRendererState state,
270 Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
271 ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
272 int series, int item, CrosshairState crosshairState, int pass) {
273
274 if (pass == 0 && dataset instanceof IntervalXYDataset
275 && getItemVisible(series, item)) {
276 IntervalXYDataset ixyd = (IntervalXYDataset) dataset;
277 PlotOrientation orientation = plot.getOrientation();
278 if (this.drawXError) {
279 // draw the error bar for the x-interval
280 double x0 = ixyd.getStartXValue(series, item);
281 double x1 = ixyd.getEndXValue(series, item);
282 double y = ixyd.getYValue(series, item);
283 RectangleEdge edge = plot.getDomainAxisEdge();
284 double xx0 = domainAxis.valueToJava2D(x0, dataArea, edge);
285 double xx1 = domainAxis.valueToJava2D(x1, dataArea, edge);
286 double yy = rangeAxis.valueToJava2D(y, dataArea,
287 plot.getRangeAxisEdge());
288 Line2D line;
289 Line2D cap1 = null;
290 Line2D cap2 = null;
291 double adj = this.capLength / 2.0;
292 if (orientation == PlotOrientation.VERTICAL) {
293 line = new Line2D.Double(xx0, yy, xx1, yy);
294 cap1 = new Line2D.Double(xx0, yy - adj, xx0, yy + adj);
295 cap2 = new Line2D.Double(xx1, yy - adj, xx1, yy + adj);
296 }
297 else { // PlotOrientation.HORIZONTAL
298 line = new Line2D.Double(yy, xx0, yy, xx1);
299 cap1 = new Line2D.Double(yy - adj, xx0, yy + adj, xx0);
300 cap2 = new Line2D.Double(yy - adj, xx1, yy + adj, xx1);
301 }
302 g2.setStroke(new BasicStroke(1.0f));
303 if (this.errorPaint != null) {
304 g2.setPaint(this.errorPaint);
305 }
306 else {
307 g2.setPaint(getItemPaint(series, item));
308 }
309 g2.draw(line);
310 g2.draw(cap1);
311 g2.draw(cap2);
312 }
313 if (this.drawYError) {
314 // draw the error bar for the y-interval
315 double y0 = ixyd.getStartYValue(series, item);
316 double y1 = ixyd.getEndYValue(series, item);
317 double x = ixyd.getXValue(series, item);
318 RectangleEdge edge = plot.getRangeAxisEdge();
319 double yy0 = rangeAxis.valueToJava2D(y0, dataArea, edge);
320 double yy1 = rangeAxis.valueToJava2D(y1, dataArea, edge);
321 double xx = domainAxis.valueToJava2D(x, dataArea,
322 plot.getDomainAxisEdge());
323 Line2D line;
324 Line2D cap1 = null;
325 Line2D cap2 = null;
326 double adj = this.capLength / 2.0;
327 if (orientation == PlotOrientation.VERTICAL) {
328 line = new Line2D.Double(xx, yy0, xx, yy1);
329 cap1 = new Line2D.Double(xx - adj, yy0, xx + adj, yy0);
330 cap2 = new Line2D.Double(xx - adj, yy1, xx + adj, yy1);
331 }
332 else { // PlotOrientation.HORIZONTAL
333 line = new Line2D.Double(yy0, xx, yy1, xx);
334 cap1 = new Line2D.Double(yy0, xx - adj, yy0, xx + adj);
335 cap2 = new Line2D.Double(yy1, xx - adj, yy1, xx + adj);
336 }
337 g2.setStroke(new BasicStroke(1.0f));
338 if (this.errorPaint != null) {
339 g2.setPaint(this.errorPaint);
340 }
341 else {
342 g2.setPaint(getItemPaint(series, item));
343 }
344 g2.draw(line);
345 g2.draw(cap1);
346 g2.draw(cap2);
347 }
348 }
349 super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis,
350 dataset, series, item, crosshairState, pass);
351 }
352
353 /**
354 * Tests this instance for equality with an arbitrary object.
355 *
356 * @param obj the object (<code>null</code> permitted).
357 *
358 * @return A boolean.
359 */
360 public boolean equals(Object obj) {
361 if (obj == this) {
362 return true;
363 }
364 if (!(obj instanceof XYErrorRenderer)) {
365 return false;
366 }
367 XYErrorRenderer that = (XYErrorRenderer) obj;
368 if (this.drawXError != that.drawXError) {
369 return false;
370 }
371 if (this.drawYError != that.drawYError) {
372 return false;
373 }
374 if (this.capLength != that.capLength) {
375 return false;
376 }
377 if (!PaintUtilities.equal(this.errorPaint, that.errorPaint)) {
378 return false;
379 }
380 return super.equals(obj);
381 }
382
383 /**
384 * Provides serialization support.
385 *
386 * @param stream the input stream.
387 *
388 * @throws IOException if there is an I/O error.
389 * @throws ClassNotFoundException if there is a classpath problem.
390 */
391 private void readObject(ObjectInputStream stream)
392 throws IOException, ClassNotFoundException {
393 stream.defaultReadObject();
394 this.errorPaint = SerialUtilities.readPaint(stream);
395 }
396
397 /**
398 * Provides serialization support.
399 *
400 * @param stream the output stream.
401 *
402 * @throws IOException if there is an I/O error.
403 */
404 private void writeObject(ObjectOutputStream stream) throws IOException {
405 stream.defaultWriteObject();
406 SerialUtilities.writePaint(this.errorPaint, stream);
407 }
408
409 }