Example usage for org.jfree.data.general DatasetUtilities calculatePieDatasetTotal

List of usage examples for org.jfree.data.general DatasetUtilities calculatePieDatasetTotal

Introduction

In this page you can find the example usage for org.jfree.data.general DatasetUtilities calculatePieDatasetTotal.

Prototype

public static double calculatePieDatasetTotal(PieDataset dataset) 

Source Link

Document

Calculates the total of all the values in a PieDataset .

Usage

From source file:com.pureinfo.srm.reports.impl.PieChartBuilder.java

private void fillChartInfo(DefaultPieDataset _dataset) {
    int n = _dataset.getItemCount();
    m_ChartInfo = new ChartInfo();
    m_ChartInfo.setChartTitle(m_sTitle);
    NumberFormat format = NumberFormat.getNumberInstance();
    String[] labels = new String[n];
    for (int i = 0; i < n; i++) {
        labels[i] = "" + _dataset.getKey(i);
        labels[i] += " = ";
        labels[i] += format.format(_dataset.getValue(i));
        double totalValue = DatasetUtilities.calculatePieDatasetTotal(_dataset);
        if (totalValue != 0) {
            labels[i] += "  ";
            labels[i] += PERCENT_NUMBER_FORMAT.format(_dataset.getValue(i).doubleValue() / totalValue);
        }/*  w  ww.j  a  v a2s. c o m*/
    }
    m_ChartInfo.setLabels(labels);
    m_ChartInfo.setShowBoder(true);
    m_ChartInfo.setChartSize(ChartInfo.SIZE_WIDE_AND_THIN);
    m_ChartInfo.setLengedPosition(ChartInfo.LENGEND_POSITION_BUTTOM);
}

From source file:org.jfree.data.general.DatasetUtilities.java

/**
 * 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.//from w  w w.j  a v  a 2 s .c om
 *
 * @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;
}