Frames | No Frames |
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.2 2006/08/24 09:22:28 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: * 47: */ 48: 49: package org.jfree.chart.plot; 50: 51: import java.awt.geom.Point2D; 52: 53: /** 54: * Maintains state information about crosshairs on a plot. 55: */ 56: public class CrosshairState { 57: 58: /** 59: * A flag that controls whether the distance is calculated in data space 60: * or Java2D space. 61: */ 62: private boolean calculateDistanceInDataSpace = false; 63: 64: /** The x-value (in data space) for the anchor point. */ 65: private double anchorX; 66: 67: /** The y-value (in data space) for the anchor point. */ 68: private double anchorY; 69: 70: /** The anchor point in Java2D space - if null, don't update crosshair. */ 71: private Point2D anchor; 72: 73: /** The x-value for the crosshair point. */ 74: private double crosshairX; 75: 76: /** The y-value for the crosshair point. */ 77: private double crosshairY; 78: 79: /** 80: * The smallest distance so far between the anchor point and a data point. 81: */ 82: private double distance; 83: 84: /** 85: * Default constructor. 86: */ 87: public CrosshairState() { 88: this(false); 89: } 90: 91: /** 92: * Creates a new info object. 93: * 94: * @param calculateDistanceInDataSpace a flag that controls whether the 95: * distance is calculated in data 96: * space or Java2D space. 97: */ 98: public CrosshairState(boolean calculateDistanceInDataSpace) { 99: this.calculateDistanceInDataSpace = calculateDistanceInDataSpace; 100: } 101: 102: /** 103: * Sets the distance between the anchor point and the current crosshair 104: * point. As each data point is processed, its distance to the anchor 105: * point is compared with this value and, if it is closer, the data point 106: * becomes the new crosshair point. 107: * 108: * @param distance the distance. 109: */ 110: public void setCrosshairDistance(double distance) { 111: this.distance = distance; 112: } 113: 114: /** 115: * Evaluates a data point and if it is the closest to the anchor point it 116: * becomes the new crosshair point. 117: * <P> 118: * To understand this method, you need to know the context in which it will 119: * be called. An instance of this class is passed to an 120: * {@link org.jfree.chart.renderer.xy.XYItemRenderer} as 121: * each data point is plotted. As the point is plotted, it is passed to 122: * this method to see if it should be the new crosshair point. 123: * 124: * @param x x coordinate (measured against the domain axis). 125: * @param y y coordinate (measured against the range axis). 126: * @param transX x translated into Java2D space. 127: * @param transY y translated into Java2D space. 128: * @param orientation the plot orientation. 129: */ 130: public void updateCrosshairPoint(double x, double y, 131: double transX, double transY, 132: PlotOrientation orientation) { 133: 134: if (this.anchor != null) { 135: double d = 0.0; 136: if (this.calculateDistanceInDataSpace) { 137: d = (x - this.anchorX) * (x - this.anchorX) 138: + (y - this.anchorY) * (y - this.anchorY); 139: } 140: else { 141: double xx = this.anchor.getX(); 142: double yy = this.anchor.getY(); 143: if (orientation == PlotOrientation.HORIZONTAL) { 144: double temp = yy; 145: yy = xx; 146: xx = temp; 147: } 148: d = (transX - xx) * (transX - xx) 149: + (transY - yy) * (transY - yy); 150: } 151: 152: if (d < this.distance) { 153: this.crosshairX = x; 154: this.crosshairY = y; 155: this.distance = d; 156: } 157: } 158: 159: } 160: 161: /** 162: * Evaluates an x-value and if it is the closest to the anchor point it 163: * becomes the new crosshair point. 164: * <P> 165: * Used in cases where only the x-axis is numerical. 166: * 167: * @param candidateX x position of the candidate for the new crosshair 168: * point. 169: */ 170: public void updateCrosshairX(double candidateX) { 171: 172: double d = Math.abs(candidateX - this.anchorX); 173: if (d < this.distance) { 174: this.crosshairX = candidateX; 175: this.distance = d; 176: } 177: 178: } 179: 180: /** 181: * Evaluates a y-value and if it is the closest to the anchor point it 182: * becomes the new crosshair point. 183: * <P> 184: * Used in cases where only the y-axis is numerical. 185: * 186: * @param candidateY y position of the candidate for the new crosshair 187: * point. 188: */ 189: public void updateCrosshairY(double candidateY) { 190: 191: double d = Math.abs(candidateY - this.anchorY); 192: if (d < this.distance) { 193: this.crosshairY = candidateY; 194: this.distance = d; 195: } 196: 197: } 198: 199: /** 200: * Sets the anchor point. This is usually the mouse click point in a chart 201: * panel, and the crosshair point will often be the data item that is 202: * closest to the anchor point. 203: * 204: * @param anchor the anchor point. 205: */ 206: public void setAnchor(Point2D anchor) { 207: this.anchor = anchor; 208: } 209: 210: /** 211: * Get the x-value for the crosshair point. 212: * 213: * @return The x position of the crosshair point. 214: */ 215: public double getCrosshairX() { 216: return this.crosshairX; 217: } 218: 219: /** 220: * Sets the x coordinate for the crosshair. This is the coordinate in data 221: * space measured against the domain axis. 222: * 223: * @param x the coordinate. 224: */ 225: public void setCrosshairX(double x) { 226: this.crosshairX = x; 227: } 228: 229: /** 230: * Get the y-value for the crosshair point. This is the coordinate in data 231: * space measured against the range axis. 232: * 233: * @return The y position of the crosshair point. 234: */ 235: public double getCrosshairY() { 236: return this.crosshairY; 237: } 238: 239: /** 240: * Sets the y coordinate for the crosshair. 241: * 242: * @param y the y coordinate. 243: */ 244: public void setCrosshairY(double y) { 245: this.crosshairY = y; 246: } 247: 248: }