    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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     * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    025     * Other names may be trademarks of their respective owners.]
    026     *
    027     * ------------------------------
    028     * CategoryItemRendererState.java
    029     * ------------------------------
    030     * (C) Copyright 2003-2009, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   Peter Kolb (patch 2497611);
    034     *
    035     * Changes (since 20-Oct-2003):
    036     * ----------------------------
    037     * 20-Oct-2003 : Added series running total (DG);
    038     * ------------- JFREECHART 1.0.x ---------------------------------------------
    039     * 01-Dec-2006 : Updated API docs (DG);
    040     * 26-Jun-2008 : Added CrosshairState (DG);
    041     * 14-Jan-2009 : Added visibleSeries[] array (PK);
    042     * 04-Feb-2009 : Added getVisibleSeriesArray() method (DG);
    043     *
    044     */
    045
    046    package org.jfree.chart.renderer.category;
    047
    048    import org.jfree.chart.plot.CategoryCrosshairState;
    049    import org.jfree.chart.plot.PlotRenderingInfo;
    050    import org.jfree.chart.renderer.RendererState;
    051
    052    /**
    053     * An object that retains temporary state information for a
    054     * {@link CategoryItemRenderer}.
    055     */
    056    public class CategoryItemRendererState extends RendererState {
    057
    058        /** The bar width. */
    059        private double barWidth;
    060
    061        /** The series running total. */
    062        private double seriesRunningTotal;
    063
    064        /** The array with the indices of the visible series.*/
    065        private int[] visibleSeries;
    066
    067        /**
    068         * State information for crosshairs in the plot (this is updated by the
    069         * renderer, but may be passed to several renderers in one chart).
    070         *
    071         * @since 1.0.11
    072         */
    073        private CategoryCrosshairState crosshairState;
    074
    075        /**
    076         * Creates a new object for recording temporary state information for a
    077         * renderer.
    078         *
    079         * @param info  the plot rendering info (<code>null</code> permitted).
    080         */
    081        public CategoryItemRendererState(PlotRenderingInfo info) {
    082            super(info);
    083            this.barWidth = 0.0;
    084            this.seriesRunningTotal = 0.0;
    085        }
    086
    087        /**
    088         * Returns the bar width.
    089         *
    090         * @return The bar width.
    091         *
    092         * @see #setBarWidth(double)
    093         */
    094        public double getBarWidth() {
    095            return this.barWidth;
    096        }
    097
    098        /**
    099         * Sets the bar width.  The renderer calculates this value and stores it
    100         * here - it is not intended that users can manually set the bar width.
    101         *
    102         * @param width  the width.
    103         *
    104         * @see #getBarWidth()
    105         */
    106        public void setBarWidth(double width) {
    107            this.barWidth = width;
    108        }
    109
    110        /**
    111         * Returns the series running total.
    112         *
    113         * @return The running total.
    114         *
    115         * @see #setSeriesRunningTotal(double)
    116         */
    117        public double getSeriesRunningTotal() {
    118            return this.seriesRunningTotal;
    119        }
    120
    121        /**
    122         * Sets the series running total (this method is intended for the use of
    123         * the renderer only).
    124         *
    125         * @param total  the new total.
    126         *
    127         * @see #getSeriesRunningTotal()
    128         */
    129        void setSeriesRunningTotal(double total) {
    130            this.seriesRunningTotal = total;
    131        }
    132
    133        /**
    134         * Returns the crosshair state, if any.
    135         *
    136         * @return The crosshair state (possibly <code>null</code>).
    137         *
    138         * @since 1.0.11
    139         *
    140         * @see #setCrosshairState(CategoryCrosshairState)
    141         */
    142        public CategoryCrosshairState getCrosshairState() {
    143            return this.crosshairState;
    144        }
    145
    146        /**
    147         * Sets the crosshair state.
    148         *
    149         * @param state  the new state (<code>null</code> permitted).
    150         *
    151         * @since 1.0.11
    152         *
    153         * @see #getCrosshairState()
    154         */
    155        public void setCrosshairState(CategoryCrosshairState state) {
    156            this.crosshairState = state;
    157        }
    158
    159        /**
    160         * Returns the index of the row relative to the visible rows.  If no
    161         * visible rows have been specified, the original row index is returned.
    162         * If the row index is not included in the array of visible rows,
    163         * -1 is returned.
    164         *
    165         * @param rowIndex  the row index.
    166         *
    167         * @return The new row index or -1.
    168         *
    169         * @since 1.0.13
    170         */
    171        public int getVisibleSeriesIndex(int rowIndex) {
    172            if (this.visibleSeries == null) {
    173                return rowIndex;
    174            }
    175            int index = -1;
    176            for (int vRow = 0; vRow < this.visibleSeries.length; vRow++) {
    177                if (this.visibleSeries[vRow] == rowIndex) {
    178                    index = vRow;
    179                    break;
    180                }
    181            }
    182            return index;
    183        }
    184
    185        /**
    186         * Returns the number of visible series or -1 if no visible series have
    187         * been specified.
    188         *
    189         * @return The number or -1.
    190         *
    191         * @since 1.0.13
    192         */
    193        public int getVisibleSeriesCount() {
    194            if (this.visibleSeries == null) {
    195                return -1;
    196            }
    197            return this.visibleSeries.length;
    198        }
    199
    200        /**
    201         * Returns a copy of the visible series array.
    202         *
    203         * @return The visible series array (possibly <code>null</code>).
    204         *
    205         * @since 1.0.13
    206         */
    207        public int[] getVisibleSeriesArray() {
    208            if (this.visibleSeries == null) {
    209                return null;
    210            }
    211            int[] result = new int[this.visibleSeries.length];
    212            System.arraycopy(this.visibleSeries, 0, result, 0,
    213                    this.visibleSeries.length);
    214            return result;
    215        }
    216
    217        /**
    218         * Sets an array with the indices of the visible rows.
    219         *
    220         * @param visibleSeries the array (<code>null</code> permitted).
    221         *
    222         * @since 1.0.13
    223         */
    224        public void setVisibleSeriesArray(int[] visibleSeries) {
    225            this.visibleSeries = visibleSeries;
    226        }
    227
    228    }