001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ------------
028 * Outlier.java
029 * ------------
030 * (C) Copyright 2003-present, by David Browning and Contributors.
031 *
032 * Original Author:  David Browning (for Australian Institute of Marine
033 *                   Science);
034 * Contributor(s):   David Gilbert;
035 *
036 */
037
038package org.jfree.chart.renderer;
039
040import java.awt.geom.Point2D;
041
042/**
043 * Represents one outlier in the box and whisker plot.
044 * <P>
045 * All the coordinates in this class are in Java2D space.
046 */
047public class Outlier implements Comparable {
048
049    /**
050     * The xy coordinates of the bounding box containing the outlier ellipse.
051     */
052    private Point2D point;
053
054    /** The radius of the ellipse */
055    private double radius;
056
057    /**
058     * Constructs an outlier item consisting of a point and the radius of the
059     * outlier ellipse
060     *
061     * @param xCoord  the x coordinate of the point.
062     * @param yCoord  the y coordinate of the point.
063     * @param radius  the radius of the ellipse.
064     */
065    public Outlier(double xCoord, double yCoord, double radius) {
066        this.point = new Point2D.Double(xCoord - radius, yCoord - radius);
067        this.radius = radius;
068    }
069
070    /**
071     * Returns the xy coordinates of the bounding box containing the outlier
072     * ellipse.
073     *
074     * @return The location of the outlier ellipse.
075     */
076    public Point2D getPoint() {
077        return this.point;
078    }
079
080    /**
081     * Sets the xy coordinates of the bounding box containing the outlier
082     * ellipse.
083     *
084     * @param point  the location.
085     */
086    public void setPoint(Point2D point) {
087        this.point = point;
088    }
089
090    /**
091     * Returns the x coordinate of the bounding box containing the outlier
092     * ellipse.
093     *
094     * @return The x coordinate.
095     */
096    public double getX() {
097        return getPoint().getX();
098    }
099
100    /**
101     * Returns the y coordinate of the bounding box containing the outlier
102     * ellipse.
103     *
104     * @return The y coordinate.
105     */
106    public double getY() {
107        return getPoint().getY();
108    }
109
110    /**
111     * Returns the radius of the outlier ellipse.
112     *
113     * @return The radius.
114     */
115    public double getRadius() {
116        return this.radius;
117    }
118
119    /**
120     * Sets the radius of the outlier ellipse.
121     *
122     * @param radius  the new radius.
123     */
124    public void setRadius(double radius) {
125        this.radius = radius;
126    }
127
128    /**
129     * Compares this object with the specified object for order, based on
130     * the outlier's point.
131     *
132     * @param   o the Object to be compared.
133     * @return A negative integer, zero, or a positive integer as this object
134     *      is less than, equal to, or greater than the specified object.
135     *
136     */
137    @Override
138    public int compareTo(Object o) {
139        Outlier outlier = (Outlier) o;
140        Point2D p1 = getPoint();
141        Point2D p2 = outlier.getPoint();
142        if (p1.equals(p2)) {
143            return 0;
144        }
145        else if ((p1.getX() < p2.getX()) || (p1.getY() < p2.getY())) {
146            return -1;
147        }
148        else {
149            return 1;
150        }
151    }
152
153    /**
154     * Returns a true if outlier is overlapped and false if it is not.
155     * Overlapping is determined by the respective bounding boxes plus
156     * a small margin.
157     *
158     * @param other  the other outlier.
159     *
160     * @return A {@code boolean} indicating whether or not an overlap has
161     *         occurred.
162     */
163    public boolean overlaps(Outlier other) {
164        return ((other.getX() >= getX() - (this.radius * 1.1))
165                && (other.getX() <= getX() + (this.radius * 1.1))
166                && (other.getY() >= getY() - (this.radius * 1.1))
167                && (other.getY() <= getY() + (this.radius * 1.1)));
168    }
169
170    /**
171     * Tests this outlier for equality with an arbitrary object.
172     *
173     * @param obj  the object ({@code null} permitted).
174     *
175     * @return A boolean.
176     */
177    @Override
178    public boolean equals(Object obj) {
179        if (obj == this) {
180            return true;
181        }
182        if (!(obj instanceof Outlier)) {
183            return false;
184        }
185        Outlier that = (Outlier) obj;
186        if (!this.point.equals(that.point)) {
187            return false;
188        }
189        if (this.radius != that.radius) {
190            return false;
191        }
192        return true;
193    }
194
195    /**
196     * Returns a textual representation of the outlier.
197     *
198     * @return A {@code String} representing the outlier.
199     */
200    @Override
201    public String toString() {
202        return "{" + getX() + "," + getY() + "}";
203    }
204
205}