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: * VectorRenderer.java
029: * -------------------
030: * (C) Copyright 2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: VectorRenderer.java,v 1.1.2.1 2007/05/25 14:44:38 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Jan-2007 : Version 1 (DG);
040: * 24-May-2007 : Updated for method name changes (DG);
041: * 25-May-2007 : Moved from experimental to the main source tree (DG);
042: *
043: */
044:
045: package org.jfree.chart.renderer.xy;
046:
047: import java.awt.Graphics2D;
048: import java.awt.geom.GeneralPath;
049: import java.awt.geom.Line2D;
050: import java.awt.geom.Rectangle2D;
051: import java.io.Serializable;
052:
053: import org.jfree.chart.axis.ValueAxis;
054: import org.jfree.chart.plot.CrosshairState;
055: import org.jfree.chart.plot.PlotOrientation;
056: import org.jfree.chart.plot.PlotRenderingInfo;
057: import org.jfree.chart.plot.XYPlot;
058: import org.jfree.data.Range;
059: import org.jfree.data.xy.VectorXYDataset;
060: import org.jfree.data.xy.XYDataset;
061:
062: /**
063: * A renderer that represents data from an {@link VectorXYDataset} by drawing a
064: * line with an arrow at each (x, y) point.
065: *
066: * @since 1.0.6
067: */
068: public class VectorRenderer extends AbstractXYItemRenderer implements
069: XYItemRenderer, Cloneable, Serializable {
070:
071: private double baseLength = 0.10;
072:
073: private double headLength = 0.14;
074:
075: /**
076: * Creates a new <code>XYBlockRenderer</code> instance with default
077: * attributes.
078: */
079: public VectorRenderer() {
080: }
081:
082: /**
083: * Returns the lower and upper bounds (range) of the x-values in the
084: * specified dataset.
085: *
086: * @param dataset the dataset (<code>null</code> permitted).
087: *
088: * @return The range (<code>null</code> if the dataset is <code>null</code>
089: * or empty).
090: */
091: public Range findDomainBounds(XYDataset dataset) {
092: if (dataset == null) {
093: throw new IllegalArgumentException(
094: "Null 'dataset' argument.");
095: }
096: double minimum = Double.POSITIVE_INFINITY;
097: double maximum = Double.NEGATIVE_INFINITY;
098: int seriesCount = dataset.getSeriesCount();
099: double lvalue;
100: double uvalue;
101: if (dataset instanceof VectorXYDataset) {
102: VectorXYDataset vdataset = (VectorXYDataset) dataset;
103: for (int series = 0; series < seriesCount; series++) {
104: int itemCount = dataset.getItemCount(series);
105: for (int item = 0; item < itemCount; item++) {
106: double delta = vdataset.getVectorXValue(series,
107: item);
108: if (delta < 0.0) {
109: uvalue = vdataset.getXValue(series, item);
110: lvalue = uvalue + delta;
111: } else {
112: lvalue = vdataset.getXValue(series, item);
113: uvalue = lvalue + delta;
114: }
115: minimum = Math.min(minimum, lvalue);
116: maximum = Math.max(maximum, uvalue);
117: }
118: }
119: } else {
120: for (int series = 0; series < seriesCount; series++) {
121: int itemCount = dataset.getItemCount(series);
122: for (int item = 0; item < itemCount; item++) {
123: lvalue = dataset.getXValue(series, item);
124: uvalue = lvalue;
125: minimum = Math.min(minimum, lvalue);
126: maximum = Math.max(maximum, uvalue);
127: }
128: }
129: }
130: if (minimum > maximum) {
131: return null;
132: } else {
133: return new Range(minimum, maximum);
134: }
135: }
136:
137: /**
138: * Returns the range of values the renderer requires to display all the
139: * items from the specified dataset.
140: *
141: * @param dataset the dataset (<code>null</code> permitted).
142: *
143: * @return The range (<code>null</code> if the dataset is <code>null</code>
144: * or empty).
145: */
146: public Range findRangeBounds(XYDataset dataset) {
147: if (dataset == null) {
148: throw new IllegalArgumentException(
149: "Null 'dataset' argument.");
150: }
151: double minimum = Double.POSITIVE_INFINITY;
152: double maximum = Double.NEGATIVE_INFINITY;
153: int seriesCount = dataset.getSeriesCount();
154: double lvalue;
155: double uvalue;
156: if (dataset instanceof VectorXYDataset) {
157: VectorXYDataset vdataset = (VectorXYDataset) dataset;
158: for (int series = 0; series < seriesCount; series++) {
159: int itemCount = dataset.getItemCount(series);
160: for (int item = 0; item < itemCount; item++) {
161: double delta = vdataset.getVectorYValue(series,
162: item);
163: if (delta < 0.0) {
164: uvalue = vdataset.getYValue(series, item);
165: lvalue = uvalue + delta;
166: } else {
167: lvalue = vdataset.getYValue(series, item);
168: uvalue = lvalue + delta;
169: }
170: minimum = Math.min(minimum, lvalue);
171: maximum = Math.max(maximum, uvalue);
172: }
173: }
174: } else {
175: for (int series = 0; series < seriesCount; series++) {
176: int itemCount = dataset.getItemCount(series);
177: for (int item = 0; item < itemCount; item++) {
178: lvalue = dataset.getYValue(series, item);
179: uvalue = lvalue;
180: minimum = Math.min(minimum, lvalue);
181: maximum = Math.max(maximum, uvalue);
182: }
183: }
184: }
185: if (minimum > maximum) {
186: return null;
187: } else {
188: return new Range(minimum, maximum);
189: }
190: }
191:
192: /**
193: * Draws the block representing the specified item.
194: *
195: * @param g2 the graphics device.
196: * @param state the state.
197: * @param dataArea the data area.
198: * @param info the plot rendering info.
199: * @param plot the plot.
200: * @param domainAxis the x-axis.
201: * @param rangeAxis the y-axis.
202: * @param dataset the dataset.
203: * @param series the series index.
204: * @param item the item index.
205: * @param crosshairState the crosshair state.
206: * @param pass the pass index.
207: */
208: public void drawItem(Graphics2D g2, XYItemRendererState state,
209: Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
210: ValueAxis domainAxis, ValueAxis rangeAxis,
211: XYDataset dataset, int series, int item,
212: CrosshairState crosshairState, int pass) {
213:
214: double x = dataset.getXValue(series, item);
215: double y = dataset.getYValue(series, item);
216: double dx = 0.0;
217: double dy = 0.0;
218: if (dataset instanceof VectorXYDataset) {
219: dx = ((VectorXYDataset) dataset).getVectorXValue(series,
220: item);
221: dy = ((VectorXYDataset) dataset).getVectorYValue(series,
222: item);
223: }
224: double xx0 = domainAxis.valueToJava2D(x, dataArea, plot
225: .getDomainAxisEdge());
226: double yy0 = rangeAxis.valueToJava2D(y, dataArea, plot
227: .getRangeAxisEdge());
228: double xx1 = domainAxis.valueToJava2D(x + dx, dataArea, plot
229: .getDomainAxisEdge());
230: double yy1 = rangeAxis.valueToJava2D(y + dy, dataArea, plot
231: .getRangeAxisEdge());
232: Line2D line;
233: PlotOrientation orientation = plot.getOrientation();
234: if (orientation.equals(PlotOrientation.HORIZONTAL)) {
235: line = new Line2D.Double(yy0, xx0, yy1, xx1);
236: } else {
237: line = new Line2D.Double(xx0, yy0, xx1, yy1);
238: }
239: g2.setPaint(getItemPaint(series, item));
240: g2.setStroke(getItemStroke(series, item));
241: g2.draw(line);
242:
243: // calculate the arrow head and draw it...
244: double dxx = (xx1 - xx0);
245: double dyy = (yy1 - yy0);
246: double bx = xx0 + (1.0 - this .baseLength) * dxx;
247: double by = yy0 + (1.0 - this .baseLength) * dyy;
248:
249: double cx = xx0 + (1.0 - this .headLength) * dxx;
250: double cy = yy0 + (1.0 - this .headLength) * dyy;
251:
252: double angle = 0.0;
253: if (dxx != 0.0) {
254: angle = Math.PI / 2.0 - Math.atan(dyy / dxx);
255: }
256: double deltaX = 2.0 * Math.cos(angle);
257: double deltaY = 2.0 * Math.sin(angle);
258:
259: double leftx = cx + deltaX;
260: double lefty = cy - deltaY;
261: double rightx = cx - deltaX;
262: double righty = cy + deltaY;
263:
264: GeneralPath p = new GeneralPath();
265: p.moveTo((float) xx1, (float) yy1);
266: p.lineTo((float) rightx, (float) righty);
267: p.lineTo((float) bx, (float) by);
268: p.lineTo((float) leftx, (float) lefty);
269: p.closePath();
270: g2.draw(p);
271:
272: }
273:
274: /**
275: * Tests this <code>VectorRenderer</code> for equality with an arbitrary
276: * object. This method returns <code>true</code> if and only if:
277: * <ul>
278: * <li><code>obj</code> is an instance of <code>VectorRenderer</code> (not
279: * <code>null</code>);</li>
280: * <li><code>obj</code> has the same field values as this
281: * <code>VectorRenderer</code>;</li>
282: * </ul>
283: *
284: * @param obj the object (<code>null</code> permitted).
285: *
286: * @return A boolean.
287: */
288: public boolean equals(Object obj) {
289: if (obj == this ) {
290: return true;
291: }
292: if (!(obj instanceof VectorRenderer)) {
293: return false;
294: }
295: VectorRenderer that = (VectorRenderer) obj;
296: if (this .baseLength != that.baseLength) {
297: return false;
298: }
299: if (this .headLength != that.headLength) {
300: return false;
301: }
302: return super .equals(obj);
303: }
304:
305: /**
306: * Returns a clone of this renderer.
307: *
308: * @return A clone of this renderer.
309: *
310: * @throws CloneNotSupportedException if there is a problem creating the
311: * clone.
312: */
313: public Object clone() throws CloneNotSupportedException {
314: return super.clone();
315: }
316:
317: }
|