List of usage examples for org.jfree.data.general DatasetUtils calculatePieDatasetTotal
public static double calculatePieDatasetTotal(PieDataset dataset)
From source file:org.jfree.data.general.DatasetUtils.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}) into a single item. The * aggregated items are assigned the specified key. Aggregation only * occurs if there are at least {@code minItems} items to aggregate. * * @param source the source dataset ({@code null} 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 ww . java2 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 = DatasetUtils.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; }