jasper training - 01 - getting started 1-30

51
iReports by Jaspersoft A TRAINING BY TECHNOFUNCTIONALS.COM © TECHNOFUNCTIONALS.COM 2015

Upload: alex-johnston

Post on 20-Aug-2015

81 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Jasper Training - 01 - Getting Started 1-30

iReports by Jaspersoft

A TRAINING BY TECHNOFUNCTIONALS.COM

© TECHNOFUNCTIONALS.COM 2015

Page 2: Jasper Training - 01 - Getting Started 1-30

Welcome to Training! This course is designed to give you the skills you need to go

from complete novice to report writing Rock Star! That said, many of the examples in this course are intended to be interactive. Download the required software and follow along to get the best results. Learning a new technical skill can be draining, so if you get stuck, take a little break and try again. If you are still having difficulties, shoot us an email with your problem. Our #1 goal is for you to be successful.

© TECHNOFUNCTIONALS.COM 2015

Page 3: Jasper Training - 01 - Getting Started 1-30

Course Overview

Getting started - Setting up iReports with a database

Building your 1st Report – We will walk through an example together

Building Charts for data visualization

Adding Sub-Reports - A report within a report

Cross Tabs and Maps

Java Formatting

Good Design Tips and Tricks

© TECHNOFUNCTIONALS.COM 2015

Page 4: Jasper Training - 01 - Getting Started 1-30

Lesson Overview What is iReports

? How and why to use it.

Required downloads for getting started

Jasper Windows

Adding Data to your report

Using Basic Parameters

Using Bands and basic Formatting

Preview your report

Adding the “no data” condition

© TECHNOFUNCTIONALS.COM 2015

Page 5: Jasper Training - 01 - Getting Started 1-30

What is iReports?

An open source reporting platform developed by JasperSoft and currently owned by TIBCO and licensed under the LGPL license

It has been continually improved and updated since 2001

It is a Java based tool that can be easily integrated with your own applications to provide built-in reporting functionality

© TECHNOFUNCTIONALS.COM 2015

Page 6: Jasper Training - 01 - Getting Started 1-30

Why iReport ?

JasperReports library is the world's most popular open source java reporting library, and iReport designer is a visual report designer for JasperReports.

The library is a report engine that can be integrated into your applications. Use the library to generate reports designed with iReport designer, display them on the screen or export them in a final format, such as pdf, openoffice, docx and many others.

Alternatively, you can stream the results through a web application or send the final document directly to a printer

© TECHNOFUNCTIONALS.COM 2015

Page 7: Jasper Training - 01 - Getting Started 1-30

Why iReport ?

Beyond the purely practical reason -- that it is an awesome tool -- a typical business analyst in the US with Jasper iReport skills can average $65/ Hour. $$$

© TECHNOFUNCTIONALS.COM 2015

Page 8: Jasper Training - 01 - Getting Started 1-30

How can you use iReports?

iReports can be run from a reporting/BI server or it can be integrated into your application

It can be used for any type of data reporting you may need from inventory, sales tracking, labor hours, etc.

It provides a free and open sourced alternative to products like Crystal Reports and SQL Server Reporting Services

© TECHNOFUNCTIONALS.COM 2015

Page 9: Jasper Training - 01 - Getting Started 1-30

Install the programs below

Download JasperSoft iReports. This tutorial uses version 5.6. Other versions are available, but 5.6 will work best with this tutorial.

Use MySQL Workbench as your data source. Select the MSI package for your operating system

We will be using the “world” database that comes standard with MySQL WorkBench Downloads.

The sample data used in the world database is Copyright Statistics Finland, http://www.stat.fi/worldinfigures.

Note: If you have a different preferred Database Management tool, feel free to use that

http://sourceforge.net/projects/ireport/

http://dev.mysql.com/downloads/workbench/

© TECHNOFUNCTIONALS.COM 2015

Page 10: Jasper Training - 01 - Getting Started 1-30

All done installing? Take a 5 min break

