001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2008, 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     * XYBoxAndWhiskerRenderer.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.  Based on code in the
039     *               CandlestickRenderer class.  Additional modifications by David
040     *               Gilbert to make the code work with 0.9.10 changes (DG);
041     * 08-Aug-2003 : Updated some of the Javadoc
042     *               Allowed BoxAndwhiskerDataset Average value to be null - the
043     *               average value is an AIMS requirement
044     *               Allow the outlier and farout coefficients to be set - though
045     *               at the moment this only affects the calculation of farouts.
046     *               Added artifactPaint variable and setter/getter
047     * 12-Aug-2003   Rewrote code to sort out and process outliers to take
048     *               advantage of changes in DefaultBoxAndWhiskerDataset
049     *               Added a limit of 10% for width of box should no width be
050     *               specified...maybe this should be setable???
051     * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
052     * 08-Sep-2003 : Changed ValueAxis API (DG);
053     * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
054     * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
055     * 23-Apr-2004 : Added fillBox attribute, extended equals() method and fixed
056     *               serialization issue (DG);
057     * 29-Apr-2004 : Fixed problem with drawing upper and lower shadows - bug id
058     *               944011 (DG);
059     * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
060     *               getYValue() (DG);
061     * 01-Oct-2004 : Renamed 'paint' --> 'boxPaint' to avoid conflict with
062     *               inherited attribute (DG);
063     * 10-Jun-2005 : Updated equals() to handle GradientPaint (DG);
064     * 06-Oct-2005 : Removed setPaint() call in drawItem(), it is causing a
065     *               loop (DG);
066     * ------------- JFREECHART 1.0.x ---------------------------------------------
067     * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
068     * 05-Feb-2007 : Added event notifications and fixed drawing for horizontal
069     *               plot orientation (DG);
070     * 13-Jun-2007 : Replaced deprecated method call (DG);
071     * 03-Jan-2008 : Check visibility of average marker before drawing it (DG);
072     * 27-Mar-2008 : If boxPaint is null, revert to itemPaint (DG);
073     *
074     */
075    
076    package org.jfree.chart.renderer.xy;
077    
078    import java.awt.Color;
079    import java.awt.Graphics2D;
080    import java.awt.Paint;
081    import java.awt.Shape;
082    import java.awt.Stroke;
083    import java.awt.geom.Ellipse2D;
084    import java.awt.geom.Line2D;
085    import java.awt.geom.Point2D;
086    import java.awt.geom.Rectangle2D;
087    import java.io.IOException;
088    import java.io.ObjectInputStream;
089    import java.io.ObjectOutputStream;
090    import java.io.Serializable;
091    import java.util.ArrayList;
092    import java.util.Collections;
093    import java.util.Iterator;
094    import java.util.List;
095    
096    import org.jfree.chart.axis.ValueAxis;
097    import org.jfree.chart.entity.EntityCollection;
098    import org.jfree.chart.event.RendererChangeEvent;
099    import org.jfree.chart.labels.BoxAndWhiskerXYToolTipGenerator;
100    import org.jfree.chart.plot.CrosshairState;
101    import org.jfree.chart.plot.PlotOrientation;
102    import org.jfree.chart.plot.PlotRenderingInfo;
103    import org.jfree.chart.plot.XYPlot;
104    import org.jfree.chart.renderer.Outlier;
105    import org.jfree.chart.renderer.OutlierList;
106    import org.jfree.chart.renderer.OutlierListCollection;
107    import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
108    import org.jfree.data.xy.XYDataset;
109    import org.jfree.io.SerialUtilities;
110    import org.jfree.ui.RectangleEdge;
111    import org.jfree.util.PaintUtilities;
112    import org.jfree.util.PublicCloneable;
113    
114    /**
115     * A renderer that draws box-and-whisker items on an {@link XYPlot}.  This
116     * renderer requires a {@link BoxAndWhiskerXYDataset}).  The example shown here
117     * is generated by the <code>BoxAndWhiskerChartDemo2.java</code> program
118     * included in the JFreeChart demo collection:
119     * <br><br>
120     * <img src="../../../../../images/XYBoxAndWhiskerRendererSample.png"
121     * alt="XYBoxAndWhiskerRendererSample.png" />
122     * <P>
123     * This renderer does not include any code to calculate the crosshair point.
124     */
125    public class XYBoxAndWhiskerRenderer extends AbstractXYItemRenderer
126            implements XYItemRenderer, Cloneable, PublicCloneable, Serializable {
127    
128        /** For serialization. */
129        private static final long serialVersionUID = -8020170108532232324L;
130    
131        /** The box width. */
132        private double boxWidth;
133    
134        /** The paint used to fill the box. */
135        private transient Paint boxPaint;
136    
137        /** A flag that controls whether or not the box is filled. */
138        private boolean fillBox;
139    
140        /**
141         * The paint used to draw various artifacts such as outliers, farout
142         * symbol, average ellipse and median line.
143         */
144        private transient Paint artifactPaint = Color.black;
145    
146        /**
147         * Creates a new renderer for box and whisker charts.
148         */
149        public XYBoxAndWhiskerRenderer() {
150            this(-1.0);
151        }
152    
153        /**
154         * Creates a new renderer for box and whisker charts.
155         * <P>
156         * Use -1 for the box width if you prefer the width to be calculated
157         * automatically.
158         *
159         * @param boxWidth  the box width.
160         */
161        public XYBoxAndWhiskerRenderer(double boxWidth) {
162            super();
163            this.boxWidth = boxWidth;
164            this.boxPaint = Color.green;
165            this.fillBox = true;
166            setBaseToolTipGenerator(new BoxAndWhiskerXYToolTipGenerator());
167        }
168    
169        /**
170         * Returns the width of each box.
171         *
172         * @return The box width.
173         *
174         * @see #setBoxWidth(double)
175         */
176        public double getBoxWidth() {
177            return this.boxWidth;
178        }
179    
180        /**
181         * Sets the box width and sends a {@link RendererChangeEvent} to all
182         * registered listeners.
183         * <P>
184         * If you set the width to a negative value, the renderer will calculate
185         * the box width automatically based on the space available on the chart.
186         *
187         * @param width  the width.
188         *
189         * @see #getBoxWidth()
190         */
191        public void setBoxWidth(double width) {
192            if (width != this.boxWidth) {
193                this.boxWidth = width;
194                fireChangeEvent();
195            }
196        }
197    
198        /**
199         * Returns the paint used to fill boxes.
200         *
201         * @return The paint (possibly <code>null</code>).
202         *
203         * @see #setBoxPaint(Paint)
204         */
205        public Paint getBoxPaint() {
206            return this.boxPaint;
207        }
208    
209        /**
210         * Sets the paint used to fill boxes and sends a {@link RendererChangeEvent}
211         * to all registered listeners.
212         *
213         * @param paint  the paint (<code>null</code> permitted).
214         *
215         * @see #getBoxPaint()
216         */
217        public void setBoxPaint(Paint paint) {
218            this.boxPaint = paint;
219            fireChangeEvent();
220        }
221    
222        /**
223         * Returns the flag that controls whether or not the box is filled.
224         *
225         * @return A boolean.
226         *
227         * @see #setFillBox(boolean)
228         */
229        public boolean getFillBox() {
230            return this.fillBox;
231        }
232    
233        /**
234         * Sets the flag that controls whether or not the box is filled and sends a
235         * {@link RendererChangeEvent} to all registered listeners.
236         *
237         * @param flag  the flag.
238         *
239         * @see #setFillBox(boolean)
240         */
241        public void setFillBox(boolean flag) {
242            this.fillBox = flag;
243            fireChangeEvent();
244        }
245    
246        /**
247         * Returns the paint used to paint the various artifacts such as outliers,
248         * farout symbol, median line and the averages ellipse.
249         *
250         * @return The paint (never <code>null</code>).
251         *
252         * @see #setArtifactPaint(Paint)
253         */
254        public Paint getArtifactPaint() {
255            return this.artifactPaint;
256        }
257    
258        /**
259         * Sets the paint used to paint the various artifacts such as outliers,
260         * farout symbol, median line and the averages ellipse, and sends a
261         * {@link RendererChangeEvent} to all registered listeners.
262         *
263         * @param paint  the paint (<code>null</code> not permitted).
264         *
265         * @see #getArtifactPaint()
266         */
267        public void setArtifactPaint(Paint paint) {
268            if (paint == null) {
269                throw new IllegalArgumentException("Null 'paint' argument.");
270            }
271            this.artifactPaint = paint;
272            fireChangeEvent();
273        }
274    
275        /**
276         * Returns the box paint or, if this is <code>null</code>, the item
277         * paint.
278         *
279         * @param series  the series index.
280         * @param item  the item index.
281         *
282         * @return The paint used to fill the box for the specified item (never
283         *         <code>null</code>).
284         *
285         * @since 1.0.10
286         */
287        protected Paint lookupBoxPaint(int series, int item) {
288            Paint p = getBoxPaint();
289            if (p != null) {
290                return p;
291            }
292            else {
293                // TODO: could change this to itemFillPaint().  For backwards
294                // compatibility, it might require a useFillPaint flag.
295                return getItemPaint(series, item);
296            }
297        }
298    
299        /**
300         * Draws the visual representation of a single data item.
301         *
302         * @param g2  the graphics device.
303         * @param state  the renderer state.
304         * @param dataArea  the area within which the plot is being drawn.
305         * @param info  collects info about the drawing.
306         * @param plot  the plot (can be used to obtain standard color
307         *              information etc).
308         * @param domainAxis  the domain axis.
309         * @param rangeAxis  the range axis.
310         * @param dataset  the dataset (must be an instance of
311         *                 {@link BoxAndWhiskerXYDataset}).
312         * @param series  the series index (zero-based).
313         * @param item  the item index (zero-based).
314         * @param crosshairState  crosshair information for the plot
315         *                        (<code>null</code> permitted).
316         * @param pass  the pass index.
317         */
318        public void drawItem(Graphics2D g2,
319                             XYItemRendererState state,
320                             Rectangle2D dataArea,
321                             PlotRenderingInfo info,
322                             XYPlot plot,
323                             ValueAxis domainAxis,
324                             ValueAxis rangeAxis,
325                             XYDataset dataset,
326                             int series,
327                             int item,
328                             CrosshairState crosshairState,
329                             int pass) {
330    
331            PlotOrientation orientation = plot.getOrientation();
332    
333            if (orientation == PlotOrientation.HORIZONTAL) {
334                drawHorizontalItem(g2, dataArea, info, plot, domainAxis, rangeAxis,
335                        dataset, series, item, crosshairState, pass);
336            }
337            else if (orientation == PlotOrientation.VERTICAL) {
338                drawVerticalItem(g2, dataArea, info, plot, domainAxis, rangeAxis,
339                        dataset, series, item, crosshairState, pass);
340            }
341    
342        }
343    
344        /**
345         * Draws the visual representation of a single data item.
346         *
347         * @param g2  the graphics device.
348         * @param dataArea  the area within which the plot is being drawn.
349         * @param info  collects info about the drawing.
350         * @param plot  the plot (can be used to obtain standard color
351         *              information etc).
352         * @param domainAxis  the domain axis.
353         * @param rangeAxis  the range axis.
354         * @param dataset  the dataset (must be an instance of
355         *                 {@link BoxAndWhiskerXYDataset}).
356         * @param series  the series index (zero-based).
357         * @param item  the item index (zero-based).
358         * @param crosshairState  crosshair information for the plot
359         *                        (<code>null</code> permitted).
360         * @param pass  the pass index.
361         */
362        public void drawHorizontalItem(Graphics2D g2,
363                                       Rectangle2D dataArea,
364                                       PlotRenderingInfo info,
365                                       XYPlot plot,
366                                       ValueAxis domainAxis,
367                                       ValueAxis rangeAxis,
368                                       XYDataset dataset,
369                                       int series,
370                                       int item,
371                                       CrosshairState crosshairState,
372                                       int pass) {
373    
374            // setup for collecting optional entity info...
375            EntityCollection entities = null;
376            if (info != null) {
377                entities = info.getOwner().getEntityCollection();
378            }
379    
380            BoxAndWhiskerXYDataset boxAndWhiskerData
381                    = (BoxAndWhiskerXYDataset) dataset;
382    
383            Number x = boxAndWhiskerData.getX(series, item);
384            Number yMax = boxAndWhiskerData.getMaxRegularValue(series, item);
385            Number yMin = boxAndWhiskerData.getMinRegularValue(series, item);
386            Number yMedian = boxAndWhiskerData.getMedianValue(series, item);
387            Number yAverage = boxAndWhiskerData.getMeanValue(series, item);
388            Number yQ1Median = boxAndWhiskerData.getQ1Value(series, item);
389            Number yQ3Median = boxAndWhiskerData.getQ3Value(series, item);
390    
391            double xx = domainAxis.valueToJava2D(x.doubleValue(), dataArea,
392                    plot.getDomainAxisEdge());
393    
394            RectangleEdge location = plot.getRangeAxisEdge();
395            double yyMax = rangeAxis.valueToJava2D(yMax.doubleValue(), dataArea,
396                    location);
397            double yyMin = rangeAxis.valueToJava2D(yMin.doubleValue(), dataArea,
398                    location);
399            double yyMedian = rangeAxis.valueToJava2D(yMedian.doubleValue(),
400                    dataArea, location);
401            double yyAverage = 0.0;
402            if (yAverage != null) {
403                yyAverage = rangeAxis.valueToJava2D(yAverage.doubleValue(),
404                        dataArea, location);
405            }
406            double yyQ1Median = rangeAxis.valueToJava2D(yQ1Median.doubleValue(),
407                    dataArea, location);
408            double yyQ3Median = rangeAxis.valueToJava2D(yQ3Median.doubleValue(),
409                    dataArea, location);
410    
411            double exactBoxWidth = getBoxWidth();
412            double width = exactBoxWidth;
413            double dataAreaX = dataArea.getHeight();
414            double maxBoxPercent = 0.1;
415            double maxBoxWidth = dataAreaX * maxBoxPercent;
416            if (exactBoxWidth <= 0.0) {
417                int itemCount = boxAndWhiskerData.getItemCount(series);
418                exactBoxWidth = dataAreaX / itemCount * 4.5 / 7;
419                if (exactBoxWidth < 3) {
420                    width = 3;
421                }
422                else if (exactBoxWidth > maxBoxWidth) {
423                    width = maxBoxWidth;
424                }
425                else {
426                    width = exactBoxWidth;
427                }
428            }
429    
430            g2.setPaint(getItemPaint(series, item));
431            Stroke s = getItemStroke(series, item);
432            g2.setStroke(s);
433    
434            // draw the upper shadow
435            g2.draw(new Line2D.Double(yyMax, xx, yyQ3Median, xx));
436            g2.draw(new Line2D.Double(yyMax, xx - width / 2, yyMax,
437                    xx + width / 2));
438    
439            // draw the lower shadow
440            g2.draw(new Line2D.Double(yyMin, xx, yyQ1Median, xx));
441            g2.draw(new Line2D.Double(yyMin, xx - width / 2, yyMin,
442                    xx + width / 2));
443    
444            // draw the body
445            Shape box = null;
446            if (yyQ1Median < yyQ3Median) {
447                box = new Rectangle2D.Double(yyQ1Median, xx - width / 2,
448                        yyQ3Median - yyQ1Median, width);
449            }
450            else {
451                box = new Rectangle2D.Double(yyQ3Median, xx - width / 2,
452                        yyQ1Median - yyQ3Median, width);
453            }
454            if (this.fillBox) {
455                g2.setPaint(lookupBoxPaint(series, item));
456                g2.fill(box);
457            }
458            g2.setStroke(getItemOutlineStroke(series, item));
459            g2.setPaint(getItemOutlinePaint(series, item));
460            g2.draw(box);
461    
462            // draw median
463            g2.setPaint(getArtifactPaint());
464            g2.draw(new Line2D.Double(yyMedian,
465                    xx - width / 2, yyMedian, xx + width / 2));
466    
467            // draw average - SPECIAL AIMS REQUIREMENT
468            if (yAverage != null) {
469                double aRadius = width / 4;
470                // here we check that the average marker will in fact be visible
471                // before drawing it...
472                if ((yyAverage > (dataArea.getMinX() - aRadius))
473                        && (yyAverage < (dataArea.getMaxX() + aRadius))) {
474                    Ellipse2D.Double avgEllipse = new Ellipse2D.Double(
475                            yyAverage - aRadius, xx - aRadius, aRadius * 2,
476                            aRadius * 2);
477                    g2.fill(avgEllipse);
478                    g2.draw(avgEllipse);
479                }
480            }
481    
482            // FIXME: draw outliers
483    
484            // add an entity for the item...
485            if (entities != null && box.intersects(dataArea)) {
486                addEntity(entities, box, dataset, series, item, yyAverage, xx);
487            }
488    
489        }
490    
491        /**
492         * Draws the visual representation of a single data item.
493         *
494         * @param g2  the graphics device.
495         * @param dataArea  the area within which the plot is being drawn.
496         * @param info  collects info about the drawing.
497         * @param plot  the plot (can be used to obtain standard color
498         *              information etc).
499         * @param domainAxis  the domain axis.
500         * @param rangeAxis  the range axis.
501         * @param dataset  the dataset (must be an instance of
502         *                 {@link BoxAndWhiskerXYDataset}).
503         * @param series  the series index (zero-based).
504         * @param item  the item index (zero-based).
505         * @param crosshairState  crosshair information for the plot
506         *                        (<code>null</code> permitted).
507         * @param pass  the pass index.
508         */
509        public void drawVerticalItem(Graphics2D g2,
510                                     Rectangle2D dataArea,
511                                     PlotRenderingInfo info,
512                                     XYPlot plot,
513                                     ValueAxis domainAxis,
514                                     ValueAxis rangeAxis,
515                                     XYDataset dataset,
516                                     int series,
517                                     int item,
518                                     CrosshairState crosshairState,
519                                     int pass) {
520    
521            // setup for collecting optional entity info...
522            EntityCollection entities = null;
523            if (info != null) {
524                entities = info.getOwner().getEntityCollection();
525            }
526    
527            BoxAndWhiskerXYDataset boxAndWhiskerData
528                = (BoxAndWhiskerXYDataset) dataset;
529    
530            Number x = boxAndWhiskerData.getX(series, item);
531            Number yMax = boxAndWhiskerData.getMaxRegularValue(series, item);
532            Number yMin = boxAndWhiskerData.getMinRegularValue(series, item);
533            Number yMedian = boxAndWhiskerData.getMedianValue(series, item);
534            Number yAverage = boxAndWhiskerData.getMeanValue(series, item);
535            Number yQ1Median = boxAndWhiskerData.getQ1Value(series, item);
536            Number yQ3Median = boxAndWhiskerData.getQ3Value(series, item);
537            List yOutliers = boxAndWhiskerData.getOutliers(series, item);
538    
539            double xx = domainAxis.valueToJava2D(x.doubleValue(), dataArea,
540                    plot.getDomainAxisEdge());
541    
542            RectangleEdge location = plot.getRangeAxisEdge();
543            double yyMax = rangeAxis.valueToJava2D(yMax.doubleValue(), dataArea,
544                    location);
545            double yyMin = rangeAxis.valueToJava2D(yMin.doubleValue(), dataArea,
546                    location);
547            double yyMedian = rangeAxis.valueToJava2D(yMedian.doubleValue(),
548                    dataArea, location);
549            double yyAverage = 0.0;
550            if (yAverage != null) {
551                yyAverage = rangeAxis.valueToJava2D(yAverage.doubleValue(),
552                        dataArea, location);
553            }
554            double yyQ1Median = rangeAxis.valueToJava2D(yQ1Median.doubleValue(),
555                    dataArea, location);
556            double yyQ3Median = rangeAxis.valueToJava2D(yQ3Median.doubleValue(),
557                    dataArea, location);
558            double yyOutlier;
559    
560    
561            double exactBoxWidth = getBoxWidth();
562            double width = exactBoxWidth;
563            double dataAreaX = dataArea.getMaxX() - dataArea.getMinX();
564            double maxBoxPercent = 0.1;
565            double maxBoxWidth = dataAreaX * maxBoxPercent;
566            if (exactBoxWidth <= 0.0) {
567                int itemCount = boxAndWhiskerData.getItemCount(series);
568                exactBoxWidth = dataAreaX / itemCount * 4.5 / 7;
569                if (exactBoxWidth < 3) {
570                    width = 3;
571                }
572                else if (exactBoxWidth > maxBoxWidth) {
573                    width = maxBoxWidth;
574                }
575                else {
576                    width = exactBoxWidth;
577                }
578            }
579    
580            g2.setPaint(getItemPaint(series, item));
581            Stroke s = getItemStroke(series, item);
582            g2.setStroke(s);
583    
584            // draw the upper shadow
585            g2.draw(new Line2D.Double(xx, yyMax, xx, yyQ3Median));
586            g2.draw(new Line2D.Double(xx - width / 2, yyMax, xx + width / 2,
587                    yyMax));
588    
589            // draw the lower shadow
590            g2.draw(new Line2D.Double(xx, yyMin, xx, yyQ1Median));
591            g2.draw(new Line2D.Double(xx - width / 2, yyMin, xx + width / 2,
592                    yyMin));
593    
594            // draw the body
595            Shape box = null;
596            if (yyQ1Median > yyQ3Median) {
597                box = new Rectangle2D.Double(xx - width / 2, yyQ3Median, width,
598                        yyQ1Median - yyQ3Median);
599            }
600            else {
601                box = new Rectangle2D.Double(xx - width / 2, yyQ1Median, width,
602                        yyQ3Median - yyQ1Median);
603            }
604            if (this.fillBox) {
605                g2.setPaint(lookupBoxPaint(series, item));
606                g2.fill(box);
607            }
608            g2.setStroke(getItemOutlineStroke(series, item));
609            g2.setPaint(getItemOutlinePaint(series, item));
610            g2.draw(box);
611    
612            // draw median
613            g2.setPaint(getArtifactPaint());
614            g2.draw(new Line2D.Double(xx - width / 2, yyMedian, xx + width / 2,
615                    yyMedian));
616    
617            double aRadius = 0;                 // average radius
618            double oRadius = width / 3;    // outlier radius
619    
620            // draw average - SPECIAL AIMS REQUIREMENT
621            if (yAverage != null) {
622                aRadius = width / 4;
623                // here we check that the average marker will in fact be visible
624                // before drawing it...
625                if ((yyAverage > (dataArea.getMinY() - aRadius))
626                        && (yyAverage < (dataArea.getMaxY() + aRadius))) {
627                    Ellipse2D.Double avgEllipse = new Ellipse2D.Double(xx - aRadius,
628                            yyAverage - aRadius, aRadius * 2, aRadius * 2);
629                    g2.fill(avgEllipse);
630                    g2.draw(avgEllipse);
631                }
632            }
633    
634            List outliers = new ArrayList();
635            OutlierListCollection outlierListCollection
636                    = new OutlierListCollection();
637    
638            /* From outlier array sort out which are outliers and put these into
639             * an arraylist. If there are any farouts, set the flag on the
640             * OutlierListCollection
641             */
642    
643            for (int i = 0; i < yOutliers.size(); i++) {
644                double outlier = ((Number) yOutliers.get(i)).doubleValue();
645                if (outlier > boxAndWhiskerData.getMaxOutlier(series,
646                        item).doubleValue()) {
647                    outlierListCollection.setHighFarOut(true);
648                }
649                else if (outlier < boxAndWhiskerData.getMinOutlier(series,
650                        item).doubleValue()) {
651                    outlierListCollection.setLowFarOut(true);
652                }
653                else if (outlier > boxAndWhiskerData.getMaxRegularValue(series,
654                        item).doubleValue()) {
655                    yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea,
656                            location);
657                    outliers.add(new Outlier(xx, yyOutlier, oRadius));
658                }
659                else if (outlier < boxAndWhiskerData.getMinRegularValue(series,
660                        item).doubleValue()) {
661                    yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea,
662                            location);
663                    outliers.add(new Outlier(xx, yyOutlier, oRadius));
664                }
665                Collections.sort(outliers);
666            }
667    
668            // Process outliers. Each outlier is either added to the appropriate
669            // outlier list or a new outlier list is made
670            for (Iterator iterator = outliers.iterator(); iterator.hasNext();) {
671                Outlier outlier = (Outlier) iterator.next();
672                outlierListCollection.add(outlier);
673            }
674    
675            // draw yOutliers
676            double maxAxisValue = rangeAxis.valueToJava2D(rangeAxis.getUpperBound(),
677                    dataArea, location) + aRadius;
678            double minAxisValue = rangeAxis.valueToJava2D(rangeAxis.getLowerBound(),
679                    dataArea, location) - aRadius;
680    
681            // draw outliers
682            for (Iterator iterator = outlierListCollection.iterator();
683                    iterator.hasNext();) {
684                OutlierList list = (OutlierList) iterator.next();
685                Outlier outlier = list.getAveragedOutlier();
686                Point2D point = outlier.getPoint();
687    
688                if (list.isMultiple()) {
689                    drawMultipleEllipse(point, width, oRadius, g2);
690                }
691                else {
692                    drawEllipse(point, oRadius, g2);
693                }
694            }
695    
696            // draw farout
697            if (outlierListCollection.isHighFarOut()) {
698                drawHighFarOut(aRadius, g2, xx, maxAxisValue);
699            }
700    
701            if (outlierListCollection.isLowFarOut()) {
702                drawLowFarOut(aRadius, g2, xx, minAxisValue);
703            }
704    
705            // add an entity for the item...
706            if (entities != null && box.intersects(dataArea)) {
707                addEntity(entities, box, dataset, series, item, xx, yyAverage);
708            }
709    
710        }
711    
712        /**
713         * Draws an ellipse to represent an outlier.
714         *
715         * @param point  the location.
716         * @param oRadius  the radius.
717         * @param g2  the graphics device.
718         */
719        protected void drawEllipse(Point2D point, double oRadius, Graphics2D g2) {
720            Ellipse2D.Double dot = new Ellipse2D.Double(point.getX() + oRadius / 2,
721                    point.getY(), oRadius, oRadius);
722            g2.draw(dot);
723        }
724    
725        /**
726         * Draws two ellipses to represent overlapping outliers.
727         *
728         * @param point  the location.
729         * @param boxWidth  the box width.
730         * @param oRadius  the radius.
731         * @param g2  the graphics device.
732         */
733        protected void drawMultipleEllipse(Point2D point, double boxWidth,
734                                           double oRadius, Graphics2D g2) {
735    
736            Ellipse2D.Double dot1 = new Ellipse2D.Double(point.getX()
737                    - (boxWidth / 2) + oRadius, point.getY(), oRadius, oRadius);
738            Ellipse2D.Double dot2 = new Ellipse2D.Double(point.getX()
739                    + (boxWidth / 2), point.getY(), oRadius, oRadius);
740            g2.draw(dot1);
741            g2.draw(dot2);
742    
743        }
744    
745        /**
746         * Draws a triangle to indicate the presence of far out values.
747         *
748         * @param aRadius  the radius.
749         * @param g2  the graphics device.
750         * @param xx  the x value.
751         * @param m  the max y value.
752         */
753        protected void drawHighFarOut(double aRadius, Graphics2D g2, double xx,
754                double m) {
755            double side = aRadius * 2;
756            g2.draw(new Line2D.Double(xx - side, m + side, xx + side, m + side));
757            g2.draw(new Line2D.Double(xx - side, m + side, xx, m));
758            g2.draw(new Line2D.Double(xx + side, m + side, xx, m));
759        }
760    
761        /**
762         * Draws a triangle to indicate the presence of far out values.
763         *
764         * @param aRadius  the radius.
765         * @param g2  the graphics device.
766         * @param xx  the x value.
767         * @param m  the min y value.
768         */
769        protected void drawLowFarOut(double aRadius, Graphics2D g2, double xx,
770                double m) {
771            double side = aRadius * 2;
772            g2.draw(new Line2D.Double(xx - side, m - side, xx + side, m - side));
773            g2.draw(new Line2D.Double(xx - side, m - side, xx, m));
774            g2.draw(new Line2D.Double(xx + side, m - side, xx, m));
775        }
776    
777        /**
778         * Tests this renderer for equality with another object.
779         *
780         * @param obj  the object (<code>null</code> permitted).
781         *
782         * @return <code>true</code> or <code>false</code>.
783         */
784        public boolean equals(Object obj) {
785            if (obj == this) {
786                return true;
787            }
788            if (!(obj instanceof XYBoxAndWhiskerRenderer)) {
789                return false;
790            }
791            if (!super.equals(obj)) {
792                return false;
793            }
794            XYBoxAndWhiskerRenderer that = (XYBoxAndWhiskerRenderer) obj;
795            if (this.boxWidth != that.getBoxWidth()) {
796                return false;
797            }
798            if (!PaintUtilities.equal(this.boxPaint, that.boxPaint)) {
799                return false;
800            }
801            if (!PaintUtilities.equal(this.artifactPaint, that.artifactPaint)) {
802                return false;
803            }
804            if (this.fillBox != that.fillBox) {
805                return false;
806            }
807            return true;
808    
809        }
810    
811        /**
812         * Provides serialization support.
813         *
814         * @param stream  the output stream.
815         *
816         * @throws IOException  if there is an I/O error.
817         */
818        private void writeObject(ObjectOutputStream stream) throws IOException {
819            stream.defaultWriteObject();
820            SerialUtilities.writePaint(this.boxPaint, stream);
821            SerialUtilities.writePaint(this.artifactPaint, stream);
822        }
823    
824        /**
825         * Provides serialization support.
826         *
827         * @param stream  the input stream.
828         *
829         * @throws IOException  if there is an I/O error.
830         * @throws ClassNotFoundException  if there is a classpath problem.
831         */
832        private void readObject(ObjectInputStream stream)
833            throws IOException, ClassNotFoundException {
834    
835            stream.defaultReadObject();
836            this.boxPaint = SerialUtilities.readPaint(stream);
837            this.artifactPaint = SerialUtilities.readPaint(stream);
838        }
839    
840        /**
841         * Returns a clone of the renderer.
842         *
843         * @return A clone.
844         *
845         * @throws CloneNotSupportedException  if the renderer cannot be cloned.
846         */
847        public Object clone() throws CloneNotSupportedException {
848            return super.clone();
849        }
850    
851    }