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

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

Introduction

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

Prototype

public void setLegendLabelGenerator(PieSectionLabelGenerator generator) 

Source Link

Document

Sets the legend label generator and sends a PlotChangeEvent to all registered listeners.

Usage

From source file:com.globalsight.util.JfreeCharUtil.java

public static void drawPieChart2D(String title, Map<String, Double> datas, File OutFile) {
    PieDataset dataset = buildDatas(datas);
    JFreeChart chart = ChartFactory.createPieChart(title, dataset, true, true, false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})",
            NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
    chart.setBackgroundPaint(Color.white);
    plot.setCircular(true);/*from w  ww  .  jav a  2 s .  com*/
    TextTitle textTitle = new TextTitle(title);
    Font font = new Font(textTitle.getFont().getName(), Font.CENTER_BASELINE, 20);
    textTitle.setFont(font);
    chart.setTitle(textTitle);
    FileOutputStream fos_jpg = null;

    try {
        fos_jpg = new FileOutputStream(OutFile);
        ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null);
        fos_jpg.close();
    } catch (Exception e) {
        s_logger.error(e.getMessage(), e);
    }
}

From source file:com.mxgraph.examples.swing.chart.PieChartDemo1.java

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

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

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

    plot.setSectionOutlinesVisible(false);
    plot.setNoDataMessage("No data available");

    plot.setCircular(true);
    plot.setLabelGap(0.01D);//
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:\r\n{1}({2})",
            NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); //  //
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));

    chart.getTitle().setFont(new Font("", Font.PLAIN, 20));//
    PiePlot piePlot = (PiePlot) chart.getPlot();//
    piePlot.setLabelFont(new Font("", Font.PLAIN, 10));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 10));

    return chart;

}

From source file:org.posterita.businesslogic.performanceanalysis.CustomPOSReportManager.java

public static PieChart generatePieChart(Properties ctx, String title, String subtitle, int account_id,
        Timestamp fromDate, Timestamp toDate, String salesGroup, String priceQtyFilter)
        throws OperationException {
    PieChart pieChart = new PieChart();
    pieChart.setTitle(title);/*from  ww  w.j a v  a2 s .  c  o  m*/
    pieChart.setSubtitle(subtitle);

    String pieChartSQL = SalesAnalysisReportManager.getPieChartDataSetSQL(ctx, account_id, fromDate, toDate,
            salesGroup);
    ArrayList<Object[]> list = ReportManager.getReportData(ctx, pieChartSQL, false);
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    StandardPieSectionLabelGenerator labelGenerator = null;

    if (priceQtyFilter.equalsIgnoreCase(Constants.PRICE)) {
        //against price
        for (Object[] obj : list) {
            String name = (String) obj[0];
            BigDecimal price = (BigDecimal) obj[1];

            pieDataset.setValue(name, price);
        }

        String currency = POSTerminalManager.getDefaultSalesCurrency(ctx).getCurSymbol();
        labelGenerator = new StandardPieSectionLabelGenerator("{0} = " + currency + "{1}");
    } else {
        //against qty
        for (Object[] obj : list) {
            String name = (String) obj[0];
            BigDecimal qty = (BigDecimal) obj[2];

            pieDataset.setValue(name, qty);
        }

        labelGenerator = new StandardPieSectionLabelGenerator();
    }

    pieChart.setDataset(pieDataset);
    PiePlot p = (PiePlot) pieChart.getChart().getPlot();
    p.setLegendLabelGenerator(labelGenerator);
    pieChart.getChart().setBackgroundPaint(Color.white);

    return pieChart;
}

From source file:dbseer.gui.chart.DBSeerChartFactory.java

