Example usage for org.jfree.chart ChartFactory createPieChart

List of usage examples for org.jfree.chart ChartFactory createPieChart

Introduction

In this page you can find the example usage for org.jfree.chart ChartFactory createPieChart.

Prototype

public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips,
        boolean urls) 

Source Link

Document

Creates a pie chart with default settings.

Usage

From source file:RMOS.PieChart.java

/**
 * Creates a chart./*  ww w.  j a  v a2s .c  o  m*/
 *
 * @param dataset  the dataset.
 *
 * @return A chart.
 */
private static JFreeChart createChart(PieDataset dataset) {

    JFreeChart chart = ChartFactory.createPieChart("Eco Systems Statistics", // chart title
            dataset, // data
            false, // no legend
            true, // tooltips
            false // no URL generation
    );

    // set a custom background for the chart
    chart.setBackgroundPaint(
            new GradientPaint(new Point(0, 0), new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));

    // customise the title position and font
    TextTitle t = chart.getTitle();
    t.setHorizontalAlignment(HorizontalAlignment.LEFT);
    t.setPaint(new Color(240, 240, 240));
    t.setFont(new Font("Arial", Font.BOLD, 26));

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(null);
    plot.setInteriorGap(0.04);
    plot.setOutlineVisible(false);

    plot.setSectionPaint(f1.getStationInGroup().get(1), Color.blue);
    plot.setSectionPaint(f1.getStationInGroup().get(1), Color.GREEN);

    // use gradients and white borders for the section colours
    /*plot.setSectionPaint("Others", createGradientPaint(new Color(200, 200, 255), Color.BLUE));
    plot.setSectionPaint("Samsung", createGradientPaint(new Color(255, 200, 200), Color.RED));
    plot.setSectionPaint("Apple", createGradientPaint(new Color(200, 255, 200), Color.GREEN));
    plot.setSectionPaint("Nokia", createGradientPaint(new Color(200, 255, 200), Color.YELLOW));*/
    plot.setBaseSectionOutlinePaint(Color.WHITE);
    plot.setSectionOutlinesVisible(true);
    plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f));

    // customise the section label appearance
    plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
    plot.setLabelLinkPaint(Color.WHITE);
    plot.setLabelLinkStroke(new BasicStroke(2.0f));
    plot.setLabelOutlineStroke(null);
    plot.setLabelPaint(Color.WHITE);
    plot.setLabelBackgroundPaint(null);

    // add a subtitle giving the data source
    TextTitle source = new TextTitle("Source: Eco Recycle Station", new Font("Courier New", Font.PLAIN, 12));
    source.setPaint(Color.WHITE);
    source.setPosition(RectangleEdge.BOTTOM);
    source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    chart.addSubtitle(source);
    return chart;

}

From source file:it.eng.spagobi.engines.chart.bo.charttypes.piecharts.SimplePie.java

public JFreeChart createChart(DatasetMap datasets) {

    Dataset dataset = (Dataset) datasets.getDatasets().get("1");

    JFreeChart chart = null;// w w w. ja v a 2 s. c  o m

    if (!threeD) {
        chart = ChartFactory.createPieChart(name, (PieDataset) dataset, // data
                legend, // include legend
                true, false);

        chart.setBackgroundPaint(color);

        TextTitle title = chart.getTitle();
        title.setToolTipText("A title tooltip!");

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font(defaultLabelsStyle.getFontName(), Font.PLAIN, defaultLabelsStyle.getSize()));
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        plot.setNoDataMessage("No data available");

        if (percentage == false) {
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})"));
        } else {
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
        }

    } else {
        chart = ChartFactory.createPieChart3D(name, (PieDataset) dataset, // data
                true, // include legend
                true, false);

        chart.setBackgroundPaint(color);

        TextTitle title = chart.getTitle();
        title.setToolTipText("A title tooltip!");

        PiePlot3D plot = (PiePlot3D) chart.getPlot();

        plot.setDarkerSides(true);
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(1.0f);
        plot.setDepthFactor(0.2);

        plot.setLabelFont(new Font(defaultLabelsStyle.getFontName(), Font.PLAIN, defaultLabelsStyle.getSize()));
        // plot.setNoDataMessages("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        plot.setNoDataMessage("No data available");

        if (percentage == false) {
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})"));
        } else {
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
        }
    }

    TextTitle title = setStyleTitle(name, styleTitle);
    chart.setTitle(title);
    if (subName != null && !subName.equals("")) {
        TextTitle subTitle = setStyleTitle(subName, styleSubTitle);
        chart.addSubtitle(subTitle);
    }

    return chart;

}

From source file:grafici.FatturePieChart.java

