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 * XYBarRenderer.java
029 * ------------------
030 * (C) Copyright 2001-2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Richard Atkinson;
034 * Christian W. Zuckschwerdt;
035 * Bill Kelemen;
036 * Marc van Glabbeek (bug 1775452);
037 * Richard West, Advanced Micro Devices, Inc.;
038 *
039 * Changes
040 * -------
041 * 13-Dec-2001 : Version 1, makes VerticalXYBarPlot class redundant (DG);
042 * 23-Jan-2002 : Added DrawInfo parameter to drawItem() method (DG);
043 * 09-Apr-2002 : Removed the translated zero from the drawItem method. Override
044 * the initialise() method to calculate it (DG);
045 * 24-May-2002 : Incorporated tooltips into chart entities (DG);
046 * 25-Jun-2002 : Removed redundant import (DG);
047 * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML
048 * image maps (RA);
049 * 25-Mar-2003 : Implemented Serializable (DG);
050 * 01-May-2003 : Modified drawItem() method signature (DG);
051 * 30-Jul-2003 : Modified entity constructor (CZ);
052 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
053 * 24-Aug-2003 : Added null checks in drawItem (BK);
054 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
055 * 07-Oct-2003 : Added renderer state (DG);
056 * 05-Dec-2003 : Changed call to obtain outline paint (DG);
057 * 10-Feb-2004 : Added state class, updated drawItem() method to make
058 * cut-and-paste overriding easier, and replaced property change
059 * with RendererChangeEvent (DG);
060 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
061 * 26-Apr-2004 : Added gradient paint transformer (DG);
062 * 19-May-2004 : Fixed bug (879709) with bar zero value for secondary axis (DG);
063 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
064 * getYValue() (DG);
065 * 01-Sep-2004 : Added a flag to control whether or not the bar outlines are
066 * drawn (DG);
067 * 03-Sep-2004 : Added option to use y-interval from dataset to determine the
068 * length of the bars (DG);
069 * 08-Sep-2004 : Added equals() method and updated clone() method (DG);
070 * 26-Jan-2005 : Added override for getLegendItem() method (DG);
071 * 20-Apr-2005 : Use generators for label tooltips and URLs (DG);
072 * 19-May-2005 : Added minimal item label implementation - needs improving (DG);
073 * 14-Oct-2005 : Fixed rendering problem with inverted axes (DG);
074 * ------------- JFREECHART 1.0.x ---------------------------------------------
075 * 21-Jun-2006 : Improved item label handling - see bug 1501768 (DG);
076 * 24-Aug-2006 : Added crosshair support (DG);
077 * 13-Dec-2006 : Updated getLegendItems() to return gradient paint
078 * transformer (DG);
079 * 02-Feb-2007 : Changed setUseYInterval() to only notify when the flag
080 * changes (DG);
081 * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG);
082 * 09-Feb-2007 : Updated getLegendItem() to observe drawBarOutline flag (DG);
083 * 05-Mar-2007 : Applied patch 1671126 by Sergei Ivanov, to fix rendering with
084 * LogarithmicAxis (DG);
085 * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
086 * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
087 * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
088 * 15-Jun-2007 : Changed default for drawBarOutline to false (DG);
089 * 26-Sep-2007 : Fixed bug 1775452, problem with bar margins for inverted
090 * axes, thanks to Marc van Glabbeek (DG);
091 * 12-Nov-2007 : Fixed NPE in drawItemLabel() method, thanks to Richard West
092 * (see patch 1827829) (DG);
093 * 17-Jun-2008 : Apply legend font and paint attributes (DG);
094 * 19-Jun-2008 : Added findRangeBounds() method override to fix bug in default
095 * axis range (DG);
096 * 24-Jun-2008 : Added new barPainter mechanism (DG);
097 *
098 */
099
100 package org.jfree.chart.renderer.xy;
101
102 import java.awt.Font;
103 import java.awt.Graphics2D;
104 import java.awt.Paint;
105 import java.awt.Shape;
106 import java.awt.Stroke;
107 import java.awt.geom.Point2D;
108 import java.awt.geom.Rectangle2D;
109 import java.io.IOException;
110 import java.io.ObjectInputStream;
111 import java.io.ObjectOutputStream;
112 import java.io.Serializable;
113
114 import org.jfree.chart.LegendItem;
115 import org.jfree.chart.axis.ValueAxis;
116 import org.jfree.chart.entity.EntityCollection;
117 import org.jfree.chart.event.RendererChangeEvent;
118 import org.jfree.chart.labels.ItemLabelAnchor;
119 import org.jfree.chart.labels.ItemLabelPosition;
120 import org.jfree.chart.labels.XYItemLabelGenerator;
121 import org.jfree.chart.labels.XYSeriesLabelGenerator;
122 import org.jfree.chart.plot.CrosshairState;
123 import org.jfree.chart.plot.PlotOrientation;
124 import org.jfree.chart.plot.PlotRenderingInfo;
125 import org.jfree.chart.plot.XYPlot;
126 import org.jfree.data.Range;
127 import org.jfree.data.general.DatasetUtilities;
128 import org.jfree.data.xy.IntervalXYDataset;
129 import org.jfree.data.xy.XYDataset;
130 import org.jfree.io.SerialUtilities;
131 import org.jfree.text.TextUtilities;
132 import org.jfree.ui.GradientPaintTransformer;
133 import org.jfree.ui.RectangleEdge;
134 import org.jfree.ui.StandardGradientPaintTransformer;
135 import org.jfree.util.ObjectUtilities;
136 import org.jfree.util.PublicCloneable;
137 import org.jfree.util.ShapeUtilities;
138
139 /**
140 * A renderer that draws bars on an {@link XYPlot} (requires an
141 * {@link IntervalXYDataset}). The example shown here is generated by the
142 * <code>XYBarChartDemo1.java</code> program included in the JFreeChart
143 * demo collection:
144 * <br><br>
145 * <img src="../../../../../images/XYBarRendererSample.png"
146 * alt="XYBarRendererSample.png" />
147 */
148 public class XYBarRenderer extends AbstractXYItemRenderer
149 implements XYItemRenderer, Cloneable, PublicCloneable, Serializable {
150
151 /** For serialization. */
152 private static final long serialVersionUID = 770559577251370036L;
153
154 /**
155 * The default bar painter assigned to each new instance of this renderer.
156 *
157 * @since 1.0.11
158 */
159 private static XYBarPainter defaultBarPainter = new GradientXYBarPainter();
160
161 /**
162 * Returns the default bar painter.
163 *
164 * @return The default bar painter.
165 *
166 * @since 1.0.11
167 */
168 public static XYBarPainter getDefaultBarPainter() {
169 return XYBarRenderer.defaultBarPainter;
170 }
171
172 /**
173 * Sets the default bar painter.
174 *
175 * @param painter the painter (<code>null</code> not permitted).
176 *
177 * @since 1.0.11
178 */
179 public static void setDefaultBarPainter(XYBarPainter painter) {
180 if (painter == null) {
181 throw new IllegalArgumentException("Null 'painter' argument.");
182 }
183 XYBarRenderer.defaultBarPainter = painter;
184 }
185
186 /**
187 * The state class used by this renderer.
188 */
189 protected class XYBarRendererState extends XYItemRendererState {
190
191 /** Base for bars against the range axis, in Java 2D space. */
192 private double g2Base;
193
194 /**
195 * Creates a new state object.
196 *
197 * @param info the plot rendering info.
198 */
199 public XYBarRendererState(PlotRenderingInfo info) {
200 super(info);
201 }
202
203 /**
204 * Returns the base (range) value in Java 2D space.
205 *
206 * @return The base value.
207 */
208 public double getG2Base() {
209 return this.g2Base;
210 }
211
212 /**
213 * Sets the range axis base in Java2D space.
214 *
215 * @param value the value.
216 */
217 public void setG2Base(double value) {
218 this.g2Base = value;
219 }
220 }
221
222 /** The default base value for the bars. */
223 private double base;
224
225 /**
226 * A flag that controls whether the bars use the y-interval supplied by the
227 * dataset.
228 */
229 private boolean useYInterval;
230
231 /** Percentage margin (to reduce the width of bars). */
232 private double margin;
233
234 /** A flag that controls whether or not bar outlines are drawn. */
235 private boolean drawBarOutline;
236
237 /**
238 * An optional class used to transform gradient paint objects to fit each
239 * bar.
240 */
241 private GradientPaintTransformer gradientPaintTransformer;
242
243 /**
244 * The shape used to represent a bar in each legend item (this should never
245 * be <code>null</code>).
246 */
247 private transient Shape legendBar;
248
249 /**
250 * The fallback position if a positive item label doesn't fit inside the
251 * bar.
252 */
253 private ItemLabelPosition positiveItemLabelPositionFallback;
254
255 /**
256 * The fallback position if a negative item label doesn't fit inside the
257 * bar.
258 */
259 private ItemLabelPosition negativeItemLabelPositionFallback;
260
261 /**
262 * The bar painter (never <code>null</code>).
263 *
264 * @since 1.0.11
265 */
266 private XYBarPainter barPainter;
267
268 /**
269 * The flag that controls whether or not shadows are drawn for the bars.
270 *
271 * @since 1.0.11
272 */
273 private boolean shadowsVisible;
274
275 /**
276 * The x-offset for the shadow effect.
277 *
278 * @since 1.0.11
279 */
280 private double shadowXOffset;
281
282 /**
283 * The y-offset for the shadow effect.
284 *
285 * @since 1.0.11
286 */
287 private double shadowYOffset;
288
289 /**
290 * The default constructor.
291 */
292 public XYBarRenderer() {
293 this(0.0);
294 }
295
296 /**
297 * Constructs a new renderer.
298 *
299 * @param margin the percentage amount to trim from the width of each bar.
300 */
301 public XYBarRenderer(double margin) {
302 super();
303 this.margin = margin;
304 this.base = 0.0;
305 this.useYInterval = false;
306 this.gradientPaintTransformer = new StandardGradientPaintTransformer();
307 this.drawBarOutline = false;
308 this.legendBar = new Rectangle2D.Double(-3.0, -5.0, 6.0, 10.0);
309 this.barPainter = getDefaultBarPainter();
310 this.shadowsVisible = true;
311 this.shadowXOffset = 4.0;
312 this.shadowYOffset = 4.0;
313 }
314
315 /**
316 * Returns the base value for the bars.
317 *
318 * @return The base value for the bars.
319 *
320 * @see #setBase(double)
321 */
322 public double getBase() {
323 return this.base;
324 }
325
326 /**
327 * Sets the base value for the bars and sends a {@link RendererChangeEvent}
328 * to all registered listeners. The base value is not used if the dataset's
329 * y-interval is being used to determine the bar length.
330 *
331 * @param base the new base value.
332 *
333 * @see #getBase()
334 * @see #getUseYInterval()
335 */
336 public void setBase(double base) {
337 this.base = base;
338 fireChangeEvent();
339 }
340
341 /**
342 * Returns a flag that determines whether the y-interval from the dataset is
343 * used to calculate the length of each bar.
344 *
345 * @return A boolean.
346 *
347 * @see #setUseYInterval(boolean)
348 */
349 public boolean getUseYInterval() {
350 return this.useYInterval;
351 }
352
353 /**
354 * Sets the flag that determines whether the y-interval from the dataset is
355 * used to calculate the length of each bar, and sends a
356 * {@link RendererChangeEvent} to all registered listeners.
357 *
358 * @param use the flag.
359 *
360 * @see #getUseYInterval()
361 */
362 public void setUseYInterval(boolean use) {
363 if (this.useYInterval != use) {
364 this.useYInterval = use;
365 fireChangeEvent();
366 }
367 }
368
369 /**
370 * Returns the margin which is a percentage amount by which the bars are
371 * trimmed.
372 *
373 * @return The margin.
374 *
375 * @see #setMargin(double)
376 */
377 public double getMargin() {
378 return this.margin;
379 }
380
381 /**
382 * Sets the percentage amount by which the bars are trimmed and sends a
383 * {@link RendererChangeEvent} to all registered listeners.
384 *
385 * @param margin the new margin.
386 *
387 * @see #getMargin()
388 */
389 public void setMargin(double margin) {
390 this.margin = margin;
391 fireChangeEvent();
392 }
393
394 /**
395 * Returns a flag that controls whether or not bar outlines are drawn.
396 *
397 * @return A boolean.
398 *
399 * @see #setDrawBarOutline(boolean)
400 */
401 public boolean isDrawBarOutline() {
402 return this.drawBarOutline;
403 }
404
405 /**
406 * Sets the flag that controls whether or not bar outlines are drawn and
407 * sends a {@link RendererChangeEvent} to all registered listeners.
408 *
409 * @param draw the flag.
410 *
411 * @see #isDrawBarOutline()
412 */
413 public void setDrawBarOutline(boolean draw) {
414 this.drawBarOutline = draw;
415 fireChangeEvent();
416 }
417
418 /**
419 * Returns the gradient paint transformer (an object used to transform
420 * gradient paint objects to fit each bar).
421 *
422 * @return A transformer (<code>null</code> possible).
423 *
424 * @see #setGradientPaintTransformer(GradientPaintTransformer)
425 */
426 public GradientPaintTransformer getGradientPaintTransformer() {
427 return this.gradientPaintTransformer;
428 }
429
430 /**
431 * Sets the gradient paint transformer and sends a
432 * {@link RendererChangeEvent} to all registered listeners.
433 *
434 * @param transformer the transformer (<code>null</code> permitted).
435 *
436 * @see #getGradientPaintTransformer()
437 */
438 public void setGradientPaintTransformer(
439 GradientPaintTransformer transformer) {
440 this.gradientPaintTransformer = transformer;
441 fireChangeEvent();
442 }
443
444 /**
445 * Returns the shape used to represent bars in each legend item.
446 *
447 * @return The shape used to represent bars in each legend item (never
448 * <code>null</code>).
449 *
450 * @see #setLegendBar(Shape)
451 */
452 public Shape getLegendBar() {
453 return this.legendBar;
454 }
455
456 /**
457 * Sets the shape used to represent bars in each legend item and sends a
458 * {@link RendererChangeEvent} to all registered listeners.
459 *
460 * @param bar the bar shape (<code>null</code> not permitted).
461 *
462 * @see #getLegendBar()
463 */
464 public void setLegendBar(Shape bar) {
465 if (bar == null) {
466 throw new IllegalArgumentException("Null 'bar' argument.");
467 }
468 this.legendBar = bar;
469 fireChangeEvent();
470 }
471
472 /**
473 * Returns the fallback position for positive item labels that don't fit
474 * within a bar.
475 *
476 * @return The fallback position (<code>null</code> possible).
477 *
478 * @see #setPositiveItemLabelPositionFallback(ItemLabelPosition)
479 * @since 1.0.2
480 */
481 public ItemLabelPosition getPositiveItemLabelPositionFallback() {
482 return this.positiveItemLabelPositionFallback;
483 }
484
485 /**
486 * Sets the fallback position for positive item labels that don't fit
487 * within a bar, and sends a {@link RendererChangeEvent} to all registered
488 * listeners.
489 *
490 * @param position the position (<code>null</code> permitted).
491 *
492 * @see #getPositiveItemLabelPositionFallback()
493 * @since 1.0.2
494 */
495 public void setPositiveItemLabelPositionFallback(
496 ItemLabelPosition position) {
497 this.positiveItemLabelPositionFallback = position;
498 fireChangeEvent();
499 }
500
501 /**
502 * Returns the fallback position for negative item labels that don't fit
503 * within a bar.
504 *
505 * @return The fallback position (<code>null</code> possible).
506 *
507 * @see #setNegativeItemLabelPositionFallback(ItemLabelPosition)
508 * @since 1.0.2
509 */
510 public ItemLabelPosition getNegativeItemLabelPositionFallback() {
511 return this.negativeItemLabelPositionFallback;
512 }
513
514 /**
515 * Sets the fallback position for negative item labels that don't fit
516 * within a bar, and sends a {@link RendererChangeEvent} to all registered
517 * listeners.
518 *
519 * @param position the position (<code>null</code> permitted).
520 *
521 * @see #getNegativeItemLabelPositionFallback()
522 * @since 1.0.2
523 */
524 public void setNegativeItemLabelPositionFallback(
525 ItemLabelPosition position) {
526 this.negativeItemLabelPositionFallback = position;
527 fireChangeEvent();
528 }
529
530 /**
531 * Returns the bar painter.
532 *
533 * @return The bar painter (never <code>null</code>).
534 *
535 * @since 1.0.11
536 */
537 public XYBarPainter getBarPainter() {
538 return this.barPainter;
539 }
540
541 /**
542 * Sets the bar painter and sends a {@link RendererChangeEvent} to all
543 * registered listeners.
544 *
545 * @param painter the painter (<code>null</code> not permitted).
546 *
547 * @since 1.0.11
548 */
549 public void setBarPainter(XYBarPainter painter) {
550 if (painter == null) {
551 throw new IllegalArgumentException("Null 'painter' argument.");
552 }
553 this.barPainter = painter;
554 fireChangeEvent();
555 }
556
557 /**
558 * Returns the flag that controls whether or not shadows are drawn for
559 * the bars.
560 *
561 * @return A boolean.
562 *
563 * @since 1.0.11
564 */
565 public boolean getShadowsVisible() {
566 return this.shadowsVisible;
567 }
568
569 /**
570 * Sets the flag that controls whether or not the renderer
571 * draws shadows for the bars, and sends a
572 * {@link RendererChangeEvent} to all registered listeners.
573 *
574 * @param visible the new flag value.
575 *
576 * @since 1.0.11
577 */
578 public void setShadowVisible(boolean visible) {
579 this.shadowsVisible = visible;
580 fireChangeEvent();
581 }
582
583 /**
584 * Returns the shadow x-offset.
585 *
586 * @return The shadow x-offset.
587 *
588 * @since 1.0.11
589 */
590 public double getShadowXOffset() {
591 return this.shadowXOffset;
592 }
593
594 /**
595 * Sets the x-offset for the bar shadow and sends a
596 * {@link RendererChangeEvent} to all registered listeners.
597 *
598 * @param offset the offset.
599 *
600 * @since 1.0.11
601 */
602 public void setShadowXOffset(double offset) {
603 this.shadowXOffset = offset;
604 fireChangeEvent();
605 }
606
607 /**
608 * Returns the shadow y-offset.
609 *
610 * @return The shadow y-offset.
611 *
612 * @since 1.0.11
613 */
614 public double getShadowYOffset() {
615 return this.shadowYOffset;
616 }
617
618 /**
619 * Sets the y-offset for the bar shadow and sends a
620 * {@link RendererChangeEvent} to all registered listeners.
621 *
622 * @param offset the offset.
623 *
624 * @since 1.0.11
625 */
626 public void setShadowYOffset(double offset) {
627 this.shadowYOffset = offset;
628 fireChangeEvent();
629 }
630
631 /**
632 * Initialises the renderer and returns a state object that should be
633 * passed to all subsequent calls to the drawItem() method. Here we
634 * calculate the Java2D y-coordinate for zero, since all the bars have
635 * their bases fixed at zero.
636 *
637 * @param g2 the graphics device.
638 * @param dataArea the area inside the axes.
639 * @param plot the plot.
640 * @param dataset the data.
641 * @param info an optional info collection object to return data back to
642 * the caller.
643 *
644 * @return A state object.
645 */
646 public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea,
647 XYPlot plot, XYDataset dataset, PlotRenderingInfo info) {
648
649 XYBarRendererState state = new XYBarRendererState(info);
650 ValueAxis rangeAxis = plot.getRangeAxisForDataset(plot.indexOf(
651 dataset));
652 state.setG2Base(rangeAxis.valueToJava2D(this.base, dataArea,
653 plot.getRangeAxisEdge()));
654 return state;
655
656 }
657
658 /**
659 * Returns a default legend item for the specified series. Subclasses
660 * should override this method to generate customised items.
661 *
662 * @param datasetIndex the dataset index (zero-based).
663 * @param series the series index (zero-based).
664 *
665 * @return A legend item for the series.
666 */
667 public LegendItem getLegendItem(int datasetIndex, int series) {
668 LegendItem result = null;
669 XYPlot xyplot = getPlot();
670 if (xyplot != null) {
671 XYDataset dataset = xyplot.getDataset(datasetIndex);
672 if (dataset != null) {
673 XYSeriesLabelGenerator lg = getLegendItemLabelGenerator();
674 String label = lg.generateLabel(dataset, series);
675 String description = label;
676 String toolTipText = null;
677 if (getLegendItemToolTipGenerator() != null) {
678 toolTipText = getLegendItemToolTipGenerator().generateLabel(
679 dataset, series);
680 }
681 String urlText = null;
682 if (getLegendItemURLGenerator() != null) {
683 urlText = getLegendItemURLGenerator().generateLabel(
684 dataset, series);
685 }
686 Shape shape = this.legendBar;
687 Paint paint = lookupSeriesPaint(series);
688 Paint outlinePaint = lookupSeriesOutlinePaint(series);
689 Stroke outlineStroke = lookupSeriesOutlineStroke(series);
690 if (this.drawBarOutline) {
691 result = new LegendItem(label, description, toolTipText,
692 urlText, shape, paint, outlineStroke, outlinePaint);
693 }
694 else {
695 result = new LegendItem(label, description, toolTipText,
696 urlText, shape, paint);
697 }
698 result.setLabelFont(lookupLegendTextFont(series));
699 Paint labelPaint = lookupLegendTextPaint(series);
700 if (labelPaint != null) {
701 result.setLabelPaint(labelPaint);
702 }
703 result.setDataset(dataset);
704 result.setDatasetIndex(datasetIndex);
705 result.setSeriesKey(dataset.getSeriesKey(series));
706 result.setSeriesIndex(series);
707 if (getGradientPaintTransformer() != null) {
708 result.setFillPaintTransformer(
709 getGradientPaintTransformer());
710 }
711 }
712 }
713 return result;
714 }
715
716 /**
717 * Draws the visual representation of a single data item.
718 *
719 * @param g2 the graphics device.
720 * @param state the renderer state.
721 * @param dataArea the area within which the plot is being drawn.
722 * @param info collects information about the drawing.
723 * @param plot the plot (can be used to obtain standard color
724 * information etc).
725 * @param domainAxis the domain axis.
726 * @param rangeAxis the range axis.
727 * @param dataset the dataset.
728 * @param series the series index (zero-based).
729 * @param item the item index (zero-based).
730 * @param crosshairState crosshair information for the plot
731 * (<code>null</code> permitted).
732 * @param pass the pass index.
733 */
734 public void drawItem(Graphics2D g2,
735 XYItemRendererState state,
736 Rectangle2D dataArea,
737 PlotRenderingInfo info,
738 XYPlot plot,
739 ValueAxis domainAxis,
740 ValueAxis rangeAxis,
741 XYDataset dataset,
742 int series,
743 int item,
744 CrosshairState crosshairState,
745 int pass) {
746
747 if (!getItemVisible(series, item)) {
748 return;
749 }
750 IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
751
752 double value0;
753 double value1;
754 if (this.useYInterval) {
755 value0 = intervalDataset.getStartYValue(series, item);
756 value1 = intervalDataset.getEndYValue(series, item);
757 }
758 else {
759 value0 = this.base;
760 value1 = intervalDataset.getYValue(series, item);
761 }
762 if (Double.isNaN(value0) || Double.isNaN(value1)) {
763 return;
764 }
765 if (value0 <= value1) {
766 if (!rangeAxis.getRange().intersects(value0, value1)) {
767 return;
768 }
769 }
770 else {
771 if (!rangeAxis.getRange().intersects(value1, value0)) {
772 return;
773 }
774 }
775
776 double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea,
777 plot.getRangeAxisEdge());
778 double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea,
779 plot.getRangeAxisEdge());
780 double bottom = Math.min(translatedValue0, translatedValue1);
781 double top = Math.max(translatedValue0, translatedValue1);
782
783 double startX = intervalDataset.getStartXValue(series, item);
784 if (Double.isNaN(startX)) {
785 return;
786 }
787 double endX = intervalDataset.getEndXValue(series, item);
788 if (Double.isNaN(endX)) {
789 return;
790 }
791 if (startX <= endX) {
792 if (!domainAxis.getRange().intersects(startX, endX)) {
793 return;
794 }
795 }
796 else {
797 if (!domainAxis.getRange().intersects(endX, startX)) {
798 return;
799 }
800 }
801
802 RectangleEdge location = plot.getDomainAxisEdge();
803 double translatedStartX = domainAxis.valueToJava2D(startX, dataArea,
804 location);
805 double translatedEndX = domainAxis.valueToJava2D(endX, dataArea,
806 location);
807
808 double translatedWidth = Math.max(1, Math.abs(translatedEndX
809 - translatedStartX));
810
811 double left = Math.min(translatedStartX, translatedEndX);
812 if (getMargin() > 0.0) {
813 double cut = translatedWidth * getMargin();
814 translatedWidth = translatedWidth - cut;
815 left = left + cut / 2;
816 }
817
818 Rectangle2D bar = null;
819 PlotOrientation orientation = plot.getOrientation();
820 if (orientation == PlotOrientation.HORIZONTAL) {
821 // clip left and right bounds to data area
822 bottom = Math.max(bottom, dataArea.getMinX());
823 top = Math.min(top, dataArea.getMaxX());
824 bar = new Rectangle2D.Double(
825 bottom, left, top - bottom, translatedWidth);
826 }
827 else if (orientation == PlotOrientation.VERTICAL) {
828 // clip top and bottom bounds to data area
829 bottom = Math.max(bottom, dataArea.getMinY());
830 top = Math.min(top, dataArea.getMaxY());
831 bar = new Rectangle2D.Double(left, bottom, translatedWidth,
832 top - bottom);
833 }
834
835 boolean positive = (value1 > 0.0);
836 boolean inverted = rangeAxis.isInverted();
837 RectangleEdge barBase;
838 if (orientation == PlotOrientation.HORIZONTAL) {
839 if (positive && inverted || !positive && !inverted) {
840 barBase = RectangleEdge.RIGHT;
841 }
842 else {
843 barBase = RectangleEdge.LEFT;
844 }
845 }
846 else {
847 if (positive && !inverted || !positive && inverted) {
848 barBase = RectangleEdge.BOTTOM;
849 }
850 else {
851 barBase = RectangleEdge.TOP;
852 }
853 }
854 if (getShadowsVisible()) {
855 this.barPainter.paintBarShadow(g2, this, series, item, bar, barBase,
856 !this.useYInterval);
857 }
858 this.barPainter.paintBar(g2, this, series, item, bar, barBase);
859
860 if (isItemLabelVisible(series, item)) {
861 XYItemLabelGenerator generator = getItemLabelGenerator(series,
862 item);
863 drawItemLabel(g2, dataset, series, item, plot, generator, bar,
864 value1 < 0.0);
865 }
866
867 // update the crosshair point
868 double x1 = (startX + endX) / 2.0;
869 double y1 = dataset.getYValue(series, item);
870 double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
871 double transY1 = rangeAxis.valueToJava2D(y1, dataArea,
872 plot.getRangeAxisEdge());
873 int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
874 int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
875 updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex,
876 rangeAxisIndex, transX1, transY1, plot.getOrientation());
877
878 EntityCollection entities = state.getEntityCollection();
879 if (entities != null) {
880 addEntity(entities, bar, dataset, series, item, 0.0, 0.0);
881 }
882
883 }
884
885 /**
886 * Draws an item label. This method is provided as an alternative to
887 * {@link #drawItemLabel(Graphics2D, PlotOrientation, XYDataset, int, int,
888 * double, double, boolean)} so that the bar can be used to calculate the
889 * label anchor point.
890 *
891 * @param g2 the graphics device.
892 * @param dataset the dataset.
893 * @param series the series index.
894 * @param item the item index.
895 * @param plot the plot.
896 * @param generator the label generator (<code>null</code> permitted, in
897 * which case the method does nothing, just returns).
898 * @param bar the bar.
899 * @param negative a flag indicating a negative value.
900 */
901 protected void drawItemLabel(Graphics2D g2, XYDataset dataset,
902 int series, int item, XYPlot plot, XYItemLabelGenerator generator,
903 Rectangle2D bar, boolean negative) {
904
905 if (generator == null) {
906 return; // nothing to do
907 }
908 String label = generator.generateLabel(dataset, series, item);
909 if (label == null) {
910 return; // nothing to do
911 }
912
913 Font labelFont = getItemLabelFont(series, item);
914 g2.setFont(labelFont);
915 Paint paint = getItemLabelPaint(series, item);
916 g2.setPaint(paint);
917
918 // find out where to place the label...
919 ItemLabelPosition position = null;
920 if (!negative) {
921 position = getPositiveItemLabelPosition(series, item);
922 }
923 else {
924 position = getNegativeItemLabelPosition(series, item);
925 }
926
927 // work out the label anchor point...
928 Point2D anchorPoint = calculateLabelAnchorPoint(
929 position.getItemLabelAnchor(), bar, plot.getOrientation());
930
931 if (isInternalAnchor(position.getItemLabelAnchor())) {
932 Shape bounds = TextUtilities.calculateRotatedStringBounds(label,
933 g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
934 position.getTextAnchor(), position.getAngle(),
935 position.getRotationAnchor());
936
937 if (bounds != null) {
938 if (!bar.contains(bounds.getBounds2D())) {
939 if (!negative) {
940 position = getPositiveItemLabelPositionFallback();
941 }
942 else {
943 position = getNegativeItemLabelPositionFallback();
944 }
945 if (position != null) {
946 anchorPoint = calculateLabelAnchorPoint(
947 position.getItemLabelAnchor(), bar,
948 plot.getOrientation());
949 }
950 }
951 }
952
953 }
954
955 if (position != null) {
956 TextUtilities.drawRotatedString(label, g2,
957 (float) anchorPoint.getX(), (float) anchorPoint.getY(),
958 position.getTextAnchor(), position.getAngle(),
959 position.getRotationAnchor());
960 }
961 }
962
963 /**
964 * Calculates the item label anchor point.
965 *
966 * @param anchor the anchor.
967 * @param bar the bar.
968 * @param orientation the plot orientation.
969 *
970 * @return The anchor point.
971 */
972 private Point2D calculateLabelAnchorPoint(ItemLabelAnchor anchor,
973 Rectangle2D bar, PlotOrientation orientation) {
974
975 Point2D result = null;
976 double offset = getItemLabelAnchorOffset();
977 double x0 = bar.getX() - offset;
978 double x1 = bar.getX();
979 double x2 = bar.getX() + offset;
980 double x3 = bar.getCenterX();
981 double x4 = bar.getMaxX() - offset;
982 double x5 = bar.getMaxX();
983 double x6 = bar.getMaxX() + offset;
984
985 double y0 = bar.getMaxY() + offset;
986 double y1 = bar.getMaxY();
987 double y2 = bar.getMaxY() - offset;
988 double y3 = bar.getCenterY();
989 double y4 = bar.getMinY() + offset;
990 double y5 = bar.getMinY();
991 double y6 = bar.getMinY() - offset;
992
993 if (anchor == ItemLabelAnchor.CENTER) {
994 result = new Point2D.Double(x3, y3);
995 }
996 else if (anchor == ItemLabelAnchor.INSIDE1) {
997 result = new Point2D.Double(x4, y4);
998 }
999 else if (anchor == ItemLabelAnchor.INSIDE2) {
1000 result = new Point2D.Double(x4, y4);
1001 }
1002 else if (anchor == ItemLabelAnchor.INSIDE3) {
1003 result = new Point2D.Double(x4, y3);
1004 }
1005 else if (anchor == ItemLabelAnchor.INSIDE4) {
1006 result = new Point2D.Double(x4, y2);
1007 }
1008 else if (anchor == ItemLabelAnchor.INSIDE5) {
1009 result = new Point2D.Double(x4, y2);
1010 }
1011 else if (anchor == ItemLabelAnchor.INSIDE6) {
1012 result = new Point2D.Double(x3, y2);
1013 }
1014 else if (anchor == ItemLabelAnchor.INSIDE7) {
1015 result = new Point2D.Double(x2, y2);
1016 }
1017 else if (anchor == ItemLabelAnchor.INSIDE8) {
1018 result = new Point2D.Double(x2, y2);
1019 }
1020 else if (anchor == ItemLabelAnchor.INSIDE9) {
1021 result = new Point2D.Double(x2, y3);
1022 }
1023 else if (anchor == ItemLabelAnchor.INSIDE10) {
1024 result = new Point2D.Double(x2, y4);
1025 }
1026 else if (anchor == ItemLabelAnchor.INSIDE11) {
1027 result = new Point2D.Double(x2, y4);
1028 }
1029 else if (anchor == ItemLabelAnchor.INSIDE12) {
1030 result = new Point2D.Double(x3, y4);
1031 }
1032 else if (anchor == ItemLabelAnchor.OUTSIDE1) {
1033 result = new Point2D.Double(x5, y6);
1034 }
1035 else if (anchor == ItemLabelAnchor.OUTSIDE2) {
1036 result = new Point2D.Double(x6, y5);
1037 }
1038 else if (anchor == ItemLabelAnchor.OUTSIDE3) {
1039 result = new Point2D.Double(x6, y3);
1040 }
1041 else if (anchor == ItemLabelAnchor.OUTSIDE4) {
1042 result = new Point2D.Double(x6, y1);
1043 }
1044 else if (anchor == ItemLabelAnchor.OUTSIDE5) {
1045 result = new Point2D.Double(x5, y0);
1046 }
1047 else if (anchor == ItemLabelAnchor.OUTSIDE6) {
1048 result = new Point2D.Double(x3, y0);
1049 }
1050 else if (anchor == ItemLabelAnchor.OUTSIDE7) {
1051 result = new Point2D.Double(x1, y0);
1052 }
1053 else if (anchor == ItemLabelAnchor.OUTSIDE8) {
1054 result = new Point2D.Double(x0, y1);
1055 }
1056 else if (anchor == ItemLabelAnchor.OUTSIDE9) {
1057 result = new Point2D.Double(x0, y3);
1058 }
1059 else if (anchor == ItemLabelAnchor.OUTSIDE10) {
1060 result = new Point2D.Double(x0, y5);
1061 }
1062 else if (anchor == ItemLabelAnchor.OUTSIDE11) {
1063 result = new Point2D.Double(x1, y6);
1064 }
1065 else if (anchor == ItemLabelAnchor.OUTSIDE12) {
1066 result = new Point2D.Double(x3, y6);
1067 }
1068
1069 return result;
1070
1071 }
1072
1073 /**
1074 * Returns <code>true</code> if the specified anchor point is inside a bar.
1075 *
1076 * @param anchor the anchor point.
1077 *
1078 * @return A boolean.
1079 */
1080 private boolean isInternalAnchor(ItemLabelAnchor anchor) {
1081 return anchor == ItemLabelAnchor.CENTER
1082 || anchor == ItemLabelAnchor.INSIDE1
1083 || anchor == ItemLabelAnchor.INSIDE2
1084 || anchor == ItemLabelAnchor.INSIDE3
1085 || anchor == ItemLabelAnchor.INSIDE4
1086 || anchor == ItemLabelAnchor.INSIDE5
1087 || anchor == ItemLabelAnchor.INSIDE6
1088 || anchor == ItemLabelAnchor.INSIDE7
1089 || anchor == ItemLabelAnchor.INSIDE8
1090 || anchor == ItemLabelAnchor.INSIDE9
1091 || anchor == ItemLabelAnchor.INSIDE10
1092 || anchor == ItemLabelAnchor.INSIDE11
1093 || anchor == ItemLabelAnchor.INSIDE12;
1094 }
1095
1096 /**
1097 * Returns the lower and upper bounds (range) of the x-values in the
1098 * specified dataset. Since this renderer uses the x-interval in the
1099 * dataset, this is taken into account for the range.
1100 *
1101 * @param dataset the dataset (<code>null</code> permitted).
1102 *
1103 * @return The range (<code>null</code> if the dataset is
1104 * <code>null</code> or empty).
1105 */
1106 public Range findDomainBounds(XYDataset dataset) {
1107 if (dataset != null) {
1108 return DatasetUtilities.findDomainBounds(dataset, true);
1109 }
1110 else {
1111 return null;
1112 }
1113 }
1114
1115 /**
1116 * Returns the lower and upper bounds (range) of the y-values in the
1117 * specified dataset. If the renderer is plotting the y-interval from the
1118 * dataset, this is taken into account for the range.
1119 *
1120 * @param dataset the dataset (<code>null</code> permitted).
1121 *
1122 * @return The range (<code>null</code> if the dataset is
1123 * <code>null</code> or empty).
1124 */
1125 public Range findRangeBounds(XYDataset dataset) {
1126 if (dataset != null) {
1127 return DatasetUtilities.findRangeBounds(dataset,
1128 this.useYInterval);
1129 }
1130 else {
1131 return null;
1132 }
1133 }
1134
1135 /**
1136 * Returns a clone of the renderer.
1137 *
1138 * @return A clone.
1139 *
1140 * @throws CloneNotSupportedException if the renderer cannot be cloned.
1141 */
1142 public Object clone() throws CloneNotSupportedException {
1143 XYBarRenderer result = (XYBarRenderer) super.clone();
1144 if (this.gradientPaintTransformer != null) {
1145 result.gradientPaintTransformer = (GradientPaintTransformer)
1146 ObjectUtilities.clone(this.gradientPaintTransformer);
1147 }
1148 result.legendBar = ShapeUtilities.clone(this.legendBar);
1149 return result;
1150 }
1151
1152 /**
1153 * Tests this renderer for equality with an arbitrary object.
1154 *
1155 * @param obj the object to test against (<code>null</code> permitted).
1156 *
1157 * @return A boolean.
1158 */
1159 public boolean equals(Object obj) {
1160 if (obj == this) {
1161 return true;
1162 }
1163 if (!(obj instanceof XYBarRenderer)) {
1164 return false;
1165 }
1166 XYBarRenderer that = (XYBarRenderer) obj;
1167 if (this.base != that.base) {
1168 return false;
1169 }
1170 if (this.drawBarOutline != that.drawBarOutline) {
1171 return false;
1172 }
1173 if (this.margin != that.margin) {
1174 return false;
1175 }
1176 if (this.useYInterval != that.useYInterval) {
1177 return false;
1178 }
1179 if (!ObjectUtilities.equal(
1180 this.gradientPaintTransformer, that.gradientPaintTransformer)
1181 ) {
1182 return false;
1183 }
1184 if (!ShapeUtilities.equal(this.legendBar, that.legendBar)) {
1185 return false;
1186 }
1187 if (!ObjectUtilities.equal(this.positiveItemLabelPositionFallback,
1188 that.positiveItemLabelPositionFallback)) {
1189 return false;
1190 }
1191 if (!ObjectUtilities.equal(this.negativeItemLabelPositionFallback,
1192 that.negativeItemLabelPositionFallback)) {
1193 return false;
1194 }
1195 if (!this.barPainter.equals(that.barPainter)) {
1196 return false;
1197 }
1198 if (this.shadowsVisible != that.shadowsVisible) {
1199 return false;
1200 }
1201 if (this.shadowXOffset != that.shadowXOffset) {
1202 return false;
1203 }
1204 if (this.shadowYOffset != that.shadowYOffset) {
1205 return false;
1206 }
1207 return super.equals(obj);
1208 }
1209
1210 /**
1211 * Provides serialization support.
1212 *
1213 * @param stream the input stream.
1214 *
1215 * @throws IOException if there is an I/O error.
1216 * @throws ClassNotFoundException if there is a classpath problem.
1217 */
1218 private void readObject(ObjectInputStream stream)
1219 throws IOException, ClassNotFoundException {
1220 stream.defaultReadObject();
1221 this.legendBar = SerialUtilities.readShape(stream);
1222 }
1223
1224 /**
1225 * Provides serialization support.
1226 *
1227 * @param stream the output stream.
1228 *
1229 * @throws IOException if there is an I/O error.
1230 */
1231 private void writeObject(ObjectOutputStream stream) throws IOException {
1232 stream.defaultWriteObject();
1233 SerialUtilities.writeShape(this.legendBar, stream);
1234 }
1235
1236 }