Tuesday, August 5, 2014

Develop a Time Series Chart Using JFreeChart

Hi All,

When we want to show a chart in our java application we can use JFreeChart library for that. Here I am going to show you how we can develop a Time Series Chart. Later we will be using this chart to display values returned from an Arduino Sensor. So lets get started.

First we have to download the JFreeChart library, extract it to a location and find the necessary jar files. You can download JFreeChart here. JFreeChart site is here.

Create a new project in Eclipse and make a folder named 'lib' inside the project folder. Then extract the library zip file and copy the 'jfreechart-1.0.19' and 'jcommon-1.0.23' jars to the 'lib' folder. Then Right Click on the project -> Build Path -> Configure Build Path -> Libraries -> Add JARs and add the two jar files in the 'lib' folder to the project.

Create a class named 'JFreeDemo1.java' and extend it from the 'ApplicationFrame' class. Then add the constructor.










Then let's create the method to generate the dataset. Here we are using a XYDataset with Time Series Collection. There are two Time Series Used here. You can add Time Series to the Time Series Collection.


private XYDataset createXYDataSet()
 {  
  TimeSeriesCollection dataset = new TimeSeriesCollection();
  TimeSeries series2012 = new TimeSeries("Car Sales");
  TimeSeries series2013 = new TimeSeries("Van Sales");
  
  Random rand = new Random();
  
  for(int type = 0 ; type <2 ; type++ ){
   for(int mon = 1 ; mon < 13 ; mon++ )
   {
    Month month = new Month(mon, 2013); 
    int value = rand.nextInt(700)+300;
    
    if(type == 0)
    {
     series2012.add(month, value);
    }
    
    if(type == 1)
    {
     series2013.add(month, value);
    }
   }
  }
  
  dataset.addSeries(series2012);
  dataset.addSeries(series2013);
  
  return dataset;
 }


Now the dataset is ready. Next we will create a method that will return a JFreeChart. We are passing a dataset as parameter for the method. This data set will be used to create the chart.
private JFreeChart createChart(XYDataset dataset){
  JFreeChart chart = ChartFactory.createTimeSeriesChart(
      "Sales Chart", // Title
      "Month",     // Time Axis
      "Sales",     // Value Axis
      dataset,     // Dataset 
      true,      // legend
      true,      // tooltips
      false);     // generate urls
  
  XYPlot plot = (XYPlot)chart.getPlot();
  
  DateAxis dateAxis = (DateAxis)plot.getDomainAxis();
  dateAxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
  
  return chart;
 }
Now we will complete the constructor of the class.
public JFreeDemo1(String title) {
  super(title);
  JFreeChart chart = createChart(createXYDataSet());
  ChartPanel chartPanel = new ChartPanel(chart);
  chartPanel.setFillZoomRectangle(true);
  chartPanel.setMouseWheelEnabled(true);
  chartPanel.setPreferredSize(new java.awt.Dimension(800,400));
  setContentPane(chartPanel);
 }
Now we can complete the main method.
public static void main(String[] args) {
  JFreeDemo1 jfd = new JFreeDemo1("Time Series Chart Demo 01");
  jfd.pack();
  jfd.setVisible(true);
 }

Run the application and you will get a chart like this with the generated random dataset.
















Following is the complete code.

package com.dc.jfreedemo;

import java.text.SimpleDateFormat;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;

public class JFreeDemo1 extends ApplicationFrame{

 public JFreeDemo1(String title) {
  super(title);
  JFreeChart chart = createChart(createXYDataSet());
  ChartPanel chartPanel = new ChartPanel(chart);
  chartPanel.setFillZoomRectangle(true);
  chartPanel.setMouseWheelEnabled(true);
  chartPanel.setPreferredSize(new java.awt.Dimension(800,400));
  setContentPane(chartPanel);
 }
 
 private XYDataset createXYDataSet()
 {  
  TimeSeriesCollection dataset = new TimeSeriesCollection();
  TimeSeries series2012 = new TimeSeries("Car Sales");
  TimeSeries series2013 = new TimeSeries("Van Sales");
  
  Random rand = new Random();
  
  for(int type = 0 ; type <2 ; type++ ){
   for(int mon = 1 ; mon < 13 ; mon++ )
   {
    Month month = new Month(mon, 2013); 
    int value = rand.nextInt(700)+300;
    
    if(type == 0)
    {
     series2012.add(month, value);
    }
    
    if(type == 1)
    {
     series2013.add(month, value);
    }
   }
  }
  
  dataset.addSeries(series2012);
  dataset.addSeries(series2013);
  
  return dataset;
 }

 private JFreeChart createChart(XYDataset dataset){
  JFreeChart chart = ChartFactory.createTimeSeriesChart(
      "Sales Chart", // Title
      "Month",     // Time Axis
      "Sales",     // Value Axis
      dataset,     // Dataset 
      true,      // legend
      true,      // tooltips
      false);     // generate urls
  
  XYPlot plot = (XYPlot)chart.getPlot();
  
  DateAxis dateAxis = (DateAxis)plot.getDomainAxis();
  dateAxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
  
  return chart;
 }
 
 public static void main(String[] args) {
  JFreeDemo1 jfd = new JFreeDemo1("Time Series Chart Demo 01");
  jfd.pack();
  jfd.setVisible(true);
 }
}



Hope this was helpful :-) Thank You.

1 comment: