JFreeChart
1. Overview
JFreeChart is a free chart library for the Java(tm) platform. It is designed for use in applications, applets, servlets and JSP. JFreeChart is distributed with complete source code subject to the terms of the GNU Lesser General Public Licence, which permits JFreeChart to be used in proprietary or free software applications.
2. Features
JFreeChart can generate pie charts, bar charts (regular and stacked, with an optional 3D-effect), line charts, scatter plots, time series charts (including moving averages, high-low-open-close charts and candlestick plots), Gantt charts, meter charts (dial, compass and thermometer), symbol charts, wind plots, combination charts and more. Additional features include: • data is accessible from any implementation of the defined interfaces; • export to PNG and JPEG image file formats (or you can use Java’s ImageIO library to export to any format supported by ImageIO); • export to any format with a Graphics2D implementation including: – PDF via iText (http://www.lowagie.com/iText/); – SVG via Batik (http://xml.apache.org/batik/); • tool tips; • interactive zooming; • chart mouse events (these can be used for drill-down charts or information pop-ups); • annotations; • HTML image map generation; • works in applications, servlets, JSP (thanks to the Cewolf project1) and applets; • distributed with complete source code subject to the terms of the GNU Lesser General Public License (LGPL).
3. Configuring Eclipse for JFreeChart
To begin with, you need to download the JFreeChart and JCommon distributions, unpack them on your local machine, and generate the API documentation. The following steps are necessary: 1. Download the latest version of the JCommon class library: http://www.jfree.org/jcommon/ …and unpack it to a directory on your computer (almost anywhere is fine). 2. From the ant subdirectory of the just-unpacked JCommon, run ant javadoc to generate the Javadocs locally. If you are unfamiliar with Ant, you can skip this step, but then Eclipse won’t be able to show you the Javadoc popups for JCommon. 3. Download the latest version of the JFreeChart class library: http://www.jfree.org/jfreechart/ …and unpack it to a directory on your computer (again, almost anywhere is fine). 4. From the ant subdirectory of the just-unpacked JFreeChart, run ant javadoc to generate the Javadocs locally. As with step 2, you can skip this step, but then you’ll be missing the API documentation. Now, launch Eclipse, and carry out the following steps to configure JFreeChart and JCommon as user libraries: 5. In Eclipse, select Preferences… from the Window menu, then choose the Java –>Build Path -> User Libraries node in the tree.
6. Click on the New… button and enter JCommon 1.0.12 as the name for a new user library. 7. Ensure that the JCommon 1.0.12 item is selected in the list, then click the Add JARs… button and select the jcommon-1.0.12.jar file from the JCommon directory created back in step 1. 8. Double-click the item that says “Source attachment: (None)”, then click the External folder… button, then select the source directory for JCommon. 9. Double-click the item that says “Javadoc location: (None)”, then click the Browse…button, then select the javadoc directory from JCommon (see step 2). 10. Click on the New… button and enter JFreeChart 1.0.7 as the name for a new user library. 11. Ensure that the JFreeChart 1.0.7 item is selected in the list, then click on the Add JARs… button and select the jfreechart-1.0.7.jar file from the JFreeChart directory (see step 3). 12. Double-click the item that says “Source attachment: (None)”, then click the External folder… button, then select the source directory for JFreeChart. 13. Double-click the item that says “Javadoc location: (None)”, then click the Browse…button, then select the javadoc directory from JFreeChart (see step 4). 
4. Creating an Eclipse Project that uses JFreeChart
Now that JFreeChart and JCommon are configured as user libraries, it is straightforward to develop an application that uses these libraries: 1. In Eclipse, select New -> Project… from the File menu, select Java Project from the list and click the Next button. 2. Enter MyAppThatUsesJFreeChart as the project name and click the Finish button. 3. Right-click on the project in the Package Explorer then select Properties from the pop-up menu. In the properties window click on the Add Library… button and select both the JCommon and JFreeChart libraries. Click OK.
4. Create a new source file.
5. Example Codes and Charts
5.1 Bar Chart
Code
import java.awt.Color; import java.awt.Dimension; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.category.DefaultCategoryDataset; public class Bar { public static void main3(String [] args){ DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(1.0, “Row 1″, “Column 1″); dataset.addValue(5.0, “Row 1″, “Column 2″); dataset.addValue(3.0, “Row 1″, “Column 3″); dataset.addValue(2.0, “Row 2″, “Column 1″); dataset.addValue(3.0, “Row 2″, “Column 2″); dataset.addValue(2.0, “Row 2″, “Column 3″); JFreeChart chart = ChartFactory.createBarChart( “Bar Chart Demo”, // chart title “Category”, // domain axis label “Value”, // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setRangeGridlinePaint(Color.white); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setSeriesPaint(0, Color.gray); renderer.setSeriesPaint(1, Color.orange); renderer.setDrawBarOutline(false); renderer.setItemMargin(0.0); ChartPanel chartPanel = new ChartPanel(chart, false); chartPanel.setPreferredSize(new Dimension(500, 270)); ChartFrame frame = new ChartFrame(“Bar”, chart); frame.pack(); frame.setVisible(true); } }
Chart

5.1 Pie Chart
Code
import java.awt.Color; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.DefaultPieDataset; public class Pie { public static void main(String [] args){ DefaultPieDataset data = new DefaultPieDataset(); data.setValue(“Category 1″, 43.2); data.setValue(“Category 2″, 27.9); data.setValue(“Category 3″, 79.5); // create a chart… JFreeChart chart = ChartFactory.createPieChart3D( “Sample Pie Chart”, data, true, // legend? true, // tooltips? false // URLs? ); PiePlot plot = (PiePlot) chart.getPlot(); plot.setSectionPaint(“Category 1″, new Color(200, 255, 255)); plot.setSectionPaint(“Category 2″, new Color(200, 200, 255)); plot.setExplodePercent(“Category 2″, 0.30); ChartFrame frame = new ChartFrame(“First”, chart); frame.pack(); frame.setVisible(true); } }
Chart
