Simple Pie Chart
JFreeChart is a free and pure java chart library for
creating professional quality charts.
This requires the libraries
jfreechart-1.0.13.jar and
jcommon-1.0.8.jar
to be in classpath.
The following example shows creating a simple pie chart.
package com.bethecoder.tutorials.jfree_charts.tests;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class SimplePieChart {
/**
* @param args
*/
public static void main ( String [] args ) {
//Prepare the data set
DefaultPieDataset pieDataset = new DefaultPieDataset () ;
pieDataset.setValue ( "Coca-Cola" , 26 ) ;
pieDataset.setValue ( "Pepsi" , 20 ) ;
pieDataset.setValue ( "Gold Spot" , 12 ) ;
pieDataset.setValue ( "Slice" , 14 ) ;
pieDataset.setValue ( "Appy Fizz" , 18 ) ;
pieDataset.setValue ( "Limca" , 10 ) ;
//Create the chart
JFreeChart chart = ChartFactory.createPieChart (
"Soft Drink Pie Chart" , pieDataset, true, true, true ) ;
//Render the frame
ChartFrame chartFrame = new ChartFrame ( "Simple Pie Chart" , chart ) ;
chartFrame.setVisible ( true ) ;
chartFrame.setSize ( 400 , 300 ) ;
}
}
It gives the following output,