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:charts.Chart.java

public static void GraficoTorta() {
    JFrame janela = new JFrame("Exemplo de Grfico de Torta");

    DefaultPieDataset pieDataset = new DefaultPieDataset();
    pieDataset.setValue("JavaWorld", new Integer(75));
    pieDataset.setValue("Other", new Integer(25));

    JFreeChart jfreechart = ChartFactory.createPieChart("Sample Pie Chart", // Title
            pieDataset, // Dataset
            true, // Show legend
            false, // Tooltips
            false // url
    );/*from w  ww . j  av a  2 s .  c o m*/

    JPanel jpanel = new ChartPanel(jfreechart);
    jpanel.setPreferredSize(new Dimension(defaultwidth, defaultheight));
    janela.setContentPane(jpanel);
    janela.pack();
    RefineryUtilities.centerFrameOnScreen(janela);
    janela.setVisible(true);

}

From source file:HW3.java

private void displayPiechartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_displayPiechartButtonActionPerformed
    // TODO add your handling code here:
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    int val[] = new int[5];
    piechartValues(val);
    if (val[0] > 0) {
        pieDataset.setValue("1 Star=" + val[0], new Integer(val[0]));
    }/*from  ww w.j a  v  a2  s . co  m*/
    if (val[1] > 0) {
        pieDataset.setValue("2 Star=" + val[1], new Integer(val[1]));
    }
    if (val[2] > 0) {
        pieDataset.setValue("3 Star=" + val[2], new Integer(val[2]));
    }
    if (val[3] > 0) {
        pieDataset.setValue("4 Star=" + val[3], new Integer(val[3]));
    }
    if (val[4] > 0) {
        pieDataset.setValue("5 Star=" + val[4], new Integer(val[4]));
    }
    int total = val[0] + val[1] + val[2] + val[3] + val[4];
    JFreeChart chart = ChartFactory.createPieChart("Piechart\n Total Reviews : " + total, pieDataset, true,
            true, true);
    PiePlot P = (PiePlot) chart.getPlot();
    ChartFrame frame = new ChartFrame("Pie Chart", chart);
    frame.setVisible(true);
    frame.setSize(500, 500);
}

From source file:beproject.MainGUI.java

void createPieChart() throws SQLException {
    DefaultPieDataset dataset = new DefaultPieDataset();
    String tmp = "select polarity, count(polarity) from tweets where moviename='" + movieName
            + "' group by polarity";
    ResultSet rs = stmt.executeQuery(tmp);
    while (rs.next()) {
        int a = rs.getInt(1);
        switch (a) {
        case 4:/*from   ww  w .j a v a 2 s  . co m*/
            dataset.setValue("Very Positive", rs.getInt(2));
            break;
        case 3:
            dataset.setValue("Positive", rs.getInt(2));
            break;
        case 2:
            dataset.setValue("Neutral", rs.getInt(2));
            break;
        case 1:
            dataset.setValue("Negative", rs.getInt(2));
            break;
        case 0:
            dataset.setValue("Very Negative", rs.getInt(2));
            break;
        }
    }

    JFreeChart chart = ChartFactory.createPieChart("Sentiment Analysis", dataset, true, true, false);
    chart.setBackgroundPaint(Color.WHITE);
    chart.setBorderVisible(false);
    PiePlot p = (PiePlot) chart.getPlot();
    //p.setSectionPaint("Very Negative", Color.BLACK);
    p.setSectionPaint("Negative", Color.BLACK);
    //p.setSectionPaint("Neutral", new Color(3, 156, 248));
    //p.setSectionPaint("Positive", new Color(96, 194, 253));
    //p.setSectionPaint("Very Positive", new Color(0xffffff));
    p.setSimpleLabels(true);
    ChartUtilities.applyCurrentTheme(chart);
    if (pieChart == null) {
        pieChart = new ChartPanel(chart);
        sentimentPanel.add(pieChart);
    } else {
        sentimentPanel.remove(pieChart);
        pieChart = new ChartPanel(chart);
        sentimentPanel.add(pieChart);
    }
    pieChart.setBackground(Color.WHITE);
    sentimentPanel.validate();
}

From source file:beproject.MainGUI.java

void createCountryPieChart() throws SQLException {
    DefaultPieDataset dataset = new DefaultPieDataset();
    String tmp = "select country, count(country) from tweets where moviename='" + movieName
            + "' group by country";
    ResultSet rs = stmt.executeQuery(tmp);
    while (rs.next()) {
        String tmp1 = rs.getString(1);
        if (tmp1.equals(""))
            tmp1 = "Others";
        dataset.setValue(tmp1, rs.getInt(2));
    }//from w  w w  .  jav a 2 s.  co m

    PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0}: {1} ({2})",
            new DecimalFormat("0"), new DecimalFormat("0.00%"));

    JFreeChart chart = ChartFactory.createPieChart("Tweets by Country", dataset, true, true, false);
    PiePlot p = (PiePlot) chart.getPlot();
    p.setSectionPaint("Others", Color.gray);
    //p.setSectionPaint(KEY2, Color.red);
    //p.setExplodePercent(KEY1, 0.10);
    p.setSimpleLabels(true);
    p.setLabelGenerator(gen);
    if (countryPieChart == null) {
        countryPieChart = new ChartPanel(chart);
        countryPanel.add(countryPieChart);
    } else {
        countryPanel.remove(countryPieChart);
        countryPieChart = new ChartPanel(chart);
        countryPanel.add(countryPieChart);
    }

    countryPanel.validate();
}

