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:  * $Id: YIntervalRenderer.java,v 1.7.2.2 2007/02/20 15:35:54 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 05-Nov-2002 : Version 1 (DG);
  40:  * 25-Mar-2003 : Implemented Serializable (DG);
  41:  * 01-May-2003 : Modified drawItem() method signature (DG);
  42:  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  43:  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  44:  * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
  45:  * 27-Sep-2004 : Access double values from dataset (DG);
  46:  * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
  47:  * 
  48:  */
  49: 
  50: package org.jfree.chart.renderer.xy;
  51: 
  52: import java.awt.Graphics2D;
  53: import java.awt.Paint;
  54: import java.awt.Shape;
  55: import java.awt.Stroke;
  56: import java.awt.geom.Line2D;
  57: import java.awt.geom.Rectangle2D;
  58: import java.io.Serializable;
  59: 
  60: import org.jfree.chart.axis.ValueAxis;
  61: import org.jfree.chart.entity.EntityCollection;
  62: import org.jfree.chart.entity.XYItemEntity;
  63: import org.jfree.chart.labels.XYToolTipGenerator;
  64: import org.jfree.chart.plot.CrosshairState;
  65: import org.jfree.chart.plot.PlotOrientation;
  66: import org.jfree.chart.plot.PlotRenderingInfo;
  67: import org.jfree.chart.plot.XYPlot;
  68: import org.jfree.data.xy.IntervalXYDataset;
  69: import org.jfree.data.xy.XYDataset;
  70: import org.jfree.ui.RectangleEdge;
  71: import org.jfree.util.PublicCloneable;
  72: import org.jfree.util.ShapeUtilities;
  73: 
  74: /**
  75:  * A renderer that draws a line connecting the start and end Y values for an 
  76:  * {@link XYPlot}.
  77:  */
  78: public class YIntervalRenderer extends AbstractXYItemRenderer 
  79:                                implements XYItemRenderer, 
  80:                                           Cloneable,
  81:                                           PublicCloneable,
  82:                                           Serializable {
  83: 
  84:     private static final long serialVersionUID = -2951586537224143260L;
  85:     
  86:     /**
  87:      * The default constructor.
  88:      */
  89:     public YIntervalRenderer() {
  90:         super();
  91:     }
  92: 
  93:     /**
  94:      * Draws the visual representation of a single data item.
  95:      *
  96:      * @param g2  the graphics device.
  97:      * @param state  the renderer state.
  98:      * @param dataArea  the area within which the plot is being drawn.
  99:      * @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: }