datasetutilities.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,512 行 · 第 1/4 页
JAVA
1,512 行
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2005, 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.] * * --------------------- * DatasetUtilities.java * --------------------- * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): Andrzej Porebski (bug fix); * Jonathan Nash (bug fix); * Richard Atkinson; * Andreas Schroeder (beatification) * * $Id: DatasetUtilities.java,v 1.18 2005/05/20 08:20:04 mungady Exp $ * * Changes (from 18-Sep-2001) * -------------------------- * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG); * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG); * 15-Nov-2001 : Moved to package com.jrefinery.data.* in the JCommon class * library (DG); * Changed to handle null values from datasets (DG); * Bug fix (thanks to Andrzej Porebski) - initial value now set * to positive or negative infinity when iterating (DG); * 22-Nov-2001 : Datasets with containing no data now return null for min and * max calculations (DG); * 13-Dec-2001 : Extended to handle HighLowDataset and IntervalXYDataset (DG); * 15-Feb-2002 : Added getMinimumStackedRangeValue() and * getMaximumStackedRangeValue() (DG); * 28-Feb-2002 : Renamed Datasets.java --> DatasetUtilities.java (DG); * 18-Mar-2002 : Fixed bug in min/max domain calculation for datasets that * implement the CategoryDataset interface AND the XYDataset * interface at the same time. Thanks to Jonathan Nash for the * fix (DG); * 23-Apr-2002 : Added getDomainExtent() and getRangeExtent() methods (DG); * 13-Jun-2002 : Modified range measurements to handle * IntervalCategoryDataset (DG); * 12-Jul-2002 : Method name change in DomainInfo interface (DG); * 30-Jul-2002 : Added pie dataset summation method (DG); * 01-Oct-2002 : Added a method for constructing an XYDataset from a Function2D * instance (DG); * 24-Oct-2002 : Amendments required following changes to the CategoryDataset * interface (DG); * 18-Nov-2002 : Changed CategoryDataset to TableDataset (DG); * 04-Mar-2003 : Added isEmpty(XYDataset) method (DG); * 05-Mar-2003 : Added a method for creating a CategoryDataset from a * KeyedValues instance (DG); * 15-May-2003 : Renamed isEmpty --> isEmptyOrNull (DG); * 25-Jun-2003 : Added limitPieDataset methods (RA); * 26-Jun-2003 : Modified getDomainExtent() method to accept null datasets (DG); * 27-Jul-2003 : Added getStackedRangeExtent(TableXYDataset data) (RA); * 18-Aug-2003 : getStackedRangeExtent(TableXYDataset data) now handles null * values (RA); * 02-Sep-2003 : Added method to check for null or empty PieDataset (DG); * 18-Sep-2003 : Fix for bug 803660 (getMaximumRangeValue for * CategoryDataset) (DG); * 20-Oct-2003 : Added getCumulativeRangeExtent() method (DG); * 09-Jan-2003 : Added argument checking code to the createCategoryDataset() * method (DG); * 23-Mar-2004 : Fixed bug in getMaximumStackedRangeValue() method (DG); * 31-Mar-2004 : Exposed the extent iteration algorithms to use one of them and * applied noninstantiation pattern (AS); * 11-May-2004 : Renamed getPieDatasetTotal --> calculatePieDatasetTotal (DG); * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue(); * 24-Aug-2004 : Added argument checks to createCategoryDataset() method (DG); * 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG); * 06-Oct-2004 : Renamed findDomainExtent() --> findDomainBounds(), * findRangeExtent() --> findRangeBounds() (DG); * 07-Jan-2005 : Renamed findStackedRangeExtent() --> findStackedRangeBounds(), * findCumulativeRangeExtent() --> findCumulativeRangeBounds(), * iterateXYRangeExtent() --> iterateXYRangeBounds(), * removed deprecated methods (DG); * 03-Feb-2005 : The findStackedRangeBounds() methods now return null for * empty datasets (DG); * 03-Mar-2005 : Moved createNumberArray() and createNumberArray2D() methods * from DatasetUtilities --> DataUtilities (DG); * */package org.jfree.data.general;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.jfree.data.DomainInfo;import org.jfree.data.KeyToGroupMap;import org.jfree.data.KeyedValues;import org.jfree.data.Range;import org.jfree.data.RangeInfo;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.category.IntervalCategoryDataset;import org.jfree.data.function.Function2D;import org.jfree.data.xy.OHLCDataset;import org.jfree.data.xy.IntervalXYDataset;import org.jfree.data.xy.TableXYDataset;import org.jfree.data.xy.XYDataset;import org.jfree.data.xy.XYSeries;import org.jfree.data.xy.XYSeriesCollection;import org.jfree.util.ArrayUtilities;/** * A collection of useful static methods relating to datasets. */public final class DatasetUtilities { /** * Private constructor for non-instanceability. */ private DatasetUtilities() { // now try to instantiate this ;-) } /** * Calculates the total of all the values in a {@link PieDataset}. If * the dataset contains negative or <code>null</code> values, they are * ignored. * * @param dataset the dataset (<code>null</code> not permitted). * * @return The total. */ public static double calculatePieDatasetTotal(PieDataset dataset) { if (dataset == null) { throw new IllegalArgumentException("Null 'dataset' argument."); } List keys = dataset.getKeys(); double totalValue = 0; Iterator iterator = keys.iterator(); while (iterator.hasNext()) { Comparable current = (Comparable) iterator.next(); if (current != null) { Number value = dataset.getValue(current); double v = 0.0; if (value != null) { v = value.doubleValue(); } if (v > 0) { totalValue = totalValue + v; } } } return totalValue; } /** * Creates a pie dataset from a table dataset by taking all the values * for a single row. * * @param dataset the dataset (<code>null</code> not permitted). * @param rowKey the row key. * * @return A pie dataset. */ public static PieDataset createPieDatasetForRow(CategoryDataset dataset, Comparable rowKey) { int row = dataset.getRowIndex(rowKey); return createPieDatasetForRow(dataset, row); } /** * Creates a pie dataset from a table dataset by taking all the values * for a single row. * * @param dataset the dataset (<code>null</code> not permitted). * @param row the row (zero-based index). * * @return A pie dataset. */ public static PieDataset createPieDatasetForRow(CategoryDataset dataset, int row) { DefaultPieDataset result = new DefaultPieDataset(); int columnCount = dataset.getColumnCount(); for (int current = 0; current < columnCount; current++) { Comparable columnKey = dataset.getColumnKey(current); result.setValue(columnKey, dataset.getValue(row, current)); } return result; } /** * Creates a pie dataset from a table dataset by taking all the values * for a single column. * * @param dataset the dataset (<code>null</code> not permitted). * @param columnKey the column key. * * @return A pie dataset. */ public static PieDataset createPieDatasetForColumn(CategoryDataset dataset, Comparable columnKey) { int column = dataset.getColumnIndex(columnKey); return createPieDatasetForColumn(dataset, column); } /** * Creates a pie dataset from a {@link CategoryDataset} by taking all the * values for a single column. * * @param dataset the dataset (<code>null</code> not permitted). * @param column the column (zero-based index). * * @return A pie dataset. */ public static PieDataset createPieDatasetForColumn(CategoryDataset dataset, int column) { DefaultPieDataset result = new DefaultPieDataset(); int rowCount = dataset.getRowCount(); for (int i = 0; i < rowCount; i++) { Comparable rowKey = dataset.getRowKey(i); result.setValue(rowKey, dataset.getValue(i, column)); } return result; } /** * Creates a new pie dataset based on the supplied dataset, but modified * by aggregating all the low value items (those whose value is lower * than the <code>percentThreshold</code>) into a single item with the * key "Other". * * @param source the source dataset (<code>null</code> not permitted). * @param key a new key for the aggregated items (<code>null</code> not * permitted). * @param minimumPercent the percent threshold. * * @return The pie dataset with (possibly) aggregated items. */ public static PieDataset createConsolidatedPieDataset(PieDataset source, Comparable key, double minimumPercent) { return DatasetUtilities.createConsolidatedPieDataset( source, key, minimumPercent, 2 ); } /** * Creates a new pie dataset based on the supplied dataset, but modified * by aggregating all the low value items (those whose value is lower * than the <code>percentThreshold</code>) into a single item. The * aggregated items are assigned the specified key. Aggregation only * occurs if there are at least <code>minItems</code> items to aggregate. * * @param source the source dataset (<code>null</code> not permitted). * @param key the key to represent the aggregated items. * @param minimumPercent the percent threshold (ten percent is 0.10). * @param minItems only aggregate low values if there are at least this * many. * * @return The pie dataset with (possibly) aggregated items. */ public static PieDataset createConsolidatedPieDataset(PieDataset source, Comparable key, double minimumPercent, int minItems) { DefaultPieDataset result = new DefaultPieDataset(); double total = DatasetUtilities.calculatePieDatasetTotal(source); // Iterate and find all keys below threshold percentThreshold List keys = source.getKeys(); ArrayList otherKeys = new ArrayList(); Iterator iterator = keys.iterator(); while (iterator.hasNext()) { Comparable currentKey = (Comparable) iterator.next(); Number dataValue = source.getValue(currentKey); if (dataValue != null) { double value = dataValue.doubleValue(); if (value / total < minimumPercent) { otherKeys.add(currentKey); } } } // Create new dataset with keys above threshold percentThreshold iterator = keys.iterator(); double otherValue = 0; while (iterator.hasNext()) { Comparable currentKey = (Comparable) iterator.next(); Number dataValue = source.getValue(currentKey); if (dataValue != null) { if (otherKeys.contains(currentKey) && otherKeys.size() >= minItems) { // Do not add key to dataset otherValue += dataValue.doubleValue(); } else { // Add key to dataset result.setValue(currentKey, dataValue); } } } // Add other category if applicable if (otherKeys.size() >= minItems) { result.setValue(key, otherValue); } return result; } /** * Creates a {@link CategoryDataset} that contains a copy of the data in an * array (instances of <code>Double</code> are created to represent the * data items). * <p> * Row and column keys are created by appending 0, 1, 2, ... to the * supplied prefixes. * * @param rowKeyPrefix the row key prefix. * @param columnKeyPrefix the column key prefix. * @param data the data. * * @return The dataset. */ public static CategoryDataset createCategoryDataset(String rowKeyPrefix, String columnKeyPrefix, double[][] data) { DefaultCategoryDataset result = new DefaultCategoryDataset(); for (int r = 0; r < data.length; r++) { String rowKey = rowKeyPrefix + (r + 1); for (int c = 0; c < data[r].length; c++) { String columnKey = columnKeyPrefix + (c + 1); result.addValue(new Double(data[r][c]), rowKey, columnKey); } } return result; } /** * Creates a {@link CategoryDataset} that contains a copy of the data in * an array. * <p> * Row and column keys are created by appending 0, 1, 2, ... to the * supplied prefixes. * * @param rowKeyPrefix the row key prefix. * @param columnKeyPrefix the column key prefix. * @param data the data. * * @return The dataset. */ public static CategoryDataset createCategoryDataset(String rowKeyPrefix, String columnKeyPrefix, Number[][] data) { DefaultCategoryDataset result = new DefaultCategoryDataset(); for (int r = 0; r < data.length; r++) { String rowKey = rowKeyPrefix + (r + 1); for (int c = 0; c < data[r].length; c++) { String columnKey = columnKeyPrefix + (c + 1); result.addValue(data[r][c], rowKey, columnKey); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?