JFreeChart SVG XY Line Chart Graph Java Example

SVG XY Line Chart / Graph in JFreeChart

In this post, we will explain how to create a XY Line Chart in SVG Format, in Java using JFreeChart library. We will use JFreechart to create the XY Line chart and Apache Batik to write the created chart as a SVG output file. The example will be on the same lines as of our line chart one, and you have to make sure you have all the JAR files (both JFreechart and Batik) to keep the example going.

Java Program - SVG XY Line Chart 

The Java program to create an SVG XY Line Chart using JFreeChart is shown below:

/* This Java Program creates a XY Line Chart as SVG using JFreeChart and Batik */
import java.io.*;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.axis.*;
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.ChartUtilities; 
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 svgXYLineChart {  
     public static void main(String[] args){
        try {
                /* Define some XY Data series for the SVG chart */
                XYSeries team1_xy_data = new XYSeries("Team 1");
                team1_xy_data.add(1990, 45);
                team1_xy_data.add(1991, 16);
                team1_xy_data.add(1992, 80);
                team1_xy_data.add(1993, 1);
                team1_xy_data.add(1994, 6);
                XYSeries team2_xy_data = new XYSeries("Team 2");
                team2_xy_data.add(1990, 2);
                team2_xy_data.add(1991, 10);
                team2_xy_data.add(1992, 60);
                team2_xy_data.add(1993, 60);
                team2_xy_data.add(1994, 18);
                XYSeries team3_xy_data = new XYSeries("Team 3");
                team3_xy_data.add(1990, 15);
                team3_xy_data.add(1991, 5);
                team3_xy_data.add(1992, 14);
                team3_xy_data.add(1993, 18);
                team3_xy_data.add(1994, 25);
                /* Add all XYSeries to XYSeriesCollection */
                //XYSeriesCollection implements XYDataset
                XYSeriesCollection svgXYDataSeries= new XYSeriesCollection();
                // add series using addSeries method
                //Use createXYLineChart to create the chart
                JFreeChart XYLineChart=ChartFactory.createXYLineChart("Team - Number of Wins","Year","Win Count",svgXYDataSeries,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 */
                XYLineChart.draw(my_svg_generator, new Rectangle2D.Double(0, 0, 640, 480), null);
                /* Write output to file */
        catch (Exception i)

The high level steps to create a SVG XY Line Chart using JFreeChart and Batik is documented below:

  • Define all the XYSeries required for the chart.
  • Add all XYSeries to XYSeriesCollection object.
  • Use "createXYLineChart" method in ChartFactory to create a JFreeChart object.
  • Create a DOMImplementation object and add a SVG Document in Batik
  • Create a SVGGraphics2D object in the document and write the JFreechart to this object.
  • Stream the Graphics 2D object to a file on the disk. This creates an SVG Chart.
The Java program provided above creates a XY line chart, which is shown as a screenshot below:

JFreeChart - Create SVG XY Line Chart - Java Example - Output
JFreeChart - Create SVG XY Line Chart - Java Example - Output

No comments:

Post a Comment