Share on Google+Share on Google+

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.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);

Output of the Program :

Download this example


Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Pie Chart   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 11, 2013
reports on jsp

can i get a code to generate reports over data from database
November 30, 2011
pie chart

nice :P
March 28, 2012
How I can change values in above ex.

I want to retrive values in graph from database how can I do this?
April 20, 2012
Pie Chart

when i run this program it just results errors with org.jfree doesnot exist.....