Example usage for org.jfree.data.general DefaultPieDataset sortByValues

List of usage examples for org.jfree.data.general DefaultPieDataset sortByValues

Introduction

In this page you can find the example usage for org.jfree.data.general DefaultPieDataset sortByValues.

Prototype

public void sortByValues(SortOrder order) 

Source Link

Document

Sorts the dataset's items by value and sends a DatasetChangeEvent to all registered listeners.

Usage

From source file:fr.gouv.diplomatie.applitutoriel.utility.Graphique.java

/**
 * Creer camember3 d.//w  w w . jav a2  s  . c om
 *
 * @param title
 *            the title
 * @param dataset
 *            the dataset
 * @param legend
 *            the legend
 * @param tooltips
 *            the tooltips
 * @param urls
 *            the urls
 * @return the j free chart
 * @throws FontFormatException
 *             the font format exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static JFreeChart creerCamember3D(final String title, final DefaultPieDataset dataset,
        final boolean legend, final boolean tooltips, final boolean urls)
        throws FontFormatException, IOException {

    dataset.sortByValues(SortOrder.DESCENDING);
    final JFreeChart jfreeChart = ChartFactory.createPieChart3D(title, dataset, legend, tooltips, urls);

    jfreeChart.setBackgroundPaint(Color.white);
    jfreeChart.setBorderVisible(true);
    jfreeChart.getLegend().setPosition(RectangleEdge.LEFT);
    final GraphicsEnvironment graph = GraphicsEnvironment.getLocalGraphicsEnvironment();
    final InputStream inputStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("hornet/framework/font/LiberationSans-Bold.ttf");
    final Font font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
    graph.registerFont(font);
    jfreeChart.getLegend().setItemFont(new Font("Liberation Sans", Font.BOLD, 11));
    jfreeChart.getLegend().setHeight(400);
    jfreeChart.getLegend().setBorder(0, 0, 0, 0);
    jfreeChart.setTitle(new TextTitle(title, new Font("Liberation Sans", Font.BOLD, 16)));
    final PiePlot piePlot = (PiePlot) jfreeChart.getPlot();

    final int nbData = dataset.getItemCount();
    int cptColor = 0;
    for (int x = 0; x < nbData; x++) {
        if (cptColor >= listColor.size()) {
            cptColor = 0;
        }
        piePlot.setSectionPaint(dataset.getKey(x), listColor.get(cptColor));

        cptColor++;

    }

    piePlot.setForegroundAlpha(0.5f);
    piePlot.setLabelFont(new Font("Liberation Sans", Font.BOLD, 12));
    piePlot.setLabelOutlineStroke(null);
    piePlot.setLabelLinkStroke(new BasicStroke(0.4f));
    piePlot.setLabelBackgroundPaint(Color.WHITE);
    piePlot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD);
    piePlot.setBackgroundAlpha(0);
    piePlot.setOutlineVisible(false);
    piePlot.setForegroundAlpha(1); // transparence
    piePlot.setInteriorGap(0); // le camembert occupe plus de place
    piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
    piePlot.setStartAngle(70);
    piePlot.setCircular(true); // force pour avoir un cercle et pas un oval
    piePlot.setMaximumLabelWidth(0.20);
    piePlot.setBaseSectionOutlinePaint(Color.BLACK); // bordure du camembert

    return jfreeChart;

}

From source file:org.sakaiproject.sitestats.impl.chart.ChartServiceImpl.java

private AbstractDataset getPieDataset(Report report) {
    List<Stat> reportData = report.getReportData();

    // fill dataset
    DefaultPieDataset dataSet = new DefaultPieDataset();
    String dataSource = report.getReportDefinition().getReportParams().getHowChartSource();
    //int total = 0;
    double total = 0;
    for (Stat s : reportData) {
        Comparable key = getStatValue(s, dataSource);
        if (key != null) {
            try {
                Number existingValue = dataSet.getValue(key);
                dataSet.setValue(key, getTotalValue(existingValue, s, report));
                total += getTotalValue(s, report).doubleValue();
            } catch (UnknownKeyException e) {
                dataSet.setValue(key, getTotalValue(s, report));
                total += getTotalValue(s, report).doubleValue();
            }// ww w  .  ja  v a  2  s .  com
        }
    }

    // sort
    dataSet.sortByValues(SortOrder.DESCENDING);

    // fill in final key values in dataset
    // show only top values (aggregate remaining in 'others')
    int maxDisplayedItems = 10;
    int currItem = 1;
    Number othersValues = Integer.valueOf(0);
    List<Comparable> keys = dataSet.getKeys();
    for (Comparable key : keys) {
        Number existingValue = dataSet.getValue(key);
        if (currItem < maxDisplayedItems) {
            // re-compute values
            double percentage = (double) existingValue.doubleValue() * 100 / total;
            double valuePercentage = Util.round(percentage, (percentage > 0.1) ? 1 : 2);
            // replace key with updated label
            StringBuilder keyStr = new StringBuilder(key.toString());
            keyStr.append(' ');
            keyStr.append(valuePercentage);
            keyStr.append("% (");
            if ((double) existingValue.intValue() == existingValue.doubleValue())
                keyStr.append(existingValue.intValue());
            else
                keyStr.append(existingValue.doubleValue());
            keyStr.append(")");
            dataSet.remove(key);
            dataSet.setValue(keyStr.toString(), existingValue);
        } else {
            othersValues = Integer.valueOf(othersValues.intValue() + existingValue.intValue());
            dataSet.remove(key);
        }
        currItem++;
    }
    // compute "Others" value
    if (othersValues.intValue() > 0) {
        double percentage = (double) othersValues.doubleValue() * 100 / total;
        double valuePercentage = Util.round(percentage, (percentage > 0.1) ? 1 : 2);
        // replace key with updated label
        StringBuilder keyStr = new StringBuilder(msgs.getString("pie_chart_others"));
        keyStr.append(' ');
        keyStr.append(valuePercentage);
        keyStr.append("% (");
        if ((double) othersValues.intValue() == othersValues.doubleValue())
            keyStr.append(othersValues.intValue());
        else
            keyStr.append(othersValues.doubleValue());
        keyStr.append(")");
        dataSet.setValue(keyStr.toString(), othersValues);
    }

    return dataSet;
}