Search Engine

Thursday, June 17, 2010

Creating Pie Charts with JFreeChart

3.1. Create Project

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

3.2. Create plugin project based on jars

Create a Plugin project "de.vogella.rcp.jfreechart.libs" for the following JFreeChart jars. Please replace the version numbers with the version you are using. How to create a new plugin project for jars is described in Create a plugin project for your jar for details).

  • jcommon-1.0.16.jar

  • jfreechart-1.0.13.jar

  • jfreechart-1.0.13-experimental.jar

  • jfreechart-1.0.13-swt.jar

  • swtgraphics2d.jar

3.3. Add the plugin dependency

JFreeChart is using SWT therefore you need to add the dependency to SWT to the project. In project "de.vogella.rcp.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.rcp.jfreechart.pie". Select the file MANIFEST.MF and the tab "Dependencies". Add "de.vogella.rcp.jfreechart.libs" as a dependency.

3.4. Create view with pie chart

Change the coding of the view to the following:

package de.vogella.rcp.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.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {

public void createPartControl(Composite parent) {
JFreeChart chart = createChart(createDataset());
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.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setNoDataMessage("No data available");
return chart;


3.5. Run the application

Launch your application. The result should look like the following.


Post a Comment