001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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 * StandardXYZToolTipGenerator.java
029 * --------------------------------
030 * (C) Copyright 2004-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
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.Objects;
044
045import org.jfree.chart.internal.Args;
046
047import org.jfree.data.xy.XYDataset;
048import org.jfree.data.xy.XYZDataset;
049
050/**
051 * A standard item label generator for use with {@link XYZDataset} data.  Each
052 * value can be formatted as a number or as a date.
053 */
054public class StandardXYZToolTipGenerator extends StandardXYToolTipGenerator
055        implements XYZToolTipGenerator, Serializable {
056
057    /** For serialization. */
058    private static final long serialVersionUID = -2961577421889473503L;
059
060    /** The default tooltip format. */
061    public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2}, {3})";
062
063    /**
064     * A number formatter for the z value - if this is null, then zDateFormat
065     * must be non-null.
066     */
067    private NumberFormat zFormat;
068
069    /**
070     * A date formatter for the z-value - if this is null, then zFormat must be
071     * non-null.
072     */
073    private DateFormat zDateFormat;
074
075    /**
076     * Creates a new tool tip generator using default number formatters for the
077     * x, y and z-values.
078     */
079    public StandardXYZToolTipGenerator() {
080        this(
081            DEFAULT_TOOL_TIP_FORMAT,
082            NumberFormat.getNumberInstance(),
083            NumberFormat.getNumberInstance(),
084            NumberFormat.getNumberInstance()
085        );
086    }
087
088    /**
089     * Constructs a new tool tip generator using the specified number
090     * formatters.
091     *
092     * @param formatString  the format string.
093     * @param xFormat  the format object for the x values ({@code null}
094     *                 not permitted).
095     * @param yFormat  the format object for the y values ({@code null}
096     *                 not permitted).
097     * @param zFormat  the format object for the z values ({@code null}
098     *                 not permitted).
099     */
100    public StandardXYZToolTipGenerator(String formatString, 
101            NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) {
102        super(formatString, xFormat, yFormat);
103        Args.nullNotPermitted(zFormat, "zFormat");
104        this.zFormat = zFormat;
105    }
106
107    /**
108     * Constructs a new tool tip generator using the specified date formatters.
109     *
110     * @param formatString  the format string.
111     * @param xFormat  the format object for the x values ({@code null}
112     *                 not permitted).
113     * @param yFormat  the format object for the y values ({@code null}
114     *                 not permitted).
115     * @param zFormat  the format object for the z values ({@code null}
116     *                 not permitted).
117     */
118    public StandardXYZToolTipGenerator(String formatString, DateFormat xFormat,
119            DateFormat yFormat, DateFormat zFormat) {
120        super(formatString, xFormat, yFormat);
121        Args.nullNotPermitted(zFormat, "zFormat");
122        this.zDateFormat = zFormat;
123    }
124
125    // TODO:  add constructors for combinations of number and date formatters.
126
127    /**
128     * Returns the number formatter for the z-values.
129     *
130     * @return The number formatter (possibly {@code null}).
131     */
132    public NumberFormat getZFormat() {
133        return this.zFormat;
134    }
135
136    /**
137     * Returns the date formatter for the z-values.
138     *
139     * @return The date formatter (possibly {@code null}).
140     */
141    public DateFormat getZDateFormat() {
142        return this.zDateFormat;
143    }
144
145    /**
146     * Generates a tool tip text item for a particular item within a series.
147     *
148     * @param dataset  the dataset ({@code null} not permitted).
149     * @param series  the series index (zero-based).
150     * @param item  the item index (zero-based).
151     *
152     * @return The tooltip text (possibly {@code null}).
153     */
154    @Override
155    public String generateToolTip(XYZDataset dataset, int series, int item) {
156        return generateLabelString(dataset, series, item);
157    }
158
159    /**
160     * Generates a label string for an item in the dataset.
161     *
162     * @param dataset  the dataset ({@code null} not permitted).
163     * @param series  the series (zero-based index).
164     * @param item  the item (zero-based index).
165     *
166     * @return The label (possibly {@code null}).
167     */
168    @Override
169    public String generateLabelString(XYDataset dataset, int series, int item) {
170        String result;
171        Object[] items = createItemArray((XYZDataset) dataset, series, item);
172        result = MessageFormat.format(getFormatString(), items);
173        return result;
174    }
175
176    /**
177     * Creates the array of items that can be passed to the
178     * {@link MessageFormat} class for creating labels.
179     *
180     * @param dataset  the dataset ({@code null} not permitted).
181     * @param series  the series (zero-based index).
182     * @param item  the item (zero-based index).
183     *
184     * @return The items (never {@code null}).
185     */
186    protected Object[] createItemArray(XYZDataset dataset,
187                                       int series, int item) {
188
189        Object[] result = new Object[4];
190        result[0] = dataset.getSeriesKey(series).toString();
191
192        Number x = dataset.getX(series, item);
193        DateFormat xf = getXDateFormat();
194        if (xf != null) {
195            result[1] = xf.format(x);
196        }
197        else {
198            result[1] = getXFormat().format(x);
199        }
200
201        Number y = dataset.getY(series, item);
202        DateFormat yf = getYDateFormat();
203        if (yf != null) {
204            result[2] = yf.format(y);
205        }
206        else {
207            result[2] = getYFormat().format(y);
208        }
209
210        Number z = dataset.getZ(series, item);
211        if (this.zDateFormat != null) {
212            result[3] = this.zDateFormat.format(z);
213        }
214        else {
215            result[3] = this.zFormat.format(z);
216        }
217
218        return result;
219
220    }
221
222    /**
223     * Tests this object for equality with an arbitrary object.
224     *
225     * @param obj  the other object ({@code null} permitted).
226     *
227     * @return A boolean.
228     */
229    @Override
230    public boolean equals(Object obj) {
231        if (obj == this) {
232            return true;
233        }
234        if (!(obj instanceof StandardXYZToolTipGenerator)) {
235            return false;
236        }
237        if (!super.equals(obj)) {
238            return false;
239        }
240        StandardXYZToolTipGenerator that = (StandardXYZToolTipGenerator) obj;
241        if (!Objects.equals(this.zFormat, that.zFormat)) {
242            return false;
243        }
244        if (!Objects.equals(this.zDateFormat, that.zDateFormat)) {
245            return false;
246        }
247        return true;
248    }
249
250    @Override
251    public int hashCode() {
252        int hash = super.hashCode();
253        hash = 79 * hash + Objects.hashCode(this.zFormat);
254        hash = 79 * hash + Objects.hashCode(this.zDateFormat);
255        return hash;
256    }
257
258}