001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * IntervalBarRenderer.java
029 * ------------------------
030 * (C) Copyright 2002-2016, by Jeremy Bowman.
031 *
032 * Original Author:  Jeremy Bowman;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *                   Christian W. Zuckschwerdt;
035 *                   Peter Kolb (patch 2497611, 2791407);
036 *
037 * Changes
038 * -------
039 * 29-Apr-2002 : Version 1, contributed by Jeremy Bowman (DG);
040 * 11-May-2002 : Use CategoryPlot.getLabelsVisible() (JB);
041 * 29-May-2002 : Added constructors (DG);
042 * 26-Jun-2002 : Added axis to initialise method (DG);
043 * 20-Sep-2002 : Added basic support for chart entities (DG);
044 * 24-Oct-2002 : Amendments for changes in CategoryDataset interface and
045 *               CategoryToolTipGenerator interface (DG);
046 * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
047 * 25-Mar-2003 : Implemented Serializable (DG);
048 * 30-Jul-2003 : Modified entity constructor (CZ);
049 * 19-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
050 * 08-Sep-2003 : Added checks for null values (DG);
051 * 07-Oct-2003 : Added renderer state (DG);
052 * 21-Oct-2003 : Bar width moved into renderer state (DG);
053 * 23-Dec-2003 : Removed the deprecated MultiIntervalCategoryDataset
054 *               interface (DG);
055 * 05-Nov-2004 : Modified drawItem() signature (DG);
056 * 20-Apr-2005 : Renamed CategoryLabelGenerator
057 *               --> CategoryItemLabelGenerator (DG);
058 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
059 * 24-Jun-2008 : Added new barPainter mechanism (DG);
060 * 07-Oct-2008 : Override equals() method to fix minor bug (DG);
061 * 14-Jan-2009 : Added support for seriesVisible flags (PK);
062 * 16-May-2009 : The findRangeBounds() method needs to include the dataset
063 *               interval (DG);
064 * 19-May-2009 : Fixed FindBugs warnings, patch by Michal Wozniak (DG);
065 * 30-Oct-2011 : Fixed alignment when setMaximumBarWidth is applied (DG);
066 * 24-Aug-2014 : Add element hinting for JFreeSVG (DG);
067 *
068 */
069
070package org.jfree.chart.renderer.category;
071
072import java.awt.Graphics2D;
073import java.awt.geom.Rectangle2D;
074
075import org.jfree.chart.axis.CategoryAxis;
076import org.jfree.chart.axis.ValueAxis;
077import org.jfree.chart.entity.EntityCollection;
078import org.jfree.chart.labels.CategoryItemLabelGenerator;
079import org.jfree.chart.plot.CategoryPlot;
080import org.jfree.chart.plot.PlotOrientation;
081import org.jfree.chart.ui.RectangleEdge;
082import org.jfree.data.Range;
083import org.jfree.data.category.CategoryDataset;
084import org.jfree.data.category.IntervalCategoryDataset;
085
086/**
087 * A renderer that handles the drawing of bars for a bar plot where
088 * each bar has a high and low value.  This renderer is for use with the
089 * {@link CategoryPlot} class.  The example shown here is generated by the
090 * {@code IntervalBarChartDemo1.java} program included in the JFreeChart
091 * Demo Collection:
092 * <br><br>
093 * <img src="../../../../../images/IntervalBarRendererSample.png"
094 * alt="IntervalBarRendererSample.png">
095 */
096public class IntervalBarRenderer extends BarRenderer {
097
098    /** For serialization. */
099    private static final long serialVersionUID = -5068857361615528725L;
100
101    /**
102     * Constructs a new renderer.
103     */
104    public IntervalBarRenderer() {
105        super();
106    }
107
108    /**
109     * Returns the range of values from the specified dataset.  For this
110     * renderer, this is equivalent to calling
111     * {@code findRangeBounds(dataset, true)}.
112     *
113     * @param dataset  the dataset ({@code null} permitted).
114     *
115     * @return The range (or {@code null} if the dataset is
116     *         {@code null} or empty).
117     */
118    @Override
119    public Range findRangeBounds(CategoryDataset dataset) {
120        return findRangeBounds(dataset, true);
121    }
122
123    /**
124     * Draws the bar for a single (series, category) data item.
125     *
126     * @param g2  the graphics device.
127     * @param state  the renderer state.
128     * @param dataArea  the data area.
129     * @param plot  the plot.
130     * @param domainAxis  the domain axis.
131     * @param rangeAxis  the range axis.
132     * @param dataset  the dataset.
133     * @param row  the row index (zero-based).
134     * @param column  the column index (zero-based).
135     * @param pass  the pass index.
136     */
137    @Override
138    public void drawItem(Graphics2D g2, CategoryItemRendererState state,
139            Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
140            ValueAxis rangeAxis, CategoryDataset dataset, int row, int column,
141            int pass) {
142
143         if (dataset instanceof IntervalCategoryDataset) {
144             IntervalCategoryDataset d = (IntervalCategoryDataset) dataset;
145             drawInterval(g2, state, dataArea, plot, domainAxis, rangeAxis,
146                     d, row, column);
147         } else {
148             super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis,
149                     dataset, row, column, pass);
150         }
151
152     }
153
154     /**
155      * Draws a single interval.
156      *
157      * @param g2  the graphics device.
158      * @param state  the renderer state.
159      * @param dataArea  the data plot area.
160      * @param plot  the plot.
161      * @param domainAxis  the domain axis.
162      * @param rangeAxis  the range axis.
163      * @param dataset  the data.
164      * @param row  the row index (zero-based).
165      * @param column  the column index (zero-based).
166      */
167     protected void drawInterval(Graphics2D g2, CategoryItemRendererState state,
168            Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
169            ValueAxis rangeAxis, IntervalCategoryDataset dataset, int row,
170                                 int column) {
171
172        int visibleRow = state.getVisibleSeriesIndex(row);
173        if (visibleRow < 0) {
174            return;
175        }
176
177        PlotOrientation orientation = plot.getOrientation();
178        double rectX = 0.0;
179        double rectY = 0.0;
180
181        RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
182
183        // Y0
184        Number value0 = dataset.getEndValue(row, column);
185        if (value0 == null) {
186            return;
187        }
188        double java2dValue0 = rangeAxis.valueToJava2D(value0.doubleValue(),
189                dataArea, rangeAxisLocation);
190
191        // Y1
192        Number value1 = dataset.getStartValue(row, column);
193        if (value1 == null) {
194            return;
195        }
196        double java2dValue1 = rangeAxis.valueToJava2D(
197                value1.doubleValue(), dataArea, rangeAxisLocation);
198
199        if (java2dValue1 < java2dValue0) {
200            double temp = java2dValue1;
201            java2dValue1 = java2dValue0;
202            java2dValue0 = temp;
203        }
204
205        // BAR WIDTH
206        double rectWidth = state.getBarWidth();
207
208        // BAR HEIGHT
209        double rectHeight = Math.abs(java2dValue1 - java2dValue0);
210
211        RectangleEdge barBase = RectangleEdge.LEFT;
212        if (orientation == PlotOrientation.HORIZONTAL) {
213            // BAR Y
214            rectX = java2dValue0;
215            rectY = calculateBarW0(getPlot(), orientation, dataArea, 
216                    domainAxis, state, visibleRow, column);
217            rectHeight = state.getBarWidth();
218            rectWidth = Math.abs(java2dValue1 - java2dValue0);
219            barBase = RectangleEdge.LEFT;
220        } else if (orientation.isVertical()) {
221            // BAR X
222            rectX = calculateBarW0(getPlot(), orientation, dataArea, 
223                    domainAxis, state, visibleRow, column);
224            rectY = java2dValue0;
225            barBase = RectangleEdge.BOTTOM;
226        }
227        Rectangle2D bar = new Rectangle2D.Double(rectX, rectY, rectWidth,
228                rectHeight);
229        BarPainter painter = getBarPainter();
230        if (state.getElementHinting()) {
231            beginElementGroup(g2, dataset.getRowKey(row), 
232                    dataset.getColumnKey(column));
233        }
234        if (getShadowsVisible()) {
235            painter.paintBarShadow(g2, this, row, column, bar, barBase, false);
236        }
237        getBarPainter().paintBar(g2, this, row, column, bar, barBase);
238        if (state.getElementHinting()) {
239            endElementGroup(g2);
240        }
241
242        CategoryItemLabelGenerator generator = getItemLabelGenerator(row,
243                column);
244        if (generator != null && isItemLabelVisible(row, column)) {
245            drawItemLabel(g2, dataset, row, column, plot, generator, bar,
246                    false);
247        }
248
249        // add an item entity, if this information is being collected
250        EntityCollection entities = state.getEntityCollection();
251        if (entities != null) {
252            addItemEntity(entities, dataset, row, column, bar);
253        }
254
255    }
256
257    /**
258     * Tests this renderer for equality with an arbitrary object.
259     *
260     * @param obj  the object ({@code null} permitted).
261     *
262     * @return A boolean.
263     */
264    @Override
265    public boolean equals(Object obj) {
266        if (obj == this) {
267            return true;
268        }
269        if (!(obj instanceof IntervalBarRenderer)) {
270            return false;
271        }
272        // there are no fields to check
273        return super.equals(obj);
274    }
275
276}