Source for org.jfree.chart.renderer.Outlier

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * Outlier.java
  29:  * ------------
  30:  * (C) Copyright 2003-2007, by David Browning and Contributors.
  31:  *
  32:  * Original Author:  David Browning (for Australian Institute of Marine 
  33:  *                   Science);
  34:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  35:  *
  36:  * Changes
  37:  * -------
  38:  * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
  39:  * 28-Aug-2003 : Minor tidy-up (DG);
  40:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  41:  * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
  42:  * 21-Nov-2007 : Implemented equals() to shut up FindBugs (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.chart.renderer;
  47: 
  48: import java.awt.geom.Point2D;
  49: 
  50: /**
  51:  * Represents one outlier in the box and whisker plot.
  52:  * <P>
  53:  * All the coordinates in this class are in Java2D space.
  54:  */
  55: public class Outlier implements Comparable {
  56: 
  57:     /** 
  58:      * The xy coordinates of the bounding box containing the outlier ellipse. 
  59:      */
  60:     private Point2D point;
  61: 
  62:     /** The radius of the ellipse */
  63:     private double radius;
  64: 
  65:     /**
  66:      * Constructs an outlier item consisting of a point and the radius of the 
  67:      * outlier ellipse
  68:      *
  69:      * @param xCoord  the x coordinate of the point.
  70:      * @param yCoord  the y coordinate of the point.
  71:      * @param radius  the radius of the ellipse.
  72:      */
  73:     public Outlier(double xCoord, double yCoord, double radius) {
  74:         this.point = new Point2D.Double(xCoord - radius, yCoord - radius);
  75:         this.radius = radius;
  76:     }
  77: 
  78:     /**
  79:      * Returns the xy coordinates of the bounding box containing the outlier 
  80:      * ellipse.
  81:      *
  82:      * @return The location of the outlier ellipse.
  83:      */
  84:     public Point2D getPoint() {
  85:         return this.point;
  86:     }
  87: 
  88:     /**
  89:      * Sets the xy coordinates of the bounding box containing the outlier 
  90:      * ellipse.
  91:      *
  92:      * @param point  the location.
  93:      */
  94:     public void setPoint(Point2D point) {
  95:         this.point = point;
  96:     }
  97: 
  98:     /**
  99:      * Returns the x coordinate of the bounding box containing the outlier 
 100:      * ellipse.
 101:      *
 102:      * @return The x coordinate.
 103:      */
 104:     public double getX() {
 105:         return getPoint().getX();
 106:     }
 107: 
 108:     /**
 109:      * Returns the y coordinate of the bounding box containing the outlier 
 110:      * ellipse.
 111:      *
 112:      * @return The y coordinate.
 113:      */
 114:     public double getY() {
 115:         return getPoint().getY();
 116:     }
 117: 
 118:     /**
 119:      * Returns the radius of the outlier ellipse.
 120:      *
 121:      * @return The radius.
 122:      */
 123:     public double getRadius() {
 124:         return this.radius;
 125:     }
 126: 
 127:     /**
 128:      * Sets the radius of the outlier ellipse.
 129:      *
 130:      * @param radius  the new radius.
 131:      */
 132:     public void setRadius(double radius) {
 133:         this.radius = radius;
 134:     }
 135: 
 136:     /**
 137:      * Compares this object with the specified object for order, based on
 138:      * the outlier's point.
 139:      *
 140:      * @param   o the Object to be compared.
 141:      * @return A negative integer, zero, or a positive integer as this object
 142:      *      is less than, equal to, or greater than the specified object.
 143:      *
 144:      */
 145:     public int compareTo(Object o) {
 146:         Outlier outlier = (Outlier) o;
 147:         Point2D p1 = getPoint();
 148:         Point2D p2 = outlier.getPoint();
 149:         if (p1.equals(p2)) {
 150:             return 0;
 151:         } 
 152:         else if ((p1.getX() < p2.getX()) || (p1.getY() < p2.getY())) {
 153:             return -1;
 154:         } 
 155:         else {
 156:             return 1;
 157:         } 
 158:     }
 159: 
 160:     /**
 161:      * Returns a true if outlier is overlapped and false if it is not.
 162:      * Overlapping is determined by the respective bounding boxes plus
 163:      * a small margin.
 164:      *
 165:      * @param other  the other outlier.
 166:      * 
 167:      * @return A <code>boolean</code> indicating whether or not an overlap has 
 168:      *         occurred.
 169:      */
 170:     public boolean overlaps(Outlier other) {
 171:         return ((other.getX() >= getX() - (this.radius * 1.1)) 
 172:                 && (other.getX() <= getX() + (this.radius * 1.1)) 
 173:                 && (other.getY() >= getY() - (this.radius * 1.1)) 
 174:                 && (other.getY() <= getY() + (this.radius * 1.1)));
 175:     }
 176: 
 177:     /**
 178:      * Tests this outlier for equality with an arbitrary object.
 179:      * 
 180:      * @param obj  the object (<code>null</code> permitted).
 181:      * 
 182:      * @return A boolean.
 183:      */
 184:     public boolean equals(Object obj) {
 185:         if (obj == this) {
 186:             return true;
 187:         }
 188:         if (!(obj instanceof Outlier)) {
 189:             return false;
 190:         }
 191:         Outlier that = (Outlier) obj;
 192:         if (!this.point.equals(that.point)) {
 193:             return false;
 194:         }
 195:         if (this.radius != that.radius) {
 196:             return false;
 197:         }
 198:         return true;
 199:     }
 200:     
 201:     /**
 202:      * Returns a textual representation of the outlier.
 203:      *
 204:      * @return A <code>String</code> representing the outlier.
 205:      */
 206:     public String toString() {
 207:         return "{" + getX() + "," + getY() + "}";
 208:     }
 209: 
 210: }