/**
 * Creates a chart.//from   ww  w.j  a va 2 s .c  o  m
 * 
 * @param dataset
 *            the dataset.
 * 
 * @return A chart.
 */
private static JFreeChart createChart(PieDataset dataset) {

    JFreeChart chart = ChartFactory.createPieChart(titolo, // chart
            // title
            dataset, // data
            true, // include legend
            true, false);

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setSectionOutlinesVisible(false);
    plot.setNoDataMessage("No data available");

    return chart;

}

From source file:edu.jhuapl.graphs.jfreechart.JFreeChartPieGraphSource.java

@SuppressWarnings("unchecked")
@Override/*w w  w  .j av a  2s  .c  o  m*/
public void initialize() throws GraphException {
    String title = getParam(GraphSource.GRAPH_TITLE, String.class, DEFAULT_TITLE);
    boolean legend = getParam(GraphSource.GRAPH_LEGEND, Boolean.class, DEFAULT_GRAPH_LEGEND);
    boolean graphToolTip = getParam(GraphSource.GRAPH_TOOL_TIP, Boolean.class, DEFAULT_GRAPH_TOOL_TIP);
    boolean graphDisplayLabel = getParam(GraphSource.GRAPH_DISPLAY_LABEL, Boolean.class, false);
    boolean legendBorder = getParam(GraphSource.LEGEND_BORDER, Boolean.class, DEFAULT_LEGEND_BORDER);
    boolean graphBorder = getParam(GraphSource.GRAPH_BORDER, Boolean.class, DEFAULT_GRAPH_BORDER);
    Font titleFont = getParam(GraphSource.GRAPH_FONT, Font.class, DEFAULT_GRAPH_TITLE_FONT);
    String noDataMessage = getParam(GraphSource.GRAPH_NO_DATA_MESSAGE, String.class,
            DEFAULT_GRAPH_NO_DATA_MESSAGE);
    PieGraphData pieGraphData = makeDataSet();
    Map<Comparable, Paint> colors = pieGraphData.colors;

    this.chart = ChartFactory.createPieChart(title, pieGraphData.data, false, graphToolTip, false);

    Paint backgroundColor = getParam(GraphSource.BACKGROUND_COLOR, Paint.class, DEFAULT_BACKGROUND_COLOR);
    Paint plotColor = getParam(JFreeChartTimeSeriesGraphSource.PLOT_COLOR, Paint.class, backgroundColor);
    Paint labelColor = getParam(JFreeChartTimeSeriesGraphSource.GRAPH_LABEL_BACKGROUND_COLOR, Paint.class,
            DEFAULT_BACKGROUND_COLOR);

    this.chart.setBackgroundPaint(backgroundColor);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(plotColor);
    plot.setNoDataMessage(noDataMessage);

    if (!graphDisplayLabel) {
        plot.setLabelGenerator(null);
    } else {
        plot.setInteriorGap(0.001);
        plot.setMaximumLabelWidth(.3);
        //           plot.setIgnoreNullValues(true);
        plot.setIgnoreZeroValues(true);
        plot.setShadowPaint(null);
        //           plot.setOutlineVisible(false);
        //TODO use title font?
        Font font = plot.getLabelFont();
        plot.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
        plot.setLabelFont(font.deriveFont(font.getSize2D() * .75f));
        plot.setLabelBackgroundPaint(labelColor);
        plot.setLabelShadowPaint(null);
        plot.setLabelOutlinePaint(null);
        plot.setLabelGap(0.001);
        plot.setLabelLinkMargin(0.0);
        plot.setLabelLinksVisible(true);
        //           plot.setSimpleLabels(true);
        //           plot.setCircular(true);
    }

    if (!graphBorder) {
        plot.setOutlineVisible(false);
    }

    if (title != null && !"".equals(title)) {
        TextTitle title1 = new TextTitle();
        title1.setText(title);
        title1.setFont(titleFont);
        title1.setPadding(3, 2, 5, 2);
        chart.setTitle(title1);
    } else {
        chart.setTitle((TextTitle) null);
    }
    plot.setLabelPadding(new RectangleInsets(1, 1, 1, 1));
    //Makes a wrapper for the legend to remove the border around it
    if (legend) {
        LegendTitle legend1 = new LegendTitle(chart.getPlot());
        BlockContainer wrapper = new BlockContainer(new BorderArrangement());

        if (legendBorder) {
            wrapper.setFrame(new BlockBorder(1, 1, 1, 1));
        } else {
            wrapper.setFrame(new BlockBorder(0, 0, 0, 0));
        }

        BlockContainer items = legend1.getItemContainer();
        items.setPadding(2, 10, 5, 2);
        wrapper.add(items);
        legend1.setWrapper(wrapper);
        legend1.setPosition(RectangleEdge.BOTTOM);
        legend1.setHorizontalAlignment(HorizontalAlignment.CENTER);

        if (params.get(GraphSource.LEGEND_FONT) instanceof Font) {
            legend1.setItemFont(((Font) params.get(GraphSource.LEGEND_FONT)));
        }

        chart.addSubtitle(legend1);
        plot.setLegendLabelGenerator(new PieGraphLabelGenerator());
    }

    for (Comparable category : colors.keySet()) {
        plot.setSectionPaint(category, colors.get(category));
    }

    plot.setToolTipGenerator(new PieGraphToolTipGenerator(pieGraphData));
    plot.setURLGenerator(new PieGraphURLGenerator(pieGraphData));

    initialized = true;
}

