KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > CrosshairInfo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------
23  * CrosshairInfo.java
24  * ------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: CrosshairInfo.java,v 1.5 2003/09/24 12:15:02 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 24-Jan-2002 : Version 1 (DG);
35  * 05-Mar-2002 : Added Javadoc comments (DG);
36  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
37  * 19-Sep-2003 : Modified crosshair distance calculation (DG);
38  *
39  */

40
41 package org.jfree.chart;
42
43 /**
44  * Maintains information about crosshairs on a plot.
45  *
46  * @author David Gilbert
47  */

48 public class CrosshairInfo {
49
50     /** A flag that controls whether the distance is calculated in data space or Java2D space. */
51     private boolean calculateDistanceInDataSpace = false;
52
53     /** The x-value for the anchor point. */
54     private double anchorX;
55
56     /** The y-value for the anchor point. */
57     private double anchorY;
58     
59     /** The X anchor value in Java2D space. */
60     private double anchorXView;
61     
62     /** The Y anchor value in Java2D space. */
63     private double anchorYView;
64
65     /** The x-value for the crosshair point. */
66     private double crosshairX;
67
68     /** The y-value for the crosshair point. */
69     private double crosshairY;
70
71     /** The smallest distance so far between the anchor point and a data point. */
72     private double distance;
73
74     /**
75      * Default constructor.
76      */

77     public CrosshairInfo() {
78     }
79
80     /**
81      * Creates a new info object.
82      *
83      * @param calculateDistanceInDataSpace a flag that controls whether the distance is calculated
84      * in data space or Java2D space.
85      */

86     public CrosshairInfo(boolean calculateDistanceInDataSpace) {
87         this.calculateDistanceInDataSpace = calculateDistanceInDataSpace;
88     }
89
90     /**
91      * Sets the distance.
92      *
93      * @param distance the distance.
94      */

95     public void setCrosshairDistance(double distance) {
96         this.distance = distance;
97     }
98
99     /**
100      * Evaluates a data point and if it is the closest to the anchor point it
101      * becomes the new crosshair point.
102      * <P>
103      * To understand this method, you need to know the context in which it will
104      * be called. An instance of this class is passed to an XYItemRenderer as
105      * each data point is plotted. As the point is plotted, it is passed to
106      * this method to see if it should be the new crosshair point.
107      *
108      * @param dataX x position of candidate for the new crosshair point.
109      * @param dataY y position of candidate for the new crosshair point.
110      * @param viewX x in Java2D space.
111      * @param viewY y in Java2D space.
112      */

113     public void updateCrosshairPoint(double dataX, double dataY, double viewX, double viewY) {
114
115         double d = 0.0;
116         if (this.calculateDistanceInDataSpace) {
117             d = (dataX - this.anchorX) * (dataX - this.anchorX)
118                     + (dataY - this.anchorY) * (dataY - this.anchorY);
119         }
120         else {
121             d = (viewX - this.anchorXView) * (viewX - this.anchorXView)
122                      + (viewY - this.anchorYView) * (viewY - this.anchorYView);
123         }
124
125         if (d < distance) {
126             crosshairX = dataX;
127             crosshairY = dataY;
128             distance = d;
129         }
130
131     }
132
133     /**
134      * Evaluates an x-value and if it is the closest to the anchor point it
135      * becomes the new crosshair point.
136      * <P>
137      * Used in cases where only the x-axis is numerical.
138      *
139      * @param candidateX x position of the candidate for the new crosshair point.
140      */

141     public void updateCrosshairX(double candidateX) {
142
143         double d = Math.abs(candidateX - anchorX);
144         if (d < distance) {
145             crosshairX = candidateX;
146             distance = d;
147         }
148
149     }
150
151     /**
152      * Evaluates a y-value and if it is the closest to the anchor point it
153      * becomes the new crosshair point.
154      * <P>
155      * Used in cases where only the y-axis is numerical.
156      *
157      * @param candidateY y position of the candidate for the new crosshair point.
158      */

159     public void updateCrosshairY(double candidateY) {
160
161         double d = Math.abs(candidateY - anchorY);
162         if (d < distance) {
163             crosshairY = candidateY;
164             distance = d;
165         }
166
167     }
168
169     /**
170      * Set the x-value for the anchor point.
171      *
172      * @param x the x position.
173      */

174     public void setAnchorX(double x) {
175         this.anchorX = x;
176         this.crosshairX = x;
177     }
178
179     /**
180      * Set the y-value for the anchor point.
181      *
182      * @param y the y position.
183      */

184     public void setAnchorY(double y) {
185         this.anchorY = y;
186         this.crosshairY = y;
187     }
188
189     /**
190      * Set the x-value for the anchor point.
191      *
192      * @param x the x position.
193      */

194     public void setAnchorXView(double x) {
195         this.anchorXView = x;
196     }
197
198     /**
199      * Set the y-value for the anchor point.
200      *
201      * @param y the y position.
202      */

203     public void setAnchorYView(double y) {
204         this.anchorYView = y;
205     }
206
207     /**
208      * Get the x-value for the crosshair point.
209      *
210      * @return the x position of the crosshair point.
211      */

212     public double getCrosshairX() {
213         return this.crosshairX;
214     }
215
216     /**
217      * Get the y-value for the crosshair point.
218      *
219      * @return the y position of the crosshair point.
220      */

221     public double getCrosshairY() {
222         return this.crosshairY;
223     }
224
225 }
226
Popular Tags