Pie Chart

Ads
 

Pie Chart

In this section we are providing you an example to create a Pie Chart.

In this section we are providing you an example to create a Pie Chart.

Pie Chart

     

In this section we are providing you an example to create a Pie Chart.

Description of Example
 
For creating a Pie Chart we use PieDataset. In this example, for creating a PieDataset we are using DefaultPieDataset class. Then we add some data in this dataset that will show in chart.

Description of Code
 
  DefaultPieDataset pieDataset = new DefaultPieDataset();
For defining a dataset for a pie chart we have to create an object of DefaultPieDataSet type :
  

   
setValue(?one?, new Integer(10));
After creating the instance of dataset then we have to add the data in the data set by invoking the method setValue(). Like
    
  JFreeChart chart = ChartFactory.createPieChart("Pie Chart using JFreeChart", pieDataset true, true, true);
After added the data in dataset we create the Pie Chart by invoking the createPieChart() method. This method is a static method of ChartFactory class and its returns the object of JFreeChart type. This method syntax is:
  Public static JFreeChart createPieChart(java.lang.String title, pieDataset dataset, boolean legend, boolean tooltips, boolean urls);

  ChartFrame frame1=new ChartFrame("Pie Chart",chart);
After this we create the object of ChartFrame. It used to display a chart.

Here is code of the program :

import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.*;

public class pie{
public static void main(String arg[]){
  DefaultPieDataset pieDataset = new DefaultPieDataset();
  pieDataset.setValue("One"new Integer(10));
  pieDataset.setValue("Two"new Integer(20));
  pieDataset.setValue("Three"new Integer(30));
  pieDataset.setValue("Four"new Integer(10));
  pieDataset.setValue("Five"new Integer(20));
  pieDataset.setValue("Six"new Integer(10));
  JFreeChart chart = ChartFactory.createPieChart
 ("Pie Chart using JFreeChart", pieDataset, true,true,true);

  ChartFrame frame1=new ChartFrame("Pie Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(300,300);
  }
}

Output of the Program :

Download this example

Ads

Ads