|
jfreechart example source code file (DefaultXYZDataset.java)
The jfreechart DefaultXYZDataset.java source code/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ---------------------- * DefaultXYZDataset.java * ---------------------- * (C) Copyright 2006-2008, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * Changes * ------- * 12-Jul-2006 : Version 1 (DG); * 06-Oct-2006 : Fixed API doc warnings (DG); * 02-Nov-2006 : Fixed a problem with adding a new series with the same key * as an existing series (see bug 1589392) (DG); * 22-Apr-2008 : Implemented PublicCloneable (DG); * */ package org.jfree.data.xy; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.jfree.data.DomainOrder; import org.jfree.data.general.DatasetChangeEvent; import org.jfree.util.PublicCloneable; /** * A default implementation of the {@link XYZDataset} interface that stores * data values in arrays of double primitives. * * @since 1.0.2 */ public class DefaultXYZDataset extends AbstractXYZDataset implements XYZDataset, PublicCloneable { /** * Storage for the series keys. This list must be kept in sync with the * seriesList. */ private List seriesKeys; /** * Storage for the series in the dataset. We use a list because the * order of the series is significant. This list must be kept in sync * with the seriesKeys list. */ private List seriesList; /** * Creates a new <code>DefaultXYZDataset instance, initially * containing no data. */ public DefaultXYZDataset() { this.seriesKeys = new java.util.ArrayList(); this.seriesList = new java.util.ArrayList(); } /** * Returns the number of series in the dataset. * * @return The series count. */ public int getSeriesCount() { return this.seriesList.size(); } /** * Returns the key for a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * * @return The key for the series. * * @throws IllegalArgumentException if <code>series is not in the * specified range. */ public Comparable getSeriesKey(int series) { if ((series < 0) || (series >= getSeriesCount())) { throw new IllegalArgumentException("Series index out of bounds"); } return (Comparable) this.seriesKeys.get(series); } /** * Returns the index of the series with the specified key, or -1 if there * is no such series in the dataset. * * @param seriesKey the series key (<code>null permitted). * * @return The index, or -1. */ public int indexOf(Comparable seriesKey) { return this.seriesKeys.indexOf(seriesKey); } /** * Returns the order of the domain (x-) values in the dataset. In this * implementation, we cannot guarantee that the x-values are ordered, so * this method returns <code>DomainOrder.NONE. * * @return <code>DomainOrder.NONE. */ public DomainOrder getDomainOrder() { return DomainOrder.NONE; } /** * Returns the number of items in the specified series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * * @return The item count. * * @throws IllegalArgumentException if <code>series is not in the * specified range. */ public int getItemCount(int series) { if ((series < 0) || (series >= getSeriesCount())) { throw new IllegalArgumentException("Series index out of bounds"); } double[][] seriesArray = (double[][]) this.seriesList.get(series); return seriesArray[0].length; } /** * Returns the x-value for an item within a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * @param item the item index (in the range <code>0 to * <code>getItemCount(series)). * * @return The x-value. * * @throws ArrayIndexOutOfBoundsException if <code>series is not * within the specified range. * @throws ArrayIndexOutOfBoundsException if <code>item is not * within the specified range. * * @see #getX(int, int) */ public double getXValue(int series, int item) { double[][] seriesData = (double[][]) this.seriesList.get(series); return seriesData[0][item]; } /** * Returns the x-value for an item within a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * @param item the item index (in the range <code>0 to * <code>getItemCount(series)). * * @return The x-value. * * @throws ArrayIndexOutOfBoundsException if <code>series is not * within the specified range. * @throws ArrayIndexOutOfBoundsException if <code>item is not * within the specified range. * * @see #getXValue(int, int) */ public Number getX(int series, int item) { return new Double(getXValue(series, item)); } /** * Returns the y-value for an item within a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * @param item the item index (in the range <code>0 to * <code>getItemCount(series)). * * @return The y-value. * * @throws ArrayIndexOutOfBoundsException if <code>series is not * within the specified range. * @throws ArrayIndexOutOfBoundsException if <code>item is not * within the specified range. * * @see #getY(int, int) */ public double getYValue(int series, int item) { double[][] seriesData = (double[][]) this.seriesList.get(series); return seriesData[1][item]; } /** * Returns the y-value for an item within a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * @param item the item index (in the range <code>0 to * <code>getItemCount(series)). * * @return The y-value. * * @throws ArrayIndexOutOfBoundsException if <code>series is not * within the specified range. * @throws ArrayIndexOutOfBoundsException if <code>item is not * within the specified range. * * @see #getX(int, int) */ public Number getY(int series, int item) { return new Double(getYValue(series, item)); } /** * Returns the z-value for an item within a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * @param item the item index (in the range <code>0 to * <code>getItemCount(series)). * * @return The z-value. * * @throws ArrayIndexOutOfBoundsException if <code>series is not * within the specified range. * @throws ArrayIndexOutOfBoundsException if <code>item is not * within the specified range. * * @see #getZ(int, int) */ public double getZValue(int series, int item) { double[][] seriesData = (double[][]) this.seriesList.get(series); return seriesData[2][item]; } /** * Returns the z-value for an item within a series. * * @param series the series index (in the range <code>0 to * <code>getSeriesCount() - 1). * @param item the item index (in the range <code>0 to * <code>getItemCount(series)). * * @return The z-value. * * @throws ArrayIndexOutOfBoundsException if <code>series is not * within the specified range. * @throws ArrayIndexOutOfBoundsException if <code>item is not * within the specified range. * * @see #getZ(int, int) */ public Number getZ(int series, int item) { return new Double(getZValue(series, item)); } /** * Adds a series or if a series with the same key already exists replaces * the data for that series, then sends a {@link DatasetChangeEvent} to * all registered listeners. * * @param seriesKey the series key (<code>null not permitted). * @param data the data (must be an array with length 3, containing three * arrays of equal length, the first containing the x-values, the * second containing the y-values and the third containing the * z-values). */ public void addSeries(Comparable seriesKey, double[][] data) { if (seriesKey == null) { throw new IllegalArgumentException( "The 'seriesKey' cannot be null."); } if (data == null) { throw new IllegalArgumentException("The 'data' is null."); } if (data.length != 3) { throw new IllegalArgumentException( "The 'data' array must have length == 3."); } if (data[0].length != data[1].length || data[0].length != data[2].length) { throw new IllegalArgumentException("The 'data' array must contain " + "three arrays all having the same length."); } int seriesIndex = indexOf(seriesKey); if (seriesIndex == -1) { // add a new series this.seriesKeys.add(seriesKey); this.seriesList.add(data); } else { // replace an existing series this.seriesList.remove(seriesIndex); this.seriesList.add(seriesIndex, data); } notifyListeners(new DatasetChangeEvent(this, this)); } /** * Removes a series from the dataset, then sends a * {@link DatasetChangeEvent} to all registered listeners. * * @param seriesKey the series key (<code>null not permitted). * */ public void removeSeries(Comparable seriesKey) { int seriesIndex = indexOf(seriesKey); if (seriesIndex >= 0) { this.seriesKeys.remove(seriesIndex); this.seriesList.remove(seriesIndex); notifyListeners(new DatasetChangeEvent(this, this)); } } /** * Tests this <code>DefaultXYDataset instance for equality with an * arbitrary object. This method returns <code>true if and only if: * <ul> * <li> Other jfreechart examples (source code examples)Here is a short list of links related to this jfreechart DefaultXYZDataset.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.