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