001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, 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 * BoxAndWhiskerXYToolTipGenerator.java
029 * ------------------------------------
030 * (C) Copyright 2003-2014, by David Browning and Contributors.
031 *
032 * Original Author:  David Browning;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
038 * 13-Aug-2003 : Implemented Cloneable (DG);
039 * 28-Aug-2003 : Updated for changes in dataset API (DG);
040 * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
041 * 27-Feb-2004 : Renamed BoxAndWhiskerItemLabelGenerator -->
042 *               BoxAndWhiskerXYItemLabelGenerator, and modified to use
043 *               MessageFormat (DG);
044 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
045 *               getYValue() (DG);
046 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
047 *
048 */
049
050package org.jfree.chart.labels;
051
052import java.io.Serializable;
053import java.text.DateFormat;
054import java.text.MessageFormat;
055import java.text.NumberFormat;
056import java.util.Date;
057
058import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
059import org.jfree.data.xy.XYDataset;
060
061/**
062 * An item label generator for plots that use data from a
063 * {@link BoxAndWhiskerXYDataset}.
064 * <P>
065 * The tooltip text and item label text are composed using a
066 * {@link java.text.MessageFormat} object, that can aggregate some or all of
067 * the following string values into a message.
068 * <ul>
069 * <li>0 : Series Name</li>
070 * <li>1 : X (value or date)</li>
071 * <li>2 : Mean</li>
072 * <li>3 : Median</li>
073 * <li>4 : Minimum</li>
074 * <li>5 : Maximum</li>
075 * <li>6 : Quartile 1</li>
076 * <li>7 : Quartile 3</li>
077 * </ul>
078 */
079public class BoxAndWhiskerXYToolTipGenerator extends StandardXYToolTipGenerator
080        implements XYToolTipGenerator, Cloneable, Serializable {
081
082    /** For serialization. */
083    private static final long serialVersionUID = -2648775791161459710L;
084
085    /** The default tooltip format string. */
086    public static final String DEFAULT_TOOL_TIP_FORMAT
087            = "X: {1} Mean: {2} Median: {3} Min: {4} Max: {5} Q1: {6} Q3: {7} ";
088
089    /**
090     * Creates a default item label generator.
091     */
092    public BoxAndWhiskerXYToolTipGenerator() {
093        super(DEFAULT_TOOL_TIP_FORMAT, NumberFormat.getInstance(),
094                NumberFormat.getInstance());
095    }
096
097    /**
098     * Creates a new item label generator.  If the date formatter is not
099     * <code>null</code>, the x-values will be formatted as dates.
100     *
101     * @param toolTipFormat  the tool tip format string (<code>null</code> not
102     *                       permitted).
103     * @param numberFormat  the number formatter (<code>null</code> not
104     *                      permitted).
105     * @param dateFormat  the date formatter (<code>null</code> permitted).
106     */
107    public BoxAndWhiskerXYToolTipGenerator(String toolTipFormat,
108                                           DateFormat dateFormat,
109                                           NumberFormat numberFormat) {
110
111        super(toolTipFormat, dateFormat, numberFormat);
112
113    }
114
115    /**
116     * Creates the array of items that can be passed to the
117     * {@link MessageFormat} class for creating labels.
118     *
119     * @param dataset  the dataset (<code>null</code> not permitted).
120     * @param series  the series (zero-based index).
121     * @param item  the item (zero-based index).
122     *
123     * @return The items (never <code>null</code>).
124     */
125    @Override
126    protected Object[] createItemArray(XYDataset dataset, int series,
127                                       int item) {
128        Object[] result = new Object[8];
129        result[0] = dataset.getSeriesKey(series).toString();
130        Number x = dataset.getX(series, item);
131        if (getXDateFormat() != null) {
132            result[1] = getXDateFormat().format(new Date(x.longValue()));
133        }
134        else {
135            result[1] = getXFormat().format(x);
136        }
137        NumberFormat formatter = getYFormat();
138
139        if (dataset instanceof BoxAndWhiskerXYDataset) {
140            BoxAndWhiskerXYDataset d = (BoxAndWhiskerXYDataset) dataset;
141            result[2] = formatter.format(d.getMeanValue(series, item));
142            result[3] = formatter.format(d.getMedianValue(series, item));
143            result[4] = formatter.format(d.getMinRegularValue(series, item));
144            result[5] = formatter.format(d.getMaxRegularValue(series, item));
145            result[6] = formatter.format(d.getQ1Value(series, item));
146            result[7] = formatter.format(d.getQ3Value(series, item));
147        }
148        return result;
149    }
150
151    /**
152     * Tests if this object is equal to another.
153     *
154     * @param obj  the other object.
155     *
156     * @return A boolean.
157     */
158    @Override
159    public boolean equals(Object obj) {
160        if (obj == this) {
161            return true;
162        }
163        if (!(obj instanceof BoxAndWhiskerXYToolTipGenerator)) {
164            return false;
165        }
166        return super.equals(obj);
167    }
168
169}