The last thing we want is for you to get frustrated and burn out before we even get started. If you are having problems installing either tool, shoot us an email and we can help direct you to other resources

© TECHNOFUNCTIONALS.COM 2015

Page 11: Jasper Training - 01 - Getting Started 1-30

User Interface

iReport Designer is a powerful environment to design reports.

Reports can be designed from scratch or from one of the many ready-to-use templates that are available.

iReport Designer will assist you during all the phases of the report development: JRXML design

Jasper compilation

Report execution

Document export or visualization

© TECHNOFUNCTIONALS.COM 2015

Page 12: Jasper Training - 01 - Getting Started 1-30

iReports User Interface

© TECHNOFUNCTIONALS.COM 2015

Page 13: Jasper Training - 01 - Getting Started 1-30

The Report Designer is where you visually design the report by dragging, positioning, aligning and resizing report elements.

The Report Inspector shows the complete structure of the report which is composed of many objects: Fields, parameters and variables

Bands (which are sections of the document)

Elements (such as text fields, images or charts)

The Elements Palette contains elements that can be dragged inside a band to display data.

The Property Sheet is used to set the properties of the currently selected component in the report (such as a field, element, band, group, or other).

iReports User Interface

© TECHNOFUNCTIONALS.COM 2015

Page 14: Jasper Training - 01 - Getting Started 1-30

Above the report designer is the data source selector, which shows the active connection used to execute the report (Figure 3).

The report designer toolbar has three buttons to easily switch from design to XML to preview mode.

When you click the preview button, iReport Designer executes the report using the active connection and displays the results using an internal previewer.

iReport Designer can also be configured to automatically export the document and open the proper viewer (such as a browser or a PDF viewer).

Figure 3

iReports User Interface

© TECHNOFUNCTIONALS.COM 2015

Page 15: Jasper Training - 01 - Getting Started 1-30

Now you're ready to see iReport Designer in action by designing

a report.

Have fun!

© TECHNOFUNCTIONALS.COM 2015

Page 16: Jasper Training - 01 - Getting Started 1-30

The report creation process has three main steps: Creating a data source or a database connection to put information on the report.

Designing the report, including the layout of its elements and parameters to represent the data.

Running the report, which includes compiling the JRXML source in a Jasper file and filling in the data to export or display onscreen.

To keep things simple, this tutorial uses a plain JDBC database connection. The data is retrieved using a SQL query embedded in the report.

We will start by creating the connection. While any database and table can be used, you may find it easier to test with the sample JasperReports database, queries and reports used in this tutorial.

The Report Creation Process

© TECHNOFUNCTIONALS.COM 2015

Page 17: Jasper Training - 01 - Getting Started 1-30

Data Sources

iReports can be run from a variety of different data sources including Excel Spreadsheets, CSV files, XML files, SQL queries, HQL(Hibernate Query Language) queries, a collection of JavaBeans, etc...

Database options include Oracle, MySQL, Postgres, and Sybase

If you are adventurous, you can also add your own drivers to modify the base options

© TECHNOFUNCTIONALS.COM 2015

Page 18: Jasper Training - 01 - Getting Started 1-30

Some SQL Resources

For the remainder of these lessons we will be using MySQL as it is also a free and open source product

If you are new to writing SQL or just need a refresher, here are some resources where you can practice http://sqlzoo.net/wiki/Main_Page

http://www.w3schools.com/sql/

Or check out our SQL Basics training!

© TECHNOFUNCTIONALS.COM 2015

Page 19: Jasper Training - 01 - Getting Started 1-30

To create a new connection in iReport, open the datasources/connections dialog by clicking its icon in the tool bar, as shown in Figure (5) below.

Figure 5

Getting Connected

© TECHNOFUNCTIONALS.COM 2015

Page 20: Jasper Training - 01 - Getting Started 1-30

Getting Connected

You will see a list of existing data source options

We want to add a new one so click the “New” button

© TECHNOFUNCTIONALS.COM 2015

