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

JFreeChart in Eclipse RCP - Tutorial

Lars Vogel

Version 0.7

29.06.2009

Revision History
Revision 0.1-0.412.10.2007Lars Vogel
Created article and made it working
Revision 0.501.01.2009Lars Vogel
New naming conventions for projects
Revision 0.617.06.2009Lars Vogel
Added link to launch configuration in Eclipse RCP article
Revision 0.729.06.2009Lars Vogel
Minor re-work, added link to http://www.vogella.de/articles/JFreeChart/article.html

Eclipse JFreeChart

This article describes the usage of JFreeChart within an Eclipse Rich Client Platform (RCP) application. It will use the JFreeChart SWT implementation.

As examples we create a pie chart in an Eclipse RCP application.

This article assume that you have already Eclipse RCP knowledge.


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. Add the plugin dependency
3.4. Create view with pie chart
3.5. Run the application
4. Creating Bar Charts with JFreeChart
4.1. Create Project
4.2. Create view with bar chart
5. Thank you
6. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. 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/

This article assumes that you have knowledge in creating simple Eclipse RCP applications. See Eclipse RCP to learn this.

Tip

If you are looking for a tutorial for JFreeChart in Swing have a look at the JFreeChart Tutorial.

2. Download JFreeChart

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

3. Creating Pie Charts with JFreeChart

3.1. Create Project

Create a new Plugin project "de.vogella.jfreechart.pie" using "RCP application with a view" as a template (for help see Eclipse RCP.

3.2. Add jars to build path of your project

Create a Plugin project "de.vogella.jfreechart.libs" for your JFreeChart jars (use Create a plugin project for your jar for details). Please replace the version numbers with the version you are using.

  • jcommon-1.0.10.jar

  • jfreechart-1.0.6.jar

  • jfreechart-1.0.6-experimental.jar

  • jfreechart-1.0.6-swt.jar

  • swtgraphics2d.jar

3.3. Add the plugin dependency

In project "de.vogella.jfreechart.libs" select the MANIFEST.MF and the tab "Dependencies". Press "Add" and select org.eclipse.swt as the same jars to your runtime environment.

Add "de.vogella.jfreechart.libs" as a dependency to project "de.vogella.jfreechart.pie". Select in "de.vogella.jfreechart.pie" the file MANIFEST.MF and select the tab "Dependencies". Press Add and select org.eclipse.swt as the same jars to your runtime environment.

3.4. Create view with pie chart

Change the coding of the view to the following:

				
package de.vogella.jfreechart.pie;

import java.awt.Font;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {
	public static final String ID = "de.vogella.jfreechart.pie.view";

	public void createPartControl(Composite parent) {
		JFreeChart chart = createChart(createDataset());
		final ChartComposite frame = new ChartComposite(parent, SWT.NONE,
				chart, true);
	}

	public void setFocus() {
	}

	/**
	 * Creates the Dataset for the Pie chart
	 */
	private PieDataset createDataset() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("One", new Double(43.2));
		dataset.setValue("Two", new Double(10.0));
		dataset.setValue("Three", new Double(27.5));
		dataset.setValue("Four", new Double(17.5));
		dataset.setValue("Five", new Double(11.0));
		dataset.setValue("Six", new Double(19.4));
		return dataset;
	}

	/**
	 * Creates the Chart based on a dataset
	 */
	private JFreeChart createChart(PieDataset dataset) {

		JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart
				// title
				dataset, // data
				true, // include legend
				true, false);

		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setSectionOutlinesVisible(false);
		plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		plot.setNoDataMessage("No data available");
		plot.setCircular(false);
		plot.setLabelGap(0.02);
		return chart;

	}
}
			

3.5. Run the application

See the links for details on how to create a Eclipse run configuration .

The result should look like the following.

4. Creating Bar Charts with JFreeChart

This example is based on the previous example. Make sure you have done the previous one.

4.1. Create Project

Create a new Plugin project "de.vogella.jfreechart.barchart" using "RCP application with a view" as a template.

Add "de.vogella.jfreechart.libs" as a dependency to project "de.vogella.jfreechart.barchart".

4.2. Create view with bar chart

Change the coding of the view to the following:

				
package de.vogella.jfreechart.barchart;

import java.awt.Color;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {
	public static final String ID = "de.vogella.jfreechart.barchart.view";

	private CategoryDataset createDataset() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		// row keys...
		String series1 = "First";
		String series2 = "Second";

		// column keys...
		String category1 = "Label 1";
		String category2 = "Label 2";
		String category3 = "Label  3";

		dataset.addValue(1.0, series1, category1);
		dataset.addValue(4.0, series1, category2);
		dataset.addValue(3.0, series1, category3);

		dataset.addValue(5.0, series2, category1);
		dataset.addValue(7.0, series2, category2);
		dataset.addValue(6.0, series2, category3);

		return dataset;
	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            dataset.
	 * 
	 * @return A chart.
	 */
	private JFreeChart createChart(CategoryDataset dataset) {

		JFreeChart chart = ChartFactory.createBarChart("Bar Chart", // chart
				// title
				"Labels", // domain axis label
				"Values", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips?
				false // URLs?
				);

		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.white);
		return chart;

	}

	public void createPartControl(Composite parent) {
		JFreeChart chart = createChart(createDataset());
		final ChartComposite frame = new ChartComposite(parent, SWT.NONE,
				chart, true);
	}

	public void setFocus() {
	}
}
			

Run it and see that it is working.

5. 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.

6. 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.

7. Links and Literature

7.1. Source Code

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