Source for org.jfree.chart.renderer.xy.YIntervalRenderer

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ----------------------
  28:  * YIntervalRenderer.java
  29:  * ----------------------
  30:  * (C) Copyright 2002-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 05-Nov-2002 : Version 1 (DG);
  38:  * 25-Mar-2003 : Implemented Serializable (DG);
  39:  * 01-May-2003 : Modified drawItem() method signature (DG);
  40:  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  41:  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  42:  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
  43:  * 27-Sep-2004 : Access double values from dataset (DG);
  44:  * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
  45:  * 
  46:  */
  47: 
  48: package org.jfree.chart.renderer.xy;
  49: 
  50: import java.awt.Graphics2D;
  51: import java.awt.Paint;
  52: import java.awt.Shape;
  53: import java.awt.Stroke;
  54: import java.awt.geom.Line2D;
  55: import java.awt.geom.Rectangle2D;
  56: import java.io.Serializable;
  57: 
  58: import org.jfree.chart.axis.ValueAxis;
  59: import org.jfree.chart.entity.EntityCollection;
  60: import org.jfree.chart.entity.XYItemEntity;
  61: import org.jfree.chart.labels.XYToolTipGenerator;
  62: import org.jfree.chart.plot.CrosshairState;
  63: import org.jfree.chart.plot.PlotOrientation;
  64: import org.jfree.chart.plot.PlotRenderingInfo;
  65: import org.jfree.chart.plot.XYPlot;
  66: import org.jfree.data.xy.IntervalXYDataset;
  67: import org.jfree.data.xy.XYDataset;
  68: import org.jfree.ui.RectangleEdge;
  69: import org.jfree.util.PublicCloneable;
  70: import org.jfree.util.ShapeUtilities;
  71: 
  72: /**
  73:  * A renderer that draws a line connecting the start and end Y values for an 
  74:  * {@link XYPlot}.
  75:  */
  76: public class YIntervalRenderer extends AbstractXYItemRenderer 
  77:                                implements XYItemRenderer, 
  78:                                           Cloneable,
  79:                                           PublicCloneable,
  80:                                           Serializable {
  81: 
  82:     /** For serialization. */
  83:     private static final long serialVersionUID = -2951586537224143260L;
  84:     
  85:     /**
  86:      * The default constructor.
  87:      */
  88:     public YIntervalRenderer() {
  89:         super();
  90:     }
  91: 
  92:     /**
  93:      * Draws the visual representation of a single data item.
  94:      *
  95:      * @param g2  the graphics device.
  96:      * @param state  the renderer state.
  97:      * @param dataArea  the area within which the plot is being drawn.
  98:      * @param info  collects information about the drawing.
  99:      * @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: }