📄 defaulttablexydataset.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, 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., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* --------------------------
* DefaultTableXYDataset.java
* --------------------------
* (C) Copyright 2003, 2004, by Richard Atkinson and Contributors.
*
* Original Author: Richard Atkinson;
* Contributor(s): Jody Brownell;
* David Gilbert (for Object Refinery Limited);
* Andreas Schroeder;
*
* $Id: DefaultTableXYDataset.java,v 1.2 2004/09/08 09:16:36 mungady Exp $
*
* Changes:
* --------
* 27-Jul-2003 : XYDataset that forces each series to have a value for every X-point
* which is essential for stacked XY area charts (RA);
* 18-Aug-2003 : Fixed event notification when removing and updating series (RA);
* 22-Sep-2003 : Functionality moved from TableXYDataset to DefaultTableXYDataset (RA);
* 23-Dec-2003 : Added patch for large datasets, submitted by Jody Brownell (DG);
* 16-Feb-2004 : Added pruning methods (DG);
* 31-Mar-2004 : Provisional implementation of IntervalXYDataset (AS);
* 01-Apr-2004 : Sound implementation of IntervalXYDataset (AS);
* 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
* 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
* 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.xy (DG);
*
*/
package org.jfree.data.xy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.jfree.data.DomainInfo;
import org.jfree.data.Range;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.SeriesChangeEvent;
import org.jfree.util.ObjectUtils;
/**
* An {@link XYDataset} where every series shares the same x-values (required for
* generating stacked area charts).
*
* @author Richard Atkinson
*/
public class DefaultTableXYDataset extends AbstractIntervalXYDataset
implements TableXYDataset, IntervalXYDataset, DomainInfo {
/** Storage for the data. */
private List data = null;
/** Storage for the x values. */
private HashSet xPoints = null;
/** A flag that controls whether or not events are propogated. */
private boolean propagateEvents = true;
/** A flag that controls auto pruning. */
private boolean autoPrune = false;
/** The delegate used to control the interval width. */
private IntervalXYDelegate intervalDelegate;
/**
* Creates a new empty dataset.
*/
public DefaultTableXYDataset() {
this(false);
}
/**
* Creates a new empty dataset.
*
* @param autoPrune a flag that controls whether or not x-values are removed whenever the
* corresponding y-values are all <code>null</code>.
*/
public DefaultTableXYDataset(boolean autoPrune) {
this.autoPrune = autoPrune;
this.data = new ArrayList();
this.xPoints = new HashSet();
this.intervalDelegate = new IntervalXYDelegate(this, false);
}
/**
* Returns the flag that controls whether or not x-values are removed from the dataset when
* the corresponding y-values are all <code>null</code>.
*
* @return A boolean.
*/
public boolean isAutoPrune() {
return this.autoPrune;
}
/**
* Adds a series to the collection and sends a {@link DatasetChangeEvent} to all registered
* listeners. The series should be configured to NOT allow duplicate x-values.
*
* @param series the series (<code>null</code> not permitted).
*/
public void addSeries(XYSeries series) {
if (series == null) {
throw new IllegalArgumentException("Null 'series' argument.");
}
if (series.getAllowDuplicateXValues()) {
throw new IllegalArgumentException("Cannot accept XY Series that allow duplicate "
+ "values. Use XYSeries(seriesName, false) constructor.");
}
updateXPoints(series);
this.data.add(series);
series.addChangeListener(this);
fireDatasetChanged();
}
/**
* Adds any unique x-values from 'series' to the dataset, and also adds any x-values that are
* in the dataset but not in 'series' to the series.
*
* @param series the series (<code>null</code> not permitted).
*/
private void updateXPoints(XYSeries series) {
if (series == null) {
throw new IllegalArgumentException("Null 'series' not permitted.");
}
HashSet seriesXPoints = new HashSet();
boolean savedState = this.propagateEvents;
this.propagateEvents = false;
for (int itemNo = 0; itemNo < series.getItemCount(); itemNo++) {
Number xValue = series.getX(itemNo);
seriesXPoints.add(xValue);
if (!this.xPoints.contains(xValue)) {
this.xPoints.add(xValue);
for (int seriesNo = 0; seriesNo < this.data.size(); seriesNo++) {
XYSeries dataSeries = (XYSeries) this.data.get(seriesNo);
if (!dataSeries.equals(series)) {
dataSeries.add(xValue, null);
}
}
}
}
Iterator iterator = this.xPoints.iterator();
while (iterator.hasNext()) {
final Number xPoint = (Number) iterator.next();
if (!seriesXPoints.contains(xPoint)) {
series.add(xPoint, null);
}
}
this.propagateEvents = savedState;
}
/**
* Updates the x-values for all the series in the dataset.
*/
public void updateXPoints() {
this.propagateEvents = false;
for (int s = 0; s < this.data.size(); s++) {
updateXPoints((XYSeries) this.data.get(s));
}
if (this.autoPrune) {
prune();
}
this.propagateEvents = true;
}
/**
* Returns the number of series in the collection.
*
* @return the number of series in the collection.
*/
public int getSeriesCount() {
return this.data.size();
}
/**
* Returns the number of x values in the dataset.
*
* @return the number of x values in the dataset.
*/
public int getItemCount() {
if (this.xPoints == null) {
return 0;
}
else {
return this.xPoints.size();
}
}
/**
* Returns a series.
*
* @param series the series (zero-based index).
*
* @return the series (never <code>null</code>).
*/
public XYSeries getSeries(int series) {
if ((series < 0) || (series > getSeriesCount())) {
throw new IllegalArgumentException(
"XYSeriesCollection.getSeries(...): index outside valid range.");
}
return (XYSeries) this.data.get(series);
}
/**
* Returns the name of a series.
*
* @param series the series (zero-based index).
*
* @return the name of a series.
*/
public String getSeriesName(int series) {
// check arguments...delegated
return getSeries(series).getName();
}
/**
* Returns the number of items in the specified series.
*
* @param series the series (zero-based index).
*
* @return the number of items in the specified series.
*/
public int getItemCount(int series) {
// check arguments...delegated
return getSeries(series).getItemCount();
}
/**
* Returns the x-value for the specified series and item.
*
* @param series the series (zero-based index).
* @param item the item (zero-based index).
*
* @return the x-value for the specified series and item.
*/
public Number getX(int series, int item) {
final XYSeries s = (XYSeries) this.data.get(series);
final XYDataItem dataItem = s.getDataItem(item);
return dataItem.getX();
}
/**
* Returns the starting X value for the specified series and item.
*
* @param series the series (zero-based index).
* @param item the item (zero-based index).
*
* @return The starting X value.
*/
public Number getStartX(int series, int item) {
return this.intervalDelegate.getStartX(series, item);
}
/**
* Returns the ending X value for the specified series and item.
*
* @param series the series (zero-based index).
* @param item the item (zero-based index).
*
* @return The ending X value.
*/
public Number getEndX(int series, int item) {
return this.intervalDelegate.getEndX(series, item);
}
/**
* Returns the y-value for the specified series and item.
*
* @param series the series (zero-based index).
* @param index the index of the item of interest (zero-based).
*
* @return the y-value for the specified series and item (possibly <code>null</code>).
*/
public Number getY(int series, int index) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -