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:org.pentaho.chart.plugin.jfreechart.JFreeChartFactoryEngine.java

public JFreeChart makePieChart(ChartModel chartModel, NamedValuesDataModel dataModel,
        final IChartLinkGenerator linkGenerator) {
    final DefaultPieDataset dataset = new DefaultPieDataset();
    for (NamedValue namedValue : dataModel) {
        if (namedValue.getName() != null) {
            dataset.setValue(namedValue.getName(),
                    scaleNumber(namedValue.getValue(), dataModel.getScalingFactor()));
        }/*  w ww .j a v a2s .c om*/
    }

    boolean showLegend = (chartModel.getLegend() != null) && (chartModel.getLegend().getVisible());

    String title = "";
    if ((chartModel.getTitle() != null) && (chartModel.getTitle().getText() != null)
            && (chartModel.getTitle().getText().trim().length() > 0)) {
        title = chartModel.getTitle().getText();
    }

    final JFreeChart chart = ChartFactory.createPieChart(title, dataset, showLegend, true, false);

    initChart(chart, chartModel);

    final PiePlot jFreePiePlot = (PiePlot) chart.getPlot();

    if (linkGenerator != null) {
        jFreePiePlot.setURLGenerator(new PieURLGenerator() {

            public String generateURL(PieDataset arg0, Comparable arg1, int arg2) {
                return linkGenerator.generateLink(arg1.toString(), arg1.toString(), arg0.getValue(arg1));
            }
        });
    }

    jFreePiePlot.setNoDataMessage("No data available"); //$NON-NLS-1$
    jFreePiePlot.setCircular(true);
    jFreePiePlot.setLabelGap(0.02);

    org.pentaho.chart.model.PiePlot chartBeansPiePlot = (org.pentaho.chart.model.PiePlot) chartModel.getPlot();

    List<Integer> colors = getPlotColors(chartBeansPiePlot);

    int index = 0;
    for (NamedValue namedValue : dataModel) {
        if (namedValue.getName() != null) {
            jFreePiePlot.setSectionPaint(namedValue.getName(),
                    new Color(0x00FFFFFF & colors.get(index % colors.size())));
        }
        index++;
    }

    if (chartBeansPiePlot.getLabels().getVisible()) {
        jFreePiePlot.setLabelGenerator(new StandardPieSectionLabelGenerator());

        Font font = ChartUtils.getFont(chartBeansPiePlot.getLabels().getFontFamily(),
                chartBeansPiePlot.getLabels().getFontStyle(), chartBeansPiePlot.getLabels().getFontWeight(),
                chartBeansPiePlot.getLabels().getFontSize());
        if (font != null) {
            jFreePiePlot.setLabelFont(font);
            if (chartBeansPiePlot.getLabels().getColor() != null) {
                jFreePiePlot.setLabelPaint(new Color(0x00FFFFFF & chartBeansPiePlot.getLabels().getColor()));
            }
            if (chartBeansPiePlot.getLabels().getBackgroundColor() != null) {
                jFreePiePlot.setLabelBackgroundPaint(
                        new Color(0x00FFFFFF & chartBeansPiePlot.getLabels().getBackgroundColor()));
            }
        }
    } else {
        jFreePiePlot.setLabelGenerator(null);
    }

    jFreePiePlot.setStartAngle(-chartBeansPiePlot.getStartAngle());

    return chart;
}

From source file:UserInterface.DoctorRole.DoctorReportChartJPanel.java

private void sourcejButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sourcejButton1ActionPerformed
    // TODO add your handling code here:

    ReportToReporter report = enterprise.getReport();
    if (report.getStatus() != null) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("First hand", report.getFirsthandsource());
        dataset.setValue("Second hand", report.getSecondhandsource());

        JFreeChart chart = ChartFactory.createPieChart("bar chart", dataset, true, true, true);

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

        ChartFrame chartFrame = new ChartFrame("Source of Reports", chart);
        chartFrame.setSize(450, 550);/*from w ww. j a v  a 2  s.c  om*/
        chartFrame.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(null, "Sorry, the final report has not been generated");
    }

}

From source file:velo.ejb.seam.action.HomeActionsBean.java

