Search in sources :

Example 1 with DefaultPieDataset

use of org.jfree.data.pie.DefaultPieDataset in project graphcode2vec by graphcode2vec.

the class PieDatasetHandler method startElement.

/**
 * Starts an element.
 *
 * @param namespaceURI  the namespace.
 * @param localName  the element name.
 * @param qName  the element name.
 * @param atts  the element attributes.
 *
 * @throws SAXException for errors.
 */
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    DefaultHandler current = getCurrentHandler();
    if (current != this) {
        current.startElement(namespaceURI, localName, qName, atts);
    } else if (qName.equals(PIEDATASET_TAG)) {
        this.dataset = new DefaultPieDataset();
    } else if (qName.equals(ITEM_TAG)) {
        ItemHandler subhandler = new ItemHandler(this, this);
        getSubHandlers().push(subhandler);
        subhandler.startElement(namespaceURI, localName, qName, atts);
    }
}
Also used : DefaultPieDataset(org.jfree.data.pie.DefaultPieDataset) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 2 with DefaultPieDataset

use of org.jfree.data.pie.DefaultPieDataset in project graphcode2vec by graphcode2vec.

the class ChartFactory method createPieChart.

/**
 * Creates a pie chart with default settings that compares 2 datasets.
 * The colour of each section will be determined by the move from the value
 * for the same key in <code>previousDataset</code>. ie if value1 > value2
 * then the section will be in green (unless <code>greenForIncrease</code>
 * is <code>false</code>, in which case it would be <code>red</code>).
 * Each section can have a shade of red or green as the difference can be
 * tailored between 0% (black) and percentDiffForMaxScale% (bright
 * red/green).
 * <p>
 * For instance if <code>percentDiffForMaxScale</code> is 10 (10%), a
 * difference of 5% will have a half shade of red/green, a difference of
 * 10% or more will have a maximum shade/brightness of red/green.
 * <P>
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 * <p>
 * Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
 * Xhenseval</a>.
 *
 * @param title  the chart title (<code>null</code> permitted).
 * @param dataset  the dataset for the chart (<code>null</code> permitted).
 * @param previousDataset  the dataset for the last run, this will be used
 *                         to compare each key in the dataset
 * @param percentDiffForMaxScale scale goes from bright red/green to black,
 *                               percentDiffForMaxScale indicate the change
 *                               required to reach top scale.
 * @param greenForIncrease  an increase since previousDataset will be
 *                          displayed in green (decrease red) if true.
 * @param legend  a flag specifying whether or not a legend is required.
 * @param subTitle displays a subtitle with colour scheme if true
 * @param showDifference  create a new dataset that will show the %
 *                        difference between the two datasets.
 *
 * @return A pie chart.
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset, int percentDiffForMaxScale, boolean greenForIncrease, boolean legend, boolean subTitle, boolean showDifference) {
    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    plot.setToolTipGenerator(new StandardPieToolTipGenerator());
    List keys = dataset.getKeys();
    DefaultPieDataset series = null;
    if (showDifference) {
        series = new DefaultPieDataset();
    }
    double colorPerPercent = 255.0 / percentDiffForMaxScale;
    for (Iterator it = keys.iterator(); it.hasNext(); ) {
        Comparable key = (Comparable) it.next();
        Number newValue = dataset.getValue(key);
        Number oldValue = previousDataset.getValue(key);
        if (oldValue == null) {
            if (greenForIncrease) {
                plot.setSectionPaint(key, Color.green);
            } else {
                plot.setSectionPaint(key, Color.red);
            }
            if (showDifference) {
                series.setValue(key + " (+100%)", newValue);
            }
        } else {
            double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
            double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255 : Math.abs(percentChange) * colorPerPercent);
            if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue() || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
                plot.setSectionPaint(key, new Color(0, (int) shade, 0));
            } else {
                plot.setSectionPaint(key, new Color((int) shade, 0, 0));
            }
            if (showDifference) {
                series.setValue(key + " (" + (percentChange >= 0 ? "+" : "") + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")", newValue);
            }
        }
    }
    if (showDifference) {
        plot.setDataset(series);
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    if (subTitle) {
        TextTitle subtitle = null;
        subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-" + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+" + percentDiffForMaxScale + "%", new Font("Tahoma", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}
Also used : DefaultPieDataset(org.jfree.data.pie.DefaultPieDataset) Color(java.awt.Color) StandardPieSectionLabelGenerator(org.jfree.chart.labels.StandardPieSectionLabelGenerator) Font(java.awt.Font) TextTitle(org.jfree.chart.title.TextTitle) StandardPieToolTipGenerator(org.jfree.chart.labels.StandardPieToolTipGenerator) Iterator(java.util.Iterator) PiePlot(org.jfree.chart.plot.PiePlot) MultiplePiePlot(org.jfree.chart.plot.MultiplePiePlot) RectangleInsets(org.jfree.chart.util.RectangleInsets) List(java.util.List)

Example 3 with DefaultPieDataset

use of org.jfree.data.pie.DefaultPieDataset in project graphcode2vec by graphcode2vec.

the class DatasetUtilities method createPieDatasetForRow.

/**
 * 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;
}
Also used : DefaultPieDataset(org.jfree.data.pie.DefaultPieDataset)

Example 4 with DefaultPieDataset

use of org.jfree.data.pie.DefaultPieDataset in project graphcode2vec by graphcode2vec.

the class ChartFactory method createPieChart.

/**
 * Creates a pie chart with default settings that compares 2 datasets.
 * The colour of each section will be determined by the move from the value
 * for the same key in <code>previousDataset</code>. ie if value1 > value2
 * then the section will be in green (unless <code>greenForIncrease</code>
 * is <code>false</code>, in which case it would be <code>red</code>).
 * Each section can have a shade of red or green as the difference can be
 * tailored between 0% (black) and percentDiffForMaxScale% (bright
 * red/green).
 * <p>
 * For instance if <code>percentDiffForMaxScale</code> is 10 (10%), a
 * difference of 5% will have a half shade of red/green, a difference of
 * 10% or more will have a maximum shade/brightness of red/green.
 * <P>
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 * <p>
 * Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
 * Xhenseval</a>.
 *
 * @param title  the chart title (<code>null</code> permitted).
 * @param dataset  the dataset for the chart (<code>null</code> permitted).
 * @param previousDataset  the dataset for the last run, this will be used
 *                         to compare each key in the dataset
 * @param percentDiffForMaxScale scale goes from bright red/green to black,
 *                               percentDiffForMaxScale indicate the change
 *                               required to reach top scale.
 * @param greenForIncrease  an increase since previousDataset will be
 *                          displayed in green (decrease red) if true.
 * @param legend  a flag specifying whether or not a legend is required.
 * @param locale  the locale (<code>null</code> not permitted).
 * @param subTitle displays a subtitle with colour scheme if true
 * @param showDifference  create a new dataset that will show the %
 *                        difference between the two datasets.
 *
 * @return A pie chart.
 *
 * @since 1.0.7
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset, int percentDiffForMaxScale, boolean greenForIncrease, boolean legend, Locale locale, boolean subTitle, boolean showDifference) {
    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
    List keys = dataset.getKeys();
    DefaultPieDataset series = null;
    if (showDifference) {
        series = new DefaultPieDataset();
    }
    double colorPerPercent = 255.0 / percentDiffForMaxScale;
    for (Iterator it = keys.iterator(); it.hasNext(); ) {
        Comparable key = (Comparable) it.next();
        Number newValue = dataset.getValue(key);
        Number oldValue = previousDataset.getValue(key);
        if (oldValue == null) {
            if (greenForIncrease) {
                plot.setSectionPaint(key, Color.green);
            } else {
                plot.setSectionPaint(key, Color.red);
            }
            if (showDifference) {
                series.setValue(key + " (+100%)", newValue);
            }
        } else {
            double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
            double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255 : Math.abs(percentChange) * colorPerPercent);
            if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue() || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
                plot.setSectionPaint(key, new Color(0, (int) shade, 0));
            } else {
                plot.setSectionPaint(key, new Color((int) shade, 0, 0));
            }
            if (showDifference) {
                series.setValue(key + " (" + (percentChange >= 0 ? "+" : "") + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")", newValue);
            }
        }
    }
    if (showDifference) {
        plot.setDataset(series);
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    if (subTitle) {
        TextTitle subtitle = null;
        subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-" + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+" + percentDiffForMaxScale + "%", new Font("Tahoma", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}
Also used : DefaultPieDataset(org.jfree.data.pie.DefaultPieDataset) Color(java.awt.Color) StandardPieSectionLabelGenerator(org.jfree.chart.labels.StandardPieSectionLabelGenerator) Font(java.awt.Font) TextTitle(org.jfree.chart.title.TextTitle) StandardPieToolTipGenerator(org.jfree.chart.labels.StandardPieToolTipGenerator) Iterator(java.util.Iterator) PiePlot(org.jfree.chart.plot.PiePlot) MultiplePiePlot(org.jfree.chart.plot.MultiplePiePlot) RectangleInsets(org.jfree.chart.util.RectangleInsets) List(java.util.List)

Example 5 with DefaultPieDataset

use of org.jfree.data.pie.DefaultPieDataset in project graphcode2vec by graphcode2vec.

the class DatasetUtilities method createConsolidatedPieDataset.

/**
 * 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;
}
Also used : DefaultPieDataset(org.jfree.data.pie.DefaultPieDataset) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DefaultPieDataset (org.jfree.data.pie.DefaultPieDataset)7 Iterator (java.util.Iterator)3 List (java.util.List)3 Color (java.awt.Color)2 Font (java.awt.Font)2 StandardPieSectionLabelGenerator (org.jfree.chart.labels.StandardPieSectionLabelGenerator)2 StandardPieToolTipGenerator (org.jfree.chart.labels.StandardPieToolTipGenerator)2 MultiplePiePlot (org.jfree.chart.plot.MultiplePiePlot)2 PiePlot (org.jfree.chart.plot.PiePlot)2 TextTitle (org.jfree.chart.title.TextTitle)2 RectangleInsets (org.jfree.chart.util.RectangleInsets)2 ArrayList (java.util.ArrayList)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1