Example usage for org.jfree.chart.plot PiePlot PiePlot

List of usage examples for org.jfree.chart.plot PiePlot PiePlot

Introduction

In this page you can find the example usage for org.jfree.chart.plot PiePlot PiePlot.

Prototype

public PiePlot(PieDataset dataset) 

Source Link

Document

Creates a plot that will draw a pie chart for the specified dataset.

Usage

From source file:KIDLYFactory.java

/**
 * Creates a pie chart with default settings.
 * <P>/*from ww w.  j ava 2  s .c o m*/
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 *
 * @param title  the chart title (<code>null</code> permitted).
 * @param dataset  the dataset for the chart (<code>null</code> permitted).
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param locale  the locale (<code>null</code> not permitted).
 *
 * @return A pie chart.
 *
 * @since 1.0.7
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips,
        Locale locale) {

    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    currentTheme.apply(chart);
    return chart;

}

From source file:KIDLYFactory.java

/**
 * Creates a pie chart with default settings.
 * <P>//  www .  ja v  a 2s . c  o  m
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 *
 * @param title  the chart title (<code>null</code> permitted).
 * @param dataset  the dataset for the chart (<code>null</code> permitted).
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param urls  configure chart to generate URLs?
 *
 * @return A pie chart.
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips,
        boolean urls) {

    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
    }
    if (urls) {
        plot.setURLGenerator(new StandardPieURLGenerator());
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    currentTheme.apply(chart);
    return chart;
}

From source file:edu.dlnu.liuwenpeng.ChartFactory.ChartFactory.java

/**    
* 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).    // w ww  .j  av  a  2  s  .  co  m
* <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 tooltips  configure chart to generate tool tips?    
* @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, boolean tooltips, 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));

    if (tooltips) {
        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("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }

    return chart;
}

From source file:KIDLYFactory.java

/**
 * 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).//from  ww w.  ja  v  a2s.  co  m
 * <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 tooltips  configure chart to generate tool tips?
 * @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, boolean tooltips, 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));

    if (tooltips) {
        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("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}

From source file:com.appnativa.rare.ui.chart.jfreechart.ChartHandler.java

protected JFreeChart createCharts(ChartPanel chartPanel, ChartDefinition cd) {
    List<DataSetValue> datasets = createDataSets(cd);
    int len = datasets.size();
    UIFont f = getAxisLabelFont(cd.getRangeAxis());
    UIFontMetrics fm = UIFontMetrics.getMetrics(f);

    tickSize = (int) fm.getHeight();

    if (cd.getChartType() == ChartType.PIE) {
        PiePlot plot;/*from w w  w. j  a  v  a 2s  .co m*/
        PieCollection pie = (PieCollection) datasets.get(0).dataset;

        if (cd.isDraw3D()) {
            plot = new PiePlot3D(pie);
        } else {
            plot = new PiePlot(pie);
        }

        customizePiePlot(cd, plot, pie);

        return new JFreeChart(null, getChartFont(), plot, false);
    }

    ChartInfo ci = (ChartInfo) cd.getChartHandlerInfo();
    XYPlot xyplot = null;
    NumberAxis yAxis = cd.isDraw3D() ? new NumberAxis3DEx(cd, true, cd.getRangeLabel())
            : new NumberAxisEx(cd, false, cd.getRangeLabel());

    yAxis.setAutoRangeIncludesZero(false);
    yAxis.setAutoRange(false);

    NumberAxis xAxis;

    if (ci.categoryDomain) {
        LabelData[] labels = ci.createLabelData(cd, fm, true);

        xAxis = cd.isDraw3D() ? new NumberAxis3DEx(cd, true, cd.getDomainLabel())
                : new NumberAxisEx(cd, labels, cd.getDomainLabel());
    } else {
        xAxis = cd.isDraw3D() ? new NumberAxis3DEx(cd, true, cd.getDomainLabel())
                : new NumberAxisEx(cd, true, cd.getDomainLabel());
    }

    xAxis.setAutoRangeIncludesZero(false);
    xAxis.setAutoRange(false);
    xyplot = new XYPlotEx(null, xAxis, yAxis, null);

    for (int i = 0; i < len; i++) {
        DataSetValue dv = datasets.get(i);

        xyplot.setDataset(i, (XYDataset) dv.dataset);

        switch (dv.chartType) {
        case SPLINE:
            xyplot.setRenderer(i, new XYSplineRendererEx(ci.seriesData));

            break;

        case LINE:
            xyplot.setRenderer(i, cd.isDraw3D() ? new XYLine3DRendererEx(ci.seriesData)
                    : new XYLineAndShapeRendererEx(ci.seriesData));

            break;

        case BAR: {
            XYClusteredBarRendererEx renderer = new XYClusteredBarRendererEx(ci.seriesData);

            renderer.setShadowVisible(false);
            xyplot.setRenderer(i, renderer);
        }

            break;

        case STACKED_BAR: {
            StackedXYBarRenderer renderer = new XYStackedBarRendererEx(0.10, ci.seriesData);

            renderer.setDrawBarOutline(false);
            renderer.setShadowVisible(false);
            xyplot.setRenderer(i, renderer);
        }

            break;

        case RANGE_AREA:
            xyplot.setRenderer(i, new XYRangeAreaRendererEx(ci.seriesData));

            break;

        case RANGE_BAR:
            xyplot.setRenderer(i, new XYRangeBarRendererEx(ci.seriesData));

            break;

        case STACKED_AREA:
            XYStackedAreaRendererEx renderer = new XYStackedAreaRendererEx(ci.seriesData);

            renderer.setOutline(false);
            xyplot.setRenderer(i, renderer);

            break;

        case SPLINE_AREA:
        case AREA:
            XYItemRenderer r;

            if (dv.chartType == ChartType.SPLINE_AREA) {
                r = new XYAreaSplineRendererEx(ci.seriesData);
            } else {
                r = new XYAreaRendererEx(ci.seriesData);
            }

            xyplot.setRenderer(r);

            break;

        default:
            throw new InvalidParameterException("Unsupported chart type");
        }
    }

    customizeXYPlot(chartPanel, cd, xyplot);

    JFreeChart chart = new JFreeChart(null, getChartFont(), xyplot, false);

    return chart;
}