public static JFreeChart createPieChart(String chartName, DBSeerDataSet dataset) throws Exception {
    StatisticalPackageRunner runner = DBSeerGUI.runner;

    runner.eval("[title legends Xdata Ydata Xlabel Ylabel timestamp] = plotter.plot" + chartName + ";");

    String title = runner.getVariableString("title");
    Object[] legends = (Object[]) runner.getVariableCell("legends");
    Object[] xCellArray = (Object[]) runner.getVariableCell("Xdata");
    Object[] yCellArray = (Object[]) runner.getVariableCell("Ydata");
    String xLabel = runner.getVariableString("Xlabel");
    String yLabel = runner.getVariableString("Ylabel");
    timestamp = runner.getVariableDouble("timestamp");

    DefaultPieDataset pieDataSet = new DefaultPieDataset();

    int numLegends = legends.length;
    int numXCellArray = xCellArray.length;
    int numYCellArray = yCellArray.length;
    int dataCount = 0;

    if (numXCellArray != numYCellArray) {
        JOptionPane.showMessageDialog(null, "The number of X dataset and Y dataset does not match.",
                "The number of X dataset and Y dataset does not match.", JOptionPane.ERROR_MESSAGE);
        return null;
    }// w w  w .jav a2 s .c o  m

    final java.util.List<String> transactionTypeNames = dataset.getTransactionTypeNames();

    for (int i = 0; i < numYCellArray; ++i) {
        double[] xArray = (double[]) xCellArray[i];
        runner.eval("yArraySize = size(Ydata{" + (i + 1) + "});");
        runner.eval("yArray = Ydata{" + (i + 1) + "};");
        double[] yArraySize = runner.getVariableDouble("yArraySize");
        double[] yArray = runner.getVariableDouble("yArray");

        int xLength = xArray.length;
        int row = (int) yArraySize[0];
        int col = (int) yArraySize[1];

        for (int c = 0; c < col; ++c) {
            if (c < transactionTypeNames.size()) {
                String name = transactionTypeNames.get(c);
                if (!name.isEmpty()) {
                    //                     pieDataSet.setValue(name, new Double(yArray.getRealValue(0, c)));
                    pieDataSet.setValue(name, yArray[c]);
                } else {
                    //                     pieDataSet.setValue("Transaction Type " + (c+1), yArray.getRealValue(0, c));
                    pieDataSet.setValue("Transaction Type " + (c + 1), yArray[c]);
                }
            }
        }
    }

    JFreeChart chart = ChartFactory.createPieChart(title, pieDataSet, true, true, false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}: {1} ({2})", new DecimalFormat("0"),
            new DecimalFormat("0%")));
    plot.setLegendLabelGenerator(new PieSectionLabelGenerator() {
        @Override
        public String generateSectionLabel(PieDataset pieDataset, Comparable comparable) {
            return (String) comparable;
        }

        @Override
        public AttributedString generateAttributedSectionLabel(PieDataset pieDataset, Comparable comparable) {
            return null;
        }
    });
    return chart;
}

From source file:edu.monash.merc.struts2.action.TLSumChartAction.java

@SuppressWarnings("unchecked")
public String piechart() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("Green", l4);
    dataset.setValue("Yellow", l3);
    dataset.setValue("Red", l2);
    dataset.setValue("Black", l1);

    chart = ChartFactory.createPieChart(dt, dataset, true, true, false);
    PiePlot plot = (PiePlot) chart.getPlot();

    plot.setBackgroundPaint(Color.WHITE);
    //no border/*  ww w  . jav a 2 s  .c o m*/
    plot.setOutlineStroke(null);
    //no label
    plot.setLabelGenerator(null);

    NumberFormat percentFormat = NumberFormat.getPercentInstance();
    percentFormat.setMaximumFractionDigits(2);
    plot.setLegendLabelGenerator(
            new StandardPieSectionLabelGenerator("{0}={2}", NumberFormat.getNumberInstance(), percentFormat));
    //set specific colors  green, yellow, red and black
    Color[] colors = { new Color(0, 140, 0), new Color(254, 172, 0), new Color(226, 0, 40),
            new Color(0, 0, 0) };
    setColor(plot, dataset, colors);

    chart.setBackgroundPaint(Color.WHITE);
    chart.getTitle().setFont(chart.getTitle().getFont().deriveFont(11.0f));
    return SUCCESS;
}

From source file:com.jtk.pengelolaanujian.view.dashboard.PiePanelUjian.java

private ChartPanel createChart(PieDataset data) {
    piechart = ChartFactory.createPieChart("Ujian Status", data, true, true, false);
    PiePlot plot = (PiePlot) piechart.getPlot();
    plot.setSectionPaint("Terlalui", new Color(60, 70, 5));
    plot.setSectionPaint("Belum", new Color(100, 20, 30));
    plot.setNoDataMessage("Data Tidak Ada");
    plot.setExplodePercent("data", 0.1D);
    plot.setLabelBackgroundPaint(new Color(255, 228, 225));
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})"));
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator());

    // Key = 0 ----> section as String (Windows, Linux, Lainnya)
    // Key = 1 ----> section as value (300,200,100)
    // KEy - 2 ----> section as percentage (50%,33%,17 %) Muncul jika aplikasi telah di running

    plot.setSimpleLabels(true);/*  w  w w . ja v a 2s. c  om*/
    plot.setInteriorGap(0.0D);
    return new ChartPanel(piechart);
}