Page 21: Jasper Training - 01 - Getting Started 1-30

Getting Connected

Choose Database JDBC connection as shown in Figure 6 from the top of the list

Click Next

© TECHNOFUNCTIONALS.COM 2015

Page 22: Jasper Training - 01 - Getting Started 1-30

Getting Connected

Field Value

Name World

JDBCDriver MySQL (org.gtj.mm.mysql.Driver)

Url: jdbc:mysql://localhost/world

Username: root

Password: root

Populate the next screen with the details below, test and save

© TECHNOFUNCTIONALS.COM 2015

Page 23: Jasper Training - 01 - Getting Started 1-30

Use your preferred database by providing the right URL, the JDBC driver and credentials.

iReport proposes a list of well known database drivers.

If the one you need is not present, just type the name.

If it is present and it is highlighted in red, it means that the driver is not available in the classpath and must be added.

To do so, select Tools > Options > iReport > Classpath and add the required JARs to the classpath.

When done, the new connection appears in the connections list and is automatically selected as the default, active connection.

Getting Connected

Tip: It's good practice to test the connection before saving it.

© TECHNOFUNCTIONALS.COM 2015

Page 24: Jasper Training - 01 - Getting Started 1-30

Now, let's create the new report. Select File > New

Select the blank A4 template

Opening A New Report

© TECHNOFUNCTIONALS.COM 2015

Page 25: Jasper Training - 01 - Getting Started 1-30

Select a name and location to store the new file

Click Next

Click Finish

A blank report appears in the designer

Opening A New Report

© TECHNOFUNCTIONALS.COM 2015

Page 26: Jasper Training - 01 - Getting Started 1-30

Click the button representing a database query in the designer tool bar:

This opens the Query dialog to define the query. This will retrieve data to fill the report.

Viewing Report Data

© TECHNOFUNCTIONALS.COM 2015

Page 27: Jasper Training - 01 - Getting Started 1-30

Viewing Report Data

Add your SQL, then click the “Read Fields” button and your fields will populate in the bottom grid

The fields are listed on the bottom portion of the window. If there is an error in the SQL, you'll get a message that explains what's wrong. When the query is valid, click OK.

If you don't know SQL, you can use the visual SQL query builder by clicking the Query designer button.

© TECHNOFUNCTIONALS.COM 2015

Page 28: Jasper Training - 01 - Getting Started 1-30

Viewing Report Data

The visual query builder lets you drag and drop the tables. Then check the fields you want to include in the report.

This is great for reports with only a few tables and limited joins, but for more complex table relationships, it is always best to use SQL.

© TECHNOFUNCTIONALS.COM 2015

Page 29: Jasper Training - 01 - Getting Started 1-30

Parameters are a way to pass input data from the user or application into a report.

The SQL query is stored inside the report and can accept parameters to filter the data using: syntax $P{} (for standard parameters)

syntax $P!{} (to use a parameter as SQL chunk).

Parameters

$P{Parameter} {}

Report

© TECHNOFUNCTIONALS.COM 2015

Page 30: Jasper Training - 01 - Getting Started 1-30

Parameters To create a new Parameter simply right click on the

word Parameters in the Report Inspector

To modify an existing parameter, select it in the report inspector and use the property sheet.

© TECHNOFUNCTIONALS.COM 2015

Page 31: Jasper Training - 01 - Getting Started 1-30

A parameter can have a default value which is defined in the default value expression property. This expression is evaluated only when a value for the parameter has not been provided by the user at run time.

Parameter Properties

© TECHNOFUNCTIONALS.COM 2015

Page 32: Jasper Training - 01 - Getting Started 1-30

Parameters will prompt you for a value when you run the report preview. This can be turned off in the same window as the default value.

You have the ability to use the default value defined earlier by clicking the button on the lower right

Parameter Properties

© TECHNOFUNCTIONALS.COM 2015

Page 33: Jasper Training - 01 - Getting Started 1-30

