Wednesday, April 13, 2011

Jfreechart : Charting tools in java

iText is a wonderful library if you want to generate PDFs in Java. It comes with a huge set of API to create/manage a PDF file. IText can be used in cases like : how to generate a pdf file in itext and also how to merge two pdf  or splitting pdf files using itext.
In this tutorial we will see how to generate Pie charts and Bar charts in Java using iText and jFreeChart library. First a brief note about jFreeChart.
JFreeChart is a free 100% Java chart library that makes it easy for developers to display professional quality charts in their applications. It gives a wide range of API to generate charts and graphs in Java.

Step 1: Download required Jar files

For this example we will need following jar files.
  • iText-2.1.5.jar (download)
  • jcommon-1.0.16.jar
  • jfreechart-1.0.13.jar
Download the jFreeChart jars from: http://sourceforge.net/projects/jfreechart/files/

Step 2: Generating Pie Charts/Bar Graph using jFreeChart

Next we will use jFreeChart library and generate the pie and bar charts.
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
 
public class PieChartDemo {
    public static void main(String[] args) {
        //TODO: Add code to generate PDFs with charts
    }
 
    public static JFreeChart generatePieChart() {
        DefaultPieDataset dataSet = new DefaultPieDataset();
        dataSet.setValue("China", 19.64);
        dataSet.setValue("India", 17.3);
        dataSet.setValue("United States", 4.54);
        dataSet.setValue("Indonesia", 3.4);
        dataSet.setValue("Brazil", 2.83);
        dataSet.setValue("Pakistan", 2.48);
        dataSet.setValue("Bangladesh", 2.38);
 
        JFreeChart chart = ChartFactory.createPieChart(
                "World Population by countries", dataSet, true, true, false);
 
        return chart;
    }
 
    public static JFreeChart generateBarChart() {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.setValue(791, "Population", "1750 AD");
        dataSet.setValue(978, "Population", "1800 AD");
        dataSet.setValue(1262, "Population", "1850 AD");
        dataSet.setValue(1650, "Population", "1900 AD");
        dataSet.setValue(2519, "Population", "1950 AD");
        dataSet.setValue(6070, "Population", "2000 AD");
 
        JFreeChart chart = ChartFactory.createBarChart(
                "World Population growth", "Year", "Population in millions",
                dataSet, PlotOrientation.VERTICAL, false, true, false);
 
        return chart;
    }
}

n above code, we have created two methods that returns JFreeChart object. These methods creates charts using jFreeChart API. Note that we have used dummy data. In real example these values must be dynamically fetched from database or other data source.

Now we can do various stuff with these charts.

Eg. Generating pdf files from these charts.
So change the main method and add following code to above code:

public static void main(String[] args) {
    writeChartToPDF(generateBarChart(), 500, 400, "C://barchart.pdf");
    writeChartToPDF(generatePieChart(), 500, 400, "C://piechart.pdf");
}
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    PdfWriter writer = null;
 
    Document document = new Document();
 
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(
                fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height,
                new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
                height);
 
        chart.draw(graphics2d, rectangle2d);
 
        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);
 
    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
}

In above code we have created a method writeChartToPDF() which take few arguments like jFreeChart object, width, height and filename and write a PDF files in that file.
java-bar-chart-pdf
java-pdf-pie-chart
This is ofcourse very preliminary example of adding pie charts in PDF using Java iText/jFreeChart. But atleast it gives basic idea of how to achieve this. Feel free to modify the above code and play around to generate different charts/graphs in PDF.

You can refer to following book for  more reading on jfreechart:

No comments:

Post a Comment

Chitika