public void createChart() {
    ResourceList rl = new ResourceList();
    rl.getResource().setActive(true);/*w ww . j  a v a2  s  .co m*/
    rl.initialize();
    List<Resource> resources = rl.getResultList();

    //Retrieve a list of tasks from last day
    TaskList tl = new TaskList();
    List<Task> tasks = tl.getResultList();

    //build the summaries
    Map<String, Long> rTasks = new HashMap<String, Long>();

    for (Task currTask : tasks) {
        if (currTask instanceof ResourceTask) {
            ResourceTask rt = (ResourceTask) currTask;

            if (!rTasks.containsKey(rt.getResourceUniqueName())) {
                rTasks.put(rt.getResourceUniqueName(), new Long(0));
            }

            rTasks.put(rt.getResourceUniqueName(), rTasks.get(rt.getResourceUniqueName()) + 1);
        }
    }

    final DefaultPieDataset dataset = new DefaultPieDataset();
    for (Resource currResource : resources) {
        if (rTasks.containsKey(currResource.getUniqueName())) {
            dataset.setValue(currResource.getDisplayName() + "("
                    + rTasks.get(currResource.getUniqueName()).longValue() + ")",
                    rTasks.get(currResource.getUniqueName()));
        } else {
            dataset.setValue(currResource.getDisplayName() + "(0)", 0);
        }

    }

    final JFreeChart chart = ChartFactory.createPieChart("Tasks Amount (1 last day) per resource", // chart title
            dataset, // dataset
            true, // include legend
            true, false);
    final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setNoDataMessage("No data available");

    try {
        this.chart = ChartUtilities.encodeAsPNG(chart.createBufferedImage(400, 400));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:graph.plotter.PieMenu.java

/**
 * This is the main working button for this class... It creates pie chart analyZing whole data set
 * /*w  w  w  . jav  a 2  s .c  om*/
 * @param evt 
 */
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    try {
        DefaultPieDataset pieDataset = new DefaultPieDataset(); /** Initializing pie dataset*/
        int i;
        i = 0;
        String genre = "a";
        if (Button == 1) { /** For User Input*/
            while (i < cnt) {
                double aa = Double.parseDouble(Table.getModel().getValueAt(i, 1).toString());
                String str = Table.getModel().getValueAt(i, 0).toString();

                pieDataset.setValue(str, new Double(aa));
                i++;
                genre += "a";

            }
        } else {
            try {
                BufferedReader br = new BufferedReader(new FileReader(jTextField3.getText()));
                String Line;
                while ((Line = br.readLine()) != null) {
                    String[] value = Line.split(",");
                    double val = Double.parseDouble(value[1]);
                    pieDataset.setValue(value[0], new Double(val));
                    //                dataset.setValue(new Double(val),genre,value[0]);
                    //                genre+="a";
                    // (value[0]);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PieMenu.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PieMenu.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        JFreeChart chart = ChartFactory.createPieChart("Pie Chart", pieDataset, true, true, true);
        PiePlot P = (PiePlot) chart.getPlot();
        P.setLabelLinkPaint(Color.BLACK);
        P.setBackgroundPaint(Color.white);
        ChartFrame frame = new ChartFrame("PieChart", chart);
        jButto1 = new JButton("Save");

        frame.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = 1;
        gc.gridy = 0;

        panel.add(jButto1, gc);

        frame.add(panel, BorderLayout.SOUTH);

        jButto1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try {
                    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
                    final File file1 = new File("Pie_Chart.png");
                    ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
                } catch (Exception ex) {

                }

            }
        });
        frame.setVisible(true);
        frame.setSize(858, 512);

        try {
            final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            final File file1 = new File("Pie_Chart.png");
            ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
        } catch (Exception e) {

        }
    } catch (Exception ex) {

    }
}

From source file:com.exam.server.ConvertPDF.java

public static JFreeChart generatePieChart(ArrayList<DeviceDTO> input) {
    int shutdownCount = 0;
    int inspectedCount = 0;
    int earlyCount = 0;
    int uninspectedCount = 0;
    int duplicateCount = 0;
    int lateCount = 0;
    int unscheduledCount = 0;
    int downloadedCount = 0;
    for (DeviceDTO deviceDTO : input) {
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("SHUTDOWN_EVENT")) {
            shutdownCount++;/*from   w ww  .j av  a2s .  c  om*/
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("INSPECTED")) {
            inspectedCount++;
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("EARLY")) {
            earlyCount++;
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("UNINSPECTED")) {
            uninspectedCount++;
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("DUPLICATE")) {
            duplicateCount++;
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("LATE")) {
            lateCount++;
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("UNSCHEDULED")) {
            unscheduledCount++;
        }
        if (deviceDTO.getData1() != null && deviceDTO.getData1().equalsIgnoreCase("DOWNLOADED")) {
            downloadedCount++;
        }
    }
    DefaultPieDataset dataSet = new DefaultPieDataset();
    dataSet.setValue("SHUTDOWN_EVENT", shutdownCount);
    dataSet.setValue("INSPECTED", inspectedCount);
    dataSet.setValue("EARLY", earlyCount);
    dataSet.setValue("UNINSPECTED", uninspectedCount);
    dataSet.setValue("DUPLICATE", duplicateCount);
    dataSet.setValue("LATE", lateCount);
    dataSet.setValue("UNSCHEDULED", unscheduledCount);
    dataSet.setValue("DOWNLOADED", downloadedCount);

    JFreeChart chart = ChartFactory.createPieChart("Device break up on basis of data1:", dataSet, true, true,
            false);

    return chart;
}

