Java, Eclipse and Web programming Tutorials
Follow me on twitter About Lars Vogel

JFreeChart - Tutorial

Lars Vogel

Version 0.1

29.06.2009

Revision History
Revision 0.129.10.2007Lars Vogel
Created article, similar to http://www.vogella.de/articles/EclipseJFreeChart/article.html

Abstract

This article describes the usage of the Java library JFreeChart.

As example we create a pie chart.


Table of Contents

1. Overview
2. Download JFreeChart
3. Creating Pie Charts with JFreeChart
3.1. Create Project
3.2. Add jars to build path of your project
3.3. Code
3.4. Run
4. Thank you
5. Questions and Discussion
6. Links and Literature
6.1. JFreeChart Resources
6.2. Source Code
6.3. Other Resources

1. Overview

JFreeChart is a free 100% Java chart library created by David Gilbert. JFreeChart makes it easy for developers to display professional quality charts in their applications. For details on JFreeChart please check the following link. http://www.jfree.org/jfreechart/

Tip

If you are looking for a tutorial for using JFreeChart in Eclipse RCP or Eclipse plugins have a look here Eclipse JFreeChart Tutorial .

2. Download JFreeChart

Download the JFreeChart distribution from the website http://www.jfree.org/jfreechart/

3. Creating Pie Charts with JFreeChart

3.1. Create Project

Create a new Java project "de.vogella.jfreechart.swing.pie". Create also a package "de.vogella.jfreechart.swing.pie".

3.2. Add jars to build path of your project

In your project create a folder "lib", and paste the JFreeChart jars into this folder.

You only need the following libraries. Please replace the version numbers with the version you are using.

  • jcommon-1.0.16.jar

  • jfreechart-1.0.13.jar

Add these libraries to your classpath. See Eclipse Java IDE - Classpath for details).

3.3. Code

Create the following two classes.

				
package de.vogella.jfreechart.swing.bar;

import javax.swing.JFrame;

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


public class PieChart extends JFrame {

	private static final long serialVersionUID = 1L;

	public PieChart(String applicationTitle, String chartTitle) {
        super(applicationTitle);
        // This will create the dataset 
        PieDataset dataset = createDataset();
        // based on the dataset we create the chart
        JFreeChart chart = createChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // add it to our application
        setContentPane(chartPanel);

    }
    
    /**
     * Creates a sample dataset 
     */
    private  PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;
        
    }
    
    /**
     * Creates a chart
     */
    private JFreeChart createChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(
            title,  				// chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
        
    }
    
   
   

}

			

				
package de.vogella.jfreechart.swing.bar;


public class Main {
	 public static void main(String[] args) {
	        PieChart demo = new PieChart("Comparison", "Which operating system are you using?");
	        demo.pack();
	        demo.setVisible(true);
	    }
}

			

I assume that the coding is pretty much self-explaining. I tried to add lots of comments to make it easier to understand. For more complex examples have a look at the JFreeChart homepage.

Tip

David Gilbert is also selling an excellent developer guide on the JFreeChart homepage. If you are intensively using JFreeChart you should buy the developer guide from David to support him and to get excellent information access.

3.4.  Run

Run Main.java. The result should look like the following.

4. Thank you

Thank you for practicing with this tutorial.

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

5. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.

I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.

6. Links and Literature

6.1. JFreeChart Resources

http://www.jfree.org/jfreechart/ JFreeChart Homepage

6.2. Source Code

http://www.vogella.de/code/codeeclipse.html Source Code of Examples