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 * Outlier.java
029 * ------------
030 * (C) Copyright 2003-2007, by David Browning and Contributors.
031 *
032 * Original Author:  David Browning (for Australian Institute of Marine 
033 *                   Science);
034 * Contributor(s):   David Gilbert (for Object Refinery Limited);
035 *
036 * Changes
037 * -------
038 * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
039 * 28-Aug-2003 : Minor tidy-up (DG);
040 * ------------- JFREECHART 1.0.x ---------------------------------------------
041 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
042 *
043 */
044
045package org.jfree.chart.renderer;
046
047import java.awt.geom.Point2D;
048
049/**
050 * Represents one outlier in the box and whisker plot.
051 * <P>
052 * All the coordinates in this class are in Java2D space.
053 */
054public class Outlier implements Comparable {
055
056    /** 
057     * The xy coordinates of the bounding box containing the outlier ellipse. 
058     */
059    private Point2D point;
060
061    /** The radius of the ellipse */
062    private double radius;
063
064    /**
065     * Constructs an outlier item consisting of a point and the radius of the 
066     * outlier ellipse
067     *
068     * @param xCoord  the x coordinate of the point.
069     * @param yCoord  the y coordinate of the point.
070     * @param radius  the radius of the ellipse.
071     */
072    public Outlier(double xCoord, double yCoord, double radius) {
073        this.point = new Point2D.Double(xCoord - radius, yCoord - radius);
074        this.radius = radius;
075    }
076
077    /**
078     * Returns the xy coordinates of the bounding box containing the outlier 
079     * ellipse.
080     *
081     * @return The location of the outlier ellipse.
082     */
083    public Point2D getPoint() {
084        return this.point;
085    }
086
087    /**
088     * Sets the xy coordinates of the bounding box containing the outlier 
089     * ellipse.
090     *
091     * @param point  the location.
092     */
093    public void setPoint(Point2D point) {
094        this.point = point;
095    }
096
097    /**
098     * Returns the x coordinate of the bounding box containing the outlier 
099     * ellipse.
100     *
101     * @return The x coordinate.
102     */
103    public double getX() {
104        return getPoint().getX();
105    }
106
107    /**
108     * Returns the y coordinate of the bounding box containing the outlier 
109     * ellipse.
110     *
111     * @return The y coordinate.
112     */
113    public double getY() {
114        return getPoint().getY();
115    }
116
117    /**
118     * Returns the radius of the outlier ellipse.
119     *
120     * @return The radius.
121     */
122    public double getRadius() {
123        return this.radius;
124    }
125
126    /**
127     * Sets the radius of the outlier ellipse.
128     *
129     * @param radius  the new radius.
130     */
131    public void setRadius(double radius) {
132        this.radius = radius;
133    }
134
135    /**
136     * Compares this object with the specified object for order, based on
137     * the outlier's point.
138     *
139     * @param   o the Object to be compared.
140     * @return A negative integer, zero, or a positive integer as this object
141     *      is less than, equal to, or greater than the specified object.
142     *
143     */
144    public int compareTo(Object o) {
145        Outlier outlier = (Outlier) o;
146        Point2D p1 = getPoint();
147        Point2D p2 = outlier.getPoint();
148        if (p1.equals(p2)) {
149            return 0;
150        } 
151        else if ((p1.getX() < p2.getX()) || (p1.getY() < p2.getY())) {
152            return -1;
153        } 
154        else {
155            return 1;
156        } 
157    }
158
159    /**
160     * Returns a true if outlier is overlapped and false if it is not.
161     * Overlapping is determined by the respective bounding boxes plus
162     * a small margin.
163     *
164     * @param other  the other outlier.
165     * 
166     * @return A <code>boolean</code> indicating whether or not an overlap has 
167     *         occurred.
168     */
169    public boolean overlaps(Outlier other) {
170        return ((other.getX() >= getX() - (this.radius * 1.1)) 
171                && (other.getX() <= getX() + (this.radius * 1.1)) 
172                && (other.getY() >= getY() - (this.radius * 1.1)) 
173                && (other.getY() <= getY() + (this.radius * 1.1)));
174    }
175
176    /**
177     * Returns a textual representation of the outlier.
178     *
179     * @return A <code>String</code> representing the outlier.
180     */
181    public String toString() {
182        return "{" + getX() + "," + getY() + "}";
183    }
184
185}