JFreeChart - A great tool to implement Graphs & Charts in Java

While working with graphs in Java, the most strong tool I have ever found is : JFreeChart. Using this , it becomes very easy to create Bar chart, Linear Chart, Pie chart etc. Not only, these charts, it also supports some less popular charts as well, e.g. - wind chart, polar chart etc.
So, to start with JFreeChat, we need 2 jars : jfreechart-1.0.13.jar & jcommon-1.0.16.jar. Just download these jars & add them in the classpath
Here are the  quick links for downloading these  jar files:

jfreechart-1.0.13.jar - Download Here
jcommon-1.0.16.jar - Download here

We will start from a basic project in JFreeChart here.










We are trying to create a simple bar chart based on a couple data which is hard coded in our code. Whenever the code is run, a JFrame will be opened with the chart or graph created from the data.

Here is the basic class which has public static void main -


public class SampleChartMainClass {

 public static void main(String[] args) {
  BarChart demo = new BarChart("Employee Salary Increment Graph",
    "");
  demo.pack();
  demo.setVisible(true);
 }

}

In this code, Barchart class is a class which I have created, and the main operation regarding the chart is written in BarChart class. Here is the class as follows :

package com.src.blog;

import java.awt.Color;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;

public class BarChart extends JFrame {

 private static final long serialVersionUID = 1L;

 public BarChart(String applicationTitle, String chartTitle) {
  super(applicationTitle);
  CategoryDataset dataset = new setValuesToDataset().createDataSet();
  JFreeChart chart = createChart(dataset, chartTitle);
  ChartPanel chartPanel = new ChartPanel(chart);
  
  // to define the size of the bar chart
  chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
  setContentPane(chartPanel);

 }

 /**
  * Creates a chart
  */

 private JFreeChart createChart(CategoryDataset dataset, String title) {
         //creates 3D bar chart with the chart name as "Employee Salary Increment"
         // In order to create a horizontal bar chart, we need yo mention Plot orientation as PlotOrientation.HORIZONTAL

  JFreeChart chart = ChartFactory.createBarChart3D(
    "Employee Salary Increment", "Employees", "Salary Increment",
    dataset, PlotOrientation.VERTICAL, true, true, false);
  Plot plot = chart.getPlot();
  
  // set the opacity of the hart
  plot.setForegroundAlpha(0.5f);
  CategoryPlot p = chart.getCategoryPlot();
  
  // set the color of the grid line
  p.setRangeGridlinePaint(Color.blue);
  //set the color of the title
  chart.getTitle().setPaint(Color.blue);
  return chart;

 }

}


Now, we need one more class where we are setting the data for the chart

package com.src.blog;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.date.DateUtilities;

public class setValuesToDataset {
 
 /**
  * bar chart / line chart
         * Enter dataset for Graph
  * @return
  */
 public DefaultCategoryDataset createDataSet() {
  DefaultCategoryDataset result = new DefaultCategoryDataset();
  result.setValue(10, "Salary Increment percentage", "A Band Employees");
  result.setValue(8, "Salary Increment percentage", "B Band Employees");
  result.setValue(6, "Salary Increment percentage", "C Band Employees");
  result.setValue(-2, "Salary Increment percentage", "D Band Employees");
  
  return result;
 }
}


Compile it & then run it. You will get the following graph as below:

So, creating Bar chart in Java is as easy as that. Even changing JFreeChart, we are able to change the appearance of the Chart as well.
I have created a sample Program where you can find the source code of my project & you can see how to create some other graphs like Piechart, windchart, line chart, polar chart etc. You can download the link for the source code of the project Here. Please note that, I have usd JFrame as I was trying to open the graph in another window while running the code. But, you can save the graph is your machine as an image also.

You can just let me know if you need some more type of graphs to be implemented using Java. I can't promise but I will try.


Happy Learning !!

No comments :

Post a Comment