AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1229&pId=-1
Creating and Designing Report Using iReport - Part 1
page
by Anuva Das
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 90116/ 88

Introduction

When creating a report using JasperReports, one of the most painful and error prone tasks is creating JRXML or XML files. Luckily, some alternatives are there. One of the most useful visual report builders is iReport. It help JasperReports users to build reports, generate jasper files and preview reports in PDF, HTML, XLS, and many other formats supported by JasperReports. It also allows users to create more complex reports with charts, images, and subreports, etc.

This article demonstrate how to use iReport to utilize the full power of JasperReports without getting a headache in designing JasperReports using XML template.

System requirements

Minimum Requirements to get started with iReport:

·         Sun JDK 1.4 or later version

·         jasperreports-{version}.jar

·         commons-beanutils-{version}.jar

·         commons-collections-{version}.jar

·         commons-digester-{version}.jar

·         commons-logging-{version}.jar

·         xercesImpl.jar-for parsing XML

·         itext-{version}.jar-For creating PDF formatted report

·         If you want to access the database, you must have a JDBC driver. 

You will find these JAR files inside the lib directory of iReport.

Installation and Configuration

·         Install JDK 1.4 or higher version

·         Download iReport. Unzip it where you want.

·         Copy tools.jar from your JDK and place it inside the lib directory of iReport.

·         Copy classes12.jar from JDBC Driver and place it inside the lib directory of iReport.

·         Before creating the report, all the required JAR files must be added to the CLASSPATH variable.

Starting iReport

You can start iReport by simply running iReport.bat under Microsoft Windows or iReport.sh under Unix operating systems .When you execute iReport for the first time, it will create a directory(.ireport) in your home directory. This directory includes all the configuration files as   XML files.

Set Compilation Directory

In this tutorial iReport, the default compilation directory is “C:\iReportExample.” To change compilation directory select "Options -> Settings..." from menu and click the "Compiler" tab and select compilation directory name by clicking on the “Browser” button. Or to store compiled jasper file in the same directory where JRXML/XML files are stored, make sure that “Use the reports directory for compiles” checkbox is checked.

Figure 1

Set Report Format Viewer

To view report using iReport select "Options -> Settings..." from menu and click on the "External programs" tab and select different format.

Figure 2

Adding New Database Connection

To retrieve data from database, you have to create a new database connection. Use the following steps for this:

·         Select “Data -> Connections/Datasources” from the menu to open the “Connection/Datasources” dialog (see Figure 3).

Figure 3

·         Click on the “New” button to open “Connections Properties” dialog.

·         Give a suitable connection name.

·         Choose appropriate the type of Connection/Datasources.

·         Choose the appropriate JDBC driver.

·         Enter JDBC URL or enter the server address and the database name, and then click on the "Wizard" button. 

·         Enter the User name and Password.

Figure 4

·         To check the database connection click on the “Test” button. It shows the following figure if the database connection is OK.

Figure 5

·         Now click on the “OK” and then “Save” buttons.        

Designing Report

To design JasperReports report, select “File -> New Document” from the menu. Enter the report name in the “Report name” field and click on the “OK” button to open a blank report design.

Adding Static Text

To add  Static text to the report, click on the "T" icon at the top of the screen or select “Edit ->Insert Element -> Static Text” from menu and  place it where you want to display the text.

Figure 6

Adding Report Query

To retrieve data from the datasources select “Data-> Report query” from the menu to open the Report query dialog. Make sure that “Automatically Retrieve Fields” checkbox is checked. Now write your SQL query in the textbox. Click the “OK” button to close the dialog.

Figure 7

Adding Text Fields

Report fields are used to display data retrieved from the datasources into the report. All the fields must have the same name and type as the columns they map .To add fields to the report click on the "F" icon at the top of the screen or select “Edit ->Insert Element-> Text Field” from the menu and place it where you want to display the field. Or simply drag and drop them from Fields onto the section of the report where you want.

Figure 8

 

Adding Images

To add an image to the report, select "Edit->Insert Element->Image" from the menu. Go to the properties of image field and click the “Image” tab. Give the path for the image by clicking on the “Find” button.