From source file:com.liferay.samplestrutsliferay.struts.action.ViewChartAction.java

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    try {/*from ww  w.j a v  a  2s. c  om*/
        if (_log.isInfoEnabled()) {
            _log.info("execute");
        }

        String attrName = "chart_name";

        // Application scoped session attributes can be fetched from the
        // servlet directly. Portlet scoped session attributes can be
        // fetched from Sun's PortletSessionUtil.

        HttpSession session = request.getSession();

        String chartName = (String) session.getAttribute(attrName);
        //(String)_getAttribute(request, attrName);

        // Chart

        String chartType = request.getParameter("chart_type");

        CategoryDataset dataset = _getDataset();

        String xName = "Soda";
        String yName = "Votes";

        JFreeChart chart = null;

        if (chartType.equals("area")) {
            chart = ChartFactory.createAreaChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else if (chartType.equals("horizontal_bar")) {
            chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.HORIZONTAL,
                    true, false, false);
        } else if (chartType.equals("line")) {
            chart = ChartFactory.createLineChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else if (chartType.equals("vertical_bar")) {
            chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else {
            PieDataset pieData = DatasetUtilities.createPieDatasetForRow(dataset, 0);

            chart = ChartFactory.createPieChart(chartName, pieData, true, false, false);
        }

        response.setContentType("image/jpeg");

        OutputStream out = response.getOutputStream();

        ChartUtilities.writeChartAsJPEG(out, chart, 400, 400);

        return mapping.findForward("/common/null.jsp");
    } catch (Exception e) {
        request.setAttribute(PageContext.EXCEPTION, e);

        return mapping.findForward("/common/error.jsp");
    }
}

From source file:com.liferay.samplestruts.struts.action.ViewChartAction.java

@Override
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    try {/*from w ww  . ja va 2  s . c  o  m*/
        if (_log.isInfoEnabled()) {
            _log.info("execute");
        }

        String attrName = "chart_name";

        // Application scoped session attributes can be fetched from the
        // servlet directly. Portlet scoped session attributes can be
        // fetched from Sun's PortletSessionUtil.

        HttpSession session = request.getSession();

        String chartName = (String) session.getAttribute(attrName);
        //(String)_getAttribute(request, attrName);

        // Chart

        String chartType = request.getParameter("chart_type");

        CategoryDataset dataset = _getDataset();

        String xName = "Soda";
        String yName = "Votes";

        JFreeChart chart = null;

        if (chartType.equals("area")) {
            chart = ChartFactory.createAreaChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else if (chartType.equals("horizontal_bar")) {
            chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.HORIZONTAL,
                    true, false, false);
        } else if (chartType.equals("line")) {
            chart = ChartFactory.createLineChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else if (chartType.equals("vertical_bar")) {
            chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else {
            PieDataset pieData = DatasetUtilities.createPieDatasetForRow(dataset, 0);

            chart = ChartFactory.createPieChart(chartName, pieData, true, false, false);
        }

        response.setContentType("image/jpeg");

        OutputStream out = response.getOutputStream();

        ChartUtilities.writeChartAsJPEG(out, chart, 400, 400);

        return actionMapping.findForward("/common/null.jsp");
    } catch (Exception e) {
        request.setAttribute(PageContext.EXCEPTION, e);

        return actionMapping.findForward("/common/error.jsp");
    }
}

From source file:com.polivoto.vistas.acciones.Datos.java

private JFreeChart crearChartPie(PieDataset dataset, String titulo) {
    JFreeChart chart = ChartFactory.createPieChart(titulo, dataset, false, true, false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setStartAngle(290);// w w w  . j ava  2s.  c o m
    plot.setDirection(Rotation.ANTICLOCKWISE);

    plot.setNoDataMessage("No hay votos");
    try {
        if (ac.getConteoOpcionesPregunta().getJSONObject(pox).getInt("participantes") != 0) {
            int j = 0;
            for (int i = 0; i < ac.getConteoOpcionesPregunta().getJSONObject(pox).getJSONArray("conteo")
                    .length(); i++) {
                try {
                    if (ac.getConteoOpcionesPregunta().getJSONObject(pox).getJSONArray("conteo")
                            .getJSONObject(i).getString("reactivo").equals("Anular mi voto")) {
                        plot.setSectionPaint("Nulo", Color.lightGray);
                    } else {
                        plot.setSectionPaint(ac.getConteoOpcionesPregunta().getJSONObject(pox)
                                .getJSONArray("conteo").getJSONObject(i).getString("reactivo"), colores.get(j));
                        j++;
                    }
                } catch (JSONException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } catch (JSONException ex) {
        ex.printStackTrace();
    }
    plot.setSimpleLabels(true);
    plot.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.white));
    PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0}: {2} de votos",
            new DecimalFormat("0"), new DecimalFormat("0.000%"));
    plot.setLabelGenerator(gen);
    if (rotating) {
        final Rotator rotate = new Rotator(plot);
        rotate.start();
    }
    return chart;
}

From source file:action.ImageAction.java

public JFreeChart getChart() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("Ford", 23.3);
    dataset.setValue("Chevy", 32.4);
    dataset.setValue("Yugo", 44.2);

    boolean legend = true;
    boolean tooltips = false;
    boolean urls = false;

    JFreeChart chart = (JFreeChart) ChartFactory.createPieChart("Cars", dataset, legend, tooltips, urls);

    chart.setBorderPaint(Color.GREEN);
    chart.setBorderStroke(new BasicStroke(5.0f));
    chart.setBorderVisible(true);/*  w ww.j  a v a2 s. c o  m*/

    return chart;
}

From source file:dumbara.view.Chart1.java

public static void loanInfo() {
    DefaultPieDataset objDataset = new DefaultPieDataset();
    try {//from  w w  w . j  av a2  s .co  m
        Loan[] loan = LoanController.viewLoan();
        for (Loan loan1 : loan) {
            objDataset.setValue(loan1.getLoanID(), Double.parseDouble(loan1.getLongAmount()));
        }
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(Chart1.class.getName()).log(Level.SEVERE, null, ex);
    }

    JFreeChart objChart = ChartFactory.createPieChart("Variation of Loan Amounts", objDataset, true, true,
            false);
    ChartFrame frame = new ChartFrame("Data Analysis Wizard - v2.1.4", objChart);
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
}

From source file:biz.ixnay.pivot.charts.skin.jfree.PieChartViewSkin.java

@Override
protected JFreeChart createChart() {
    PieChartView chartView = (PieChartView) getComponent();

    String title = chartView.getTitle();
    boolean showLegend = chartView.getShowLegend();

    ChartView.CategorySequence categories = chartView.getCategories();
    String seriesNameKey = chartView.getSeriesNameKey();
    List<?> chartData = chartView.getChartData();

    JFreeChart chart;/*ww  w .j ava  2s .  c om*/
    if (threeDimensional) {
        if (chartData.getLength() > 1) {
            CategorySeriesDataset dataset = new CategorySeriesDataset(categories, seriesNameKey, chartData);

            chart = ChartFactory.createMultiplePieChart3D(title, dataset, TableOrder.BY_ROW, showLegend, false,
                    false);
        } else {
            PieSeriesDataset dataset = new PieSeriesDataset(categories, chartData.get(0));
            chart = ChartFactory.createPieChart3D(title, dataset, showLegend, false, false);

            PiePlot3D plot = (PiePlot3D) chart.getPlot();
            plot.setDarkerSides(darkerSides);
            plot.setDepthFactor(depthFactor);
        }
    } else {
        if (chartData.getLength() > 1) {
            CategorySeriesDataset dataset = new CategorySeriesDataset(categories, seriesNameKey, chartData);

            chart = ChartFactory.createMultiplePieChart(title, dataset, TableOrder.BY_ROW, showLegend, false,
                    false);
        } else {
            PieSeriesDataset dataset = new PieSeriesDataset(categories, chartData.get(0));
            chart = ChartFactory.createPieChart(title, dataset, showLegend, false, false);

            HashMap<String, String> categoryLabels = new HashMap<String, String>();
            for (int i = 0, n = categories.getLength(); i < n; i++) {
                ChartView.Category category = categories.get(i);
                categoryLabels.put(category.getKey(), category.getLabel());
            }

            PiePlot plot = (PiePlot) chart.getPlot();
            for (String categoryKey : explodePercentages) {
                plot.setExplodePercent(categoryLabels.get(categoryKey),
                        explodePercentages.get(categoryKey).doubleValue());
            }
        }
    }

    return chart;
}