tumblr counter

JFreeChart Example

JFreeChart Example

JFreeChart Example  explains about How to create chart using JFreeChart library

Consider a situation where you need to show dynamic (ie; data is populated dynamically) charts such as Pie Chart, Line Chart, Bar Chart, Bubble Chart, Polar Chart, Wind Chart etc, in your application. In that case you can use JFreeChart

JFreeChart is a free and open source java chart library used for creating professional quality charts. JFreeChart is purely written in java language, we can very easily incorporate JFreeChart in our java standalone and web applications.

Here I am showing an example about How to create  a  Pie Chart using JFreeChart.

Tools Needed For This Tutorial

You need to download

  1. JFreeChart

Following jar must be in classpath

  1. jfreechart-1.0.13.jar
  2. jcommon-1.0.16.jar

We can create following professional quality charts by using jfreechart.

  • Single valued Charts such as compass, speedometer, thermometer
  • Line Chart
  • Pie Chart
  • Bar Chart
  • Bubble Chart
  • Wind Chart
  • Polar Chart

JFreeChart Example

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;


public class JFreeChartExample extends JFrame {

 
private static final long serialVersionUID = 1L;

 
public JFreeChartExample(String applicationTitle, String chartTitle) {
       
super(applicationTitle);
       
//Creates a sample dataset
       
DefaultPieDataset dataSet = new DefaultPieDataset();
        dataSet.setValue
("Chrome", 29);
        dataSet.setValue
("InternetExplorer", 36);
        dataSet.setValue
("Firefox", 35);
       
       
// based on the dataset we create the chart
       
JFreeChart pieChart = ChartFactory.createPieChart3D(chartTitle, dataSet, true, true, false);
        PiePlot plot =
(PiePlot) pieChart.getPlot();
        plot.setStartAngle
(290);
        plot.setDirection
(Rotation.CLOCKWISE);
        plot.setForegroundAlpha
(0.5f);

       
// Adding chart into a chart panel
       
ChartPanel chartPanel = new ChartPanel(pieChart);
       
       
// settind default size
       
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
       
       
// add to contentPane
       
setContentPane(chartPanel);
   
}
   
public static void main(String[] args) {
     
JFreeChartExample chart = new JFreeChartExample("Browser Usage Statistics", "Which Browser are you using?");
        chart.pack
();
        chart.setVisible
(true);
   
}
}
Output

JFreeChart Example



Comments (4)
20.12.2012 00:27:25 Sam_brutus
Can you please tell me how to bring this graph to browser. Thanks, Sam sam_brutus@yahoo.com
21.12.2012 02:58:53 admin
you need to create pdf file dynamically using ireport and show on browser
11.06.2013 22:42:07 CG
Following jar must be in classpath

1.jfreechart-1.0.13.jar
2.jcommon-1.0.16.jar

Where do I get these? I am completely lost. I have been trying to find out how to use JFreeChart, but nothing is helping. I've downloaded JFreeChart. Now what do I do?
11.06.2013 22:53:48 CG
You need to download

1.JFreeChart
Following jar must be in classpath

1.jfreechart-1.0.13.jar
2.jcommon-1.0.16.jar

It even says those files must be in classpath....where is class path?