Figure 9

Adding Parameters

Parameters are used to dynamically modify SQL Query. To add a parameter to the report select “View -> Parameters” from the menu, then Click on the “New” tab. In the “Add/modify parameter” dialog enter/select the following values and click the “OK” button afterwards.

·         Enter the Parameter Name (e.g. City).

·         Select theParameter class type (e.g. java.lang.String).

·         Make “Is for prompting” checkbox checked so that when you run the report from iReport it will prompt you to enter the value for that parameter.

·         Enter the Default value expression (e.g. “Bombay”).

Figure 10

Adding variables

A report variable is used to perform various calculations on the corresponding expressions. Here are some important things that you should follow when designing the report using variables.

·         The variable class type must be matched with class type of the field on which it will perform calculation.

·         The calculation type tells JasperReports what to do.

·         The variable expression represents the value to be evaluated in the calculation. It may be Fields, Variables, Parameters, wizards, recent expressions, etc.

·         As a variable can reference other variables, so the referenced variables must be defined previously in the report design. So the order of declaring variables is important in report design.

To add the report variables, select "View->Variables" from menu and click on the “New” button. In the “Add/modify variable” dialog enter/select the following and click the “OK” button afterwards (see figure 11).

·         Enter the Variable Name (total).

·         Select the Variable Class Type (java.lang.BigDecimal).

·         Select the Calculation Type (Sum).

·         Select the Variable Expression ($F{QTY_SALES).

Figure 11

Now drag and drop this variable into the columnFooter, lastPageFooter or Summary section to display result.

Adding Groups

Groups in a report define a collection of related records. A report can contain more than one group and a group can contain another group, so the order of declaring a group in a report design is important.

Suppose you want to create a report that will group salesman by state and calculate total number of item sold by each group. For that, first define a report group by selecting "View/Report Groups" menu and then click the “New” button. In the “Add/modify group” dialog Enter Group name and Group Expression, then  click the “OK” button.

Figure 12

To create a report using the group you must write the SQL query properly ("order by state"). When you add a new group, you will see two new sections at the top and bottom of detail section. In this example it is called "stateNameHeader" and "stateNameFooter." Suppose you want to display the total number of items sold at the end of each group. For this create a variable ($V{totalQty}) and make the “Reset type” as a group, then drag the variable into the "stateNameFooter" section. The generated report looks like the following figure.

Figure 13

 

 

Adding Charts

Using iReport you can design reports with charts. JasperReports currently supports the following types of charts: Pie, Pie 3D, Bar, Bar 3D, XY Bar, Stacked Bar, Stacked Bar 3D, Line, XY Line, Area, YX Area, Scatter, Bubble, Time series, High Low, Candlestick, meter, thermometer, and MultiAxis. Chart parameters are different for each type of chart.

Suppose you want to add a pie chart that shows the total number of items sold by each salesperson at the end. For that, first add a new chart in the summary section using the "Edit->Insert Element->Chart" menu. Choose a chart. Open the properties by right clicking on the chart then go to the “Chart” tab.  Click on the “Edit Chart Properties” button and go to the “Chart data  -> Details.” In the Chart properties dialog enter the three expression as shown in the following figure and click the “Close” button afterwards.

Figure 14

Now the user has to make the desired visual settings for it. When you run the report, you will get a pie chart at the end of your report as shown in the following figure.

Figure 15

Creating report from iReport

After creating JRXML or XML file we can create reports in different format from iReport as well from JAVA. In my previous article entitled “Creating Report Using JasperReports,” I have explained how to create a report from Java. Now to create a report from iReport, compile the JRXML or XML file. To compile the report, click on the “Build -> Compile” menu; it will create a Jasper file. Now select any format from the “Build” menu. Finally, click the “Build -> Execute" (with active connection) from the menu to create a report.

Figure 16

Conclusion

So we can say iReport makes the JasperReports designing task faster. This part of the tutorial covered a few features of iReport and how to create report using iReport. In the next part I will discuss the advanced features (such as subreport,crosstab) of iReport.

By Anuva Das

 

 

 

 


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 10:19:00 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search