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 implements
079: XYItemRenderer, Cloneable, PublicCloneable, Serializable {
080:
081: private static final long serialVersionUID = -2951586537224143260L;
082:
083: /**
084: * The default constructor.
085: */
086: public YIntervalRenderer() {
087: super ();
088: }
089:
090: /**
091: * Draws the visual representation of a single data item.
092: *
093: * @param g2 the graphics device.
094: * @param state the renderer state.
095: * @param dataArea the area within which the plot is being drawn.
096: * @param info collects information about the drawing.
097: * @param plot the plot (can be used to obtain standard color
098: * information etc).
099: * @param domainAxis the domain axis.
100: * @param rangeAxis the range axis.
101: * @param dataset the dataset.
102: * @param series the series index (zero-based).
103: * @param item the item index (zero-based).
104: * @param crosshairState crosshair information for the plot
105: * (<code>null</code> permitted).
106: * @param pass the pass index (ignored here).
107: */
108: public void drawItem(Graphics2D g2, XYItemRendererState state,
109: Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
110: ValueAxis domainAxis, ValueAxis rangeAxis,
111: XYDataset dataset, int series, int item,
112: CrosshairState crosshairState, int pass) {
113:
114: // setup for collecting optional entity info...
115: Shape entityArea = null;
116: EntityCollection entities = null;
117: if (info != null) {
118: entities = info.getOwner().getEntityCollection();
119: }
120:
121: IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
122:
123: double x = intervalDataset.getXValue(series, item);
124: double yLow = intervalDataset.getStartYValue(series, item);
125: double yHigh = intervalDataset.getEndYValue(series, item);
126:
127: RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
128: RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
129:
130: double xx = domainAxis
131: .valueToJava2D(x, dataArea, xAxisLocation);
132: double yyLow = rangeAxis.valueToJava2D(yLow, dataArea,
133: yAxisLocation);
134: double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea,
135: yAxisLocation);
136:
137: Paint p = getItemPaint(series, item);
138: Stroke s = getItemStroke(series, item);
139:
140: Line2D line = null;
141: Shape shape = getItemShape(series, item);
142: Shape top = null;
143: Shape bottom = null;
144: PlotOrientation orientation = plot.getOrientation();
145: if (orientation == PlotOrientation.HORIZONTAL) {
146: line = new Line2D.Double(yyLow, xx, yyHigh, xx);
147: top = ShapeUtilities.createTranslatedShape(shape, yyHigh,
148: xx);
149: bottom = ShapeUtilities.createTranslatedShape(shape, yyLow,
150: xx);
151: } else if (orientation == PlotOrientation.VERTICAL) {
152: line = new Line2D.Double(xx, yyLow, xx, yyHigh);
153: top = ShapeUtilities.createTranslatedShape(shape, xx,
154: yyHigh);
155: bottom = ShapeUtilities.createTranslatedShape(shape, xx,
156: yyLow);
157: }
158: g2.setPaint(p);
159: g2.setStroke(s);
160: g2.draw(line);
161:
162: g2.fill(top);
163: g2.fill(bottom);
164:
165: // add an entity for the item...
166: if (entities != null) {
167: if (entityArea == null) {
168: entityArea = line.getBounds();
169: }
170: String tip = null;
171: XYToolTipGenerator generator = getToolTipGenerator(series,
172: item);
173: if (generator != null) {
174: tip = generator.generateToolTip(dataset, series, item);
175: }
176: String url = null;
177: if (getURLGenerator() != null) {
178: url = getURLGenerator().generateURL(dataset, series,
179: item);
180: }
181: XYItemEntity entity = new XYItemEntity(entityArea, dataset,
182: series, item, tip, url);
183: entities.add(entity);
184: }
185:
186: }
187:
188: /**
189: * Returns a clone of the renderer.
190: *
191: * @return A clone.
192: *
193: * @throws CloneNotSupportedException if the renderer cannot be cloned.
194: */
195: public Object clone() throws CloneNotSupportedException {
196: return super.clone();
197: }
198:
199: }
|