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