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 * XYBarDataset.java
029 * -----------------
030 * (C) Copyright 2004-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.xy;
038
039import org.jfree.chart.util.PublicCloneable;
040import org.jfree.data.general.DatasetChangeEvent;
041import org.jfree.data.general.DatasetChangeListener;
042
043/**
044 * A dataset wrapper class that converts a standard {@link XYDataset} into an
045 * {@link IntervalXYDataset} suitable for use in creating XY bar charts.
046 */
047public class XYBarDataset extends AbstractIntervalXYDataset
048        implements IntervalXYDataset, DatasetChangeListener, PublicCloneable {
049
050    /** The underlying dataset. */
051    private XYDataset underlying;
052
053    /** The bar width. */
054    private double barWidth;
055
056    /**
057     * Creates a new dataset.
058     *
059     * @param underlying  the underlying dataset ({@code null} not
060     *     permitted).
061     * @param barWidth  the width of the bars.
062     */
063    public XYBarDataset(XYDataset underlying, double barWidth) {
064        this.underlying = underlying;
065        this.underlying.addChangeListener(this);
066        this.barWidth = barWidth;
067    }
068
069    /**
070     * Returns the underlying dataset that was specified via the constructor.
071     *
072     * @return The underlying dataset (never {@code null}).
073     */
074    public XYDataset getUnderlyingDataset() {
075        return this.underlying;
076    }
077
078    /**
079     * Returns the bar width.
080     *
081     * @return The bar width.
082     *
083     * @see #setBarWidth(double)
084     */
085    public double getBarWidth() {
086        return this.barWidth;
087    }
088
089    /**
090     * Sets the bar width and sends a {@link DatasetChangeEvent} to all
091     * registered listeners.
092     *
093     * @param barWidth  the bar width.
094     *
095     * @see #getBarWidth()
096     */
097    public void setBarWidth(double barWidth) {
098        this.barWidth = barWidth;
099        notifyListeners(new DatasetChangeEvent(this, this));
100    }
101
102    /**
103     * Returns the number of series in the dataset.
104     *
105     * @return The series count.
106     */
107    @Override
108    public int getSeriesCount() {
109        return this.underlying.getSeriesCount();
110    }
111
112    /**
113     * Returns the key for a series.
114     *
115     * @param series  the series index (in the range {@code 0} to
116     *     {@code getSeriesCount() - 1}).
117     *
118     * @return The series key.
119     */
120    @Override
121    public Comparable getSeriesKey(int series) {
122        return this.underlying.getSeriesKey(series);
123    }
124
125    /**
126     * Returns the number of items in a series.
127     *
128     * @param series  the series index (zero-based).
129     *
130     * @return The item count.
131     */
132    @Override
133    public int getItemCount(int series) {
134        return this.underlying.getItemCount(series);
135    }
136
137    /**
138     * Returns the x-value for an item within a series.
139     *
140     * @param series  the series index (zero-based).
141     * @param item  the item index (zero-based).
142     *
143     * @return The x-value.
144     *
145     * @see #getXValue(int, int)
146     */
147    @Override
148    public Number getX(int series, int item) {
149        return this.underlying.getX(series, item);
150    }
151
152    /**
153     * Returns the x-value (as a double primitive) for an item within a series.
154     *
155     * @param series  the series index (zero-based).
156     * @param item  the item index (zero-based).
157     *
158     * @return The value.
159     *
160     * @see #getX(int, int)
161     */
162    @Override
163    public double getXValue(int series, int item) {
164        return this.underlying.getXValue(series, item);
165    }
166
167    /**
168     * Returns the y-value for an item within a series.
169     *
170     * @param series  the series index (zero-based).
171     * @param item  the item index (zero-based).
172     *
173     * @return The y-value (possibly {@code null}).
174     *
175     * @see #getYValue(int, int)
176     */
177    @Override
178    public Number getY(int series, int item) {
179        return this.underlying.getY(series, item);
180    }
181
182    /**
183     * Returns the y-value (as a double primitive) for an item within a series.
184     *
185     * @param series  the series index (zero-based).
186     * @param item  the item index (zero-based).
187     *
188     * @return The value.
189     *
190     * @see #getY(int, int)
191     */
192    @Override
193    public double getYValue(int series, int item) {
194        return this.underlying.getYValue(series, item);
195    }
196
197    /**
198     * Returns the starting X value for the specified series and item.
199     *
200     * @param series  the series index (zero-based).
201     * @param item  the item index (zero-based).
202     *
203     * @return The value.
204     */
205    @Override
206    public Number getStartX(int series, int item) {
207        Number result = null;
208        Number xnum = this.underlying.getX(series, item);
209        if (xnum != null) {
210             result = xnum.doubleValue() - this.barWidth / 2.0;
211        }
212        return result;
213    }
214
215    /**
216     * Returns the starting x-value (as a double primitive) for an item within
217     * a series.
218     *
219     * @param series  the series index (zero-based).
220     * @param item  the item index (zero-based).
221     *
222     * @return The value.
223     *
224     * @see #getXValue(int, int)
225     */
226    @Override
227    public double getStartXValue(int series, int item) {
228        return getXValue(series, item) - this.barWidth / 2.0;
229    }
230
231    /**
232     * Returns the ending X value for the specified series and item.
233     *
234     * @param series  the series index (zero-based).
235     * @param item  the item index (zero-based).
236     *
237     * @return The value.
238     */
239    @Override
240    public Number getEndX(int series, int item) {
241        Number result = null;
242        Number xnum = this.underlying.getX(series, item);
243        if (xnum != null) {
244             result = xnum.doubleValue() + this.barWidth / 2.0;
245        }
246        return result;
247    }
248
249    /**
250     * Returns the ending x-value (as a double primitive) for an item within
251     * a series.
252     *
253     * @param series  the series index (zero-based).
254     * @param item  the item index (zero-based).
255     *
256     * @return The value.
257     *
258     * @see #getXValue(int, int)
259     */
260    @Override
261    public double getEndXValue(int series, int item) {
262        return getXValue(series, item) + this.barWidth / 2.0;
263    }
264
265    /**
266     * Returns the starting Y value for the specified series and item.
267     *
268     * @param series  the series index (zero-based).
269     * @param item  the item index (zero-based).
270     *
271     * @return The value.
272     */
273    @Override
274    public Number getStartY(int series, int item) {
275        return this.underlying.getY(series, item);
276    }
277
278    /**
279     * Returns the starting y-value (as a double primitive) for an item within
280     * a series.
281     *
282     * @param series  the series index (zero-based).
283     * @param item  the item index (zero-based).
284     *
285     * @return The value.
286     *
287     * @see #getYValue(int, int)
288     */
289    @Override
290    public double getStartYValue(int series, int item) {
291        return getYValue(series, item);
292    }
293
294    /**
295     * Returns the ending Y value for the specified series and item.
296     *
297     * @param series  the series index (zero-based).
298     * @param item  the item index (zero-based).
299     *
300     * @return The value.
301     */
302    @Override
303    public Number getEndY(int series, int item) {
304        return this.underlying.getY(series, item);
305    }
306
307    /**
308     * Returns the ending y-value (as a double primitive) for an item within
309     * a series.
310     *
311     * @param series  the series index (zero-based).
312     * @param item  the item index (zero-based).
313     *
314     * @return The value.
315     *
316     * @see #getYValue(int, int)
317     */
318    @Override
319    public double getEndYValue(int series, int item) {
320        return getYValue(series, item);
321    }
322
323    /**
324     * Receives notification of an dataset change event.
325     *
326     * @param event  information about the event.
327     */
328    @Override
329    public void datasetChanged(DatasetChangeEvent event) {
330        notifyListeners(event);
331    }
332
333    /**
334     * Tests this dataset for equality with an arbitrary object.
335     *
336     * @param obj  the object ({@code null} permitted).
337     *
338     * @return A boolean.
339     */
340    @Override
341    public boolean equals(Object obj) {
342        if (obj == this) {
343            return true;
344        }
345        if (!(obj instanceof XYBarDataset)) {
346            return false;
347        }
348        XYBarDataset that = (XYBarDataset) obj;
349        if (!this.underlying.equals(that.underlying)) {
350            return false;
351        }
352        if (this.barWidth != that.barWidth) {
353            return false;
354        }
355        return true;
356    }
357
358    /**
359     * Returns an independent copy of the dataset.  Note that:
360     * <ul>
361     * <li>the underlying dataset is only cloned if it implements the
362     * {@link PublicCloneable} interface;</li>
363     * <li>the listeners registered with this dataset are not carried over to
364     * the cloned dataset.</li>
365     * </ul>
366     *
367     * @return An independent copy of the dataset.
368     *
369     * @throws CloneNotSupportedException if the dataset cannot be cloned for
370     *         any reason.
371     */
372    @Override
373    public Object clone() throws CloneNotSupportedException {
374        XYBarDataset clone = (XYBarDataset) super.clone();
375        if (this.underlying instanceof PublicCloneable) {
376            PublicCloneable pc = (PublicCloneable) this.underlying;
377            clone.underlying = (XYDataset) pc.clone();
378        }
379        return clone;
380    }
381
382}