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 * IntervalXYItemLabelGenerator.java
029 * ---------------------------------
030 * (C) Copyright 2008-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.Date;
044import org.jfree.chart.util.PublicCloneable;
045
046import org.jfree.data.xy.IntervalXYDataset;
047import org.jfree.data.xy.XYDataset;
048
049/**
050 * An item label generator for datasets that implement the
051 * {@link IntervalXYDataset} interface.
052 */
053public class IntervalXYItemLabelGenerator extends AbstractXYItemLabelGenerator
054        implements XYItemLabelGenerator, Cloneable, PublicCloneable,
055                   Serializable {
056
057    /** The default item label format. */
058    public static final String DEFAULT_ITEM_LABEL_FORMAT = "{5} - {6}";
059
060    /**
061     * Creates an item label generator using default number formatters.
062     */
063    public IntervalXYItemLabelGenerator() {
064        this(DEFAULT_ITEM_LABEL_FORMAT, NumberFormat.getNumberInstance(),
065            NumberFormat.getNumberInstance());
066    }
067
068    /**
069     * Creates an item label generator using the specified number formatters.
070     *
071     * @param formatString  the item label format string ({@code null} not
072     *                      permitted).
073     * @param xFormat  the format object for the x values ({@code null}
074     *                 not permitted).
075     * @param yFormat  the format object for the y values ({@code null}
076     *                 not permitted).
077     */
078    public IntervalXYItemLabelGenerator(String formatString,
079        NumberFormat xFormat, NumberFormat yFormat) {
080
081        super(formatString, xFormat, yFormat);
082    }
083
084    /**
085     * Creates an item label generator using the specified formatters.
086     *
087     * @param formatString  the item label format string ({@code null}
088     *                      not permitted).
089     * @param xFormat  the format object for the x values ({@code null}
090     *                 not permitted).
091     * @param yFormat  the format object for the y values ({@code null}
092     *                 not permitted).
093     */
094    public IntervalXYItemLabelGenerator(String formatString,
095        DateFormat xFormat, NumberFormat yFormat) {
096
097        super(formatString, xFormat, yFormat);
098    }
099
100    /**
101     * Creates an item label generator using the specified formatters (a
102     * number formatter for the x-values and a date formatter for the
103     * y-values).
104     *
105     * @param formatString  the item label format string ({@code null}
106     *                      not permitted).
107     * @param xFormat  the format object for the x values ({@code null}
108     *                 permitted).
109     * @param yFormat  the format object for the y values ({@code null}
110     *                 not permitted).
111     */
112    public IntervalXYItemLabelGenerator(String formatString,
113            NumberFormat xFormat, DateFormat yFormat) {
114
115        super(formatString, xFormat, yFormat);
116    }
117
118    /**
119     * Creates a label generator using the specified date formatters.
120     *
121     * @param formatString  the label format string ({@code null} not
122     *                      permitted).
123     * @param xFormat  the format object for the x values ({@code null}
124     *                 not permitted).
125     * @param yFormat  the format object for the y values ({@code null}
126     *                 not permitted).
127     */
128    public IntervalXYItemLabelGenerator(String formatString,
129            DateFormat xFormat, DateFormat yFormat) {
130
131        super(formatString, xFormat, yFormat);
132    }
133
134    /**
135     * Creates the array of items that can be passed to the
136     * {@link MessageFormat} class for creating labels.
137     *
138     * @param dataset  the dataset ({@code null} not permitted).
139     * @param series  the series (zero-based index).
140     * @param item  the item (zero-based index).
141     *
142     * @return An array of seven items from the dataset formatted as
143     *         {@code String} objects (never {@code null}).
144     */
145    @Override
146    protected Object[] createItemArray(XYDataset dataset, int series,
147                                       int item) {
148
149        IntervalXYDataset intervalDataset = null;
150        if (dataset instanceof IntervalXYDataset) {
151            intervalDataset = (IntervalXYDataset) dataset;
152        }
153        Object[] result = new Object[7];
154        result[0] = dataset.getSeriesKey(series).toString();
155
156        double x = dataset.getXValue(series, item);
157        double xs = x;
158        double xe = x;
159        double y = dataset.getYValue(series, item);
160        double ys = y;
161        double ye = y;
162        if (intervalDataset != null) {
163            xs = intervalDataset.getStartXValue(series, item);
164            xe = intervalDataset.getEndXValue(series, item);
165            ys = intervalDataset.getStartYValue(series, item);
166            ye = intervalDataset.getEndYValue(series, item);
167        }
168
169        DateFormat xdf = getXDateFormat();
170        if (xdf != null) {
171            result[1] = xdf.format(new Date((long) x));
172            result[2] = xdf.format(new Date((long) xs));
173            result[3] = xdf.format(new Date((long) xe));
174        }
175        else {
176            NumberFormat xnf = getXFormat();
177            result[1] = xnf.format(x);
178            result[2] = xnf.format(xs);
179            result[3] = xnf.format(xe);
180        }
181
182        NumberFormat ynf = getYFormat();
183        DateFormat ydf = getYDateFormat();
184        if (Double.isNaN(y) && dataset.getY(series, item) == null) {
185            result[4] = getNullYString();
186        }
187        else {
188            if (ydf != null) {
189                result[4] = ydf.format(new Date((long) y));
190            }
191            else {
192                result[4] = ynf.format(y);
193            }
194        }
195        if (Double.isNaN(ys) && intervalDataset != null
196                && intervalDataset.getStartY(series, item) == null) {
197            result[5] = getNullYString();
198        }
199        else {
200            if (ydf != null) {
201                result[5] = ydf.format(new Date((long) ys));
202            }
203            else {
204                result[5] = ynf.format(ys);
205            }
206        }
207        if (Double.isNaN(ye) && intervalDataset != null
208                && intervalDataset.getEndY(series, item) == null) {
209            result[6] = getNullYString();
210        }
211        else {
212            if (ydf != null) {
213                result[6] = ydf.format(new Date((long) ye));
214            }
215            else {
216                result[6] = ynf.format(ye);
217            }
218        }
219        return result;
220    }
221
222    /**
223     * Generates the item label text for an item in a dataset.
224     *
225     * @param dataset  the dataset ({@code null} not permitted).
226     * @param series  the series index (zero-based).
227     * @param item  the item index (zero-based).
228     *
229     * @return The label text (possibly {@code null}).
230     */
231    @Override
232    public String generateLabel(XYDataset dataset, int series, int item) {
233        return generateLabelString(dataset, series, item);
234    }
235
236    /**
237     * Returns an independent copy of the generator.
238     *
239     * @return A clone.
240     *
241     * @throws CloneNotSupportedException if cloning is not supported.
242     */
243    @Override
244    public Object clone() throws CloneNotSupportedException {
245        return super.clone();
246    }
247
248    /**
249     * Tests this object for equality with an arbitrary object.
250     *
251     * @param obj  the other object ({@code null} permitted).
252     *
253     * @return A boolean.
254     */
255    @Override
256    public boolean equals(Object obj) {
257        if (obj == this) {
258            return true;
259        }
260        if (!(obj instanceof IntervalXYItemLabelGenerator)) {
261            return false;
262        }
263        return super.equals(obj);
264    }
265
266}