From source file:com.jtk.pengelolaanujian.view.dashboard.PiePanelVnv.java

private ChartPanel createChart(PieDataset data) {
    piechart = ChartFactory.createPieChart("Soal Vnv", data, true, true, false);
    PiePlot plot = (PiePlot) piechart.getPlot();
    plot.setSectionPaint("Belum Di VNV", new Color(135, 206, 250));
    plot.setSectionPaint("sudah Di VNV", new Color(205, 133, 63));
    plot.setNoDataMessage("Data Tidak Ada");
    plot.setExplodePercent("data", 0.1D);
    plot.setLabelBackgroundPaint(new Color(255, 228, 225));
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})"));
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator());

    // Key = 0 ----> section as String (Windows, Linux, Lainnya)
    // Key = 1 ----> section as value (300,200,100)
    // KEy - 2 ----> section as percentage (50%,33%,17 %) Muncul jika aplikasi telah di running
    plot.setSimpleLabels(true);//www.  j  a  v a  2  s  .c o  m
    plot.setInteriorGap(0.0D);
    return new ChartPanel(piechart);
}

From source file:com.jtk.pengelolaanujian.view.dashboard.PiePanelNilai.java

private ChartPanel createChart(PieDataset data) {
    piechart = ChartFactory.createPieChart("Nilai Upload", data, true, true, false);
    PiePlot plot = (PiePlot) piechart.getPlot();
    plot.setSectionPaint("Belum Di Print", new Color(60, 70, 5));
    plot.setSectionPaint("Sudah Di Print", new Color(100, 20, 30));
    plot.setNoDataMessage("Data Tidak Ada");
    plot.setExplodePercent("data", 0.1D);
    plot.setLabelBackgroundPaint(new Color(255, 228, 225));
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})"));
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator());

    // Key = 0 ----> section as String (Windows, Linux, Lainnya)
    // Key = 1 ----> section as value (300,200,100)
    // KEy - 2 ----> section as percentage (50%,33%,17 %) Muncul jika aplikasi telah di running

    plot.setSimpleLabels(true);/* ww  w. j  a v a2  s . c o  m*/
    plot.setInteriorGap(0.0D);
    return new ChartPanel(piechart);
}

From source file:com.jtk.pengelolaanujian.view.dashboard.PiePanelSoal.java

private ChartPanel createChart(PieDataset data) {

    piechart = ChartFactory.createPieChart("Soal Upload", data, true, true, false);
    PiePlot plot = (PiePlot) piechart.getPlot();
    plot.setSectionPaint("Belum Di Print", new Color(135, 206, 250));
    plot.setSectionPaint("Sudah Di Print", new Color(205, 133, 63));
    plot.setNoDataMessage("Data Tidak Ada");
    plot.setExplodePercent("data", 0.1D);
    plot.setLabelBackgroundPaint(new Color(255, 228, 225));
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1})"));
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator());

    // Key = 0 ----> section as String (Windows, Linux, Lainnya)
    // Key = 1 ----> section as value (300,200,100)
    // KEy - 2 ----> section as percentage (50%,33%,17 %) Muncul jika aplikasi telah di running
    plot.setSimpleLabels(true);/* w  ww  . j a  v  a 2  s.c  om*/
    plot.setInteriorGap(0.0D);
    return new ChartPanel(piechart);
}

From source file:org.openfaces.component.chart.impl.plots.PiePlot3DAdapter.java

private void setupLegendLabels(PiePlot plot, Chart chart, PieChartView chartView) {
    if (!chart.isLegendVisible())
        return;//from   ww w . j  a  v a 2 s  .  co  m

    ChartLegend legend = chart.getLegend();
    if (legend == null || legend.getLabels() == null)
        return;

    final ChartLabels labels = legend.getLabels();
    if (labels.getText() != null) {
        // simple text = the same string for every item
        plot.setLegendLabelGenerator(new PieSectionLabelGenerator() {
            public String generateSectionLabel(PieDataset pieDataset, Comparable comparable) {
                return labels.getText();
            }

            public AttributedString generateAttributedSectionLabel(PieDataset dataset, Comparable key) {
                return null;
            }
        });
    } else if (labels.getDynamicText() != null) {
        plot.setLegendLabelGenerator(new DynamicPieGenerator(chartView, labels.getDynamicText()));
    } else {
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator());
    }

}