KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > CrosshairState


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * CrosshairState.java
29  * -------------------
30  * (C) Copyright 2002-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: CrosshairState.java,v 1.3.2.3 2006/10/13 15:16:18 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 24-Jan-2002 : Version 1 (DG);
40  * 05-Mar-2002 : Added Javadoc comments (DG);
41  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
42  * 19-Sep-2003 : Modified crosshair distance calculation (DG);
43  * 04-Dec-2003 : Crosshair anchor point now stored outside chart since it is
44  * dependent on the display target (DG);
45  * 25-Feb-2004 : Replaced CrosshairInfo --> CrosshairState (DG);
46  * ------------- JFREECHART 1.0.x ------------------------------------------
47  * 13-Oct-2006 : Fixed initialisation of CrosshairState - see bug report
48  * 1565168 (DG);
49  *
50  */

51
52 package org.jfree.chart.plot;
53
54 import java.awt.geom.Point2D JavaDoc;
55
56 /**
57  * Maintains state information about crosshairs on a plot between successive
58  * calls to the renderer's draw method. This class is used internally by
59  * JFreeChart - it is not intended for external use.
60  */

61 public class CrosshairState {
62
63     /**
64      * A flag that controls whether the distance is calculated in data space
65      * or Java2D space.
66      */

67     private boolean calculateDistanceInDataSpace = false;
68
69     /** The x-value (in data space) for the anchor point. */
70     private double anchorX;
71
72     /** The y-value (in data space) for the anchor point. */
73     private double anchorY;
74     
75     /** The anchor point in Java2D space - if null, don't update crosshair. */
76     private Point2D JavaDoc anchor;
77     
78     /** The x-value for the current crosshair point. */
79     private double crosshairX;
80
81     /** The y-value for the current crosshair point. */
82     private double crosshairY;
83
84     /**
85      * The smallest distance (so far) between the anchor point and a data point.
86      */

87     private double distance;
88
89     /**
90      * Creates a new <code>CrosshairState</code> instance that calculates
91      * distance in Java2D space.
92      */

93     public CrosshairState() {
94         this(false);
95     }
96
97     /**
98      * Creates a new <code>CrosshairState</code> instance.
99      *
100      * @param calculateDistanceInDataSpace a flag that controls whether the
101      * distance is calculated in data
102      * space or Java2D space.
103      */

104     public CrosshairState(boolean calculateDistanceInDataSpace) {
105         this.calculateDistanceInDataSpace = calculateDistanceInDataSpace;
106     }
107
108     /**
109      * Returns the distance between the anchor point and the current crosshair
110      * point.
111      *
112      * @return The distance.
113      *
114      * @see #setCrosshairDistance(double)
115      * @since 1.0.3
116      */

117     public double getCrosshairDistance() {
118         return this.distance;
119     }
120     
121     /**
122      * Sets the distance between the anchor point and the current crosshair
123      * point. As each data point is processed, its distance to the anchor
124      * point is compared with this value and, if it is closer, the data point
125      * becomes the new crosshair point.
126      *
127      * @param distance the distance.
128      *
129      * @see #getCrosshairDistance()
130      */

131     public void setCrosshairDistance(double distance) {
132         this.distance = distance;
133     }
134
135     /**
136      * Evaluates a data point and if it is the closest to the anchor point it
137      * becomes the new crosshair point.
138      * <P>
139      * To understand this method, you need to know the context in which it will
140      * be called. An instance of this class is passed to an
141      * {@link org.jfree.chart.renderer.xy.XYItemRenderer} as
142      * each data point is plotted. As the point is plotted, it is passed to
143      * this method to see if it should be the new crosshair point.
144      *
145      * @param x x coordinate (measured against the domain axis).
146      * @param y y coordinate (measured against the range axis).
147      * @param transX x translated into Java2D space.
148      * @param transY y translated into Java2D space.
149      * @param orientation the plot orientation.
150      */

151     public void updateCrosshairPoint(double x, double y,
152                                      double transX, double transY,
153                                      PlotOrientation orientation) {
154
155         if (this.anchor != null) {
156             double d = 0.0;
157             if (this.calculateDistanceInDataSpace) {
158                 d = (x - this.anchorX) * (x - this.anchorX)
159                   + (y - this.anchorY) * (y - this.anchorY);
160             }
161             else {
162                 double xx = this.anchor.getX();
163                 double yy = this.anchor.getY();
164                 if (orientation == PlotOrientation.HORIZONTAL) {
165                     double temp = yy;
166                     yy = xx;
167                     xx = temp;
168                 }
169                 d = (transX - xx) * (transX - xx)
170                     + (transY - yy) * (transY - yy);
171             }
172
173             if (d < this.distance) {
174                 this.crosshairX = x;
175                 this.crosshairY = y;
176                 this.distance = d;
177             }
178         }
179
180     }
181
182     /**
183      * Evaluates an x-value and if it is the closest to the anchor x-value it
184      * becomes the new crosshair value.
185      * <P>
186      * Used in cases where only the x-axis is numerical.
187      *
188      * @param candidateX x position of the candidate for the new crosshair
189      * point.
190      */

191     public void updateCrosshairX(double candidateX) {
192
193         double d = Math.abs(candidateX - this.anchorX);
194         if (d < this.distance) {
195             this.crosshairX = candidateX;
196             this.distance = d;
197         }
198
199     }
200
201     /**
202      * Evaluates a y-value and if it is the closest to the anchor y-value it
203      * becomes the new crosshair value.
204      * <P>
205      * Used in cases where only the y-axis is numerical.
206      *
207      * @param candidateY y position of the candidate for the new crosshair
208      * point.
209      */

210     public void updateCrosshairY(double candidateY) {
211
212         double d = Math.abs(candidateY - this.anchorY);
213         if (d < this.distance) {
214             this.crosshairY = candidateY;
215             this.distance = d;
216         }
217
218     }
219
220     /**
221      * Returns the anchor point.
222      *
223      * @return The anchor point.
224      *
225      * @see #setAnchor(Point2D)
226      * @since 1.0.3
227      */

228     public Point2D JavaDoc getAnchor() {
229         return this.anchor;
230     }
231     
232     /**
233      * Sets the anchor point. This is usually the mouse click point in a chart
234      * panel, and the crosshair point will often be the data item that is
235      * closest to the anchor point.
236      * <br><br>
237      * Note that the x and y coordinates (in data space) are not updated by
238      * this method - the caller is responsible for ensuring that this happens
239      * in sync.
240      *
241      * @param anchor the anchor point (<code>null</code> permitted).
242      *
243      * @see #getAnchor()
244      */

245     public void setAnchor(Point2D JavaDoc anchor) {
246         this.anchor = anchor;
247     }
248     
249     /**
250      * Returns the x-coordinate (in data space) for the anchor point.
251      *
252      * @return The x-coordinate of the anchor point.
253      *
254      * @since 1.0.3
255      */

256     public double getAnchorX() {
257         return this.anchorX;
258     }
259     
260     /**
261      * Sets the x-coordinate (in data space) for the anchor point. Note that
262      * this does NOT update the anchor itself - the caller is responsible for
263      * ensuring this is done in sync.
264      *
265      * @param x the x-coordinate.
266      *
267      * @since 1.0.3
268      */

269     public void setAnchorX(double x) {
270         this.anchorX = x;
271     }
272     
273     /**
274      * Returns the y-coordinate (in data space) for the anchor point.
275      *
276      * @return The y-coordinate of teh anchor point.
277      *
278      * @since 1.0.3
279      */

280     public double getAnchorY() {
281         return this.anchorY;
282     }
283     
284     /**
285      * Sets the y-coordinate (in data space) for the anchor point. Note that
286      * this does NOT update the anchor itself - the caller is responsible for
287      * ensuring this is done in sync.
288      *
289      * @param y the y-coordinate.
290      *
291      * @since 1.0.3
292      */

293     public void setAnchorY(double y) {
294         this.anchorY = y;
295     }
296     
297     /**
298      * Get the x-value for the crosshair point.
299      *
300      * @return The x position of the crosshair point.
301      *
302      * @see #setCrosshairX(double)
303      */

304     public double getCrosshairX() {
305         return this.crosshairX;
306     }
307     
308     /**
309      * Sets the x coordinate for the crosshair. This is the coordinate in data
310      * space measured against the domain axis.
311      *
312      * @param x the coordinate.
313      *
314      * @see #getCrosshairX()
315      * @see #setCrosshairY(double)
316      * @see #updateCrosshairPoint(double, double, double, double,
317      * PlotOrientation)
318      */

319     public void setCrosshairX(double x) {
320         this.crosshairX = x;
321     }
322
323     /**
324      * Get the y-value for the crosshair point. This is the coordinate in data
325      * space measured against the range axis.
326      *
327      * @return The y position of the crosshair point.
328      *
329      * @see #setCrosshairY(double)
330      */

331     public double getCrosshairY() {
332         return this.crosshairY;
333     }
334
335     /**
336      * Sets the y coordinate for the crosshair.
337      *
338      * @param y the y coordinate.
339      *
340      * @see #getCrosshairY()
341      * @see #setCrosshairX(double)
342      * @see #updateCrosshairPoint(double, double, double, double,
343      * PlotOrientation)
344      */

345     public void setCrosshairY(double y) {
346         this.crosshairY = y;
347     }
348
349 }
350
Popular Tags