JFreeChart-Pie Chart

JfreeChart Pie Chart with Tutorial, Basic Terminology, What is Jfree Chart, Jfreechart Installation, Jfreechart class, Jfreechart's key classes, ChartUtilities class, ChartFrame class, PiePlot3D class, NumberAxis Class, XYSeriesCollection Class, TimeSeries Class, TimeSeriesCollection, PiePlot class etc.

JFreeChart-Pie Chart

JFreeChart-Pie Chart

A pie chart is a circular graph that is divided into sectors in which the area of each sector represents the size of the data.

Pie Chart example

Let us consider a sample data of marks distribution for a class.

Marks Range Number of Students
80-100 120
60-79 80
40-59 20
20-39 7
0-19 3

The following codes create a pie chart from the above sample data:

PieChartExample.java

  1. import java.text.DecimalFormat;  
  2.   
  3. import javax.swing.JFrame;  
  4. import javax.swing.SwingUtilities;  
  5. import javax.swing.WindowConstants;  
  6.   
  7. import org.jfree.chart.ChartFactory;  
  8. import org.jfree.chart.ChartPanel;  
  9. import org.jfree.chart.JFreeChart;  
  10. import org.jfree.chart.labels.PieSectionLabelGenerator;  
  11. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;  
  12. import org.jfree.chart.plot.PiePlot;  
  13. import org.jfree.data.general.DefaultPieDataset;  
  14. import org.jfree.data.general.PieDataset;  
  15. public class PieChartExample extends JFrame {  
  16.   private static final long serialVersionUID = 6294689542092367723L;  
  17.   
  18.   public PieChartExample(String title) {  
  19.     super(title);  
  20.   
  21.     // Create dataset  
  22.     PieDataset dataset = createDataset();  
  23.   
  24.     // Create chart  
  25.     JFreeChart chart = ChartFactory.createPieChart(  
  26.         "Pie Chart Example",  
  27.         dataset,  
  28.         true,   
  29.         true,  
  30.         false);  
  31.   
  32.     //Format Label  
  33.     PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator(  
  34.         "Marks {0} : ({2})"new DecimalFormat("0"), new DecimalFormat("0%"));  
  35.     ((PiePlot) chart.getPlot()).setLabelGenerator(labelGenerator);  
  36.       
  37.     // Create Panel  
  38.     ChartPanel panel = new ChartPanel(chart);  
  39.     setContentPane(panel);  
  40.   }  
  41.   
  42.   private PieDataset createDataset() {  
  43.   
  44.     DefaultPieDataset dataset=new DefaultPieDataset();  
  45.     dataset.setValue("80-100"120);  
  46.     dataset.setValue("60-79"80);  
  47.     dataset.setValue("40-59"20);  
  48.     dataset.setValue("20-39"7);  
  49.     dataset.setValue("0-19"3);  
  50.     return dataset;  
  51.   }  
  52.   
  53.   public static void main(String[] args) {  
  54.     SwingUtilities.invokeLater(() -> {  
  55.       PieChartExample example = new PieChartExample(?Pie Chart Example?);  
  56.       example.setSize(800400);  
  57.       example.setLocationRelativeTo(null);  
  58.       example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  59.       example.setVisible(true);  
  60.     });  
  61.   }  
  62. }