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     * BarRenderer.java
029     * ----------------
030     * (C) Copyright 2002-2008, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   Christian W. Zuckschwerdt;
034     *
035     * Changes
036     * -------
037     * 14-Mar-2002 : Version 1 (DG);
038     * 23-May-2002 : Added tooltip generator to renderer (DG);
039     * 29-May-2002 : Moved tooltip generator to abstract super-class (DG);
040     * 25-Jun-2002 : Changed constructor to protected and removed redundant
041     *               code (DG);
042     * 26-Jun-2002 : Added axis to initialise method, and record upper and lower
043     *               clip values (DG);
044     * 24-Sep-2002 : Added getLegendItem() method (DG);
045     * 09-Oct-2002 : Modified constructor to include URL generator (DG);
046     * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
047     * 10-Jan-2003 : Moved get/setItemMargin() method up from subclasses (DG);
048     * 17-Jan-2003 : Moved plot classes into a separate package (DG);
049     * 25-Mar-2003 : Implemented Serializable (DG);
050     * 01-May-2003 : Modified clipping to allow for dual axes and datasets (DG);
051     * 12-May-2003 : Merged horizontal and vertical bar renderers (DG);
052     * 12-Jun-2003 : Updates for item labels (DG);
053     * 30-Jul-2003 : Modified entity constructor (CZ);
054     * 02-Sep-2003 : Changed initialise method to fix bug 790407 (DG);
055     * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
056     * 07-Oct-2003 : Added renderer state (DG);
057     * 27-Oct-2003 : Merged drawHorizontalItem() and drawVerticalItem()
058     *               methods (DG);
059     * 28-Oct-2003 : Added support for gradient paint on bars (DG);
060     * 14-Nov-2003 : Added 'maxBarWidth' attribute (DG);
061     * 10-Feb-2004 : Small changes inside drawItem() method to ease cut-and-paste
062     *               overriding (DG);
063     * 19-Mar-2004 : Fixed bug introduced with separation of tool tip and item
064     *               label generators.  Fixed equals() method (DG);
065     * 11-May-2004 : Fix for null pointer exception (bug id 951127) (DG);
066     * 05-Nov-2004 : Modified drawItem() signature (DG);
067     * 26-Jan-2005 : Provided override for getLegendItem() method (DG);
068     * 20-Apr-2005 : Generate legend labels, tooltips and URLs (DG);
069     * 18-May-2005 : Added configurable base value (DG);
070     * 09-Jun-2005 : Use addItemEntity() method from superclass (DG);
071     * 01-Dec-2005 : Update legend item to use/not use outline (DG);
072     * ------------: JFreeChart 1.0.x ---------------------------------------------
073     * 06-Dec-2005 : Fixed bug 1374222 (JDK 1.4 specific code) (DG);
074     * 11-Jan-2006 : Fixed bug 1401856 (bad rendering for non-zero base) (DG);
075     * 04-Aug-2006 : Fixed bug 1467706 (missing item labels for zero value
076     *               bars) (DG);
077     * 04-Dec-2006 : Fixed bug in rendering to non-primary axis (DG);
078     * 13-Dec-2006 : Add support for GradientPaint display in legend items (DG);
079     * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
080     * 11-May-2007 : Check for visibility in getLegendItem() (DG);
081     * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
082     * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
083     * 07-May-2008 : If minimumBarLength is > 0.0, extend the non-base end of the
084     *               bar (DG);
085     * 17-Jun-2008 : Apply legend shape, font and paint attributes (DG);
086     * 24-Jun-2008 : Added barPainter mechanism (DG);
087     * 26-Jun-2008 : Added crosshair support (DG);
088     * 13-Aug-2008 : Added shadowPaint attribute (DG);
089     *
090     */
091    
092    package org.jfree.chart.renderer.category;
093    
094    import java.awt.BasicStroke;
095    import java.awt.Color;
096    import java.awt.Font;
097    import java.awt.Graphics2D;
098    import java.awt.Paint;
099    import java.awt.Shape;
100    import java.awt.Stroke;
101    import java.awt.geom.Line2D;
102    import java.awt.geom.Point2D;
103    import java.awt.geom.Rectangle2D;
104    import java.io.IOException;
105    import java.io.ObjectInputStream;
106    import java.io.ObjectOutputStream;
107    import java.io.Serializable;
108    
109    import org.jfree.chart.LegendItem;
110    import org.jfree.chart.axis.CategoryAxis;
111    import org.jfree.chart.axis.ValueAxis;
112    import org.jfree.chart.entity.EntityCollection;
113    import org.jfree.chart.event.RendererChangeEvent;
114    import org.jfree.chart.labels.CategoryItemLabelGenerator;
115    import org.jfree.chart.labels.ItemLabelAnchor;
116    import org.jfree.chart.labels.ItemLabelPosition;
117    import org.jfree.chart.plot.CategoryPlot;
118    import org.jfree.chart.plot.PlotOrientation;
119    import org.jfree.chart.plot.PlotRenderingInfo;
120    import org.jfree.data.Range;
121    import org.jfree.data.category.CategoryDataset;
122    import org.jfree.data.general.DatasetUtilities;
123    import org.jfree.io.SerialUtilities;
124    import org.jfree.text.TextUtilities;
125    import org.jfree.ui.GradientPaintTransformer;
126    import org.jfree.ui.RectangleEdge;
127    import org.jfree.ui.StandardGradientPaintTransformer;
128    import org.jfree.util.ObjectUtilities;
129    import org.jfree.util.PaintUtilities;
130    import org.jfree.util.PublicCloneable;
131    
132    /**
133     * A {@link CategoryItemRenderer} that draws individual data items as bars.
134     * The example shown here is generated by the <code>BarChartDemo1.java</code>
135     * program included in the JFreeChart Demo Collection:
136     * <br><br>
137     * <img src="../../../../../images/BarRendererSample.png"
138     * alt="BarRendererSample.png" />
139     */
140    public class BarRenderer extends AbstractCategoryItemRenderer
141            implements Cloneable, PublicCloneable, Serializable {
142    
143        /** For serialization. */
144        private static final long serialVersionUID = 6000649414965887481L;
145    
146        /** The default item margin percentage. */
147        public static final double DEFAULT_ITEM_MARGIN = 0.20;
148    
149        /**
150         * Constant that controls the minimum width before a bar has an outline
151         * drawn.
152         */
153        public static final double BAR_OUTLINE_WIDTH_THRESHOLD = 3.0;
154    
155        /**
156         * The default bar painter assigned to each new instance of this renderer.
157         *
158         * @since 1.0.11
159         */
160        private static BarPainter defaultBarPainter = new GradientBarPainter();
161    
162        /**
163         * Returns the default bar painter.
164         *
165         * @return The default bar painter.
166         *
167         * @since 1.0.11
168         */
169        public static BarPainter getDefaultBarPainter() {
170            return BarRenderer.defaultBarPainter;
171        }
172    
173        /**
174         * Sets the default bar painter.
175         *
176         * @param painter  the painter (<code>null</code> not permitted).
177         *
178         * @since 1.0.11
179         */
180        public static void setDefaultBarPainter(BarPainter painter) {
181            if (painter == null) {
182                throw new IllegalArgumentException("Null 'painter' argument.");
183            }
184            BarRenderer.defaultBarPainter = painter;
185        }
186    
187        /** The margin between items (bars) within a category. */
188        private double itemMargin;
189    
190        /** A flag that controls whether or not bar outlines are drawn. */
191        private boolean drawBarOutline;
192    
193        /** The maximum bar width as a percentage of the available space. */
194        private double maximumBarWidth;
195    
196        /** The minimum bar length (in Java2D units). */
197        private double minimumBarLength;
198    
199        /**
200         * An optional class used to transform gradient paint objects to fit each
201         * bar.
202         */
203        private GradientPaintTransformer gradientPaintTransformer;
204    
205        /**
206         * The fallback position if a positive item label doesn't fit inside the
207         * bar.
208         */
209        private ItemLabelPosition positiveItemLabelPositionFallback;
210    
211        /**
212         * The fallback position if a negative item label doesn't fit inside the
213         * bar.
214         */
215        private ItemLabelPosition negativeItemLabelPositionFallback;
216    
217        /** The upper clip (axis) value for the axis. */
218        private double upperClip;
219        // TODO:  this needs to move into the renderer state
220    
221        /** The lower clip (axis) value for the axis. */
222        private double lowerClip;
223        // TODO:  this needs to move into the renderer state
224    
225        /** The base value for the bars (defaults to 0.0). */
226        private double base;
227    
228        /**
229         * A flag that controls whether the base value is included in the range
230         * returned by the findRangeBounds() method.
231         */
232        private boolean includeBaseInRange;
233    
234        /**
235         * The bar painter (never <code>null</code>).
236         *
237         * @since 1.0.11
238         */
239        private BarPainter barPainter;
240    
241        /**
242         * The flag that controls whether or not shadows are drawn for the bars.
243         *
244         * @since 1.0.11
245         */
246        private boolean shadowsVisible;
247    
248        /**
249         * The shadow paint.
250         *
251         * @since 1.0.11
252         */
253        private transient Paint shadowPaint;
254    
255        /**
256         * The x-offset for the shadow effect.
257         *
258         * @since 1.0.11
259         */
260        private double shadowXOffset;
261    
262        /**
263         * The y-offset for the shadow effect.
264         *
265         * @since 1.0.11
266         */
267        private double shadowYOffset;
268    
269        /**
270         * Creates a new bar renderer with default settings.
271         */
272        public BarRenderer() {
273            super();
274            this.base = 0.0;
275            this.includeBaseInRange = true;
276            this.itemMargin = DEFAULT_ITEM_MARGIN;
277            this.drawBarOutline = false;
278            this.maximumBarWidth = 1.0;
279                // 100 percent, so it will not apply unless changed
280            this.positiveItemLabelPositionFallback = null;
281            this.negativeItemLabelPositionFallback = null;
282            this.gradientPaintTransformer = new StandardGradientPaintTransformer();
283            this.minimumBarLength = 0.0;
284            setBaseLegendShape(new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0));
285            this.barPainter = getDefaultBarPainter();
286            this.shadowsVisible = true;
287            this.shadowPaint = Color.gray;
288            this.shadowXOffset = 4.0;
289            this.shadowYOffset = 4.0;
290        }
291    
292        /**
293         * Returns the base value for the bars.  The default value is
294         * <code>0.0</code>.
295         *
296         * @return The base value for the bars.
297         *
298         * @see #setBase(double)
299         */
300        public double getBase() {
301            return this.base;
302        }
303    
304        /**
305         * Sets the base value for the bars and sends a {@link RendererChangeEvent}
306         * to all registered listeners.
307         *
308         * @param base  the new base value.
309         *
310         * @see #getBase()
311         */
312        public void setBase(double base) {
313            this.base = base;
314            fireChangeEvent();
315        }
316    
317        /**
318         * Returns the item margin as a percentage of the available space for all
319         * bars.
320         *
321         * @return The margin percentage (where 0.10 is ten percent).
322         *
323         * @see #setItemMargin(double)
324         */
325        public double getItemMargin() {
326            return this.itemMargin;
327        }
328    
329        /**
330         * Sets the item margin and sends a {@link RendererChangeEvent} to all
331         * registered listeners.  The value is expressed as a percentage of the
332         * available width for plotting all the bars, with the resulting amount to
333         * be distributed between all the bars evenly.
334         *
335         * @param percent  the margin (where 0.10 is ten percent).
336         *
337         * @see #getItemMargin()
338         */
339        public void setItemMargin(double percent) {
340            this.itemMargin = percent;
341            fireChangeEvent();
342        }
343    
344        /**
345         * Returns a flag that controls whether or not bar outlines are drawn.
346         *
347         * @return A boolean.
348         *
349         * @see #setDrawBarOutline(boolean)
350         */
351        public boolean isDrawBarOutline() {
352            return this.drawBarOutline;
353        }
354    
355        /**
356         * Sets the flag that controls whether or not bar outlines are drawn and
357         * sends a {@link RendererChangeEvent} to all registered listeners.
358         *
359         * @param draw  the flag.
360         *
361         * @see #isDrawBarOutline()
362         */
363        public void setDrawBarOutline(boolean draw) {
364            this.drawBarOutline = draw;
365            fireChangeEvent();
366        }
367    
368        /**
369         * Returns the maximum bar width, as a percentage of the available drawing
370         * space.
371         *
372         * @return The maximum bar width.
373         *
374         * @see #setMaximumBarWidth(double)
375         */
376        public double getMaximumBarWidth() {
377            return this.maximumBarWidth;
378        }
379    
380        /**
381         * Sets the maximum bar width, which is specified as a percentage of the
382         * available space for all bars, and sends a {@link RendererChangeEvent} to
383         * all registered listeners.
384         *
385         * @param percent  the percent (where 0.05 is five percent).
386         *
387         * @see #getMaximumBarWidth()
388         */
389        public void setMaximumBarWidth(double percent) {
390            this.maximumBarWidth = percent;
391            fireChangeEvent();
392        }
393    
394        /**
395         * Returns the minimum bar length (in Java2D units).  The default value is
396         * 0.0.
397         *
398         * @return The minimum bar length.
399         *
400         * @see #setMinimumBarLength(double)
401         */
402        public double getMinimumBarLength() {
403            return this.minimumBarLength;
404        }
405    
406        /**
407         * Sets the minimum bar length and sends a {@link RendererChangeEvent} to
408         * all registered listeners.  The minimum bar length is specified in Java2D
409         * units, and can be used to prevent bars that represent very small data
410         * values from disappearing when drawn on the screen.  Typically you would
411         * set this to (say) 0.5 or 1.0 Java 2D units.  Use this attribute with
412         * caution, however, because setting it to a non-zero value will
413         * artificially increase the length of bars representing small values,
414         * which may misrepresent your data.
415         *
416         * @param min  the minimum bar length (in Java2D units, must be >= 0.0).
417         *
418         * @see #getMinimumBarLength()
419         */
420        public void setMinimumBarLength(double min) {
421            if (min < 0.0) {
422                throw new IllegalArgumentException("Requires 'min' >= 0.0");
423            }
424            this.minimumBarLength = min;
425            fireChangeEvent();
426        }
427    
428        /**
429         * Returns the gradient paint transformer (an object used to transform
430         * gradient paint objects to fit each bar).
431         *
432         * @return A transformer (<code>null</code> possible).
433         *
434         * @see #setGradientPaintTransformer(GradientPaintTransformer)
435         */
436        public GradientPaintTransformer getGradientPaintTransformer() {
437            return this.gradientPaintTransformer;
438        }
439    
440        /**
441         * Sets the gradient paint transformer and sends a
442         * {@link RendererChangeEvent} to all registered listeners.
443         *
444         * @param transformer  the transformer (<code>null</code> permitted).
445         *
446         * @see #getGradientPaintTransformer()
447         */
448        public void setGradientPaintTransformer(
449                GradientPaintTransformer transformer) {
450            this.gradientPaintTransformer = transformer;
451            fireChangeEvent();
452        }
453    
454        /**
455         * Returns the fallback position for positive item labels that don't fit
456         * within a bar.
457         *
458         * @return The fallback position (<code>null</code> possible).
459         *
460         * @see #setPositiveItemLabelPositionFallback(ItemLabelPosition)
461         */
462        public ItemLabelPosition getPositiveItemLabelPositionFallback() {
463            return this.positiveItemLabelPositionFallback;
464        }
465    
466        /**
467         * Sets the fallback position for positive item labels that don't fit
468         * within a bar, and sends a {@link RendererChangeEvent} to all registered
469         * listeners.
470         *
471         * @param position  the position (<code>null</code> permitted).
472         *
473         * @see #getPositiveItemLabelPositionFallback()
474         */
475        public void setPositiveItemLabelPositionFallback(
476                ItemLabelPosition position) {
477            this.positiveItemLabelPositionFallback = position;
478            fireChangeEvent();
479        }
480    
481        /**
482         * Returns the fallback position for negative item labels that don't fit
483         * within a bar.
484         *
485         * @return The fallback position (<code>null</code> possible).
486         *
487         * @see #setPositiveItemLabelPositionFallback(ItemLabelPosition)
488         */
489        public ItemLabelPosition getNegativeItemLabelPositionFallback() {
490            return this.negativeItemLabelPositionFallback;
491        }
492    
493        /**
494         * Sets the fallback position for negative item labels that don't fit
495         * within a bar, and sends a {@link RendererChangeEvent} to all registered
496         * listeners.
497         *
498         * @param position  the position (<code>null</code> permitted).
499         *
500         * @see #getNegativeItemLabelPositionFallback()
501         */
502        public void setNegativeItemLabelPositionFallback(
503                ItemLabelPosition position) {
504            this.negativeItemLabelPositionFallback = position;
505            fireChangeEvent();
506        }
507    
508        /**
509         * Returns the flag that controls whether or not the base value for the
510         * bars is included in the range calculated by
511         * {@link #findRangeBounds(CategoryDataset)}.
512         *
513         * @return <code>true</code> if the base is included in the range, and
514         *         <code>false</code> otherwise.
515         *
516         * @since 1.0.1
517         *
518         * @see #setIncludeBaseInRange(boolean)
519         */
520        public boolean getIncludeBaseInRange() {
521            return this.includeBaseInRange;
522        }
523    
524        /**
525         * Sets the flag that controls whether or not the base value for the bars
526         * is included in the range calculated by
527         * {@link #findRangeBounds(CategoryDataset)}.  If the flag is changed,
528         * a {@link RendererChangeEvent} is sent to all registered listeners.
529         *
530         * @param include  the new value for the flag.
531         *
532         * @since 1.0.1
533         *
534         * @see #getIncludeBaseInRange()
535         */
536        public void setIncludeBaseInRange(boolean include) {
537            if (this.includeBaseInRange != include) {
538                this.includeBaseInRange = include;
539                fireChangeEvent();
540            }
541        }
542    
543        /**
544         * Returns the bar painter.
545         *
546         * @return The bar painter (never <code>null</code>).
547         *
548         * @see #setBarPainter(BarPainter)
549         *
550         * @since 1.0.11
551         */
552        public BarPainter getBarPainter() {
553            return this.barPainter;
554        }
555    
556        /**
557         * Sets the bar painter for this renderer and sends a
558         * {@link RendererChangeEvent} to all registered listeners.
559         *
560         * @param painter  the painter (<code>null</code> not permitted).
561         *
562         * @see #getBarPainter()
563         *
564         * @since 1.0.11
565         */
566        public void setBarPainter(BarPainter painter) {
567            if (painter == null) {
568                throw new IllegalArgumentException("Null 'painter' argument.");
569            }
570            this.barPainter = painter;
571            fireChangeEvent();
572        }
573    
574        /**
575         * Returns the flag that controls whether or not shadows are drawn for
576         * the bars.
577         *
578         * @return A boolean.
579         *
580         * @since 1.0.11
581         */
582        public boolean getShadowsVisible() {
583            return this.shadowsVisible;
584        }
585    
586        /**
587         * Sets the flag that controls whether or not shadows are
588         * drawn by the renderer.
589         *
590         * @param visible  the new flag value.
591         *
592         * @since 1.0.11
593         */
594        public void setShadowVisible(boolean visible) {
595            this.shadowsVisible = visible;
596            fireChangeEvent();
597        }
598    
599        /**
600         * Returns the shadow paint.
601         *
602         * @return The shadow paint.
603         *
604         * @see #setShadowPaint(Paint)
605         *
606         * @since 1.0.11
607         */
608        public Paint getShadowPaint() {
609            return this.shadowPaint;
610        }
611    
612        /**
613         * Sets the shadow paint and sends a {@link RendererChangeEvent} to all
614         * registered listeners.
615         *
616         * @param paint  the paint (<code>null</code> not permitted).
617         *
618         * @see #getShadowPaint()
619         *
620         * @since 1.0.11
621         */
622        public void setShadowPaint(Paint paint) {
623            if (paint == null) {
624                throw new IllegalArgumentException("Null 'paint' argument.");
625            }
626            this.shadowPaint = paint;
627            fireChangeEvent();
628        }
629    
630        /**
631         * Returns the shadow x-offset.
632         *
633         * @return The shadow x-offset.
634         *
635         * @since 1.0.11
636         */
637        public double getShadowXOffset() {
638            return this.shadowXOffset;
639        }
640    
641        /**
642         * Sets the x-offset for the bar shadow and sends a
643         * {@link RendererChangeEvent} to all registered listeners.
644         *
645         * @param offset  the offset.
646         *
647         * @since 1.0.11
648         */
649        public void setShadowXOffset(double offset) {
650            this.shadowXOffset = offset;
651            fireChangeEvent();
652        }
653    
654        /**
655         * Returns the shadow y-offset.
656         *
657         * @return The shadow y-offset.
658         *
659         * @since 1.0.11
660         */
661        public double getShadowYOffset() {
662            return this.shadowYOffset;
663        }
664    
665        /**
666         * Sets the y-offset for the bar shadow and sends a
667         * {@link RendererChangeEvent} to all registered listeners.
668         *
669         * @param offset  the offset.
670         *
671         * @since 1.0.11
672         */
673        public void setShadowYOffset(double offset) {
674            this.shadowYOffset = offset;
675            fireChangeEvent();
676        }
677    
678        /**
679         * Returns the lower clip value.  This value is recalculated in the
680         * initialise() method.
681         *
682         * @return The value.
683         */
684        public double getLowerClip() {
685            // TODO:  this attribute should be transferred to the renderer state.
686            return this.lowerClip;
687        }
688    
689        /**
690         * Returns the upper clip value.  This value is recalculated in the
691         * initialise() method.
692         *
693         * @return The value.
694         */
695        public double getUpperClip() {
696            // TODO:  this attribute should be transferred to the renderer state.
697            return this.upperClip;
698        }
699    
700        /**
701         * Initialises the renderer and returns a state object that will be passed
702         * to subsequent calls to the drawItem method.  This method gets called
703         * once at the start of the process of drawing a chart.
704         *
705         * @param g2  the graphics device.
706         * @param dataArea  the area in which the data is to be plotted.
707         * @param plot  the plot.
708         * @param rendererIndex  the renderer index.
709         * @param info  collects chart rendering information for return to caller.
710         *
711         * @return The renderer state.
712         */
713        public CategoryItemRendererState initialise(Graphics2D g2,
714                                                    Rectangle2D dataArea,
715                                                    CategoryPlot plot,
716                                                    int rendererIndex,
717                                                    PlotRenderingInfo info) {
718    
719            CategoryItemRendererState state = super.initialise(g2, dataArea, plot,
720                    rendererIndex, info);
721    
722            // get the clipping values...
723            ValueAxis rangeAxis = plot.getRangeAxisForDataset(rendererIndex);
724            this.lowerClip = rangeAxis.getRange().getLowerBound();
725            this.upperClip = rangeAxis.getRange().getUpperBound();
726    
727            // calculate the bar width
728            calculateBarWidth(plot, dataArea, rendererIndex, state);
729    
730            return state;
731    
732        }
733    
734        /**
735         * Calculates the bar width and stores it in the renderer state.
736         *
737         * @param plot  the plot.
738         * @param dataArea  the data area.
739         * @param rendererIndex  the renderer index.
740         * @param state  the renderer state.
741         */
742        protected void calculateBarWidth(CategoryPlot plot,
743                                         Rectangle2D dataArea,
744                                         int rendererIndex,
745                                         CategoryItemRendererState state) {
746    
747            CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex);
748            CategoryDataset dataset = plot.getDataset(rendererIndex);
749            if (dataset != null) {
750                int columns = dataset.getColumnCount();
751                int rows = dataset.getRowCount();
752                double space = 0.0;
753                PlotOrientation orientation = plot.getOrientation();
754                if (orientation == PlotOrientation.HORIZONTAL) {
755                    space = dataArea.getHeight();
756                }
757                else if (orientation == PlotOrientation.VERTICAL) {
758                    space = dataArea.getWidth();
759                }
760                double maxWidth = space * getMaximumBarWidth();
761                double categoryMargin = 0.0;
762                double currentItemMargin = 0.0;
763                if (columns > 1) {
764                    categoryMargin = domainAxis.getCategoryMargin();
765                }
766                if (rows > 1) {
767                    currentItemMargin = getItemMargin();
768                }
769                double used = space * (1 - domainAxis.getLowerMargin()
770                                         - domainAxis.getUpperMargin()
771                                         - categoryMargin - currentItemMargin);
772                if ((rows * columns) > 0) {
773                    state.setBarWidth(Math.min(used / (rows * columns), maxWidth));
774                }
775                else {
776                    state.setBarWidth(Math.min(used, maxWidth));
777                }
778            }
779        }
780    
781        /**
782         * Calculates the coordinate of the first "side" of a bar.  This will be
783         * the minimum x-coordinate for a vertical bar, and the minimum
784         * y-coordinate for a horizontal bar.
785         *
786         * @param plot  the plot.
787         * @param orientation  the plot orientation.
788         * @param dataArea  the data area.
789         * @param domainAxis  the domain axis.
790         * @param state  the renderer state (has the bar width precalculated).
791         * @param row  the row index.
792         * @param column  the column index.
793         *
794         * @return The coordinate.
795         */
796        protected double calculateBarW0(CategoryPlot plot,
797                                        PlotOrientation orientation,
798                                        Rectangle2D dataArea,
799                                        CategoryAxis domainAxis,
800                                        CategoryItemRendererState state,
801                                        int row,
802                                        int column) {
803            // calculate bar width...
804            double space = 0.0;
805            if (orientation == PlotOrientation.HORIZONTAL) {
806                space = dataArea.getHeight();
807            }
808            else {
809                space = dataArea.getWidth();
810            }
811            double barW0 = domainAxis.getCategoryStart(column, getColumnCount(),
812                    dataArea, plot.getDomainAxisEdge());
813            int seriesCount = getRowCount();
814            int categoryCount = getColumnCount();
815            if (seriesCount > 1) {
816                double seriesGap = space * getItemMargin()
817                                   / (categoryCount * (seriesCount - 1));
818                double seriesW = calculateSeriesWidth(space, domainAxis,
819                        categoryCount, seriesCount);
820                barW0 = barW0 + row * (seriesW + seriesGap)
821                              + (seriesW / 2.0) - (state.getBarWidth() / 2.0);
822            }
823            else {
824                barW0 = domainAxis.getCategoryMiddle(column, getColumnCount(),
825                        dataArea, plot.getDomainAxisEdge()) - state.getBarWidth()
826                        / 2.0;
827            }
828            return barW0;
829        }
830    
831        /**
832         * Calculates the coordinates for the length of a single bar.
833         *
834         * @param value  the value represented by the bar.
835         *
836         * @return The coordinates for each end of the bar (or <code>null</code> if
837         *         the bar is not visible for the current axis range).
838         */
839        protected double[] calculateBarL0L1(double value) {
840            double lclip = getLowerClip();
841            double uclip = getUpperClip();
842            double barLow = Math.min(this.base, value);
843            double barHigh = Math.max(this.base, value);
844            if (barHigh < lclip) {  // bar is not visible
845                return null;
846            }
847            if (barLow > uclip) {   // bar is not visible
848                return null;
849            }
850            barLow = Math.max(barLow, lclip);
851            barHigh = Math.min(barHigh, uclip);
852            return new double[] {barLow, barHigh};
853        }
854    
855        /**
856         * Returns the range of values the renderer requires to display all the
857         * items from the specified dataset.  This takes into account the range
858         * of values in the dataset, plus the flag that determines whether or not
859         * the base value for the bars should be included in the range.
860         *
861         * @param dataset  the dataset (<code>null</code> permitted).
862         *
863         * @return The range (or <code>null</code> if the dataset is
864         *         <code>null</code> or empty).
865         */
866        public Range findRangeBounds(CategoryDataset dataset) {
867            if (dataset == null) {
868                return null;
869            }
870            Range result = DatasetUtilities.findRangeBounds(dataset);
871            if (result != null) {
872                if (this.includeBaseInRange) {
873                    result = Range.expandToInclude(result, this.base);
874                }
875            }
876            return result;
877        }
878    
879        /**
880         * Returns a legend item for a series.
881         *
882         * @param datasetIndex  the dataset index (zero-based).
883         * @param series  the series index (zero-based).
884         *
885         * @return The legend item (possibly <code>null</code>).
886         */
887        public LegendItem getLegendItem(int datasetIndex, int series) {
888    
889            CategoryPlot cp = getPlot();
890            if (cp == null) {
891                return null;
892            }
893    
894            // check that a legend item needs to be displayed...
895            if (!isSeriesVisible(series) || !isSeriesVisibleInLegend(series)) {
896                return null;
897            }
898    
899            CategoryDataset dataset = cp.getDataset(datasetIndex);
900            String label = getLegendItemLabelGenerator().generateLabel(dataset,
901                    series);
902            String description = label;
903            String toolTipText = null;
904            if (getLegendItemToolTipGenerator() != null) {
905                toolTipText = getLegendItemToolTipGenerator().generateLabel(
906                        dataset, series);
907            }
908            String urlText = null;
909            if (getLegendItemURLGenerator() != null) {
910                urlText = getLegendItemURLGenerator().generateLabel(dataset,
911                        series);
912            }
913            Shape shape = lookupLegendShape(series);
914            Paint paint = lookupSeriesPaint(series);
915            Paint outlinePaint = lookupSeriesOutlinePaint(series);
916            Stroke outlineStroke = lookupSeriesOutlineStroke(series);
917    
918            LegendItem result = new LegendItem(label, description, toolTipText,
919                    urlText, true, shape, true, paint, isDrawBarOutline(),
920                    outlinePaint, outlineStroke, false, new Line2D.Float(),
921                    new BasicStroke(1.0f), Color.black);
922            result.setLabelFont(lookupLegendTextFont(series));
923            Paint labelPaint = lookupLegendTextPaint(series);
924            if (labelPaint != null) {
925                result.setLabelPaint(labelPaint);
926            }
927            result.setDataset(dataset);
928            result.setDatasetIndex(datasetIndex);
929            result.setSeriesKey(dataset.getRowKey(series));
930            result.setSeriesIndex(series);
931            if (this.gradientPaintTransformer != null) {
932                result.setFillPaintTransformer(this.gradientPaintTransformer);
933            }
934            return result;
935        }
936    
937        /**
938         * Draws the bar for a single (series, category) data item.
939         *
940         * @param g2  the graphics device.
941         * @param state  the renderer state.
942         * @param dataArea  the data area.
943         * @param plot  the plot.
944         * @param domainAxis  the domain axis.
945         * @param rangeAxis  the range axis.
946         * @param dataset  the dataset.
947         * @param row  the row index (zero-based).
948         * @param column  the column index (zero-based).
949         * @param pass  the pass index.
950         */
951        public void drawItem(Graphics2D g2,
952                             CategoryItemRendererState state,
953                             Rectangle2D dataArea,
954                             CategoryPlot plot,
955                             CategoryAxis domainAxis,
956                             ValueAxis rangeAxis,
957                             CategoryDataset dataset,
958                             int row,
959                             int column,
960                             int pass) {
961    
962            // nothing is drawn for null values...
963            Number dataValue = dataset.getValue(row, column);
964            if (dataValue == null) {
965                return;
966            }
967    
968            final double value = dataValue.doubleValue();
969            PlotOrientation orientation = plot.getOrientation();
970            double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis,
971                    state, row, column);
972            double[] barL0L1 = calculateBarL0L1(value);
973            if (barL0L1 == null) {
974                return;  // the bar is not visible
975            }
976    
977            RectangleEdge edge = plot.getRangeAxisEdge();
978            double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
979            double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);
980    
981            // in the following code, barL0 is (in Java2D coordinates) the LEFT
982            // end of the bar for a horizontal bar chart, and the TOP end of the
983            // bar for a vertical bar chart.  Whether this is the BASE of the bar
984            // or not depends also on (a) whether the data value is 'negative'
985            // relative to the base value and (b) whether or not the range axis is
986            // inverted.  This only matters if/when we apply the minimumBarLength
987            // attribute, because we should extend the non-base end of the bar
988            boolean positive = (value >= this.base);
989            boolean inverted = rangeAxis.isInverted();
990            double barL0 = Math.min(transL0, transL1);
991            double barLength = Math.abs(transL1 - transL0);
992            double barLengthAdj = 0.0;
993            if (barLength > 0.0 && barLength < getMinimumBarLength()) {
994                barLengthAdj = getMinimumBarLength() - barLength;
995            }
996            double barL0Adj = 0.0;
997            RectangleEdge barBase;
998            if (orientation == PlotOrientation.HORIZONTAL) {
999                if (positive && inverted || !positive && !inverted) {
1000                    barL0Adj = barLengthAdj;
1001                    barBase = RectangleEdge.RIGHT;
1002                }
1003                else {
1004                    barBase = RectangleEdge.LEFT;
1005                }
1006            }
1007            else {
1008                if (positive && !inverted || !positive && inverted) {
1009                    barL0Adj = barLengthAdj;
1010                    barBase = RectangleEdge.BOTTOM;
1011                }
1012                else {
1013                    barBase = RectangleEdge.TOP;
1014                }
1015            }
1016    
1017            // draw the bar...
1018            Rectangle2D bar = null;
1019            if (orientation == PlotOrientation.HORIZONTAL) {
1020                bar = new Rectangle2D.Double(barL0 - barL0Adj, barW0,
1021                        barLength + barLengthAdj, state.getBarWidth());
1022            }
1023            else {
1024                bar = new Rectangle2D.Double(barW0, barL0 - barL0Adj,
1025                        state.getBarWidth(), barLength + barLengthAdj);
1026            }
1027            if (getShadowsVisible()) {
1028                this.barPainter.paintBarShadow(g2, this, row, column, bar, barBase,
1029                    true);
1030            }
1031            this.barPainter.paintBar(g2, this, row, column, bar, barBase);
1032    
1033            CategoryItemLabelGenerator generator = getItemLabelGenerator(row,
1034                    column);
1035            if (generator != null && isItemLabelVisible(row, column)) {
1036                drawItemLabel(g2, dataset, row, column, plot, generator, bar,
1037                        (value < 0.0));
1038            }
1039    
1040            // submit the current data point as a crosshair candidate
1041            int datasetIndex = plot.indexOf(dataset);
1042            updateCrosshairValues(state.getCrosshairState(),
1043                    dataset.getRowKey(row), dataset.getColumnKey(column), value,
1044                    datasetIndex, barW0, barL0, orientation);
1045    
1046            // add an item entity, if this information is being collected
1047            EntityCollection entities = state.getEntityCollection();
1048            if (entities != null) {
1049                addItemEntity(entities, dataset, row, column, bar);
1050            }
1051    
1052        }
1053    
1054        /**
1055         * Calculates the available space for each series.
1056         *
1057         * @param space  the space along the entire axis (in Java2D units).
1058         * @param axis  the category axis.
1059         * @param categories  the number of categories.
1060         * @param series  the number of series.
1061         *
1062         * @return The width of one series.
1063         */
1064        protected double calculateSeriesWidth(double space, CategoryAxis axis,
1065                                              int categories, int series) {
1066            double factor = 1.0 - getItemMargin() - axis.getLowerMargin()
1067                                - axis.getUpperMargin();
1068            if (categories > 1) {
1069                factor = factor - axis.getCategoryMargin();
1070            }
1071            return (space * factor) / (categories * series);
1072        }
1073    
1074        /**
1075         * Draws an item label.  This method is overridden so that the bar can be
1076         * used to calculate the label anchor point.
1077         *
1078         * @param g2  the graphics device.
1079         * @param data  the dataset.
1080         * @param row  the row.
1081         * @param column  the column.
1082         * @param plot  the plot.
1083         * @param generator  the label generator.
1084         * @param bar  the bar.
1085         * @param negative  a flag indicating a negative value.
1086         */
1087        protected void drawItemLabel(Graphics2D g2,
1088                                     CategoryDataset data,
1089                                     int row,
1090                                     int column,
1091                                     CategoryPlot plot,
1092                                     CategoryItemLabelGenerator generator,
1093                                     Rectangle2D bar,
1094                                     boolean negative) {
1095    
1096            String label = generator.generateLabel(data, row, column);
1097            if (label == null) {
1098                return;  // nothing to do
1099            }
1100    
1101            Font labelFont = getItemLabelFont(row, column);
1102            g2.setFont(labelFont);
1103            Paint paint = getItemLabelPaint(row, column);
1104            g2.setPaint(paint);
1105    
1106            // find out where to place the label...
1107            ItemLabelPosition position = null;
1108            if (!negative) {
1109                position = getPositiveItemLabelPosition(row, column);
1110            }
1111            else {
1112                position = getNegativeItemLabelPosition(row, column);
1113            }
1114    
1115            // work out the label anchor point...
1116            Point2D anchorPoint = calculateLabelAnchorPoint(
1117                    position.getItemLabelAnchor(), bar, plot.getOrientation());
1118    
1119            if (isInternalAnchor(position.getItemLabelAnchor())) {
1120                Shape bounds = TextUtilities.calculateRotatedStringBounds(label,
1121                        g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
1122                        position.getTextAnchor(), position.getAngle(),
1123                        position.getRotationAnchor());
1124    
1125                if (bounds != null) {
1126                    if (!bar.contains(bounds.getBounds2D())) {
1127                        if (!negative) {
1128                            position = getPositiveItemLabelPositionFallback();
1129                        }
1130                        else {
1131                            position = getNegativeItemLabelPositionFallback();
1132                        }
1133                        if (position != null) {
1134                            anchorPoint = calculateLabelAnchorPoint(
1135                                    position.getItemLabelAnchor(), bar,
1136                                    plot.getOrientation());
1137                        }
1138                    }
1139                }
1140    
1141            }
1142    
1143            if (position != null) {
1144                TextUtilities.drawRotatedString(label, g2,
1145                        (float) anchorPoint.getX(), (float) anchorPoint.getY(),
1146                        position.getTextAnchor(), position.getAngle(),
1147                        position.getRotationAnchor());
1148            }
1149        }
1150    
1151        /**
1152         * Calculates the item label anchor point.
1153         *
1154         * @param anchor  the anchor.
1155         * @param bar  the bar.
1156         * @param orientation  the plot orientation.
1157         *
1158         * @return The anchor point.
1159         */
1160        private Point2D calculateLabelAnchorPoint(ItemLabelAnchor anchor,
1161                                                  Rectangle2D bar,
1162                                                  PlotOrientation orientation) {
1163    
1164            Point2D result = null;
1165            double offset = getItemLabelAnchorOffset();
1166            double x0 = bar.getX() - offset;
1167            double x1 = bar.getX();
1168            double x2 = bar.getX() + offset;
1169            double x3 = bar.getCenterX();
1170            double x4 = bar.getMaxX() - offset;
1171            double x5 = bar.getMaxX();
1172            double x6 = bar.getMaxX() + offset;
1173    
1174            double y0 = bar.getMaxY() + offset;
1175            double y1 = bar.getMaxY();
1176            double y2 = bar.getMaxY() - offset;
1177            double y3 = bar.getCenterY();
1178            double y4 = bar.getMinY() + offset;
1179            double y5 = bar.getMinY();
1180            double y6 = bar.getMinY() - offset;
1181    
1182            if (anchor == ItemLabelAnchor.CENTER) {
1183                result = new Point2D.Double(x3, y3);
1184            }
1185            else if (anchor == ItemLabelAnchor.INSIDE1) {
1186                result = new Point2D.Double(x4, y4);
1187            }
1188            else if (anchor == ItemLabelAnchor.INSIDE2) {
1189                result = new Point2D.Double(x4, y4);
1190            }
1191            else if (anchor == ItemLabelAnchor.INSIDE3) {
1192                result = new Point2D.Double(x4, y3);
1193            }
1194            else if (anchor == ItemLabelAnchor.INSIDE4) {
1195                result = new Point2D.Double(x4, y2);
1196            }
1197            else if (anchor == ItemLabelAnchor.INSIDE5) {
1198                result = new Point2D.Double(x4, y2);
1199            }
1200            else if (anchor == ItemLabelAnchor.INSIDE6) {
1201                result = new Point2D.Double(x3, y2);
1202            }
1203            else if (anchor == ItemLabelAnchor.INSIDE7) {
1204                result = new Point2D.Double(x2, y2);
1205            }
1206            else if (anchor == ItemLabelAnchor.INSIDE8) {
1207                result = new Point2D.Double(x2, y2);
1208            }
1209            else if (anchor == ItemLabelAnchor.INSIDE9) {
1210                result = new Point2D.Double(x2, y3);
1211            }
1212            else if (anchor == ItemLabelAnchor.INSIDE10) {
1213                result = new Point2D.Double(x2, y4);
1214            }
1215            else if (anchor == ItemLabelAnchor.INSIDE11) {
1216                result = new Point2D.Double(x2, y4);
1217            }
1218            else if (anchor == ItemLabelAnchor.INSIDE12) {
1219                result = new Point2D.Double(x3, y4);
1220            }
1221            else if (anchor == ItemLabelAnchor.OUTSIDE1) {
1222                result = new Point2D.Double(x5, y6);
1223            }
1224            else if (anchor == ItemLabelAnchor.OUTSIDE2) {
1225                result = new Point2D.Double(x6, y5);
1226            }
1227            else if (anchor == ItemLabelAnchor.OUTSIDE3) {
1228                result = new Point2D.Double(x6, y3);
1229            }
1230            else if (anchor == ItemLabelAnchor.OUTSIDE4) {
1231                result = new Point2D.Double(x6, y1);
1232            }
1233            else if (anchor == ItemLabelAnchor.OUTSIDE5) {
1234                result = new Point2D.Double(x5, y0);
1235            }
1236            else if (anchor == ItemLabelAnchor.OUTSIDE6) {
1237                result = new Point2D.Double(x3, y0);
1238            }
1239            else if (anchor == ItemLabelAnchor.OUTSIDE7) {
1240                result = new Point2D.Double(x1, y0);
1241            }
1242            else if (anchor == ItemLabelAnchor.OUTSIDE8) {
1243                result = new Point2D.Double(x0, y1);
1244            }
1245            else if (anchor == ItemLabelAnchor.OUTSIDE9) {
1246                result = new Point2D.Double(x0, y3);
1247            }
1248            else if (anchor == ItemLabelAnchor.OUTSIDE10) {
1249                result = new Point2D.Double(x0, y5);
1250            }
1251            else if (anchor == ItemLabelAnchor.OUTSIDE11) {
1252                result = new Point2D.Double(x1, y6);
1253            }
1254            else if (anchor == ItemLabelAnchor.OUTSIDE12) {
1255                result = new Point2D.Double(x3, y6);
1256            }
1257    
1258            return result;
1259    
1260        }
1261    
1262        /**
1263         * Returns <code>true</code> if the specified anchor point is inside a bar.
1264         *
1265         * @param anchor  the anchor point.
1266         *
1267         * @return A boolean.
1268         */
1269        private boolean isInternalAnchor(ItemLabelAnchor anchor) {
1270            return anchor == ItemLabelAnchor.CENTER
1271                   || anchor == ItemLabelAnchor.INSIDE1
1272                   || anchor == ItemLabelAnchor.INSIDE2
1273                   || anchor == ItemLabelAnchor.INSIDE3
1274                   || anchor == ItemLabelAnchor.INSIDE4
1275                   || anchor == ItemLabelAnchor.INSIDE5
1276                   || anchor == ItemLabelAnchor.INSIDE6
1277                   || anchor == ItemLabelAnchor.INSIDE7
1278                   || anchor == ItemLabelAnchor.INSIDE8
1279                   || anchor == ItemLabelAnchor.INSIDE9
1280                   || anchor == ItemLabelAnchor.INSIDE10
1281                   || anchor == ItemLabelAnchor.INSIDE11
1282                   || anchor == ItemLabelAnchor.INSIDE12;
1283        }
1284    
1285        /**
1286         * Tests this instance for equality with an arbitrary object.
1287         *
1288         * @param obj  the object (<code>null</code> permitted).
1289         *
1290         * @return A boolean.
1291         */
1292        public boolean equals(Object obj) {
1293            if (obj == this) {
1294                return true;
1295            }
1296            if (!(obj instanceof BarRenderer)) {
1297                return false;
1298            }
1299            BarRenderer that = (BarRenderer) obj;
1300            if (this.base != that.base) {
1301                return false;
1302            }
1303            if (this.itemMargin != that.itemMargin) {
1304                return false;
1305            }
1306            if (this.drawBarOutline != that.drawBarOutline) {
1307                return false;
1308            }
1309            if (this.maximumBarWidth != that.maximumBarWidth) {
1310                return false;
1311            }
1312            if (this.minimumBarLength != that.minimumBarLength) {
1313                return false;
1314            }
1315            if (!ObjectUtilities.equal(this.gradientPaintTransformer,
1316                    that.gradientPaintTransformer)) {
1317                return false;
1318            }
1319            if (!ObjectUtilities.equal(this.positiveItemLabelPositionFallback,
1320                that.positiveItemLabelPositionFallback)) {
1321                return false;
1322            }
1323            if (!ObjectUtilities.equal(this.negativeItemLabelPositionFallback,
1324                that.negativeItemLabelPositionFallback)) {
1325                return false;
1326            }
1327            if (!this.barPainter.equals(that.barPainter)) {
1328                return false;
1329            }
1330            if (this.shadowsVisible != that.shadowsVisible) {
1331                return false;
1332            }
1333            if (!PaintUtilities.equal(this.shadowPaint, that.shadowPaint)) {
1334                return false;
1335            }
1336            if (this.shadowXOffset != that.shadowXOffset) {
1337                return false;
1338            }
1339            if (this.shadowYOffset != that.shadowYOffset) {
1340                return false;
1341            }
1342            return super.equals(obj);
1343        }
1344    
1345        /**
1346         * Provides serialization support.
1347         *
1348         * @param stream  the output stream.
1349         *
1350         * @throws IOException  if there is an I/O error.
1351         */
1352        private void writeObject(ObjectOutputStream stream) throws IOException {
1353            stream.defaultWriteObject();
1354            SerialUtilities.writePaint(this.shadowPaint, stream);
1355        }
1356    
1357        /**
1358         * Provides serialization support.
1359         *
1360         * @param stream  the input stream.
1361         *
1362         * @throws IOException  if there is an I/O error.
1363         * @throws ClassNotFoundException  if there is a classpath problem.
1364         */
1365        private void readObject(ObjectInputStream stream)
1366                throws IOException, ClassNotFoundException {
1367            stream.defaultReadObject();
1368            this.shadowPaint = SerialUtilities.readPaint(stream);
1369        }
1370    
1371    }