JFreeChart Java Line Chart Example

Java Line Chart Example


In this tutorial, we will see how to create a line chart / graph in Java, by using JFreechart open source library. I must admit that the more I write about JFreechart, the more I'm impressed with the way it provides different customization options to your chart. We will create a static PNG chart image in this tutorial. In later posts, we will discuss how to create SVG line charts with JFreechart also. To get started with this example, you need to download a copy of JFreechart library and have the JAR files viz jfreechart-xxx.jar and jcommon-xxx.jar file in your classpath.

JFreeChart - Line Chart - Java Program Example


The complete Java program to generate a line chart with JFreeChart Java library is provided below. Note that this program is based on DefaultCategoryDataset. It is also possible to use XYDataset for line chart. We will cover that in a separate post.

import java.io.*;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.ChartUtilities; 
public class lineChart {  
      public static void main(String[] args){
         try {
                
                /* Step - 1: Define the data for the line chart  */
                DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
                line_chart_dataset.addValue(15, "schools", "1970");
                line_chart_dataset.addValue(30, "schools", "1980");
                line_chart_dataset.addValue(60, "schools", "1990");
                line_chart_dataset.addValue(120, "schools", "2000");
                line_chart_dataset.addValue(240, "schools", "2010");                
                
                /* Step -2:Define the JFreeChart object to create line chart */
                JFreeChart lineChartObject=ChartFactory.createLineChart("Schools Vs Years","Year","Schools Count",line_chart_dataset,PlotOrientation.VERTICAL,true,true,false);                
                          
                /* Step -3 : Write line chart to a file */               
                 int width=640; /* Width of the image */
                 int height=480; /* Height of the image */                
                 File lineChart=new File("line_Chart_example.png");              
                 ChartUtilities.saveChartAsPNG(lineChart,lineChartObject,width,height); 
         }
         catch (Exception i)
         {
             System.out.println(i);
         }
     }
 }

Note that we are using createLineChart method in ChartFactory class, to create the line chart for us. The output of the program is shown below:

JFreechart - Java- Line Chart Example - Output - Plot Orientation Vertical
JFreechart - Java- Line Chart Example - Output - Plot Orientation Vertical
You can also set the Plot Orientation to HORIZONTAL to create a horizontal line chart. The code you need to change for that is provided below:

                /* Step -2:Define the JFreeChart object to create bar chart */
                JFreeChart lineChartObject=ChartFactory.createLineChart("Schools Vs Years","Year","Schools Count",line_chart_dataset,PlotOrientation.HORIZONTAL,true,true,false);                

JFreeChart - Java line chart example - Plot Orientation Horizontal
JFreeChart - Java line chart example - Plot Orientation Horizontal
That completes a quick tutorial to create line charts in JFreeChart in Java, using DefaultCategoryDataset. In the upcoming posts, we will discuss more options on creating line chart and see how to go about on formatting the chart plot area.

No comments: