Version 1.4
Copyright © 2009, 2010, 2011, 2011, 2012 Lars Vogel
01.05.2012
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 29.10.2007 | Lars Vogel |
Created |
| Revision 0.2 - 1.4 | 16.03.2008 - 01.05.2012 | Lars Vogel |
updates and bugfixes |
Table of Contents
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/
Download the JFreeChart distribution from the website http://www.jfree.org/jfreechart/
Create a new Java project "de.vogella.jfreechart.swing.pie". Create also a package "de.vogella.jfreechart.swing.pie".
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).
Create the following two classes.
package de.vogella.jfreechart.swing.pie; 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; package de.vogella.jfreechart.swing.pie; 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.pie; 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.
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
http://www.jfree.org/jfreechart/ JFreeChart Homepage
vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system