From source file:net.sf.jsfcomp.chartcreator.utils.ChartUtils.java

public static JFreeChart createChartWithPieDataSet(ChartData chartData) {
    PieDataset dataset = (PieDataset) chartData.getDatasource();
    String type = chartData.getType();
    boolean legend = chartData.isLegend();
    JFreeChart chart = null;/*w w  w .jav a2s .  com*/

    if (type.equalsIgnoreCase("pie")) {
        if (chartData.isChart3d()) {
            chart = ChartFactory.createPieChart3D("", dataset, legend, true, false);
            PiePlot3D plot = (PiePlot3D) chart.getPlot();
            plot.setDepthFactor((float) chartData.getDepth() / 100);
        } else {
            chart = ChartFactory.createPieChart("", dataset, legend, true, false);
        }
    } else if (type.equalsIgnoreCase("ring")) {
        chart = ChartFactory.createRingChart("", dataset, legend, true, false);
    }

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setStartAngle((float) chartData.getStartAngle());
    if (chartData.getGenerateMap() != null)
        plot.setURLGenerator(new StandardPieURLGenerator(""));

    setPieSectionColors(chart, chartData);

    return chart;
}

From source file:com.rapidminer.template.gui.RoleRequirementSelector.java

private JFreeChart makeChart(ExampleSet exampleSet, Attribute att) {
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    if (att.isNominal()) {
        for (String val : att.getMapping().getValues()) {
            pieDataset.setValue(val, exampleSet.getStatistics(att, Statistics.COUNT, val));
        }/*from w  ww .  j a  v a2 s  . c  om*/
    }
    JFreeChart chart = ChartFactory.createPieChart(null, pieDataset, true, false, false);
    chart.setBackgroundPaint(Color.WHITE);
    chart.getLegend().setFrame(BlockBorder.NONE);
    chart.setBackgroundImageAlpha(0.0f);
    chart.setBorderVisible(false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(null);
    plot.setShadowPaint(null);
    plot.setOutlineVisible(false);
    plot.setBackgroundPaint(new Color(255, 255, 255, 0));
    plot.setBackgroundImageAlpha(0.0f);
    plot.setCircular(true);
    return chart;
}

From source file:frames.Screen2.java

private void PiechartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PiechartActionPerformed
    DefaultPieDataset piedataset = new DefaultPieDataset();
    piedataset.setValue("Process one", new Integer(10));
    piedataset.setValue("Process two", new Integer(20));
    piedataset.setValue("Process three", new Integer(30));
    piedataset.setValue("Process four", new Integer(40));
    JFreeChart chart = ChartFactory.createPieChart("Piechart", piedataset, true, true, true);
    PiePlot P = (PiePlot) chart.getPlot();
    //P.setForegroundAlpha(TOP_ALIGNMENT);
    ChartFrame frame = new ChartFrame("piechart", chart);
    frame.setVisible(true);//from w w  w .j  a v  a 2 s  .c  om
    frame.setSize(450, 500);

}

From source file:com.uttesh.pdfngreport.handler.PdfReportHandler.java

/**
 * Not Used for the current release/*from  ww  w.j  ava 2 s . com*/
 *
 * @param dataSet
 */
