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     * StackedBarRenderer3D.java
029     * -------------------------
030     * (C) Copyright 2000-2008, by Serge V. Grachov and Contributors.
031     *
032     * Original Author:  Serge V. Grachov;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *                   Richard Atkinson;
035     *                   Christian W. Zuckschwerdt;
036     *                   Max Herfort (patch 1459313);
037     *
038     * Changes
039     * -------
040     * 31-Oct-2001 : Version 1, contributed by Serge V. Grachov (DG);
041     * 15-Nov-2001 : Modified to allow for null data values (DG);
042     * 13-Dec-2001 : Added tooltips (DG);
043     * 15-Feb-2002 : Added isStacked() method (DG);
044     * 24-May-2002 : Incorporated tooltips into chart entities (DG);
045     * 19-Jun-2002 : Added check for null info in drawCategoryItem method (DG);
046     * 25-Jun-2002 : Removed redundant imports (DG);
047     * 26-Jun-2002 : Small change to entity (DG);
048     * 05-Aug-2002 : Small modification to drawCategoryItem method to support URLs
049     *               for HTML image maps (RA);
050     * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
051     * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and
052     *               CategoryToolTipGenerator interface (DG);
053     * 05-Nov-2002 : Replaced references to CategoryDataset with TableDataset (DG);
054     * 26-Nov-2002 : Replaced isStacked() method with getRangeType() method (DG);
055     * 17-Jan-2003 : Moved plot classes to a separate package (DG);
056     * 25-Mar-2003 : Implemented Serializable (DG);
057     * 01-May-2003 : Added default constructor (bug 726235) and fixed bug
058     *               726260) (DG);
059     * 13-May-2003 : Renamed StackedVerticalBarRenderer3D
060     *               --> StackedBarRenderer3D (DG);
061     * 30-Jul-2003 : Modified entity constructor (CZ);
062     * 07-Oct-2003 : Added renderer state (DG);
063     * 21-Nov-2003 : Added a new constructor (DG);
064     * 27-Nov-2003 : Modified code to respect maxBarWidth setting (DG);
065     * 11-Aug-2004 : Fixed bug where isDrawBarOutline() was ignored (DG);
066     * 05-Nov-2004 : Modified drawItem() signature (DG);
067     * 07-Jan-2005 : Renamed getRangeExtent() --> findRangeBounds (DG);
068     * 18-Mar-2005 : Override for getPassCount() method (DG);
069     * 20-Apr-2005 : Renamed CategoryLabelGenerator
070     *               --> CategoryItemLabelGenerator (DG);
071     * 09-Jun-2005 : Use addItemEntity() method from superclass (DG);
072     * 22-Sep-2005 : Renamed getMaxBarWidth() --> getMaximumBarWidth() (DG);
073     * ------------- JFREECHART 1.0.x ---------------------------------------------
074     * 31-Mar-2006 : Added renderAsPercentages option - see patch 1459313 submitted
075     *               by Max Herfort (DG);
076     * 16-Jan-2007 : Replaced rendering code to draw whole stack at once (DG);
077     * 18-Jan-2007 : Fixed bug handling null values in createStackedValueList()
078     *               method (DG);
079     * 18-Jan-2007 : Updated block drawing code to take account of inverted axes,
080     *               see bug report 1599652 (DG);
081     * 08-May-2007 : Fixed bugs 1713401 (drawBarOutlines flag) and  1713474
082     *               (shading) (DG);
083     * 15-Aug-2008 : Fixed bug 2031407 - no negative zero for stack encoding (DG);
084     *
085     */
086    
087    package org.jfree.chart.renderer.category;
088    
089    import java.awt.Color;
090    import java.awt.Graphics2D;
091    import java.awt.Paint;
092    import java.awt.Shape;
093    import java.awt.geom.GeneralPath;
094    import java.awt.geom.Point2D;
095    import java.awt.geom.Rectangle2D;
096    import java.io.Serializable;
097    import java.util.ArrayList;
098    import java.util.List;
099    
100    import org.jfree.chart.axis.CategoryAxis;
101    import org.jfree.chart.axis.ValueAxis;
102    import org.jfree.chart.entity.EntityCollection;
103    import org.jfree.chart.event.RendererChangeEvent;
104    import org.jfree.chart.labels.CategoryItemLabelGenerator;
105    import org.jfree.chart.plot.CategoryPlot;
106    import org.jfree.chart.plot.PlotOrientation;
107    import org.jfree.data.DataUtilities;
108    import org.jfree.data.Range;
109    import org.jfree.data.category.CategoryDataset;
110    import org.jfree.data.general.DatasetUtilities;
111    import org.jfree.util.BooleanUtilities;
112    import org.jfree.util.PublicCloneable;
113    
114    /**
115     * Renders stacked bars with 3D-effect, for use with the {@link CategoryPlot}
116     * class.  The example shown here is generated by the
117     * <code>StackedBarChart3DDemo1.java</code> program included in the
118     * JFreeChart Demo Collection:
119     * <br><br>
120     * <img src="../../../../../images/StackedBarRenderer3DSample.png"
121     * alt="StackedBarRenderer3DSample.png" />
122     */
123    public class StackedBarRenderer3D extends BarRenderer3D
124            implements Cloneable, PublicCloneable, Serializable {
125    
126        /** For serialization. */
127        private static final long serialVersionUID = -5832945916493247123L;
128    
129        /** A flag that controls whether the bars display values or percentages. */
130        private boolean renderAsPercentages;
131    
132        /**
133         * Creates a new renderer with no tool tip generator and no URL generator.
134         * <P>
135         * The defaults (no tool tip or URL generators) have been chosen to
136         * minimise the processing required to generate a default chart.  If you
137         * require tool tips or URLs, then you can easily add the required
138         * generators.
139         */
140        public StackedBarRenderer3D() {
141            this(false);
142        }
143    
144        /**
145         * Constructs a new renderer with the specified '3D effect'.
146         *
147         * @param xOffset  the x-offset for the 3D effect.
148         * @param yOffset  the y-offset for the 3D effect.
149         */
150        public StackedBarRenderer3D(double xOffset, double yOffset) {
151            super(xOffset, yOffset);
152        }
153    
154        /**
155         * Creates a new renderer.
156         *
157         * @param renderAsPercentages  a flag that controls whether the data values
158         *                             are rendered as percentages.
159         *
160         * @since 1.0.2
161         */
162        public StackedBarRenderer3D(boolean renderAsPercentages) {
163            super();
164            this.renderAsPercentages = renderAsPercentages;
165        }
166    
167        /**
168         * Constructs a new renderer with the specified '3D effect'.
169         *
170         * @param xOffset  the x-offset for the 3D effect.
171         * @param yOffset  the y-offset for the 3D effect.
172         * @param renderAsPercentages  a flag that controls whether the data values
173         *                             are rendered as percentages.
174         *
175         * @since 1.0.2
176         */
177        public StackedBarRenderer3D(double xOffset, double yOffset,
178                boolean renderAsPercentages) {
179            super(xOffset, yOffset);
180            this.renderAsPercentages = renderAsPercentages;
181        }
182    
183        /**
184         * Returns <code>true</code> if the renderer displays each item value as
185         * a percentage (so that the stacked bars add to 100%), and
186         * <code>false</code> otherwise.
187         *
188         * @return A boolean.
189         *
190         * @since 1.0.2
191         */
192        public boolean getRenderAsPercentages() {
193            return this.renderAsPercentages;
194        }
195    
196        /**
197         * Sets the flag that controls whether the renderer displays each item
198         * value as a percentage (so that the stacked bars add to 100%), and sends
199         * a {@link RendererChangeEvent} to all registered listeners.
200         *
201         * @param asPercentages  the flag.
202         *
203         * @since 1.0.2
204         */
205        public void setRenderAsPercentages(boolean asPercentages) {
206            this.renderAsPercentages = asPercentages;
207            fireChangeEvent();
208        }
209    
210        /**
211         * Returns the range of values the renderer requires to display all the
212         * items from the specified dataset.
213         *
214         * @param dataset  the dataset (<code>null</code> not permitted).
215         *
216         * @return The range (or <code>null</code> if the dataset is empty).
217         */
218        public Range findRangeBounds(CategoryDataset dataset) {
219            if (this.renderAsPercentages) {
220                return new Range(0.0, 1.0);
221            }
222            else {
223                return DatasetUtilities.findStackedRangeBounds(dataset);
224            }
225        }
226    
227        /**
228         * Calculates the bar width and stores it in the renderer state.
229         *
230         * @param plot  the plot.
231         * @param dataArea  the data area.
232         * @param rendererIndex  the renderer index.
233         * @param state  the renderer state.
234         */
235        protected void calculateBarWidth(CategoryPlot plot,
236                                         Rectangle2D dataArea,
237                                         int rendererIndex,
238                                         CategoryItemRendererState state) {
239    
240            // calculate the bar width
241            CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex);
242            CategoryDataset data = plot.getDataset(rendererIndex);
243            if (data != null) {
244                PlotOrientation orientation = plot.getOrientation();
245                double space = 0.0;
246                if (orientation == PlotOrientation.HORIZONTAL) {
247                    space = dataArea.getHeight();
248                }
249                else if (orientation == PlotOrientation.VERTICAL) {
250                    space = dataArea.getWidth();
251                }
252                double maxWidth = space * getMaximumBarWidth();
253                int columns = data.getColumnCount();
254                double categoryMargin = 0.0;
255                if (columns > 1) {
256                    categoryMargin = domainAxis.getCategoryMargin();
257                }
258    
259                double used = space * (1 - domainAxis.getLowerMargin()
260                                         - domainAxis.getUpperMargin()
261                                         - categoryMargin);
262                if (columns > 0) {
263                    state.setBarWidth(Math.min(used / columns, maxWidth));
264                }
265                else {
266                    state.setBarWidth(Math.min(used, maxWidth));
267                }
268            }
269    
270        }
271    
272        /**
273         * Returns a list containing the stacked values for the specified series
274         * in the given dataset, plus the supplied base value.
275         *
276         * @param dataset  the dataset (<code>null</code> not permitted).
277         * @param category  the category key (<code>null</code> not permitted).
278         * @param base  the base value.
279         * @param asPercentages  a flag that controls whether the values in the
280         *     list are converted to percentages of the total.
281         *
282         * @return The value list.
283         *
284         * @since 1.0.4
285         */
286        protected static List createStackedValueList(CategoryDataset dataset,
287                Comparable category, double base, boolean asPercentages) {
288    
289            List result = new ArrayList();
290            double posBase = base;
291            double negBase = base;
292            double total = 0.0;
293            if (asPercentages) {
294                total = DataUtilities.calculateColumnTotal(dataset,
295                        dataset.getColumnIndex(category));
296            }
297    
298            int baseIndex = -1;
299            int seriesCount = dataset.getRowCount();
300            for (int s = 0; s < seriesCount; s++) {
301                Number n = dataset.getValue(dataset.getRowKey(s), category);
302                if (n == null) {
303                    continue;
304                }
305                double v = n.doubleValue();
306                if (asPercentages) {
307                    v = v / total;
308                }
309                if (v >= 0.0) {
310                    if (baseIndex < 0) {
311                        result.add(new Object[] {null, new Double(base)});
312                        baseIndex = 0;
313                    }
314                    posBase = posBase + v;
315                    result.add(new Object[] {new Integer(s), new Double(posBase)});
316                }
317                else if (v < 0.0) {
318                    if (baseIndex < 0) {
319                        result.add(new Object[] {null, new Double(base)});
320                        baseIndex = 0;
321                    }
322                    negBase = negBase + v; // '+' because v is negative
323                    result.add(0, new Object[] {new Integer(-s - 1),
324                            new Double(negBase)});
325                    baseIndex++;
326                }
327            }
328            return result;
329    
330        }
331    
332        /**
333         * Draws the visual representation of one data item from the chart (in
334         * fact, this method does nothing until it reaches the last item for each
335         * category, at which point it draws all the items for that category).
336         *
337         * @param g2  the graphics device.
338         * @param state  the renderer state.
339         * @param dataArea  the plot area.
340         * @param plot  the plot.
341         * @param domainAxis  the domain (category) axis.
342         * @param rangeAxis  the range (value) axis.
343         * @param dataset  the data.
344         * @param row  the row index (zero-based).
345         * @param column  the column index (zero-based).
346         * @param pass  the pass index.
347         */
348        public void drawItem(Graphics2D g2,
349                             CategoryItemRendererState state,
350                             Rectangle2D dataArea,
351                             CategoryPlot plot,
352                             CategoryAxis domainAxis,
353                             ValueAxis rangeAxis,
354                             CategoryDataset dataset,
355                             int row,
356                             int column,
357                             int pass) {
358    
359            // wait till we are at the last item for the row then draw the
360            // whole stack at once
361            if (row < dataset.getRowCount() - 1) {
362                return;
363            }
364            Comparable category = dataset.getColumnKey(column);
365    
366            List values = createStackedValueList(dataset,
367                    dataset.getColumnKey(column), getBase(),
368                    this.renderAsPercentages);
369    
370            Rectangle2D adjusted = new Rectangle2D.Double(dataArea.getX(),
371                    dataArea.getY() + getYOffset(),
372                    dataArea.getWidth() - getXOffset(),
373                    dataArea.getHeight() - getYOffset());
374    
375    
376            PlotOrientation orientation = plot.getOrientation();
377    
378            // handle rendering separately for the two plot orientations...
379            if (orientation == PlotOrientation.HORIZONTAL) {
380                drawStackHorizontal(values, category, g2, state, adjusted, plot,
381                        domainAxis, rangeAxis, dataset);
382            }
383            else {
384                drawStackVertical(values, category, g2, state, adjusted, plot,
385                        domainAxis, rangeAxis, dataset);
386            }
387    
388        }
389    
390        /**
391         * Draws a stack of bars for one category, with a horizontal orientation.
392         *
393         * @param values  the value list.
394         * @param category  the category.
395         * @param g2  the graphics device.
396         * @param state  the state.
397         * @param dataArea  the data area (adjusted for the 3D effect).
398         * @param plot  the plot.
399         * @param domainAxis  the domain axis.
400         * @param rangeAxis  the range axis.
401         * @param dataset  the dataset.
402         *
403         * @since 1.0.4
404         */
405        protected void drawStackHorizontal(List values, Comparable category,
406                Graphics2D g2, CategoryItemRendererState state,
407                Rectangle2D dataArea, CategoryPlot plot,
408                CategoryAxis domainAxis, ValueAxis rangeAxis,
409                CategoryDataset dataset) {
410    
411            int column = dataset.getColumnIndex(category);
412            double barX0 = domainAxis.getCategoryMiddle(column,
413                    dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge())
414                    - state.getBarWidth() / 2.0;
415            double barW = state.getBarWidth();
416    
417            // a list to store the series index and bar region, so we can draw
418            // all the labels at the end...
419            List itemLabelList = new ArrayList();
420    
421            // draw the blocks
422            boolean inverted = rangeAxis.isInverted();
423            int blockCount = values.size() - 1;
424            for (int k = 0; k < blockCount; k++) {
425                int index = (inverted ? blockCount - k - 1 : k);
426                Object[] prev = (Object[]) values.get(index);
427                Object[] curr = (Object[]) values.get(index + 1);
428                int series = 0;
429                if (curr[0] == null) {
430                    series = -((Integer) prev[0]).intValue() - 1;
431                }
432                else {
433                    series = ((Integer) curr[0]).intValue();
434                    if (series < 0) {
435                        series = -((Integer) prev[0]).intValue() - 1;
436                    }
437                }
438                double v0 = ((Double) prev[1]).doubleValue();
439                double vv0 = rangeAxis.valueToJava2D(v0, dataArea,
440                        plot.getRangeAxisEdge());
441    
442                double v1 = ((Double) curr[1]).doubleValue();
443                double vv1 = rangeAxis.valueToJava2D(v1, dataArea,
444                        plot.getRangeAxisEdge());
445    
446                Shape[] faces = createHorizontalBlock(barX0, barW, vv0, vv1,
447                        inverted);
448                Paint fillPaint = getItemPaint(series, column);
449                Paint fillPaintDark = fillPaint;
450                if (fillPaintDark instanceof Color) {
451                    fillPaintDark = ((Color) fillPaint).darker();
452                }
453                boolean drawOutlines = isDrawBarOutline();
454                Paint outlinePaint = fillPaint;
455                if (drawOutlines) {
456                    outlinePaint = getItemOutlinePaint(series, column);
457                    g2.setStroke(getItemOutlineStroke(series, column));
458                }
459                for (int f = 0; f < 6; f++) {
460                    if (f == 5) {
461                        g2.setPaint(fillPaint);
462                    }
463                    else {
464                        g2.setPaint(fillPaintDark);
465                    }
466                    g2.fill(faces[f]);
467                    if (drawOutlines) {
468                        g2.setPaint(outlinePaint);
469                        g2.draw(faces[f]);
470                    }
471                }
472    
473                itemLabelList.add(new Object[] {new Integer(series),
474                        faces[5].getBounds2D(),
475                        BooleanUtilities.valueOf(v0 < getBase())});
476    
477                // add an item entity, if this information is being collected
478                EntityCollection entities = state.getEntityCollection();
479                if (entities != null) {
480                    addItemEntity(entities, dataset, series, column, faces[5]);
481                }
482    
483            }
484    
485            for (int i = 0; i < itemLabelList.size(); i++) {
486                Object[] record = (Object[]) itemLabelList.get(i);
487                int series = ((Integer) record[0]).intValue();
488                Rectangle2D bar = (Rectangle2D) record[1];
489                boolean neg = ((Boolean) record[2]).booleanValue();
490                CategoryItemLabelGenerator generator
491                        = getItemLabelGenerator(series, column);
492                if (generator != null && isItemLabelVisible(series, column)) {
493                    drawItemLabel(g2, dataset, series, column, plot, generator,
494                            bar, neg);
495                }
496    
497            }
498        }
499    
500        /**
501         * Creates an array of shapes representing the six sides of a block in a
502         * horizontal stack.
503         *
504         * @param x0  left edge of bar (in Java2D space).
505         * @param width  the width of the bar (in Java2D units).
506         * @param y0  the base of the block (in Java2D space).
507         * @param y1  the top of the block (in Java2D space).
508         * @param inverted  a flag indicating whether or not the block is inverted
509         *     (this changes the order of the faces of the block).
510         *
511         * @return The sides of the block.
512         */
513        private Shape[] createHorizontalBlock(double x0, double width, double y0,
514                double y1, boolean inverted) {
515            Shape[] result = new Shape[6];
516            Point2D p00 = new Point2D.Double(y0, x0);
517            Point2D p01 = new Point2D.Double(y0, x0 + width);
518            Point2D p02 = new Point2D.Double(p01.getX() + getXOffset(),
519                    p01.getY() - getYOffset());
520            Point2D p03 = new Point2D.Double(p00.getX() + getXOffset(),
521                    p00.getY() - getYOffset());
522    
523            Point2D p0 = new Point2D.Double(y1, x0);
524            Point2D p1 = new Point2D.Double(y1, x0 + width);
525            Point2D p2 = new Point2D.Double(p1.getX() + getXOffset(),
526                    p1.getY() - getYOffset());
527            Point2D p3 = new Point2D.Double(p0.getX() + getXOffset(),
528                    p0.getY() - getYOffset());
529    
530            GeneralPath bottom = new GeneralPath();
531            bottom.moveTo((float) p1.getX(), (float) p1.getY());
532            bottom.lineTo((float) p01.getX(), (float) p01.getY());
533            bottom.lineTo((float) p02.getX(), (float) p02.getY());
534            bottom.lineTo((float) p2.getX(), (float) p2.getY());
535            bottom.closePath();
536    
537            GeneralPath top = new GeneralPath();
538            top.moveTo((float) p0.getX(), (float) p0.getY());
539            top.lineTo((float) p00.getX(), (float) p00.getY());
540            top.lineTo((float) p03.getX(), (float) p03.getY());
541            top.lineTo((float) p3.getX(), (float) p3.getY());
542            top.closePath();
543    
544            GeneralPath back = new GeneralPath();
545            back.moveTo((float) p2.getX(), (float) p2.getY());
546            back.lineTo((float) p02.getX(), (float) p02.getY());
547            back.lineTo((float) p03.getX(), (float) p03.getY());
548            back.lineTo((float) p3.getX(), (float) p3.getY());
549            back.closePath();
550    
551            GeneralPath front = new GeneralPath();
552            front.moveTo((float) p0.getX(), (float) p0.getY());
553            front.lineTo((float) p1.getX(), (float) p1.getY());
554            front.lineTo((float) p01.getX(), (float) p01.getY());
555            front.lineTo((float) p00.getX(), (float) p00.getY());
556            front.closePath();
557    
558            GeneralPath left = new GeneralPath();
559            left.moveTo((float) p0.getX(), (float) p0.getY());
560            left.lineTo((float) p1.getX(), (float) p1.getY());
561            left.lineTo((float) p2.getX(), (float) p2.getY());
562            left.lineTo((float) p3.getX(), (float) p3.getY());
563            left.closePath();
564    
565            GeneralPath right = new GeneralPath();
566            right.moveTo((float) p00.getX(), (float) p00.getY());
567            right.lineTo((float) p01.getX(), (float) p01.getY());
568            right.lineTo((float) p02.getX(), (float) p02.getY());
569            right.lineTo((float) p03.getX(), (float) p03.getY());
570            right.closePath();
571            result[0] = bottom;
572            result[1] = back;
573            if (inverted) {
574                result[2] = right;
575                result[3] = left;
576            }
577            else {
578                result[2] = left;
579                result[3] = right;
580            }
581            result[4] = top;
582            result[5] = front;
583            return result;
584        }
585    
586        /**
587         * Draws a stack of bars for one category, with a vertical orientation.
588         *
589         * @param values  the value list.
590         * @param category  the category.
591         * @param g2  the graphics device.
592         * @param state  the state.
593         * @param dataArea  the data area (adjusted for the 3D effect).
594         * @param plot  the plot.
595         * @param domainAxis  the domain axis.
596         * @param rangeAxis  the range axis.
597         * @param dataset  the dataset.
598         *
599         * @since 1.0.4
600         */
601        protected void drawStackVertical(List values, Comparable category,
602                Graphics2D g2, CategoryItemRendererState state,
603                Rectangle2D dataArea, CategoryPlot plot,
604                CategoryAxis domainAxis, ValueAxis rangeAxis,
605                CategoryDataset dataset) {
606    
607            int column = dataset.getColumnIndex(category);
608            double barX0 = domainAxis.getCategoryMiddle(column,
609                    dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge())
610                    - state.getBarWidth() / 2.0;
611            double barW = state.getBarWidth();
612    
613            // a list to store the series index and bar region, so we can draw
614            // all the labels at the end...
615            List itemLabelList = new ArrayList();
616    
617            // draw the blocks
618            boolean inverted = rangeAxis.isInverted();
619            int blockCount = values.size() - 1;
620            for (int k = 0; k < blockCount; k++) {
621                int index = (inverted ? blockCount - k - 1 : k);
622                Object[] prev = (Object[]) values.get(index);
623                Object[] curr = (Object[]) values.get(index + 1);
624                int series = 0;
625                if (curr[0] == null) {
626                    series = -((Integer) prev[0]).intValue() - 1;
627                }
628                else {
629                    series = ((Integer) curr[0]).intValue();
630                    if (series < 0) {
631                        series = -((Integer) prev[0]).intValue() - 1;
632                    }
633                }
634                double v0 = ((Double) prev[1]).doubleValue();
635                double vv0 = rangeAxis.valueToJava2D(v0, dataArea,
636                        plot.getRangeAxisEdge());
637    
638                double v1 = ((Double) curr[1]).doubleValue();
639                double vv1 = rangeAxis.valueToJava2D(v1, dataArea,
640                        plot.getRangeAxisEdge());
641    
642                Shape[] faces = createVerticalBlock(barX0, barW, vv0, vv1,
643                        inverted);
644                Paint fillPaint = getItemPaint(series, column);
645                Paint fillPaintDark = fillPaint;
646                if (fillPaintDark instanceof Color) {
647                    fillPaintDark = ((Color) fillPaint).darker();
648                }
649                boolean drawOutlines = isDrawBarOutline();
650                Paint outlinePaint = fillPaint;
651                if (drawOutlines) {
652                    outlinePaint = getItemOutlinePaint(series, column);
653                    g2.setStroke(getItemOutlineStroke(series, column));
654                }
655    
656                for (int f = 0; f < 6; f++) {
657                    if (f == 5) {
658                        g2.setPaint(fillPaint);
659                    }
660                    else {
661                        g2.setPaint(fillPaintDark);
662                    }
663                    g2.fill(faces[f]);
664                    if (drawOutlines) {
665                        g2.setPaint(outlinePaint);
666                        g2.draw(faces[f]);
667                    }
668                }
669    
670                itemLabelList.add(new Object[] {new Integer(series),
671                        faces[5].getBounds2D(),
672                        BooleanUtilities.valueOf(v0 < getBase())});
673    
674                // add an item entity, if this information is being collected
675                EntityCollection entities = state.getEntityCollection();
676                if (entities != null) {
677                    addItemEntity(entities, dataset, series, column, faces[5]);
678                }
679    
680            }
681    
682            for (int i = 0; i < itemLabelList.size(); i++) {
683                Object[] record = (Object[]) itemLabelList.get(i);
684                int series = ((Integer) record[0]).intValue();
685                Rectangle2D bar = (Rectangle2D) record[1];
686                boolean neg = ((Boolean) record[2]).booleanValue();
687                CategoryItemLabelGenerator generator
688                        = getItemLabelGenerator(series, column);
689                if (generator != null && isItemLabelVisible(series, column)) {
690                    drawItemLabel(g2, dataset, series, column, plot, generator,
691                            bar, neg);
692                }
693    
694            }
695        }
696    
697        /**
698         * Creates an array of shapes representing the six sides of a block in a
699         * vertical stack.
700         *
701         * @param x0  left edge of bar (in Java2D space).
702         * @param width  the width of the bar (in Java2D units).
703         * @param y0  the base of the block (in Java2D space).
704         * @param y1  the top of the block (in Java2D space).
705         * @param inverted  a flag indicating whether or not the block is inverted
706         *     (this changes the order of the faces of the block).
707         *
708         * @return The sides of the block.
709         */
710        private Shape[] createVerticalBlock(double x0, double width, double y0,
711                double y1, boolean inverted) {
712            Shape[] result = new Shape[6];
713            Point2D p00 = new Point2D.Double(x0, y0);
714            Point2D p01 = new Point2D.Double(x0 + width, y0);
715            Point2D p02 = new Point2D.Double(p01.getX() + getXOffset(),
716                    p01.getY() - getYOffset());
717            Point2D p03 = new Point2D.Double(p00.getX() + getXOffset(),
718                    p00.getY() - getYOffset());
719    
720    
721            Point2D p0 = new Point2D.Double(x0, y1);
722            Point2D p1 = new Point2D.Double(x0 + width, y1);
723            Point2D p2 = new Point2D.Double(p1.getX() + getXOffset(),
724                    p1.getY() - getYOffset());
725            Point2D p3 = new Point2D.Double(p0.getX() + getXOffset(),
726                    p0.getY() - getYOffset());
727    
728            GeneralPath right = new GeneralPath();
729            right.moveTo((float) p1.getX(), (float) p1.getY());
730            right.lineTo((float) p01.getX(), (float) p01.getY());
731            right.lineTo((float) p02.getX(), (float) p02.getY());
732            right.lineTo((float) p2.getX(), (float) p2.getY());
733            right.closePath();
734    
735            GeneralPath left = new GeneralPath();
736            left.moveTo((float) p0.getX(), (float) p0.getY());
737            left.lineTo((float) p00.getX(), (float) p00.getY());
738            left.lineTo((float) p03.getX(), (float) p03.getY());
739            left.lineTo((float) p3.getX(), (float) p3.getY());
740            left.closePath();
741    
742            GeneralPath back = new GeneralPath();
743            back.moveTo((float) p2.getX(), (float) p2.getY());
744            back.lineTo((float) p02.getX(), (float) p02.getY());
745            back.lineTo((float) p03.getX(), (float) p03.getY());
746            back.lineTo((float) p3.getX(), (float) p3.getY());
747            back.closePath();
748    
749            GeneralPath front = new GeneralPath();
750            front.moveTo((float) p0.getX(), (float) p0.getY());
751            front.lineTo((float) p1.getX(), (float) p1.getY());
752            front.lineTo((float) p01.getX(), (float) p01.getY());
753            front.lineTo((float) p00.getX(), (float) p00.getY());
754            front.closePath();
755    
756            GeneralPath top = new GeneralPath();
757            top.moveTo((float) p0.getX(), (float) p0.getY());
758            top.lineTo((float) p1.getX(), (float) p1.getY());
759            top.lineTo((float) p2.getX(), (float) p2.getY());
760            top.lineTo((float) p3.getX(), (float) p3.getY());
761            top.closePath();
762    
763            GeneralPath bottom = new GeneralPath();
764            bottom.moveTo((float) p00.getX(), (float) p00.getY());
765            bottom.lineTo((float) p01.getX(), (float) p01.getY());
766            bottom.lineTo((float) p02.getX(), (float) p02.getY());
767            bottom.lineTo((float) p03.getX(), (float) p03.getY());
768            bottom.closePath();
769    
770            result[0] = bottom;
771            result[1] = back;
772            result[2] = left;
773            result[3] = right;
774            result[4] = top;
775            result[5] = front;
776            if (inverted) {
777                result[0] = top;
778                result[4] = bottom;
779            }
780            return result;
781        }
782    
783        /**
784         * Tests this renderer for equality with an arbitrary object.
785         *
786         * @param obj  the object (<code>null</code> permitted).
787         *
788         * @return A boolean.
789         */
790        public boolean equals(Object obj) {
791            if (obj == this) {
792                return true;
793            }
794            if (!(obj instanceof StackedBarRenderer3D)) {
795                return false;
796            }
797            if (!super.equals(obj)) {
798                return false;
799            }
800            StackedBarRenderer3D that = (StackedBarRenderer3D) obj;
801            if (this.renderAsPercentages != that.getRenderAsPercentages()) {
802                return false;
803            }
804            return true;
805        }
806    
807    }