From source file:org.pentaho.platform.uifoundation.chart.JFreeChartEngine.java

private static JFreeChart createPieDatasetChart(final PieDatasetChartDefinition chartDefinition) {
    // TODO Make the following accessible from the chartDefinition
    boolean tooltips = true;
    boolean urls = true;
    // -----------------------------------------------------------

    String title = chartDefinition.getTitle();
    boolean legend = chartDefinition.isLegendIncluded();

    PiePlot plot = null;//from   ww  w .  j  a  v a2s.c o  m
    plot = chartDefinition.isThreeD() ? new PiePlot3D(chartDefinition) : new PiePlot(chartDefinition);
    JFreeChartEngine.updatePlot(plot, chartDefinition);
    JFreeChart pieChart = new JFreeChart(title, chartDefinition.getTitleFont(), plot, legend);
    TextTitle seriesTitle = new TextTitle("Series Title", new Font("SansSerif", Font.BOLD, 12)); //$NON-NLS-1$ //$NON-NLS-2$
    seriesTitle.setPosition(RectangleEdge.BOTTOM);
    pieChart.setTitle(title);
    pieChart.setBackgroundPaint(chartDefinition.getChartBackgroundPaint());

    if (tooltips) {
        PieToolTipGenerator tooltipGenerator = new StandardPieToolTipGenerator();
        plot.setToolTipGenerator(tooltipGenerator);
    }

    if (urls) {
        PieURLGenerator urlGenerator = new StandardPieURLGenerator();
        plot.setURLGenerator(urlGenerator);
    }

    return pieChart;
}

From source file:edu.dlnu.liuwenpeng.ChartFactory.ChartFactory.java