public void pieExplodeChart(DefaultPieDataset dataSet, String os) {
    try {
        JFreeChart chart = ChartFactory.createPieChart("", dataSet, true, true, false);
        ChartStyle.theme(chart);
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setForegroundAlpha(0.6f);
        plot.setCircular(true);
        plot.setSectionPaint("Passed", Color.decode("#019244"));
        plot.setSectionPaint("Failed", Color.decode("#EE6044"));
        plot.setSectionPaint("Skipped", Color.decode("#F0AD4E"));
        Color transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
        //plot.setLabelLinksVisible(Boolean.FALSE);
        plot.setLabelOutlinePaint(transparent);
        plot.setLabelBackgroundPaint(transparent);
        plot.setLabelShadowPaint(transparent);
        plot.setLabelLinkPaint(Color.GRAY);
        Font font = new Font("SansSerif", Font.PLAIN, 10);
        plot.setLabelFont(font);
        plot.setLabelPaint(Color.DARK_GRAY);
        plot.setExplodePercent("Passed", 0.10);
        //plot.setExplodePercent("Failed", 0.10);
        //plot.setExplodePercent("Skipped", 0.10);
        plot.setSimpleLabels(true);
        PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{1} ({2})", new DecimalFormat("0"),
                new DecimalFormat("0%"));
        plot.setLabelGenerator(gen);

        if (os != null && os.equalsIgnoreCase("w")) {
            ChartUtilities.saveChartAsPNG(
                    new File(reportLocation + Constants.BACKWARD_SLASH + Constants.REPORT_CHART_FILE), chart,
                    560, 200);
        } else {
            ChartUtilities.saveChartAsPNG(
                    new File(reportLocation + Constants.FORWARD_SLASH + Constants.REPORT_CHART_FILE), chart,
                    560, 200);
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
        if (os != null && os.equalsIgnoreCase("w")) {
            new File(reportLocation + Constants.BACKWARD_SLASH + Constants.REPORT_CHART_FILE).delete();
        } else {
            new File(reportLocation + Constants.FORWARD_SLASH + Constants.REPORT_CHART_FILE).delete();
        }
        System.exit(-1);
    }
}

From source file:org.gridchem.client.gui.charts.UsageChart.java

private void init(CollaboratorBean collab) {

    removeAll();// www  .j a  v a2 s  .c  om

    // TODO: Add title and footer with expiration date to chart
    String title = "";
    ProjectBean project = projectCollabTable.keySet().iterator().next();
    //        
    if (CURRENT_CHARTTYPE.equals(ChartType.JOB)) {
        //          dataset = createJobDataset(project);
        dataset = new DefaultPieDataset();
    } else if (CURRENT_CHARTTYPE.equals(ChartType.PROJECT)) {
        dataset = createProjectDataset(projectCollabTable, collab);
        title = "Overall Utilization of Project " + project.getName() + " by " + collab.getFirstName() + " "
                + collab.getLastName();
        ;
    } else if (CURRENT_CHARTTYPE.equals(ChartType.RESOURCE)) {
        dataset = createResourceDataset(projectCollabTable, collab);
        title = "CCG Resource Utilization for Project " + project.getName() + " by " + collab.getFirstName()
                + " " + collab.getLastName();
        ;
    } else if (CURRENT_CHARTTYPE.equals(ChartType.USER)) {
        dataset = createUserDataset(projectCollabTable, collab);
        title = "User Utilization of Project " + project.getName() + " by " + collab.getFirstName() + " "
                + collab.getLastName();
    }

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

    chart.addSubtitle(new TextTitle("Project " + project.getName() + " expires on " + project.getEndDate()));

    if (CURRENT_CHARTTYPE.equals(ChartType.JOB)) {
        chart.getPlot().setNoDataMessage("Comprehensive job information is not currently available.");
    }

    ((PiePlot) chart.getPlot()).setCircular(true);

    //        ((PiePlot)chart.getPlot()).setToolTipGenerator(new PieToolTipGenerator() {
    //
    //            public String generateToolTip(PieDataset ds, Comparable arg1) {
    //                // TODO Auto-generated method stub
    //                return ";
    //            }
    //            
    //        });

    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(size);

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;
    add(chartPanel, c);

    GridBagConstraints c1 = new GridBagConstraints();
    c1.weightx = 0;
    c1.weighty = 0;
    c1.gridx = 0;
    c1.gridy = 1;
    c1.fill = GridBagConstraints.BOTH;
    add(navPanel, c1);
    revalidate();
}