Create SVG Bar Chart - JFreeChart Java Example

SVG Bar Charts in Java - Introduction


In the last tutorial, we described how to create a SVG Pie Chart in Java using JFreeChart / Batik with an example. Creating a SVG bar chart is no different to this approach. We just have to change the base JFreeChart code generation logic to create a bar chart instead of a Pie Chart. Also, this is across any type of chart you create using JFreeChart, you can just standardize and extend this approach. Since I was getting some questions around generating SVG bar chart with JFreeChart, I thought I would answer this with a Java example program. Let us see a quick example to create a SVG bar chart now. For detailed explanation of the code, I would suggest you to refer to the code provided in the earlier tutorial.

Java Program - SVG Bar Chart Example - JFreeChart - Batik


The Java program to create a SVG Bar Chart using JFreeChart / Apache Batik is provided below:

import java.io.*;
import org.jfree.data.category.DefaultCategoryDataset; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart;
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;
public class svgBarChart {  
     public static void main(String[] args){
        try {
                /* Define the data range for SVG Bar Chart */
                DefaultCategoryDataset my_bar_chart_dataset = new DefaultCategoryDataset();  
                my_bar_chart_dataset.addValue(77,"Keyword","JFreeChart");
                my_bar_chart_dataset.addValue(80,"Keyword","Batik");
                my_bar_chart_dataset.addValue(55,"Keyword","Chart");
                my_bar_chart_dataset.addValue(67,"Keyword","Apache");
                my_bar_chart_dataset.addValue(80,"Keyword","Java");                 
                /* This method returns a JFreeChart object back to us */                                
                JFreeChart BarChartObject=ChartFactory.createBarChart("KeywordVsCount- SVG Chart","Keyword","Count",my_bar_chart_dataset,PlotOrientation.VERTICAL,true,true,true);                  
                /* Our logical bar chart is ready at this step. We can now write the chart as SVG using Batik */
                /* 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 */
                BarChartObject.draw(my_svg_generator, new Rectangle2D.Double(0, 0, 640, 480), null);
                /* Write output to file */
                my_svg_generator.stream("output_bar_chart.svg");            
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}

This program creates a SVG Bar Chart as expected, which can be seen from a sample screenshot below:
JFreeChart- SVG Bar Chart Example Output - Java Program- Batik
JFreeChart- SVG Bar Chart Example Output - Java Program- Batik
That completes our tutorial to generate a SVG Bar chart using JFreechart / Batik in Java. Try this bar chart example with your code and post a comment if you are stuck somewhere. See you in a different JFreechart tutorial after some time. 

No comments:

Post a Comment