/**    
* 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).    //www.  j a  va  2  s.co  m
* <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 tooltips  configure chart to generate tool tips?    
* @param urls  configure chart to generate URLs?    
* @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 tooltips, boolean urls,
        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));

    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
    }
    if (urls) {
        plot.setURLGenerator(new StandardPieURLGenerator());
    }

    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("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }

    return chart;
}

From source file:org.orbeon.oxf.processor.serializer.legacy.JFreeChartSerializer.java

protected JFreeChart drawChart(ChartConfig chartConfig, final Dataset ds) {
    JFreeChart chart = null;//from   w  ww.  j  a  v  a2 s . c  o  m
    Axis categoryAxis = null;
    if (ds instanceof XYSeriesCollection) {
        categoryAxis = new RestrictedNumberAxis(chartConfig.getCategoryTitle());
    } else if (ds instanceof TimeSeriesCollection) {
        categoryAxis = new DateAxis(chartConfig.getCategoryTitle());
        ((DateAxis) categoryAxis).setDateFormatOverride(new SimpleDateFormat(chartConfig.getDateFormat()));
        if (chartConfig.getCategoryLabelAngle() == 90) {
            ((DateAxis) categoryAxis).setVerticalTickLabels(true);
        } else {
            if (chartConfig.getCategoryLabelAngle() != 0)
                throw new OXFException(
                        "The only supported values of category-label-angle for time-series charts are 0 or 90");
        }
    } else {
        categoryAxis = new CategoryAxis(chartConfig.getCategoryTitle());
        ((CategoryAxis) categoryAxis).setCategoryLabelPositions(chartConfig.getCategoryLabelPosition());
    }
    NumberAxis valueAxis = new RestrictedNumberAxis(chartConfig.getSerieTitle());
    valueAxis.setAutoRangeIncludesZero(chartConfig.getSerieAutoRangeIncludeZero());
    AbstractRenderer renderer = null;
    Plot plot = null;

    switch (chartConfig.getType()) {
    case ChartConfig.VERTICAL_BAR_TYPE:
    case ChartConfig.HORIZONTAL_BAR_TYPE:
        renderer = (ds instanceof ItemPaintCategoryDataset) ? new BarRenderer() {
            public Paint getItemPaint(int row, int column) {
                Paint p = ((ItemPaintCategoryDataset) ds).getItemPaint(row, column);
                if (p != null)
                    return p;
                else
                    return getSeriesPaint(row);
            }
        } : new BarRenderer();

        plot = new CategoryPlot((CategoryDataset) ds, (CategoryAxis) categoryAxis, (ValueAxis) valueAxis,
                (CategoryItemRenderer) renderer);

        if (chartConfig.getType() == ChartConfig.VERTICAL_BAR_TYPE)
            ((CategoryPlot) plot).setOrientation(PlotOrientation.VERTICAL);
        else
            ((CategoryPlot) plot).setOrientation(PlotOrientation.HORIZONTAL);

        break;
    case ChartConfig.STACKED_VERTICAL_BAR_TYPE:
    case ChartConfig.STACKED_HORIZONTAL_BAR_TYPE:
        renderer = (ds instanceof ItemPaintCategoryDataset) ? new StackedBarRenderer() {
            public Paint getItemPaint(int row, int column) {
                Paint p = ((ItemPaintCategoryDataset) ds).getItemPaint(row, column);
                if (p != null)
                    return p;
                else
                    return getSeriesPaint(row);
            }
        } : new StackedBarRenderer();
        plot = new CategoryPlot((CategoryDataset) ds, (CategoryAxis) categoryAxis, (ValueAxis) valueAxis,
                (CategoryItemRenderer) renderer);

        if (chartConfig.getType() == ChartConfig.STACKED_VERTICAL_BAR_TYPE)
            ((CategoryPlot) plot).setOrientation(PlotOrientation.VERTICAL);
        else
            ((CategoryPlot) plot).setOrientation(PlotOrientation.HORIZONTAL);
        break;
    case ChartConfig.LINE_TYPE:
        renderer = (ds instanceof ItemPaintCategoryDataset) ? new LineAndShapeRenderer(true, false) {
            public Paint getItemPaint(int row, int column) {
                Paint p = ((ItemPaintCategoryDataset) ds).getItemPaint(row, column);
                if (p != null)
                    return p;
                else
                    return getSeriesPaint(row);
            }
        } : (new LineAndShapeRenderer(true, false));
        plot = new CategoryPlot((CategoryDataset) ds, (CategoryAxis) categoryAxis, (ValueAxis) valueAxis,
                (CategoryItemRenderer) renderer);
        ((CategoryPlot) plot).setOrientation(PlotOrientation.VERTICAL);
        break;
    case ChartConfig.AREA_TYPE:
        renderer = (ds instanceof ItemPaintCategoryDataset) ? new AreaRenderer() {
            public Paint getItemPaint(int row, int column) {
                Paint p = ((ItemPaintCategoryDataset) ds).getItemPaint(row, column);
                if (p != null)
                    return p;
                else
                    return getSeriesPaint(row);
            }
        } : new AreaRenderer();
        plot = new CategoryPlot((CategoryDataset) ds, (CategoryAxis) categoryAxis, (ValueAxis) valueAxis,
                (CategoryItemRenderer) renderer);
        ((CategoryPlot) plot).setOrientation(PlotOrientation.VERTICAL);
        break;
    case ChartConfig.VERTICAL_BAR3D_TYPE:
    case ChartConfig.HORIZONTAL_BAR3D_TYPE:
        categoryAxis = new CategoryAxis3D(chartConfig.getCategoryTitle());
        valueAxis = new NumberAxis3D(chartConfig.getSerieTitle());
        renderer = (ds instanceof ItemPaintCategoryDataset) ? new BarRenderer3D() {
            public Paint getItemPaint(int row, int column) {
                Paint p = ((ItemPaintCategoryDataset) ds).getItemPaint(row, column);
                if (p != null)
                    return p;
                else
                    return getSeriesPaint(row);
            }
        } : new BarRenderer3D();
        plot = new CategoryPlot((CategoryDataset) ds, (CategoryAxis) categoryAxis, (ValueAxis) valueAxis,
                (CategoryItemRenderer) renderer);

        if (chartConfig.getType() == ChartConfig.VERTICAL_BAR3D_TYPE)
            ((CategoryPlot) plot).setOrientation(PlotOrientation.VERTICAL);
        else
            ((CategoryPlot) plot).setOrientation(PlotOrientation.HORIZONTAL);

        break;
    case ChartConfig.STACKED_VERTICAL_BAR3D_TYPE:
    case ChartConfig.STACKED_HORIZONTAL_BAR3D_TYPE:
        categoryAxis = new CategoryAxis3D(chartConfig.getCategoryTitle());
        valueAxis = new NumberAxis3D(chartConfig.getSerieTitle());
        renderer = (ds instanceof ItemPaintCategoryDataset) ? new StackedBarRenderer3D() {
            public Paint getItemPaint(int row, int column) {
                Paint p = ((ItemPaintCategoryDataset) ds).getItemPaint(row, column);
                if (p != null)
                    return p;
                else
                    return getSeriesPaint(row);
            }
        } : new StackedBarRenderer3D();
        plot = new CategoryPlot((CategoryDataset) ds, (CategoryAxis) categoryAxis, (ValueAxis) valueAxis,
                (CategoryItemRenderer) renderer);

        if (chartConfig.getType() == ChartConfig.STACKED_VERTICAL_BAR3D_TYPE)
            ((CategoryPlot) plot).setOrientation(PlotOrientation.VERTICAL);
        else
            ((CategoryPlot) plot).setOrientation(PlotOrientation.HORIZONTAL);

        break;
    case ChartConfig.PIE_TYPE:
    case ChartConfig.PIE3D_TYPE:
        categoryAxis = null;
        valueAxis = null;
        renderer = null;
        ExtendedPieDataset pds = (ExtendedPieDataset) ds;

        plot = chartConfig.getType() == ChartConfig.PIE_TYPE ? new PiePlot(pds) : new PiePlot3D(pds);

        PiePlot pp = (PiePlot) plot;
        pp.setLabelGenerator(new StandardPieSectionLabelGenerator());

        for (int i = 0; i < pds.getItemCount(); i++) {
            Paint p = pds.getPaint(i);
            if (p != null)
                pp.setSectionPaint(i, p);

            pp.setExplodePercent(i, pds.getExplodePercent(i));

            Paint paint = pds.getPaint(i);
            if (paint != null)
                pp.setSectionPaint(i, paint);
        }
        break;
    case ChartConfig.XY_LINE_TYPE:
        renderer = new XYLineAndShapeRenderer(true, false);
        plot = new XYPlot((XYDataset) ds, (ValueAxis) categoryAxis, (ValueAxis) valueAxis,
                (XYLineAndShapeRenderer) renderer);
        break;
    case ChartConfig.TIME_SERIES_TYPE:
        renderer = new XYLineAndShapeRenderer(true, false);
        plot = new XYPlot((XYDataset) ds, (DateAxis) categoryAxis, (ValueAxis) valueAxis,
                (XYLineAndShapeRenderer) renderer);
        break;
    default:
        throw new OXFException("Chart Type not supported");
    }

    if (categoryAxis != null) {
        categoryAxis.setLabelPaint(chartConfig.getTitleColor());
        categoryAxis.setTickLabelPaint(chartConfig.getTitleColor());
        categoryAxis.setTickMarkPaint(chartConfig.getTitleColor());
        if (categoryAxis instanceof RestrictedNumberAxis) {
            ((RestrictedNumberAxis) categoryAxis).setMaxTicks(chartConfig.getMaxNumOfLabels());
        }
        if (categoryAxis instanceof CategoryAxis && chartConfig.getCategoryMargin() != 0)
            ((CategoryAxis) categoryAxis).setCategoryMargin(chartConfig.getCategoryMargin());
    }

    if (valueAxis != null) {
        valueAxis.setLabelPaint(chartConfig.getTitleColor());
        valueAxis.setTickLabelPaint(chartConfig.getTitleColor());
        valueAxis.setTickMarkPaint(chartConfig.getTitleColor());
        ((RestrictedNumberAxis) valueAxis).setMaxTicks(chartConfig.getMaxNumOfLabels());
    }

    if (renderer != null) {
        if (renderer instanceof XYLineAndShapeRenderer) {
            ((XYLineAndShapeRenderer) renderer).setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
        } else {
            ((CategoryItemRenderer) renderer)
                    .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        }
        if (renderer instanceof BarRenderer)
            ((BarRenderer) renderer).setItemMargin(chartConfig.getBarMargin());

        int j = 0;
        for (Iterator i = chartConfig.getValueIterator(); i.hasNext();) {
            Value v = (Value) i.next();
            renderer.setSeriesPaint(j, v.getColor());
            j++;
        }
    }

    plot.setOutlinePaint(chartConfig.getTitleColor());
    CustomLegend legend = chartConfig.getLegendConfig();
    chart = new JFreeChart(chartConfig.getTitle(), TextTitle.DEFAULT_FONT, plot, false);
    if (legend.isVisible()) {
        legend.setSources(new LegendItemSource[] { plot });
        chart.addLegend(legend);
    }
    chart.setBackgroundPaint(chartConfig.getBackgroundColor());
    TextTitle textTitle = new TextTitle(chartConfig.getTitle(), TextTitle.DEFAULT_FONT,
            chartConfig.getTitleColor(), TextTitle.DEFAULT_POSITION, TextTitle.DEFAULT_HORIZONTAL_ALIGNMENT,
            TextTitle.DEFAULT_VERTICAL_ALIGNMENT, TextTitle.DEFAULT_PADDING);
    chart.setTitle(textTitle);
    return chart;
}

From source file:KIDLYFactory.java

/**
 * 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)./*from  w w w.  j  a  v a2  s  .  c  o  m*/
 * <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 tooltips  configure chart to generate tool tips?
 * @param urls  configure chart to generate URLs?
 * @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 tooltips, boolean urls,
        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));

    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
    }
    if (urls) {
        plot.setURLGenerator(new StandardPieURLGenerator());
    }

    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("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}

From source file:com.qspin.qtaste.reporter.testresults.html.HTMLReportFormatter.java

private void generatePieChart() {
    if (currentTestSuite == null) {
        return;/*w  w  w. j  a  v a 2 s  . c  o  m*/
    }

    File testSummaryFile = new File(reportFile.getParentFile(), testSummaryFileName);
    File tempTestSummaryFile = new File(testSummaryFile.getPath() + ".tmp");

    final DefaultPieDataset pieDataSet = new DefaultPieDataset();

    pieDataSet.setValue("Passed", new Integer(currentTestSuite.getNbTestsPassed()));
    pieDataSet.setValue("Failed", new Integer(currentTestSuite.getNbTestsFailed()));
    pieDataSet.setValue("Tests in error", new Integer(currentTestSuite.getNbTestsNotAvailable()));
    pieDataSet.setValue("Not executed",
            new Integer(currentTestSuite.getNbTestsToExecute() - currentTestSuite.getNbTestsExecuted()));
    JFreeChart chart = null;
    final boolean drilldown = true;

    // create the chart...
    if (drilldown) {
        final PiePlot plot = new PiePlot(pieDataSet);

        Color[] colors = { new Color(100, 230, 40), new Color(210, 35, 35), new Color(230, 210, 40),
                new Color(100, 90, 40) };
        PieRenderer renderer = new PieRenderer(colors);
        renderer.setColor(plot, (DefaultPieDataset) pieDataSet);

        plot.setURLGenerator(new StandardPieURLGenerator("pie_chart_detail.jsp"));
        plot.setLabelGenerator(new TestSectiontLabelPieGenerator());
        chart = new JFreeChart("Test summary", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    } else {
        chart = ChartFactory.createPieChart("Test summary", // chart title
                pieDataSet, // data
                true, // include legend
                true, false);
    }

    chart.setBackgroundPaint(java.awt.Color.white);

    try {
        final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        ChartUtilities.saveChartAsPNG(tempTestSummaryFile, chart, 600, 400, info);
    } catch (IOException e) {
        logger.error("Problem saving png chart", e);
    }

    testSummaryFile.delete();
    if (!tempTestSummaryFile.renameTo(testSummaryFile)) {
        logger.error("Couldn't rename test summary file " + tempTestSummaryFile + " into " + testSummaryFile);
    }
}