    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, 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     * YIntervalRenderer.java
    029     * ----------------------
    030     * (C) Copyright 2002-2007, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: YIntervalRenderer.java,v 1.7.2.2 2007/02/20 15:35:54 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 05-Nov-2002 : Version 1 (DG);
    040     * 25-Mar-2003 : Implemented Serializable (DG);
    041     * 01-May-2003 : Modified drawItem() method signature (DG);
    042     * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
    043     * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
    044     * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
    045     * 27-Sep-2004 : Access double values from dataset (DG);
    046     * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
    047     *
    048     */
    049
    050    package org.jfree.chart.renderer.xy;
    051
    052    import java.awt.Graphics2D;
    053    import java.awt.Paint;
    054    import java.awt.Shape;
    055    import java.awt.Stroke;
    056    import java.awt.geom.Line2D;
    057    import java.awt.geom.Rectangle2D;
    058    import java.io.Serializable;
    059
    060    import org.jfree.chart.axis.ValueAxis;
    061    import org.jfree.chart.entity.EntityCollection;
    062    import org.jfree.chart.entity.XYItemEntity;
    063    import org.jfree.chart.labels.XYToolTipGenerator;
    064    import org.jfree.chart.plot.CrosshairState;
    065    import org.jfree.chart.plot.PlotOrientation;
    066    import org.jfree.chart.plot.PlotRenderingInfo;
    067    import org.jfree.chart.plot.XYPlot;
    068    import org.jfree.data.xy.IntervalXYDataset;
    069    import org.jfree.data.xy.XYDataset;
    070    import org.jfree.ui.RectangleEdge;
    071    import org.jfree.util.PublicCloneable;
    072    import org.jfree.util.ShapeUtilities;
    073
    074    /**
    075     * A renderer that draws a line connecting the start and end Y values for an
    076     * {@link XYPlot}.
    077     */
    078    public class YIntervalRenderer extends AbstractXYItemRenderer
    079                                   implements XYItemRenderer,
    080                                              Cloneable,
    081                                              PublicCloneable,
    082                                              Serializable {
    083
    084        private static final long serialVersionUID = -2951586537224143260L;
    085
    086        /**
    087         * The default constructor.
    088         */
    089        public YIntervalRenderer() {
    090            super();
    091        }
    092
    093        /**
    094         * Draws the visual representation of a single data item.
    095         *
    096         * @param g2  the graphics device.
    097         * @param state  the renderer state.
    098         * @param dataArea  the area within which the plot is being drawn.
    099         * @param info  collects information about the drawing.
    100         * @param plot  the plot (can be used to obtain standard color
    101         *              information etc).
    102         * @param domainAxis  the domain axis.
    103         * @param rangeAxis  the range axis.
    104         * @param dataset  the dataset.
    105         * @param series  the series index (zero-based).
    106         * @param item  the item index (zero-based).
    107         * @param crosshairState  crosshair information for the plot
    108         *                        (<code>null</code> permitted).
    109         * @param pass  the pass index (ignored here).
    110         */
    111        public void drawItem(Graphics2D g2,
    112                             XYItemRendererState state,
    113                             Rectangle2D dataArea,
    114                             PlotRenderingInfo info,
    115                             XYPlot plot,
    116                             ValueAxis domainAxis,
    117                             ValueAxis rangeAxis,
    118                             XYDataset dataset,
    119                             int series,
    120                             int item,
    121                             CrosshairState crosshairState,
    122                             int pass) {
    123
    124            // setup for collecting optional entity info...
    125            Shape entityArea = null;
    126            EntityCollection entities = null;
    127            if (info != null) {
    128                entities = info.getOwner().getEntityCollection();
    129            }
    130
    131            IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
    132
    133            double x = intervalDataset.getXValue(series, item);
    134            double yLow   = intervalDataset.getStartYValue(series, item);
    135            double yHigh  = intervalDataset.getEndYValue(series, item);
    136
    137            RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    138            RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    139
    140            double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
    141            double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation);
    142            double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation);
    143
    144            Paint p = getItemPaint(series, item);
    145            Stroke s = getItemStroke(series, item);
    146
    147            Line2D line = null;
    148            Shape shape = getItemShape(series, item);
    149            Shape top = null;
    150            Shape bottom = null;
    151            PlotOrientation orientation = plot.getOrientation();
    152            if (orientation == PlotOrientation.HORIZONTAL) {
    153                line = new Line2D.Double(yyLow, xx, yyHigh, xx);
    154                top = ShapeUtilities.createTranslatedShape(shape, yyHigh, xx);
    155                bottom = ShapeUtilities.createTranslatedShape(shape, yyLow, xx);
    156            }
    157            else if (orientation == PlotOrientation.VERTICAL) {
    158                line = new Line2D.Double(xx, yyLow, xx, yyHigh);
    159                top = ShapeUtilities.createTranslatedShape(shape, xx, yyHigh);
    160                bottom = ShapeUtilities.createTranslatedShape(shape, xx, yyLow);
    161            }
    162            g2.setPaint(p);
    163            g2.setStroke(s);
    164            g2.draw(line);
    165
    166            g2.fill(top);
    167            g2.fill(bottom);
    168
    169            // add an entity for the item...
    170            if (entities != null) {
    171                if (entityArea == null) {
    172                    entityArea = line.getBounds();
    173                }
    174                String tip = null;
    175                XYToolTipGenerator generator = getToolTipGenerator(series, item);
    176                if (generator != null) {
    177                    tip = generator.generateToolTip(dataset, series, item);
    178                }
    179                String url = null;
    180                if (getURLGenerator() != null) {
    181                    url = getURLGenerator().generateURL(dataset, series, item);
    182                }
    183                XYItemEntity entity = new XYItemEntity(entityArea, dataset, series,
    184                        item, tip, url);
    185                entities.add(entity);
    186            }
    187
    188        }
    189
    190        /**
    191         * Returns a clone of the renderer.
    192         *
    193         * @return A clone.
    194         *
    195         * @throws CloneNotSupportedException  if the renderer cannot be cloned.
    196         */
    197        public Object clone() throws CloneNotSupportedException {
    198            return super.clone();
    199        }
    200
    201    }