Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Chart a new course with JFreeChart

Add charts to your Java applications

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

As a developer, I am often asked to demonstrate new applications. After doing many demos, I noticed that users are often initially more interested in what an application looks like than what it does. I have also noticed that one of the best ways to make a good first impression is with a colorful, three-dimensional chart.

JFreeChart is a popular open source Java charting library that can generate most common chart types, including pie, bar, line, and Gantt charts. In addition, the JFreeChart API supports many interactive features, such as tool tips and zooming. JFreeChart provides an excellent choice for developers who need to add charts to Swing- or Web-based applications.

Note: The following examples are based on JFreeChart version 0.9.4. To compile and run the code included with this column, you must have two jar files from the JFreeChart distribution, jfreechart-0.9.4.jar and jcommon-0.7.1.jar, in your classpath.

Charts and datasets

To create a chart using JFreeChart, you must create a Dataset, which you then use to create a JFreeChart. A Dataset contains the data that displays in the chart. JFreeChart features many different Dataset objects, which you can use to create assorted types of charts. Once you create a Dataset, you next create the actual chart. JFreeChart uses an object appropriately named JFreeChart to represent charts. You create JFreeChart objects from Dataset objects with the ChartFactory class. In the following examples, we will create pie, XY, and bar charts along with their corresponding Dataset objects.

Pie chart

A pie chart is created from a PieDataset. The following example creates a PieDataset using the DefaultPieDataset class, adds two values via the setValue() method, and then creates a pie chart with the ChartFactory's createPieChart() method. This example will create a pie chart with the title "Sample Pie Chart," a legend, and two slices: JavaWorld with 75 percent of the pie, and Other with the other 25 percent:

DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart
                     ("Sample Pie Chart",   // Title
                      pieDataset,           // Dataset
                      true                  // Show legend  
                     );


XY chart

An XYDataset can create area, line, and step XY charts. The following example creates an XYDataset from a series of data containing three XY points. Next, ChartFactory's createAreaXYChart() method creates an area XY chart. In addition to parameters for title, dataset, and legend, createAreaXYChart() takes in the labels for the X and Y axes:

XYSeries series = new XYSeries("Average Size");
series.add(20.0, 10.0);
series.add(40.0, 20.0);
series.add(70.0, 50.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createAreaXYChart
                     ("Sample XY Chart",  // Title
                      "Height",           // X-Axis label
                      "Weight",           // Y-Axis label
                      xyDataset,          // Dataset
                      true                // Show legend
                     );


Bar chart

A CategoryDataset can create numerous different charts, including horizontal and vertical bar charts. The following example creates a CatagoryDataset with two series of data and two categories, and then creates a 3D vertical bar chart from this dataset. This example creates a chart that compares the sales growth in two quarters over two years:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (11)
Login
Forgot your account info?

Running A JfreeChart CodeBy Anonymous on February 19, 2010, 11:49 ami want to know how to set class path to run jfree code.. And i Also Want to make graph in jsp code ... what i hav to do to run the code on tomcat?????

Reply | Read entire comment

adding scrollbarBy Anonymous on October 22, 2009, 5:29 pmOnce you create a BufferedImage from the chart and use the image as an icon for a JLabel, you can just add the JLabel component to a JScrollPane container like you...

Reply | Read entire comment

downloadBy Anonymous on October 8, 2009, 5:50 amyou make my program work

Reply | Read entire comment

adding scrollbarBy Anonymous on September 29, 2009, 2:52 amHow to add a scroll bar to the bar chart generated

Reply | Read entire comment

JFree ChartBy Anonymous on August 20, 2009, 2:18 amIts very very useful

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources