⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datasetutilities.java

📁 Web图形化的Java库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        }
                    }
                }
                minimum = Math.min(minimum, total);

            }

            result = new Double(minimum);

        }

        return result;

    }

    /**
     * Returns the maximum value in the dataset range, assuming that values in
     * each category are "stacked".
     *
     * @param data  the dataset.
     *
     * @return the maximum value.
     */
    public static Number getMaximumStackedRangeValue(CategoryDataset data) {

        Number result = null;

        if (data != null) {

            double maximum = 0.0;

            int categoryCount = data.getRowCount();
            for (int item = 0; item < categoryCount; item++) {
                double total = 0.0;

                int seriesCount = data.getColumnCount();
                for (int series = 0; series < seriesCount; series++) {
                    Number number = data.getValue(series, item);
                    if (number != null) {
                        double value = number.doubleValue();
                        if (value > 0.0) {
                            total = total + value;
                        }
                    }
                }
                maximum = Math.max(maximum, total);

            }

            result = new Double(maximum);

        }

        return result;

    }

    /**
     * Creates an {@link XYDataset} by sampling the specified function over a fixed range.
     *
     * @param f  the function.
     * @param start  the start value for the range.
     * @param end  the end value for the range.
     * @param samples  the number of samples (must be > 1).
     * @param seriesName  the name to give the resulting series.
     *
     * @return  the dataset.
     */
    public static XYDataset sampleFunction2D(Function2D f,
                                             double start, double end, int samples,
                                             String seriesName) {

        if (start >= end) {
            throw new IllegalArgumentException("DatasetUtilities.createXYDataset(...): "
                + "start must be before end.");
        }

        if (samples < 2) {
            throw new IllegalArgumentException("DatasetUtilities.createXYDataset(...): "
                + "samples must be at least 2.");
        }

        XYSeries series = new XYSeries(seriesName);

        double step = (end - start) / samples;
        for (int i = 0; i <= samples; i++) {
            double x = start + (step * i);
            series.add(x, f.getValue(x));
        }

        XYSeriesCollection collection = new XYSeriesCollection(series);
        return collection;

    }

    /**
     * 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);
            }
        }
        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 taken from the supplied arrays.
     *
     * @param rowKeys  the row keys.
     * @param columnKeys  the column keys.
     * @param data  the data.
     *
     * @return The dataset.
     */
    public static CategoryDataset createCategoryDataset(String[] rowKeys,
                                                        String[] columnKeys,
                                                        double[][] data) {

        DefaultCategoryDataset result = new DefaultCategoryDataset();
        for (int r = 0; r < data.length; r++) {
            String rowKey = rowKeys[r];
            for (int c = 0; c < data[r].length; c++) {
                String columnKey = columnKeys[c];
                result.addValue(new Double(data[r][c]), rowKey, columnKey);
            }
        }
        return result;

    }

    /**
     * Creates a {@link CategoryDataset} by copying the data from the supplied {@link KeyedValues}
     * instance.
     *
     * @param rowKey  the row key.
     * @param rowData  the row data.
     *
     * @return A dataset.
     */
    public static CategoryDataset createCategoryDataset(String rowKey, KeyedValues rowData) {

        DefaultCategoryDataset result = new DefaultCategoryDataset();
        for (int i = 0; i < rowData.getItemCount(); i++) {
            result.addValue(rowData.getValue(i), rowKey, rowData.getKey(i));
        }
        return result;

    }

    /**
     * Returns <code>true</code> if the dataset is empty (or <code>null</code>), and
     * <code>false</code> otherwise.
     *
     * @param data  the dataset (<code>null</code> permitted).
     *
     * @return A boolean.
     */
    public static boolean isEmptyOrNull(XYDataset data) {

        boolean result = true;

        if (data != null) {
            for (int s = 0; s < data.getSeriesCount(); s++) {
                if (data.getItemCount(s) > 0) {
                    result = false;
                    continue;
                }
            }
        }

        return result;

    }

    /**
     * Returns <code>true</code> if the dataset is empty (or <code>null</code>), and
     * <code>false</code> otherwise.
     *
     * @param dataset  the dataset (<code>null</code> permitted).
     *
     * @return A boolean.
     */
    public static boolean isEmptyOrNull(PieDataset dataset) {

        if (dataset == null) {
            return true;
        }

        int itemCount = dataset.getItemCount();
        if (itemCount == 0) {
            return true;
        }

        for (int item = 0; item < itemCount; item++) {
            Number y = dataset.getValue(item);
            if (y != null) {
                double yy = y.doubleValue();
                if (yy > 0.0) {
                    return false;
                }
            }
        }

        return true;

    }

    /**
     * Returns <code>true</code> if the dataset is empty (or <code>null</code>), and
     * <code>false</code> otherwise.
     *
     * @param data  the dataset (<code>null</code> permitted).
     *
     * @return A boolean.
     */
    public static boolean isEmptyOrNull(CategoryDataset data) {

        if (data == null) {
            return true;
        }

        int rowCount = data.getRowCount();
        int columnCount = data.getColumnCount();
        if (rowCount == 0 || columnCount == 0) {
            return true;
        }

        for (int r = 0; r < rowCount; r++) {
            for (int c = 0; c < columnCount; c++) {
                if (data.getValue(r, c) != null) {
                    return false;
                }

            }
        }

        return true;

    }

    /**
     * Creates an "Other" slice for percentages below the percent threshold.
     *
     * @param data  the PieDataset.
     * @param percentThreshold  the percent threshold.
     * @return A PieDataset.
     */
    public static PieDataset limitPieDataset(PieDataset data, double percentThreshold) {
        return DatasetUtilities.limitPieDataset(data, percentThreshold, 1, "Other");
    }

    /**
     * Create an "Other" slice for percentages below the percent threshold providing there
     * are more slices below the percent threshold than specified in the slice threshold.
     *
     * @param data  the PieDataset.
     * @param percentThreshold  the percent threshold.
     * @param sliceThreshold  the slice threshold.
     * @return A PieDataset.
     */
    public static PieDataset limitPieDataset(PieDataset data,
                                             double percentThreshold, int sliceThreshold) {
        return DatasetUtilities.limitPieDataset(data, percentThreshold, sliceThreshold, "Other");
    }

    /**
     * Create an Other slice with a given label for percentages below the percent threshold
     * providing there are more slices below the percent threshold than specified in the slice
     * threshold.
     *
     * @param data  the PieDataset.
     * @param percentThreshold  the percent threshold.
     * @param sliceThreshold  the slice threshold.
     * @param label  the label to give the "Other" slice.
     * @return A PieDataset.
     */
    public static PieDataset limitPieDataset(PieDataset data,
                                             double percentThreshold, int sliceThreshold,
                                             String label) {
        DefaultPieDataset newDataset = new DefaultPieDataset();
        double total = DatasetUtilities.getPieDatasetTotal(data);

        //  Iterate and find all keys below threshold percentThreshold
        List keys = data.getKeys();
        ArrayList otherKeys = new ArrayList();
        Iterator kIter = keys.iterator();
        while (kIter.hasNext()) {
            Comparable key = (Comparable) kIter.next();
            Number dataValue = data.getValue(key);
            if (dataValue != null) {
                double value = dataValue.doubleValue();
                if (value / total < percentThreshold / 100) {
                    otherKeys.add(key);
                }
            }
        }

        //  Create new dataset with keys above threshold percentThreshold
        kIter = keys.iterator();
        double otherValue = 0;
        while (kIter.hasNext()) {
            Comparable key = (Comparable) kIter.next();
            Number dataValue = data.getValue(key);
            if (dataValue != null) {
                if (otherKeys.contains(key) && otherKeys.size() > sliceThreshold) {
                    //  Do not add key to dataset
                    otherValue += dataValue.doubleValue();
                }
                else {
                    //  Add key to dataset
                    newDataset.setValue(key, dataValue);
                }
            }
            //  Add other category if applicable
            if (otherKeys.size() > sliceThreshold) {
                newDataset.setValue(label, otherValue);
            }
        }
        return newDataset;
    }

    /**
     * Returns the minimum and maximum values for the dataset's range,
     * assuming that the series are stacked.
     *
     * @param data  the dataset.
     * @return  the value range.
     */
    public static Range getStackedRangeExtent(TableXYDataset data) {
        // check parameters...
        if (data == null) {
            return null;
        }
        double minimum = Double.POSITIVE_INFINITY;
        double maximum = Double.NEGATIVE_INFINITY;
        for (int itemNo = 0; itemNo < data.getItemCount(); itemNo++) {
            double value = 0;
            for (int seriesNo = 0; seriesNo < data.getSeriesCount(); seriesNo++) {
                if (data.getYValue(seriesNo, itemNo) != null) {
                    value += (data.getYValue(seriesNo, itemNo).doubleValue());
                }
            }
            if (value > maximum) {
                maximum = value;
            } 
            if (value < minimum) {
                minimum = value;
            } 
        }
        if (minimum == Double.POSITIVE_INFINITY) {
            return null;
        } 
        else {
            return new Range(minimum, maximum);
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -