A Tutorial On Reporting in JAVA Using Jasper Reports, Ireport and JFreeChart
A Tutorial On Reporting in JAVA Using Jasper Reports, Ireport and JFreeChart
Prepared By:
Onur Derin
JAVA Developer
A Tutorial on Reporting in JAVA using
JasperReports, iReport and JFreeChart
This tutorial aims to get the reader acquianted with the three of the open source JAVA
reporting tools, namely JasperReports[1], iReport[2] and JFreeChart[3].
Parsera Information
Technologies
[email protected]
www.parsera.com
Information Technologies
A. What is a Report?
A report is a nicely formatted way of presenting the data that you have entered. Reports are all about querying a database and displaying the results
in a nice format. Data in the database may be displayed just as is, that is, in hundreds of pages. However, most of the time, reports are used to grasp
information out of a bunch of data. This is mainly done by charts which are graphical representations of information. When the data is processed to
fill out a report, calculations are done over the specific fields of the entries. The results appear as bars or pies in a chart or as text at the end of the
report.
Many of the commercially available reporting tools have the following layout for a report page.
title appears only once at the very beginning of the report. Title of the report is written in this part. eg. “Employee Performance Report”
pageHeader appears at the top of each page. This part may contain date and time information and/or organization name.
columnHeader lists names of those specific fields which you want to display. eg. “Employee Name”, “Starting Hour”, “Finishing Hour”, “Hours
Worked”, “Date”
detail is the part where those fields of the entries are shown. eg. “John Doe”, “09:00”, “18:00”, “9”, “16.07.2004”
columnFooter may display summation of any of the fields. eg. “Total Hours Worked: 180”
pageFooter appears at the bottom of each page. This part may contain page count information like “1/7”.
summary is the part where information inferred from the data in the “detail” part is displayed. For example, after listing the worked
hours for each employee in “detail” part, total hours worked for each employee can be put in a pie to enable a better visual
comparison between the employees.
JasperReports
JasperReports is an engine that takes an XML file and forms a report out of that file using the data source specified in the XML file. A
sample XML file is given in Appendix A. This file defines exactly what appears where in the report. Writing this file by hand is not
practical. This is where iReport comes into play.
iReport
iReport is a visual tool to obtain XML files for JasperReports. It provides a WYSIWYG environment to design reports. Sample.jrxml in
Appendix A is obtained using iReport.
Anything that can be placed in a report (static text, geometric shapes, images, subreports, groups, texts and images coming from a
data source)can be put together in drag’n’drop fashion according to the report template shown in Figure 1.
JFreeChart
JFreeChart is a free Java class library for generating charts. Several chart types are supported such as pie charts (2D and 3D), bar
charts (regular and stacked, with an optional 3D effect), line and area charts, scatter plots and bubble charts, time series,
high/low/open/close charts and candle stick charts, combination charts, Pareto charts, Gantt charts, wind plots, meter charts and
symbol charts, wafer map charts. This library is used to create the images of the charts which we want to embed into the report.
We want to show the same data in the table, however in a different format, for example, all entries for each employee grouped
together. We also want to display the total hours worked for each employee. And finally, to give a comparison among the employees,
we want to display these information in a chart. Figure 3 shows what we want to obtain.
Specify title to be “Employee Work Hours Report”. Using geometric shapes, the appearance of the report can be enhanced. However,
for the time being, we won’t bother with the aesthetics.
To add a group to the report, press the “Groups” icon. On the opened dialog, press New. Specify group name as “employee” and
group expression as “$F{EmployeeName}”, press OK with other fields in default values. You will see that two new bands are added
as “employeeHeader” and “employeeFooter”.
According to the instructions given above, obtain the design in Figure 4. Specify the Report Query through Edit-Report query as
“SELECT * FROM tutorial_table ORDER BY EmployeeName”. “Order by” is necessary because we created “employee” group with
group expression “$F{EmployeeName}”
Notice that you can refer to the final XML file in Appendix A whenever you have a question about forming the above design. Finally
save your design and compile it, you will have a jrxml file.
One advantage of JasperReports is that it can export the final print to an HTML file which makes dynamic reporting possible in web
pages. The last line in Figure 5 exports the report to an HTML file.
Pie Chart
The following code segment taken from sample.java given in Appendix B demonstrates the process of creating a java.awt.Image
object from scratch.
// create a dataset
DefaultPieDataset data = new DefaultPieDataset();
// create and return the image with the size specified in the XML design
return chart.createBufferedImage(500, 220);
Bar Chart
// create a dataset
DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
String s = "Employee";
// create and return the image with the size specified in the XML design
return chart.createBufferedImage(500, 220);
Hints
• In order not to experience problems when displaying texts in the report, select the encoding of the XML file as UTF-8 and
write local characters in their UTF-8 forms.
• On a Windows system, after the installation of iReport, if iReport fails to create .iReport/config.xml under your home directory,
create the directory named .iReport under your home directory in a command prompt yourself by “mkdir .iReport”.
Referrences
• http://jasperreports.sourceforge.net
• http://ireport.sourceforge.net
• http://www.jfree.org/jfreechart/
Useful Links
• http://jasperreports.sourceforge.net/tutorial/index.html
• http://ireport.sourceforge.net/docs.html
Appendix A
Sample JasperReport XML File – sample.jrxml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
name="EmployeeReport"
columnCount="1"
printOrder="Vertical"
orientation="Portrait"
pageWidth="595"
pageHeight="842"
Appendix B
Sample JasperReports JAVA File - sample.java
/*
* Sample.java
*
* Created on July 19, 2004, 12:49 PM
*/
package com.medyanet.kumas.raporlama.dokuma.performans;
import org.jfree.data.DefaultPieDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFrame;
import org.jfree.data.XYDataset;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.CategoryDataset;
import org.jfree.data.DefaultCategoryDataset;
import dori.jasper.engine.design.JasperDesign;
import dori.jasper.engine.JasperManager;
import dori.jasper.engine.JasperReport;
import dori.jasper.engine.JasperPrint;
import dori.jasper.engine.JasperPrintManager;
import dori.jasper.view.JasperViewer;
import java.util.HashMap;
import java.util.Map;
import java.sql.*;
/**
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
// First, load JasperDesign from XML and compile it into JasperReport
JasperDesign jasperDesign = JasperManager.loadXmlDesign("path-to-your-jrxml-file\\sample.jrxml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();