JFree Chart Tutorial with basic example

JfreeChart is open source library for Java. It allows user to create / generate graphs easily. What ever we do, we need to report in some or the other way.

In Selenium, as you all know there is no reporting mechanism, we will depend on other frameworks like TestNG mainly for Reports.

How can you generate reports without TestNG. Using JFreechart user can easily generate graphs with basic commands which JfreeChart provides.

Before getting started, user should have JFreeChart jar files. You can download the lastes version of JfreeChart from Download JFreeChart Latest Version.

We can generate PieChart, 3DPieChart, BarChart, Bar Chart3D, Area Chart, XYBar Chart, Timeseries Chart Etc..

We will first demonstrate a vert basic example of generating a simple PieChart.

We will name the chart as "Test Case Execution Status" and send the data as "PASS", "FAIL" and "N/A".

Below is the example program for generating a PieChart.

import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class JFreeChartExample {
	public void pieChartReport()
		// Creating a simple pie chart with 
		 DefaultPieDataset pieDataset = new DefaultPieDataset();
		 pieDataset.setValue("PASS", new Integer(65));
		 pieDataset.setValue("FAIL", new Integer(25));
		 pieDataset.setValue("N/A", new Integer(10));

		 JFreeChart piechart = ChartFactory.createPieChart("Test Case Execution Status", pieDataset, true, true, false);

		 try {
			ChartUtilities.saveChartAsJPEG(new File("D:\\simplePiechart.jpg"), piechart, 400, 400);
		} catch (IOException e) {
			// TODO Auto-generated catch block
 public static void main(String[] args) {
 	 JFreeChartExample get = new JFreeChartExample();

Please refer the below output file which is generated by JFreeChart.
jfreechart piechart


I like your tutorials and examples.

I have a need for automated testing of charts and graphs on GUI application. Is there any library that helps in automatically test whether the generated graphs, charts, are proper or not?

In above example, through automation how to test the piechart generated with right values, right colors for PASS, FAIl, N/A ?


Add new comment