From source file:edu.ucla.stat.SOCR.chart.ChartGenerator_JTable.java

private JFreeChart createPieChart(String titleLabel, PieDataset dataset) {
    // System.out.println("rotation="+rotation);
    if (dimension.equalsIgnoreCase("3D")) {
        JFreeChart chart = ChartFactory.createPieChart3D(titleLabel, // chart title
                dataset, // data
                true, // include legend
                true, false);/* w  ww .j a v a 2 s . co m*/

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        if (rotation.equalsIgnoreCase("clockwise")) {
            plot.setStartAngle(290);
            plot.setDirection(Rotation.CLOCKWISE);
            Rotator rotator = new Rotator(plot);
            rotator.start();
        } else if (rotation.equalsIgnoreCase("counter_clockwise")) {
            plot.setStartAngle(290);
            plot.setDirection(Rotation.ANTICLOCKWISE);
            Rotator rotator = new Rotator(plot);
            rotator.start();
        }
        plot.setForegroundAlpha(0.5f);
        plot.setNoDataMessage("No data to display");
        return chart;
    } //end of 3D

    //2D ring
    if (rotation.equalsIgnoreCase("ring")) {
        JFreeChart chart = ChartFactory.createRingChart(titleLabel, // chart title
                dataset, // data
                false, // include legend
                true, false);

        RingPlot plot = (RingPlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;
    }

    //2D
    JFreeChart chart = ChartFactory.createPieChart(titleLabel, // chart title
            dataset, // data
            true, // include legend
            true, false);
    TextTitle title = chart.getTitle();
    title.setToolTipText("A title tooltip!");

    PiePlot plot = (PiePlot) chart.getPlot();
    if (rotation.equalsIgnoreCase("clockwise")) {
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        Rotator rotator = new Rotator(plot);
        rotator.start();
    } else if (rotation.equalsIgnoreCase("counter_clockwise")) {
        plot.setStartAngle(290);
        plot.setDirection(Rotation.ANTICLOCKWISE);
        Rotator rotator = new Rotator(plot);
        rotator.start();
    }

    plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
    plot.setNoDataMessage("No data available");
    plot.setCircular(false);
    plot.setLabelGap(0.02);

    return chart;
}

From source file:gskproject.Analyze.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    if (tableDepartmentAccident != null) {
        DefaultPieDataset pieDataSet = new DefaultPieDataset();
        Vector<Object> row = tableDepartmentAccident.get(tableDepartmentAccident.size() - 1);

        pieDataSet.setValue("First Aid", Double.parseDouble(row.get(1).toString()));
        pieDataSet.setValue("LTI", Double.parseDouble(row.get(2).toString()));
        pieDataSet.setValue("Near Miss", Double.parseDouble(row.get(3).toString()));

        JFreeChart chart = ChartFactory.createPieChart("Overrall All Accident Types Spread", pieDataSet, true,
                true, true);/*  ww  w.j a  va  2  s.  c  o m*/

        PiePlot p = (PiePlot) chart.getPlot();
        ChartFrame frame = new ChartFrame("pie Chart", chart);
        frame.setVisible(true);
        frame.setSize(700, 500);
    } else {
        JOptionPane.showMessageDialog(this, "Please Filter Data!");
    }
}

From source file:cnu.eslab.fileTest.NewJFrame.java

