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 * StatisticalBarRenderer.java
029 * ---------------------------
030 * (C) Copyright 2002-2008, by Pascal Collet and Contributors.
031 *
032 * Original Author: Pascal Collet;
033 * Contributor(s): David Gilbert (for Object Refinery Limited);
034 * Christian W. Zuckschwerdt;
035 *
036 * Changes
037 * -------
038 * 21-Aug-2002 : Version 1, contributed by Pascal Collet (DG);
039 * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
040 * 24-Oct-2002 : Changes to dataset interface (DG);
041 * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
042 * 05-Feb-2003 : Updates for new DefaultStatisticalCategoryDataset (DG);
043 * 25-Mar-2003 : Implemented Serializable (DG);
044 * 30-Jul-2003 : Modified entity constructor (CZ);
045 * 06-Oct-2003 : Corrected typo in exception message (DG);
046 * 05-Nov-2004 : Modified drawItem() signature (DG);
047 * 15-Jun-2005 : Added errorIndicatorPaint attribute (DG);
048 * ------------- JFREECHART 1.0.x ---------------------------------------------
049 * 19-May-2006 : Added support for tooltips and URLs (DG);
050 * 12-Jul-2006 : Added support for item labels (DG);
051 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
052 * 28-Aug-2007 : Fixed NullPointerException - see bug 1779941 (DG);
053 * 14-Nov-2007 : Added errorIndicatorStroke, and fixed bugs with drawBarOutline
054 * and gradientPaintTransformer attributes being ignored (DG);
055 *
056 */
057
058 package org.jfree.chart.renderer.category;
059
060 import java.awt.BasicStroke;
061 import java.awt.Color;
062 import java.awt.GradientPaint;
063 import java.awt.Graphics2D;
064 import java.awt.Paint;
065 import java.awt.Stroke;
066 import java.awt.geom.Line2D;
067 import java.awt.geom.Rectangle2D;
068 import java.io.IOException;
069 import java.io.ObjectInputStream;
070 import java.io.ObjectOutputStream;
071 import java.io.Serializable;
072
073 import org.jfree.chart.axis.CategoryAxis;
074 import org.jfree.chart.axis.ValueAxis;
075 import org.jfree.chart.entity.EntityCollection;
076 import org.jfree.chart.event.RendererChangeEvent;
077 import org.jfree.chart.labels.CategoryItemLabelGenerator;
078 import org.jfree.chart.plot.CategoryPlot;
079 import org.jfree.chart.plot.PlotOrientation;
080 import org.jfree.data.category.CategoryDataset;
081 import org.jfree.data.statistics.StatisticalCategoryDataset;
082 import org.jfree.io.SerialUtilities;
083 import org.jfree.ui.GradientPaintTransformer;
084 import org.jfree.ui.RectangleEdge;
085 import org.jfree.util.ObjectUtilities;
086 import org.jfree.util.PaintUtilities;
087 import org.jfree.util.PublicCloneable;
088
089 /**
090 * A renderer that handles the drawing a bar plot where
091 * each bar has a mean value and a standard deviation line. The example shown
092 * here is generated by the <code>StatisticalBarChartDemo1.java</code> program
093 * included in the JFreeChart Demo Collection:
094 * <br><br>
095 * <img src="../../../../../images/StatisticalBarRendererSample.png"
096 * alt="StatisticalBarRendererSample.png" />
097 */
098 public class StatisticalBarRenderer extends BarRenderer
099 implements CategoryItemRenderer, Cloneable, PublicCloneable,
100 Serializable {
101
102 /** For serialization. */
103 private static final long serialVersionUID = -4986038395414039117L;
104
105 /** The paint used to show the error indicator. */
106 private transient Paint errorIndicatorPaint;
107
108 /**
109 * The stroke used to draw the error indicators.
110 *
111 * @since 1.0.8
112 */
113 private transient Stroke errorIndicatorStroke;
114
115 /**
116 * Default constructor.
117 */
118 public StatisticalBarRenderer() {
119 super();
120 this.errorIndicatorPaint = Color.gray;
121 this.errorIndicatorStroke = new BasicStroke(1.0f);
122 }
123
124 /**
125 * Returns the paint used for the error indicators.
126 *
127 * @return The paint used for the error indicators (possibly
128 * <code>null</code>).
129 *
130 * @see #setErrorIndicatorPaint(Paint)
131 */
132 public Paint getErrorIndicatorPaint() {
133 return this.errorIndicatorPaint;
134 }
135
136 /**
137 * Sets the paint used for the error indicators (if <code>null</code>,
138 * the item outline paint is used instead) and sends a
139 * {@link RendererChangeEvent} to all registered listeners.
140 *
141 * @param paint the paint (<code>null</code> permitted).
142 *
143 * @see #getErrorIndicatorPaint()
144 */
145 public void setErrorIndicatorPaint(Paint paint) {
146 this.errorIndicatorPaint = paint;
147 fireChangeEvent();
148 }
149
150 /**
151 * Returns the stroke used to draw the error indicators. If this is
152 * <code>null</code>, the renderer will use the item outline stroke).
153 *
154 * @return The stroke (possibly <code>null</code>).
155 *
156 * @see #setErrorIndicatorStroke(Stroke)
157 *
158 * @since 1.0.8
159 */
160 public Stroke getErrorIndicatorStroke() {
161 return this.errorIndicatorStroke;
162 }
163
164 /**
165 * Sets the stroke used to draw the error indicators, and sends a
166 * {@link RendererChangeEvent} to all registered listeners. If you set
167 * this to <code>null</code>, the renderer will use the item outline
168 * stroke.
169 *
170 * @param stroke the stroke (<code>null</code> permitted).
171 *
172 * @see #getErrorIndicatorStroke()
173 *
174 * @since 1.0.8
175 */
176 public void setErrorIndicatorStroke(Stroke stroke) {
177 this.errorIndicatorStroke = stroke;
178 fireChangeEvent();
179 }
180
181 /**
182 * Draws the bar with its standard deviation line range for a single
183 * (series, category) data item.
184 *
185 * @param g2 the graphics device.
186 * @param state the renderer state.
187 * @param dataArea the data area.
188 * @param plot the plot.
189 * @param domainAxis the domain axis.
190 * @param rangeAxis the range axis.
191 * @param data the data.
192 * @param row the row index (zero-based).
193 * @param column the column index (zero-based).
194 * @param pass the pass index.
195 */
196 public void drawItem(Graphics2D g2,
197 CategoryItemRendererState state,
198 Rectangle2D dataArea,
199 CategoryPlot plot,
200 CategoryAxis domainAxis,
201 ValueAxis rangeAxis,
202 CategoryDataset data,
203 int row,
204 int column,
205 int pass) {
206
207 // defensive check
208 if (!(data instanceof StatisticalCategoryDataset)) {
209 throw new IllegalArgumentException(
210 "Requires StatisticalCategoryDataset.");
211 }
212 StatisticalCategoryDataset statData = (StatisticalCategoryDataset) data;
213
214 PlotOrientation orientation = plot.getOrientation();
215 if (orientation == PlotOrientation.HORIZONTAL) {
216 drawHorizontalItem(g2, state, dataArea, plot, domainAxis,
217 rangeAxis, statData, row, column);
218 }
219 else if (orientation == PlotOrientation.VERTICAL) {
220 drawVerticalItem(g2, state, dataArea, plot, domainAxis, rangeAxis,
221 statData, row, column);
222 }
223 }
224
225 /**
226 * Draws an item for a plot with a horizontal orientation.
227 *
228 * @param g2 the graphics device.
229 * @param state the renderer state.
230 * @param dataArea the data area.
231 * @param plot the plot.
232 * @param domainAxis the domain axis.
233 * @param rangeAxis the range axis.
234 * @param dataset the data.
235 * @param row the row index (zero-based).
236 * @param column the column index (zero-based).
237 */
238 protected void drawHorizontalItem(Graphics2D g2,
239 CategoryItemRendererState state,
240 Rectangle2D dataArea,
241 CategoryPlot plot,
242 CategoryAxis domainAxis,
243 ValueAxis rangeAxis,
244 StatisticalCategoryDataset dataset,
245 int row,
246 int column) {
247
248 RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
249
250 // BAR Y
251 double rectY = domainAxis.getCategoryStart(column, getColumnCount(),
252 dataArea, xAxisLocation);
253
254 int seriesCount = getRowCount();
255 int categoryCount = getColumnCount();
256 if (seriesCount > 1) {
257 double seriesGap = dataArea.getHeight() * getItemMargin()
258 / (categoryCount * (seriesCount - 1));
259 rectY = rectY + row * (state.getBarWidth() + seriesGap);
260 }
261 else {
262 rectY = rectY + row * state.getBarWidth();
263 }
264
265 // BAR X
266 Number meanValue = dataset.getMeanValue(row, column);
267 if (meanValue == null) {
268 return;
269 }
270 double value = meanValue.doubleValue();
271 double base = 0.0;
272 double lclip = getLowerClip();
273 double uclip = getUpperClip();
274
275 if (uclip <= 0.0) { // cases 1, 2, 3 and 4
276 if (value >= uclip) {
277 return; // bar is not visible
278 }
279 base = uclip;
280 if (value <= lclip) {
281 value = lclip;
282 }
283 }
284 else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
285 if (value >= uclip) {
286 value = uclip;
287 }
288 else {
289 if (value <= lclip) {
290 value = lclip;
291 }
292 }
293 }
294 else { // cases 9, 10, 11 and 12
295 if (value <= lclip) {
296 return; // bar is not visible
297 }
298 base = getLowerClip();
299 if (value >= uclip) {
300 value = uclip;
301 }
302 }
303
304 RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
305 double transY1 = rangeAxis.valueToJava2D(base, dataArea, yAxisLocation);
306 double transY2 = rangeAxis.valueToJava2D(value, dataArea,
307 yAxisLocation);
308 double rectX = Math.min(transY2, transY1);
309
310 double rectHeight = state.getBarWidth();
311 double rectWidth = Math.abs(transY2 - transY1);
312
313 Rectangle2D bar = new Rectangle2D.Double(rectX, rectY, rectWidth,
314 rectHeight);
315 Paint itemPaint = getItemPaint(row, column);
316 GradientPaintTransformer t = getGradientPaintTransformer();
317 if (t != null && itemPaint instanceof GradientPaint) {
318 itemPaint = t.transform((GradientPaint) itemPaint, bar);
319 }
320 g2.setPaint(itemPaint);
321 g2.fill(bar);
322
323 // draw the outline...
324 if (isDrawBarOutline()
325 && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
326 Stroke stroke = getItemOutlineStroke(row, column);
327 Paint paint = getItemOutlinePaint(row, column);
328 if (stroke != null && paint != null) {
329 g2.setStroke(stroke);
330 g2.setPaint(paint);
331 g2.draw(bar);
332 }
333 }
334
335 // standard deviation lines
336 Number n = dataset.getStdDevValue(row, column);
337 if (n != null) {
338 double valueDelta = n.doubleValue();
339 double highVal = rangeAxis.valueToJava2D(meanValue.doubleValue()
340 + valueDelta, dataArea, yAxisLocation);
341 double lowVal = rangeAxis.valueToJava2D(meanValue.doubleValue()
342 - valueDelta, dataArea, yAxisLocation);
343
344 if (this.errorIndicatorPaint != null) {
345 g2.setPaint(this.errorIndicatorPaint);
346 }
347 else {
348 g2.setPaint(getItemOutlinePaint(row, column));
349 }
350 if (this.errorIndicatorStroke != null) {
351 g2.setStroke(this.errorIndicatorStroke);
352 }
353 else {
354 g2.setStroke(getItemOutlineStroke(row, column));
355 }
356 Line2D line = null;
357 line = new Line2D.Double(lowVal, rectY + rectHeight / 2.0d,
358 highVal, rectY + rectHeight / 2.0d);
359 g2.draw(line);
360 line = new Line2D.Double(highVal, rectY + rectHeight * 0.25,
361 highVal, rectY + rectHeight * 0.75);
362 g2.draw(line);
363 line = new Line2D.Double(lowVal, rectY + rectHeight * 0.25,
364 lowVal, rectY + rectHeight * 0.75);
365 g2.draw(line);
366 }
367
368 CategoryItemLabelGenerator generator = getItemLabelGenerator(row,
369 column);
370 if (generator != null && isItemLabelVisible(row, column)) {
371 drawItemLabel(g2, dataset, row, column, plot, generator, bar,
372 (value < 0.0));
373 }
374
375 // add an item entity, if this information is being collected
376 EntityCollection entities = state.getEntityCollection();
377 if (entities != null) {
378 addItemEntity(entities, dataset, row, column, bar);
379 }
380
381 }
382
383 /**
384 * Draws an item for a plot with a vertical orientation.
385 *
386 * @param g2 the graphics device.
387 * @param state the renderer state.
388 * @param dataArea the data area.
389 * @param plot the plot.
390 * @param domainAxis the domain axis.
391 * @param rangeAxis the range axis.
392 * @param dataset the data.
393 * @param row the row index (zero-based).
394 * @param column the column index (zero-based).
395 */
396 protected void drawVerticalItem(Graphics2D g2,
397 CategoryItemRendererState state,
398 Rectangle2D dataArea,
399 CategoryPlot plot,
400 CategoryAxis domainAxis,
401 ValueAxis rangeAxis,
402 StatisticalCategoryDataset dataset,
403 int row,
404 int column) {
405
406 RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
407
408 // BAR X
409 double rectX = domainAxis.getCategoryStart(column, getColumnCount(),
410 dataArea, xAxisLocation);
411
412 int seriesCount = getRowCount();
413 int categoryCount = getColumnCount();
414 if (seriesCount > 1) {
415 double seriesGap = dataArea.getWidth() * getItemMargin()
416 / (categoryCount * (seriesCount - 1));
417 rectX = rectX + row * (state.getBarWidth() + seriesGap);
418 }
419 else {
420 rectX = rectX + row * state.getBarWidth();
421 }
422
423 // BAR Y
424 Number meanValue = dataset.getMeanValue(row, column);
425 if (meanValue == null) {
426 return;
427 }
428
429 double value = meanValue.doubleValue();
430 double base = 0.0;
431 double lclip = getLowerClip();
432 double uclip = getUpperClip();
433
434 if (uclip <= 0.0) { // cases 1, 2, 3 and 4
435 if (value >= uclip) {
436 return; // bar is not visible
437 }
438 base = uclip;
439 if (value <= lclip) {
440 value = lclip;
441 }
442 }
443 else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
444 if (value >= uclip) {
445 value = uclip;
446 }
447 else {
448 if (value <= lclip) {
449 value = lclip;
450 }
451 }
452 }
453 else { // cases 9, 10, 11 and 12
454 if (value <= lclip) {
455 return; // bar is not visible
456 }
457 base = getLowerClip();
458 if (value >= uclip) {
459 value = uclip;
460 }
461 }
462
463 RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
464 double transY1 = rangeAxis.valueToJava2D(base, dataArea, yAxisLocation);
465 double transY2 = rangeAxis.valueToJava2D(value, dataArea,
466 yAxisLocation);
467 double rectY = Math.min(transY2, transY1);
468
469 double rectWidth = state.getBarWidth();
470 double rectHeight = Math.abs(transY2 - transY1);
471
472 Rectangle2D bar = new Rectangle2D.Double(rectX, rectY, rectWidth,
473 rectHeight);
474 Paint itemPaint = getItemPaint(row, column);
475 GradientPaintTransformer t = getGradientPaintTransformer();
476 if (t != null && itemPaint instanceof GradientPaint) {
477 itemPaint = t.transform((GradientPaint) itemPaint, bar);
478 }
479 g2.setPaint(itemPaint);
480 g2.fill(bar);
481 // draw the outline...
482 if (isDrawBarOutline()
483 && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
484 Stroke stroke = getItemOutlineStroke(row, column);
485 Paint paint = getItemOutlinePaint(row, column);
486 if (stroke != null && paint != null) {
487 g2.setStroke(stroke);
488 g2.setPaint(paint);
489 g2.draw(bar);
490 }
491 }
492
493 // standard deviation lines
494 Number n = dataset.getStdDevValue(row, column);
495 if (n != null) {
496 double valueDelta = n.doubleValue();
497 double highVal = rangeAxis.valueToJava2D(meanValue.doubleValue()
498 + valueDelta, dataArea, yAxisLocation);
499 double lowVal = rangeAxis.valueToJava2D(meanValue.doubleValue()
500 - valueDelta, dataArea, yAxisLocation);
501
502 if (this.errorIndicatorPaint != null) {
503 g2.setPaint(this.errorIndicatorPaint);
504 }
505 else {
506 g2.setPaint(getItemOutlinePaint(row, column));
507 }
508 if (this.errorIndicatorStroke != null) {
509 g2.setStroke(this.errorIndicatorStroke);
510 }
511 else {
512 g2.setStroke(getItemOutlineStroke(row, column));
513 }
514
515 Line2D line = null;
516 line = new Line2D.Double(rectX + rectWidth / 2.0d, lowVal,
517 rectX + rectWidth / 2.0d, highVal);
518 g2.draw(line);
519 line = new Line2D.Double(rectX + rectWidth / 2.0d - 5.0d, highVal,
520 rectX + rectWidth / 2.0d + 5.0d, highVal);
521 g2.draw(line);
522 line = new Line2D.Double(rectX + rectWidth / 2.0d - 5.0d, lowVal,
523 rectX + rectWidth / 2.0d + 5.0d, lowVal);
524 g2.draw(line);
525 }
526
527 CategoryItemLabelGenerator generator = getItemLabelGenerator(row,
528 column);
529 if (generator != null && isItemLabelVisible(row, column)) {
530 drawItemLabel(g2, dataset, row, column, plot, generator, bar,
531 (value < 0.0));
532 }
533
534 // add an item entity, if this information is being collected
535 EntityCollection entities = state.getEntityCollection();
536 if (entities != null) {
537 addItemEntity(entities, dataset, row, column, bar);
538 }
539 }
540
541 /**
542 * Tests this renderer for equality with an arbitrary object.
543 *
544 * @param obj the object (<code>null</code> permitted).
545 *
546 * @return A boolean.
547 */
548 public boolean equals(Object obj) {
549 if (obj == this) {
550 return true;
551 }
552 if (!(obj instanceof StatisticalBarRenderer)) {
553 return false;
554 }
555 StatisticalBarRenderer that = (StatisticalBarRenderer) obj;
556 if (!PaintUtilities.equal(this.errorIndicatorPaint,
557 that.errorIndicatorPaint)) {
558 return false;
559 }
560 if (!ObjectUtilities.equal(this.errorIndicatorStroke,
561 that.errorIndicatorStroke)) {
562 return false;
563 }
564 return super.equals(obj);
565 }
566
567 /**
568 * Provides serialization support.
569 *
570 * @param stream the output stream.
571 *
572 * @throws IOException if there is an I/O error.
573 */
574 private void writeObject(ObjectOutputStream stream) throws IOException {
575 stream.defaultWriteObject();
576 SerialUtilities.writePaint(this.errorIndicatorPaint, stream);
577 SerialUtilities.writeStroke(this.errorIndicatorStroke, stream);
578 }
579
580 /**
581 * Provides serialization support.
582 *
583 * @param stream the input stream.
584 *
585 * @throws IOException if there is an I/O error.
586 * @throws ClassNotFoundException if there is a classpath problem.
587 */
588 private void readObject(ObjectInputStream stream)
589 throws IOException, ClassNotFoundException {
590 stream.defaultReadObject();
591 this.errorIndicatorPaint = SerialUtilities.readPaint(stream);
592 this.errorIndicatorStroke = SerialUtilities.readStroke(stream);
593 }
594
595 }