    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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     * [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     * OutlierList.java
    029     * ----------------
    030     * (C) Copyright 2003-2008, 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 including Javadocs (DG);
    040     * ------------- JFREECHART 1.0.x ---------------------------------------------
    041     * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
    042     *
    043     */
    044
    045    package org.jfree.chart.renderer;
    046
    047    import java.awt.geom.Point2D;
    048    import java.util.ArrayList;
    049    import java.util.Iterator;
    050    import java.util.List;
    051
    052    /**
    053     * A collection of outliers for a single entity in a box and whisker plot.
    054     *
    055     * Outliers are grouped in lists for each entity. Lists contain
    056     * one or more outliers, determined by whether overlaps have
    057     * occured. Overlapping outliers are grouped in the same list.
    058     *
    059     * Each list contains an averaged outlier, which is the same as a single
    060     * outlier if there is only one outlier in the list, but the average of
    061     * all the outliers in the list if there is more than one.
    062     *
    063     * NB This is simply my scheme for displaying outliers, and might not be
    064     * acceptable by the wider community.
    065     */
    066    public class OutlierList {
    067
    068        /** Storage for the outliers. */
    069        private List outliers;
    070
    071        /** The averaged outlier. */
    072        private Outlier averagedOutlier;
    073
    074        /**
    075         * A flag that indicates whether or not there are multiple outliers in the
    076         * list.
    077         */
    078        private boolean multiple = false;
    079
    080        /**
    081         * Creates a new list containing a single outlier.
    082         *
    083         * @param outlier  the outlier.
    084         */
    085        public OutlierList(Outlier outlier) {
    086            this.outliers = new ArrayList();
    087            setAveragedOutlier(outlier);
    088        }
    089
    090        /**
    091         * Adds an outlier to the list.
    092         *
    093         * @param outlier  the outlier.
    094         *
    095         * @return A boolean.
    096         */
    097        public boolean add(Outlier outlier) {
    098            return this.outliers.add(outlier);
    099        }
    100
    101        /**
    102         * Returns the number of outliers in the list.
    103         *
    104         * @return The item count.
    105         */
    106        public int getItemCount() {
    107            return this.outliers.size();
    108        }
    109
    110        /**
    111         * Returns the averaged outlier.
    112         *
    113         * @return The averaged outlier.
    114         */
    115        public Outlier getAveragedOutlier() {
    116            return this.averagedOutlier;
    117        }
    118
    119        /**
    120         * Sets the averaged outlier.
    121         *
    122         * @param averagedOutlier  the averaged outlier.
    123         */
    124        public void setAveragedOutlier(Outlier averagedOutlier) {
    125            this.averagedOutlier = averagedOutlier;
    126        }
    127
    128        /**
    129         * Returns <code>true</code> if the list contains multiple outliers, and
    130         * <code>false</code> otherwise.
    131         *
    132         * @return A boolean.
    133         */
    134        public boolean isMultiple() {
    135            return this.multiple;
    136        }
    137
    138        /**
    139         * Sets the flag that indicates whether or not this list represents
    140         * multiple outliers.
    141         *
    142         * @param multiple  the flag.
    143         */
    144        public void setMultiple(boolean multiple) {
    145            this.multiple = multiple;
    146        }
    147
    148        /**
    149         * Returns <code>true</code> if the outlier overlaps, and
    150         * <code>false</code> otherwise.
    151         *
    152         * @param other  the outlier.
    153         *
    154         * @return A boolean.
    155         */
    156        public boolean isOverlapped(Outlier other) {
    157
    158            if (other == null) {
    159                return false;
    160            }
    161
    162            boolean result = other.overlaps(getAveragedOutlier());
    163            return result;
    164
    165        }
    166
    167        /**
    168         * Updates the averaged outlier.
    169         *
    170         */
    171        public void updateAveragedOutlier() {
    172            double totalXCoords = 0.0;
    173            double totalYCoords = 0.0;
    174            int size = getItemCount();
    175            for (Iterator iterator = this.outliers.iterator();
    176                    iterator.hasNext();) {
    177                Outlier o = (Outlier) iterator.next();
    178                totalXCoords += o.getX();
    179                totalYCoords += o.getY();
    180            }
    181            getAveragedOutlier().getPoint().setLocation(
    182                    new Point2D.Double(totalXCoords / size, totalYCoords / size));
    183        }
    184
    185    }