public void PieGraphGenerate(String[] arGrop, double[] arValue, String arTitle, String arSubTitle) {
    // ??   ?./*from   ww  w . j  ava2s.c o m*/
    DefaultPieDataset data = new DefaultPieDataset();
    data.setValue(arGrop[0], arValue[0]); //CPU
    data.setValue(arGrop[1], arValue[1]); //WIFI
    data.setValue(arGrop[5], arValue[5]); //3G
    data.setValue(arGrop[2], arValue[2]); //LED
    data.setValue(arGrop[3], arValue[3]); //GPS
    data.setValue(arGrop[4], arValue[4]); //AUDIO

    //offset data .
    RectangleInsets pieOffset = new RectangleInsets(50.0, 50.0, 50.0, 50.0);

    //  ?  ?.
    JFreeChart chart = ChartFactory.createPieChart(arTitle, data, true, true, false);
    TextTitle subTitle = new TextTitle(arSubTitle);
    chart.setBackgroundPaint(Color.WHITE);
    chart.addSubtitle(subTitle);
    PiePlot pieplot = (PiePlot) chart.getPlot();
    pieplot.setNoDataMessage("No data available");
    pieplot.setExplodePercent("LED", 0.20000000000000001D);
    pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({1} mW) ({2} percent)"));
    pieplot.setLabelBackgroundPaint(new Color(220, 220, 220));
    pieplot.setLegendLabelToolTipGenerator(new StandardPieSectionLabelGenerator("Tooltip for legend item {0}"));
    //? 
    /*pieplot.setSectionPaint(arGrop[0], new Color(0,0,0));
    pieplot.setSectionPaint(arGrop[1], new Color(60,60,60));
    pieplot.setSectionPaint(arGrop[2], new Color(120,120,120));
    pieplot.setSectionPaint(arGrop[4], new Color(180,180,180));*/

    //pieplot.setSimpleLabels(true);
    //pieplot.setSimpleLabelOffset(pieOffset);   //?? offset? .

    pieplot.setInteriorGap(0.0D);

    // ??   ?.
    ChartPanel chartPanel = new ChartPanel(chart);
    JFrame f = new JFrame("");
    f.setSize(600, 600);
    f.getContentPane().add(chartPanel);

    // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:semaforo.Semaforo.java

public static void editGrafico() {
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);/* w w w  . ja  v a 2s . co  m*/
    double cfd = 0.0;
    double bull = 0.0;
    double bear = 0.0;
    if (countCfd != 0)
        cfd = (countCfd * 100) / (countBear + countBull + countCfd);
    if (countBull != 0)
        bull = ((countBull * 100) / (countBear + countBull + countCfd));
    if (countBear != 0)
        bear = ((countBear * 100) / (countBear + countBull + countCfd));
    Semaforo.l1.setText("CFD (" + String.format("%.2f", cfd) + "%)");
    Semaforo.l2.setText("BULL (" + String.format("%.2f", bull) + "%)");
    Semaforo.l3.setText("BEAR (" + String.format("%.2f", bear) + "%)");
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    pieDataset.setValue("CFD (" + cfd + "%)", new Integer((int) countCfd));
    pieDataset.setValue("BULL (" + bull + "%)", new Integer((int) countBull));
    pieDataset.setValue("BEAR (" + bear + "%)", new Integer((int) countBear));
    JFreeChart chart = null;
    chart = ChartFactory.createPieChart("", // chart title
            pieDataset, // data
            false, // no legend
            false, // tooltips
            false // no URL generation
    );

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

    // 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, 0));
    PiePlot plot = null;
    plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(null);
    plot.setInteriorGap(0.00);
    plot.setOutlineVisible(true);

    // use gradients and white borders for the section colours

    plot.setBaseSectionOutlinePaint(Color.BLACK);
    plot.setSectionOutlinesVisible(true);
    plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));

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

    // add a subtitle giving the data source       

    // Mostramos la grafica dentro del jPanel1
    Semaforo.panel.setChart(chart);
}

From source file:recyclingsimulationUI.MainFrame.java

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
    // TODO add your handling code here:
    DbConnect conn = new DbConnect();
    ArrayList<String> name_array = new ArrayList<String>();
    ArrayList<String> count_array = new ArrayList<String>();
    try {//from   w w  w. j  a v  a2s  .  c o  m
        String totalCount = conn.getTotalCountOfTransacations();
        Object[] name_and_count_array = new Object[10];
        name_and_count_array = conn.getTransactionDetailsCount();
        name_array = (ArrayList<String>) name_and_count_array[0];
        count_array = (ArrayList<String>) name_and_count_array[1];
    } catch (SQLException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    for (int i = 0; i < name_array.size(); i++) {
        System.out.print(name_array.get(i));
    }
    DefaultPieDataset dp = new DefaultPieDataset();
    for (int i = 0; i < name_array.size(); i++) {
        dp.setValue(name_array.get(i), Integer.parseInt(count_array.get(i)));
    }

    JFreeChart pieChart = ChartFactory.createPieChart("Pie chart representing usage of each RCM", dp, true,
            true, false);
    PiePlot p = (PiePlot) pieChart.getPlot();

    ChartFrame cf = new ChartFrame("Pie Chart", pieChart);
    cf.setVisible(true);
    cf.setSize(750, 500);
}

From source file:recyclingsimulationUI.MainFrame.java

private void seeMaxCapacityChartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seeMaxCapacityChartActionPerformed
    // TODO add your handling code here:
    DbConnect conn = new DbConnect();
    ArrayList<String> capacity_array = new ArrayList<String>();
    ArrayList<String> name_array = new ArrayList<String>();
    try {/*from  w ww  .  j  av a2 s . c o  m*/
        Object[] obj = new Object[2];
        obj = conn.getCapacityOfRcms();
        name_array = (ArrayList<String>) obj[0];
        capacity_array = (ArrayList<String>) obj[1];
    } catch (SQLException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    for (int i = 0; i < name_array.size(); i++) {
        System.out.print(name_array.get(i));
    }
    DefaultPieDataset dp = new DefaultPieDataset();
    for (int i = 0; i < name_array.size(); i++) {
        dp.setValue(name_array.get(i), Integer.parseInt(capacity_array.get(i)));
    }

    JFreeChart pieChart = ChartFactory.createPieChart("Pie chart representing capacity of each RCM", dp, true,
            true, false);
    PiePlot p = (PiePlot) pieChart.getPlot();

    ChartFrame cf = new ChartFrame("Pie Chart", pieChart);
    cf.setVisible(true);
    cf.setSize(750, 500);
}