All reports contain a set of predefined parameters that are available by default and contain some run time information.

Examples include: REPORT_CONNECTION - Holds the JDBC connection

used to run the SQL query of the report

REPORT_DATA_SOURCE - Contains the data source used to fill the report

REPORT_LOCALE - Contains the Locale used to fill the report.

The predefined parameters cannot be altered or deleted.

To hide the predefined parameters from the report inspector, click the filter icon at the bottom of the report inspector.

Predefined Parameters

Filter Icon

© TECHNOFUNCTIONALS.COM 2015

Page 34: Jasper Training - 01 - Getting Started 1-30

Report bands (such as Title, Page Header, Column Header, Detail1) provide a way to organize what the report is displaying

You can view what is contained in a particular band by clicking the plus sign in the report inspector

Report Bands

Page 35: Jasper Training - 01 - Getting Started 1-30

Like fields, parameters and the report itself, Bands have properties that can be used to change the appearance of the report. Depending on the type of band, your options may include the band height, when to print, and if the band repeats after the 1st page.

We can resize some bands and remove others by setting their height to 0.

Change the height of band by dragging its bottom edge. A shortcut to reduce the height of the band is to double-click on its bottom edge. This will set it to the bottom edge of its lowest element.

Report Bands Settings

© TECHNOFUNCTIONALS.COM 2015

Page 36: Jasper Training - 01 - Getting Started 1-30

Adding Fields

Fields can be dragged and dropped onto the designer directly from the Report Inspector

When the field object is dragged inside the detail band, iReport creates a textfield element and sets the textfield expression for that element e.g $F{Language}

This is a simple expression to print the value of the field LANGUAGE (the syntax $F{})

Also note that a column header of the same name is created automatically © TECHNOFUNCTIONALS.COM 2015

Page 37: Jasper Training - 01 - Getting Started 1-30

You can format the textfields element by stretching, dragging and aligning them.

It is important that the textfields are all placed within the detail band.

To add other elements (such lines or labels), drag them from the palette shown in the Figure to the right into the designer view. Then resize and arrange them as desired.

Adding Fields and Elements

© TECHNOFUNCTIONALS.COM 2015

Page 38: Jasper Training - 01 - Getting Started 1-30

For this example: Add a label (Static Text) in the title band for the report title

Add column labels using label elements placed in the column header band

Place a thin line just under the textfields in the detail band.

You can also set properties such as the height of the band in the property sheet under the palette.

The property sheet lets you view and edit the properties of the currently selected element in the designer.

Click on an element in the designer or in the report inspector to select it and view its properties.

For bands, you can also click in an unused part of the band. Figure in the next slide shows the final design of the sample report.

Adding Fields

© TECHNOFUNCTIONALS.COM 2015

Page 39: Jasper Training - 01 - Getting Started 1-30

To preview the report, switch to preview mode by clicking Preview in the designer tool bar.

The preview compiles the report in the background and fills it with data retrieved by the query through the JDBC connection.

As shown in this Figure , the detail band repeats for every row in the query results, creating a simple table report.

Preview your Report

© TECHNOFUNCTIONALS.COM 2015

Page 40: Jasper Training - 01 - Getting Started 1-30

Congratulations!

You have just created your first

report!

© TECHNOFUNCTIONALS.COM 2015

Page 41: Jasper Training - 01 - Getting Started 1-30

When you click the preview button in the designer tool bar, iReport performs a set of operations to create the final report. The first operation is to compile the JRXML source file in a Jasper file. Error Occurrence : This first step can fail if the elements are not correctly

positioned (for example, if an element is placed outside of a band), or if an expression in the report has errors and cannot be compiled.

Solution : Make sure you move all fields to be completely inside a specified band

If Previewing Your Report Fails

© TECHNOFUNCTIONALS.COM 2015

Page 42: Jasper Training - 01 - Getting Started 1-30

If the compilation runs successfully, the produced Jasper file is loaded and filled using the active connection or data source. Error Occurrence : This second

