JFreeChart-Line Chart

JFreeChart Line Chart with Tutorial, Basic Terminology, What is Jfree Chart, Jfreechart Installation, Jfreechart class, Jfreechart's key classes, ChartUtilities class, ChartFrame class, PiePlot3D class, NumberAxis Class, XYSeriesCollection Class, TimeSeries Class, TimeSeriesCollection, PiePlot class etc.

JFreeChart-Line Chart

JFreeChart-Line Chart

A line chart is a series of points connected by a straight line segment. The information is shown through these connected lines. The line chart represents how data alters at equal time-frequency.

Line Chart example

Let us consider a sample data which shows frequent traffic on our website www.SamTpoint.com.

Date Total number of daily visitors
2016-12-19 200
2016-12-20 150
2016-12-21 100
2016-12-22 210
2016-12-23 240
2016-12-24 195
2016-12-25 245
Date Total number of unique visitors
2016-12-19 150
2016-12-20 130
2016-12-21 95
2016-12-22 195
2016-12-23 200
2016-12-24 180
2016-12-25 230

The following codes create a line chart from the above sample data:

LineChartExample.java

  1. import javax.swing.JFrame;  
  2. import javax.swing.SwingUtilities;  
  3.   
  4. import org.jfree.chart.ChartFactory;  
  5. import org.jfree.chart.ChartPanel;  
  6. import org.jfree.chart.JFreeChart;  
  7. import org.jfree.data.category.DefaultCategoryDataset;  
  8.   
  9. public class LineChartExample extends JFrame {  
  10.   
  11.   private static final long serialVersionUID = 1L;  
  12.   
  13.   public LineChartExample(String title) {  
  14.     super(title);  
  15.     // Create dataset  
  16.     DefaultCategoryDataset dataset = createDataset();  
  17.     // Create chart  
  18.     JFreeChart chart = ChartFactory.createLineChart(  
  19.         "Site Traffic"// Chart title  
  20.         "Date"// X-Axis Label  
  21.         "Number of Visitor"// Y-Axis Label  
  22.         dataset  
  23.         );  
  24.   
  25.     ChartPanel panel = new ChartPanel(chart);  
  26.     setContentPane(panel);  
  27.   }  
  28.   
  29.   private DefaultCategoryDataset createDataset() {  
  30.   
  31.     String series1 = "Visitor";  
  32.     String series2 = "Unique Visitor";  
  33.   
  34.     DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  35.   
  36.     dataset.addValue(200, series1, "2016-12-19");  
  37.     dataset.addValue(150, series1, "2016-12-20");  
  38.     dataset.addValue(100, series1, "2016-12-21");  
  39.     dataset.addValue(210, series1, "2016-12-22");  
  40.     dataset.addValue(240, series1, "2016-12-23");  
  41.     dataset.addValue(195, series1, "2016-12-24");  
  42.     dataset.addValue(245, series1, "2016-12-25");  
  43.   
  44.     dataset.addValue(150, series2, "2016-12-19");  
  45.     dataset.addValue(130, series2, "2016-12-20");  
  46.     dataset.addValue(95, series2, "2016-12-21");  
  47.     dataset.addValue(195, series2, "2016-12-22");  
  48.     dataset.addValue(200, series2, "2016-12-23");  
  49.     dataset.addValue(180, series2, "2016-12-24");  
  50.     dataset.addValue(230, series2, "2016-12-25");  
  51.   
  52.     return dataset;  
  53.   }  
  54.   
  55.   public static void main(String[] args) {  
  56.     SwingUtilities.invokeLater(() -> {  
  57.       LineChartExample example = new LineChartExample("Line Chart Example");  
  58.       example.setAlwaysOnTop(true);  
  59.       example.pack();  
  60.       example.setSize(600400);  
  61.       example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  62.       example.setVisible(true);  
  63.     });  
  64.   }  
  65. }