JFreeChart - Time Series Chart example

Posted on February 10, 2017


A Time Series chart represent a series of numerical data collected sequentially usually at fixed intervals of time.

In this post, we will show you how to create a Time Series chart using the JFreeChart java library.

JAR dependencies

Add the following jars to build path of your project.

  • jfreechart-1.0.19.jar
  • jcommon-1.0.23.jar

Sample data 

Consider the following sample data for a Time Series chart.

DateSeries 1Series 2
2017-01-015040
2017-01-024035
2017-01-034526
2017-01-043045
2017-01-055040
2017-01-064535
2017-01-076045
2017-01-084548
2017-01-095531
2017-01-104832
2017-01-116021
2017-01-124535
2017-01-136510
2017-01-144525
2017-01-155515

Java program

The following example create a Time Series chart from the above sample data.

TimeSeriesChartExample.java

package com.boraji.tutorial.jfreechart;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

/**
 * @author imssbora
 *
 */
public class TimeSeriesChartExample extends JFrame {

  private static final long serialVersionUID = 1L;

  public TimeSeriesChartExample(String title) {
    super(title);
    // Create dataset
    XYDataset dataset = createDataset();
    // Create chart
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Time Series Chart Example | WWW.BORAJI.COM", // Chart
        "Date", // X-Axis Label
        "Number", // Y-Axis Label
        dataset);

    //Changes background color
    XYPlot plot = (XYPlot)chart.getPlot();
    plot.setBackgroundPaint(new Color(255,228,196));
    
    ChartPanel panel = new ChartPanel(chart);
    setContentPane(panel);
  }

  private XYDataset createDataset() {
    TimeSeriesCollection dataset = new TimeSeriesCollection();

    TimeSeries series1 = new TimeSeries("Series1");
    series1.add(new Day(1, 1, 2017), 50);
    series1.add(new Day(2, 1, 2017), 40);
    series1.add(new Day(3, 1, 2017), 45);
    series1.add(new Day(4, 1, 2017), 30);
    series1.add(new Day(5, 1, 2017), 50);
    series1.add(new Day(6, 1, 2017), 45);
    series1.add(new Day(7, 1, 2017), 60);
    series1.add(new Day(8, 1, 2017), 45);
    series1.add(new Day(9, 1, 2017), 55);
    series1.add(new Day(10, 1, 2017), 48);
    series1.add(new Day(11, 1, 2017), 60);
    series1.add(new Day(12, 1, 2017), 45);
    series1.add(new Day(13, 1, 2017), 65);
    series1.add(new Day(14, 1, 2017), 45);
    series1.add(new Day(15, 1, 2017), 55);
    dataset.addSeries(series1);

    TimeSeries series2 = new TimeSeries("Series2");
    series2.add(new Day(1, 1, 2017), 40);
    series2.add(new Day(2, 1, 2017), 35);
    series2.add(new Day(3, 1, 2017), 26);
    series2.add(new Day(4, 1, 2017), 45);
    series2.add(new Day(5, 1, 2017), 40);
    series2.add(new Day(6, 1, 2017), 35);
    series2.add(new Day(7, 1, 2017), 45);
    series2.add(new Day(8, 1, 2017), 48);
    series2.add(new Day(9, 1, 2017), 31);
    series2.add(new Day(10, 1, 2017), 32);
    series2.add(new Day(11, 1, 2017), 21);
    series2.add(new Day(12, 1, 2017), 35);
    series2.add(new Day(13, 1, 2017), 10);
    series2.add(new Day(14, 1, 2017), 25);
    series2.add(new Day(15, 1, 2017), 15);
    dataset.addSeries(series2);
    

    return dataset;
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      TimeSeriesChartExample example = new TimeSeriesChartExample("Time Series Chart");
      example.setSize(800, 400);
      example.setLocationRelativeTo(null);
      example.setVisible(true);
      example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    });
  }
}

Output

 timeseries.png