Home Chartgraphs Pie Chart



Pie Chart
Posted on: April 13, 2007 at 12:00 AM
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

Related Tags for Pie Chart:
cclassdatascriptdatasetiochardefaultipvichartusingthisidsetforieexamplewithtoexamdescriptioneitdessectionuseulartfaultinasmclesasepiepiproedacreatingxaxampsatishampleaandarcodcodertscrssriripthatiefaultpleplprndodeono


More Tutorials from this section

Ask Questions?    Discuss: Pie Chart   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.