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