JFreechart SVG Line Chart Graph Example

SVG Line Chart / Graph in Java Using JFreeChart


In this tutorial, we will explain how to use JFreeChart library to create a SVG line chart / graph in Java, with an example. JFreeChart component would be used to create the line chart and it will be rendered as SVG using Apache Batik library. You need to have all the JAR files supplied with JFreeChart and Batik distribution for this example to work. 

Required libraries for SVG Line Chart:

  • Apache Batik - 1.7 or equivalent
  • JFreeChart - 1.0.14
You can still use lower versions if required, as long as the methods we use in this example are supported.

Java Program - SVG Line Chart in JFreeChart


The complete Java program to create a SVG line chart in Java using JFreeChart and Batik is provided below: (Note that we had provided a step by step guide on creating SVG Charts earlier)

/* This Java Program creates a Line Chart as SVG using JFreeChart and Batik */
import java.io.*;
/* To define the line chart data */
import org.jfree.data.category.DefaultCategoryDataset;
/* To create line chart object */
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart;
/* To convert line chart as SVG */
import org.jfree.chart.plot.PlotOrientation;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import java.awt.geom.Rectangle2D;
import org.apache.batik.svggen.SVGGraphics2D;
/* batik - 1.7
JFreeChart - 1.0.14 */
public class lineChartSVG {  
     public static void main(String[] args){
        try {
                 /* Step - 1: Define the data for the line chart  */
                DefaultCategoryDataset svglineChartDataset = new DefaultCategoryDataset();
                svglineChartDataset.addValue(15, "schools", "1970");
                svglineChartDataset.addValue(30, "schools", "1980");
                svglineChartDataset.addValue(60, "schools", "1990");
                svglineChartDataset.addValue(120, "schools", "2000");
                svglineChartDataset.addValue(240, "schools", "2010"); 
                
                /* Create line chart object - we can then convert to SVG */
                JFreeChart lineChartObject=ChartFactory.createLineChart("Schools Vs Years","Year","Schools Count",svglineChartDataset,PlotOrientation.VERTICAL,true,true,false);   
                 
                /* Write Chart output as SVG */
                /* Get DOM Implementation */
                DOMImplementation mySVGDOM= GenericDOMImplementation.getDOMImplementation();
                /* create Document object */
                Document document = mySVGDOM.createDocument(null, "svg", null);
                /* Create SVG Generator */
                SVGGraphics2D my_svg_generator = new SVGGraphics2D(document);
                /* Render chart as SVG 2D Graphics object */
                lineChartObject.draw(my_svg_generator, new Rectangle2D.Double(0, 0, 640, 480), null);
                /* Write output to file */
                my_svg_generator.stream("svg_line_chart.svg");            
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}

The key steps in creating the line graph is documented below:


  • Define the dataset for the line chart
  • Create JFreeChart object using "createLineChart" method.
  • Get DOMImplementation to begin writing the chart as SVG
  • Create SVGGraphics2D object from the SVG document
  • Write the line chart as SVGGraphics2D object
  • Use "stream" method to write the graphics object to a physical SVG File.
The output of this program is shown below. (you will get a SVG version when you run. Showing a JPG version as an example)

JFreeChart - SVG Line Chart - Graph - Output - Example
JFreeChart - SVG Line Chart - Graph - Output - Example

No comments:

Post a Comment