JFreeChart - Line Chart example

Posted on December 25, 2016


A line chart is a series of points connected by a straight line segments. In this post we will show you how to create a line chart using the JFreeChart java library.

JAR dependencies

Add the following jars to build path of your project, if you are using Eclipse.

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

Or add the following dependency to pom.xml file, if you are using Maven.

<dependencies>
  <dependency>
     <groupId>org.jfree</groupId>
     <artifactId>jfreechart</artifactId>     
     <version>1.0.19</version>
  </dependency>
</dependencies>

Prepare sample data for Line chart

In this example we are using the sample traffic data of www.boraji.com site.

Total number of daily visitor

DateNumber of Visitors              
2016-12-19200
2016-12-20150
2016-12-21100
2016-12-22210
2016-12-23240
2016-12-24195
2016-12-25245

Total number of daily unique visitor

DateNumber of UNIQUE Visitors
2016-12-19150
2016-12-20130
2016-12-2195
2016-12-22195
2016-12-23200
2016-12-24180
2016-12-25230

 

Line Chart example

LineChartExample.java

package com.boraji.tutorial.jfreechart;

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

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;

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

  private static final long serialVersionUID = 1L;

  public LineChartExample(String title) {
    super(title);
    // Create dataset
    DefaultCategoryDataset dataset = createDataset();
    // Create chart
    JFreeChart chart = ChartFactory.createLineChart(
        "Site Traffic (WWW.BORAJI.COM)", // Chart title
        "Date", // X-Axis Label
        "Number of Visitor", // Y-Axis Label
        dataset
        );

    ChartPanel panel = new ChartPanel(chart);
    setContentPane(panel);
  }

  private DefaultCategoryDataset createDataset() {

    String series1 = "Vistor";
    String series2 = "Unique Visitor";

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.addValue(200, series1, "2016-12-19");
    dataset.addValue(150, series1, "2016-12-20");
    dataset.addValue(100, series1, "2016-12-21");
    dataset.addValue(210, series1, "2016-12-22");
    dataset.addValue(240, series1, "2016-12-23");
    dataset.addValue(195, series1, "2016-12-24");
    dataset.addValue(245, series1, "2016-12-25");

    dataset.addValue(150, series2, "2016-12-19");
    dataset.addValue(130, series2, "2016-12-20");
    dataset.addValue(95, series2, "2016-12-21");
    dataset.addValue(195, series2, "2016-12-22");
    dataset.addValue(200, series2, "2016-12-23");
    dataset.addValue(180, series2, "2016-12-24");
    dataset.addValue(230, series2, "2016-12-25");

    return dataset;
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      LineChartExample example = new LineChartExample("Line Chart Example");
      example.setAlwaysOnTop(true);
      example.pack();
      example.setSize(600, 400);
      example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      example.setVisible(true);
    });
  }
}

Outputlinechart.png