operation can again lead to errors, for example if the referenced database is not active, an invalid query has been provided, or a null field produced an error in an expression during the filling process.

Solution : Test your connection and make sure your SQL runs in your database management tool

If Previewing Your Report Fails

© TECHNOFUNCTIONALS.COM 2015

Page 43: Jasper Training - 01 - Getting Started 1-30

Errors are shown in the Report Problems Window. Operation results are shown in the iReport output, both of which share the bottom of the iReport screen.

Previewing Your Report: Error Handling

© TECHNOFUNCTIONALS.COM 2015

Page 44: Jasper Training - 01 - Getting Started 1-30

Reports can be exported in several formats by clicking Save in the preview tool bar or by setting a proper preview format in the Preview menu.

Previewing Your Report: Formats

© TECHNOFUNCTIONALS.COM 2015

Page 45: Jasper Training - 01 - Getting Started 1-30

To export a report, click Run again in the preview tool bar. Running a report again is useful when A subreport changes

A datasource changes

When you want to execute the report with different input parameters

Previewing Your Report: Reruns

© TECHNOFUNCTIONALS.COM 2015

Page 46: Jasper Training - 01 - Getting Started 1-30

When you set a preview format, the report is automatically exported to the chosen format when you click Preview, and the corresponding viewer application is opened

iReport is able to identify the proper viewer for each format automatically based on the system defaults. However, it is possible to manually set the viewer application for each format by selecting

Tools > Options > iReport > Viewers:

Previewing Your Report: Default Programs

© TECHNOFUNCTIONALS.COM 2015

Page 47: Jasper Training - 01 - Getting Started 1-30

This is the default response if the final report does not contain any pages. iReport displays the following message instead of the document:

This is not an error. iReport is stating that the report is empty. This can happen under several circumstances; usually when the SQL query or the data source has not provided any data. If you see this message, check your database connection and test the query in your report.

What if there is no data?

© TECHNOFUNCTIONALS.COM 2015

Page 48: Jasper Training - 01 - Getting Started 1-30

What if there is no data? If you do not like the popup, you can

customize how the report responds. From the designer, click on the report and display the report properties. To run empty reports: Display a formatted report, but with

no data

Display a blank page

Display the contents of the “When No Data”. This is a band just like “Details” where you can add text or parameters

© TECHNOFUNCTIONALS.COM 2015

Page 49: Jasper Training - 01 - Getting Started 1-30

Jasper reports is extremely easy to integrate in an java application, but if you need an environment to use the reports without having to write a custom application, you may consider using jasper reports server.

Jasper server provides a web based interface to manage, schedule, and run the reports; a repository to store all the report resources like images, fonts, data sources and much more.

It is a security service to decide who can execute which report; and a web services api to execute the reports from external applications (so you can generate reports from any kind of environment, like php or .Net).

In the big picture, iReport designer allows you to design reports, jasper reports allows you to execute them and generate output in a java application, and jasper server allows both end users and external applications to access, view, and publish your reports securely.

Jasper server also makes reports interactive by adding drill down and drill up capabilities to your documents.

Technical Notes: Jasper Reports Server

© TECHNOFUNCTIONALS.COM 2015

Page 50: Jasper Training - 01 - Getting Started 1-30

Report Life Cycle

When you design a report using iReport Designer you are creating a JRXML file, which is an XML document that contains the definition of the report layout.

The layout is completely designed in a visual way. You can ignore the real structure of the JRXML file. (WARNING! Making changes directly to your JRXML can corrupt your report!)

Before executing a report, the JRXML must be compiled in a binary object called a Jasper file.

This compilation is done for performance reasons. Jasper files are what you need to ship with your application in order to run the reports.

Figure 1

Design

Execution Export

Compile Fill

© TECHNOFUNCTIONALS.COM 2015

Page 51: Jasper Training - 01 - Getting Started 1-30

Congratulations!

You have just run your first report!

© TECHNOFUNCTIONALS.COM 2015