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