Learn How to create charting applications in Java by using JFreeChart

0
1930
jfreechart

Charts and graphs are simple and effective ways to represent information such as revenue growth comparisons in different lines of business over a period of time. JFreeChart allows creating a wide range of charts in Java and Swing-based applications. JFreeChart is an open source library in Java which provides multiple APIs to create a wide range of charts on Java and Swing applications. It supports a number of outputs including all the standard industry outputs. It can be used in both server-side and client-side applications. JFreeChart is one of the most popular charting libraries among Java developers.

Importance of JFreeChart
The importance of JFreeChart has been captured in the following points:

  • Charts are still considered one of the most effective ways to represent data. JFreeChart has been supporting the creation of a wide variety of charts in Java and Swing-based applications.
  • Given that Java is used in almost all types of web-based applications, websites, portals, intranets and browsers, it is easy for developers to create charts with JFreeChart in such systems.
  • JFreeChart provides dedicated, well-documented APIs to create charts that range from simple charts such as the Pie Chart to complex charts such as the 3D Charts and Time Series charts. The only thing that the developers need to do is to call the APIs.
  • JFreeChart charts can be exported to all industry-standard outputs such as PDF, JPEG, PNG and SVG.
  • It is easy to install and use.
  • Since it is open source and its APIs or methods can be used and modified for commercial use, it is more popular than licensed, proprietary applications.

Where to use JFreeChart?
JFreeChart is relevant in the following cases and more:

  • When a Java or Swing-based application is being developed and it will be required to show data in the form of charts. The data will be fed to the application and the chart will be generated dynamically.
  • When you need chart outputs in standard formats such as PDF, JPEG and SVG.
  • When you need to develop or add a capability of displaying charts in your Java or Swing-based application and the turnaround time is really short. In such a case, the application just needs to call the APIs the JFreeChart library exposes.
  • When you need your application to represent data in the form of charts but you do not have the budget to buy licensed software that creates charts. JFreeChart is an open source library and is available for commercial use free of cost.

Environment setup
The sections below provide the steps to create the environment to setup the JFreeChart and then install JFreeChart.

Verify system prerequisites
The table below provides the minimum system requirements for installing JFreeChart.

JDK

1.5 or above

Memory

Minimum 2 GB RAM

Disk space

No minimum requirement

OS version

Linux or Windows

Installing JFreeChart
To install JFreeChart, complete the steps below:

Install Java
Verify that your system has the Java version “1.7.0_60 installed. To verify, run the commands as given below:

OS

Task

Command

Windows

Open the command terminal

java -version

Linux

Open the command console

$java -version

If the latest Java version is available, you will likely get the following message:

OS

Message

Windows

Java version “1.7.0_60” Java (TM) SE Run Time Environment (build 1.7.0_60-b19) Java HotSpot(TM) 64-bit/32-bit Server VM (build 24.60-b09,mixed mode)

Linux

java version “1.7.0_25” OpenJDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64) OpenJDK 64-Bit/32-bit Server VM (build 23.7-b01, mixed mode)

If Java is not installed, install the Java SDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html. After installing, complete the steps given above to verify that Java has been successfully installed.

Set Java home environment
Set the JAVA_HOME variable to point to the location or folder where you have Java installed. For example:

OS

Steps

Windows

Set Environmental variable JAVA_HOME to C:\ProgramFilesx86\java\jdk1.7.0_60

Linux

export JAVA_HOME=/usr/local/java-current

Add the Java compiler location
Add the Java compiler location to the system path as shown below:

OS

Steps

Windows

Add the string; C:\Program Filesx86\Java\jdk1.7.0_60\bin to the end of the system variable path.

Linux

export PATH=$PATH:$JAVA_HOME/bin/

Verify environment availability
To verify that the environment for JFreeChart has been successfully set up, run the command java –version as mentioned earlier in this article.

Install JFreeChart

  1. Download JFreeChart from the location http://www.jfree.org/jfreechart/download/.
  2. Extract the downloaded files to your local disk.
  3. Locate the files jfreechart-1.0.18.jar and jcommon-1.0.22.jar files in the extracted folder.
  4. Append the complete path of jfreechart-1.0.18.jar and jcommon-1.0.22.jar files to the classpath.

Writing Sample Application
Now, let us check one sample example. In this example, we will build a pie chart using jfree chart. Following is the work space set up for building the application. Here we have created the Java file and imported the required jar files.

work-space-setup

Figure1: Work space setup

Following is the complete code for the Java file.
Listing 1: Sample pie chart by using JFree chart in Java

package eduonix.jfree.demo;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
public class JFreePieDemo extends ApplicationFrame
{
public JFreePieDemo( String title )
{
super( title );
setContentPane(createDemoPanel( ));
}
private static PieDataset createDataset( )
{
DefaultPieDataset demodataset = new DefaultPieDataset( );
demodataset.setValue( "Python" , new Double( 20 ) );
demodataset.setValue( "Java" , new Double( 50 ) );
demodataset.setValue( "Oracle" , new Double( 20 ) );
demodataset.setValue( "C++" , new Double( 10 ) );
return demodataset;
}
private static JFreeChart createChart( PieDataset demodataset )
{
//Populating chart title and data
JFreeChart demochart = ChartFactory.createPieChart(
"Eduonix Training Report", // chart title
demodataset, // chart data
true,
true,
false);
return demochart;
}
public static JPanel createDemoPanel( )
{
JFreeChart demochart = createChart(createDataset( ) );
return new ChartPanel( demochart );
}
public static void main( String[ ] args )
{
JFreePieDemo demochrt = new JFreePieDemo( "Eduonix Training Report" );
demochrt.setSize( 600 , 400 );
RefineryUtilities.centerFrameOnScreen( demochrt );
demochrt.setVisible( true );
}
}

Now, we will run the application as shown in the following screen shot. The same application can also be executed from the command prompt.

run-the-application

Figure2: Run the application

Following will be the output of the application. It will show the data set in a pie chart.

output-of-the-application

Figure3: Output of the application

Conclusion :
JFreeChart is probably the most popular software available for creating charts on Java and Swing applications. What adds to its appeal are the well-documented APIs that help develop simple to complex charts in a short time; free of cost availability and simple and straightforward installation process. The library is being constantly updated and the APIs are getting better day by day.

LEAVE A REPLY

Please enter your comment!
Please enter your name here