Tag Archives: charts

Create charts in Java with JFreeChart

When you have to write a report or do a presentation you will probably need charts. We are about to see how to create charts efficiently in Java with a free API called JFreeChart.
By using this library you can generate any kind of chart: line, bar, pie, area, etc. You can fully customize the look & feel of your chart and even create 3D charts.

To use the library you should follow the following schema:

  1. Retrieve and format your data
  2. Create and fill the dataset of your chart
  3. Initialize your chart
  4. Customize its look & feel
  5. Render it

To make it easier we will start without retrieving the data and we will add some basic stuff in our dataset. You should know that you are able to get your data from a database (using JDBC) or from an XML file, and any other common data storage you can use.

Use JFreeChart in your project

Once you have downloaded the library’s archive, you need to add jcommon-x.x.xx.jar and jfreechart-x.x.xx.jar in your project’s classpath.

Create a 3D Pie Chart

/* Initialize the dataset and fill it. */
DefaultPieDataset dataset = new DefaultPieDataset();
 
dataset.setValue("IT Articles", 5);
dataset.setValue("Projects", 3);
dataset.setValue("Public", 1);
 
/* Create a 3D Pie chart. */
JFreeChart chart = ChartFactory.createPieChart3D("Posts on this blog", dataset, true, true, false);
 
/* Save the chart on the hard drive. */
ChartUtilities.saveChartAsJPEG(new File("data/posts-3d-pie.jpg"), chart, 500, 280);

It seems easy, isn’t it? Well, I will still explain this snippet.
I have followed the schema I gave you before. I create a dataset and fill it, then I create my chart and save it in a file (you can also instantly render it by using a BufferedImage for a website or a software application). The library use a factory to create charts which means each and every time you want to create a chart, from any kind (bar, line, etc), you will have to use this factory class.
Here is what you will get with this piece of code:

Customize the chart’s render

This library give you the ability to fully customize the look & feel of your chart: colors, legends, values’ format and so on.
In this article we will cover how to display the value of each category in our labels.

To do that we can create a custom rendering class which will be dedicated to define the label of each category. JFreeChart give us an interface for that, we will need to implements the required methods.

public class CustomPieSectionLabelGenerator implements PieSectionLabelGenerator
{
	/* other stuff... */
 
	@Override
	public String generateSectionLabel(PieDataset dataset, Comparable key)
	{
		StringBuilder label = new StringBuilder();
 
		if(dataset.getItemCount() > 0 && key != null)
		{
			/* I want to display the key
			 * but I also want to disply the value as an integer.
			 */
			label.append(key);
			label.append(": ");
			label.append(dataset.getValue(key).intValue());
		}
 
		return label.toString();
	}
}

With this custom label generator we will see ‘It Articles: 5‘ instead of ‘IT Articles‘ in the chart.
Last step: configure the chart to use our custom generator class.

/* Initialize the dataset and fill it. */
DefaultPieDataset dataset = new DefaultPieDataset();
 
dataset.setValue("IT Articles", 5);
dataset.setValue("Projects", 3);
dataset.setValue("Public", 1);
 
/* Create a 3D Pie chart. */
JFreeChart chart = ChartFactory.createPieChart3D("Posts on this blog", dataset, true, true, false);
 
/* We also want to display our value next to the legend. */
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setLabelGenerator(new CustomPieSectionLabelGenerator());
 
/* Save the chart on the hard drive. */
ChartUtilities.saveChartAsJPEG(new File("data/posts-3d-pie-custom.jpg"), chart, 500, 280);

Every time you will want to customize something it will follow the same pattern. Create a custom rendering class and implements the correct methods, configure your chart adequately. Take a look at the API documentation for more information.
Here is the result:

There are plenty of API in Java to generate charts and if you want to create a fully report I recommend iReport.