Jump to content
Changes to the Jaspersoft community edition download ×
  • Designing a Report with Jaspersoft Studio


    morlandin
    • Edited on:
    • Features: Reports Version: v8.2.0, v8.1.0, v8.0.0 Product: Jaspersoft® Studio

    Overview

    In this tutorial, we cover the basics of how to design a report. The report creation process has three main steps:

    1. Creating a data source or a database connection used to fill the report.
    2. Designing the report, including the layout of its elements and parameters to represent the data.
    3. Runing the report, which includes compiling the JRXML source file in a Jasper file and filling in the data for export or onscreen display.

    To keep things simple, this tutorial uses a sample database included in the installation of Jaspersoft Studio. The data to print is retrieved using an SQL query embedded in the report.

    Creation of the New Report

    The first thing to do is create a new report. To do this, open the File menu, select New, and then click Jasper Report. A new window appears, as shown in the following figure:

    1(9).png.a4511f8d1c40084add69b4a874fe41fb.png

    Here you can select a template. A template defines the initial graphical layout of a report. From this dialog, in just a few clicks, you can create a very simple blank layout, or a much more complex one, with image, text, number of pages and so on. As an example, let's select a template with some elements in it. From the window displayed in the figure above, select Coffee and click Next.

    In the next step, select the folder in the workspace where you want to put the report, and name the new report. You can use the default folder MyReports, name the report use Example1, as shown in the following figure,  then click Next. If you want to create a new project folder for the report, go to the chapter Create a Project Folder, at the end of this tutorial.

    2(6).png.08126849137dd4e602f28023c31234d8.png

    Now for the most complex part of this tutorial: the selection of a data source. The data source is the location from which the data is taken to fill the report. In this step, you must select a data adapter, which is essentially a connection to a data source. Here you have three options:

    1. Selecting the data adapter "One Empty Record - Empty rows": This is default data adapter used to create a report without any data. It can be used to define the layout of the report before connecting it to a data source. In fact, a data source can be provided at a later time, after the creation of the report.
    2. Selecting the data adapter "Sample DB - Database JBDC Connection": This is a an SQL database used as an example, provided with the Jaspersoft Studio installation, for use in this tutorial.
    3. Creating a new data adapter with the "New" button: Jaspersoft Studio supports a wide variety of data sources. Using the "New" button allows you to browse and connect to an existing data source. Since this is a complex functionality, we'll address it in another tutorial.

    For this example, we will use the Sample DB, but this is not sufficent to create the report. A SQL database can contain many tables, with many fields, so we must specify which of these fields we will use in the report. To do this we must provide an SQL query that selects all the fields we want include in the report. To keep this tutorial simple, we will select all the fields from the table named "orders". Write the query as shown in the image below and click Next.

    3(3).png.f8487d75893c4ee73c53bedf3171be6a.png

    Using this query ensures the database will be explored and all the fields found can be added to the report.

    In the next step of the wizard there are two lists. On the left there are all the discovered fields, and on the right the ones that will be added to the report. At first the right list is empty, but we can move one or more elements from the left list to the right one using the buttons between the two lists or by double clicking on the field. An example is shown in the figure below:

    4(3).png.2efaa3b7c84b6382c315a1fd026ffba2.png

    After selecting the fields click Next to proceed with the wizard.

    In the next window you can group fields. Grouping is a function that, for now, we will not use, so click Next. The wizard is now complete. The only thing to do is click Finish. Jaspersoft Studio now builds the report layout with the selected fields already included, as shown in the following figure:

    5(2).png.6c19ba4497f4ba29b51cf9c1e8c0d685.png

    Here, we can delete a field  on the report by right-clicking on it and selecting delete, or adding a field from the Outline view (the tab on the bottom-left of Jaspersoft Studio). To include the value of a field in the report, drag it from the outline view to the design view. For example, drag a field into the detail band, a section of the report that is printed for each record/row in the query results. The title and the summary bands are printed just once in each report, while the page header and footer are printed once at the top and bottom of each page, respectively. In the figure below, see the  example where the field SHIPREGION was removed from the report and in its place the file SHIPCOUNTRY is added.

    6(2).png.3e2521bf616d7f0430717b21869ec878.png

    When the field object is dragged inside the detail band, Jaspersoft Studio creates a text field element and sets the text field expression for that element to $F{SHIPNAME}. This is a simple expression to print the value of the field with the name SHIPNAME (the syntax $F{}). Repeat the same operation for other fields and format the text fields element by stretching, dragging and aligning them. It is important that the text fields are all placed inside the detail band.

    To add other elements (such lines or labels), drag them from the palette shown in the following figure into the designer view, then resize and arrange them as desired:

    7.png.b39114d6a348b51c3110ac719ce12421.png

    In this case, we add a label in the title band for the report title, we add column labels using label elements placed in the column header band, and we place a thin line just under the text fields in the detail band. 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, which aligns it with the bottom edge of its lowest element. 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.

    To preview the report, switch to the preview mode by clicking the Preview tab at the bottom of the designer window. Then click the green arrow. The preview compiles the report in the background and fills it with data retrieved by the query through the JDBC connection. As shown in the following figure, the detail band repeats for every row in the query results, creating a simple table report:

    8.png.4beb9f77c1cbec0c33ca6a6dc9c8b7bc.png

    Add more Fields to the Report

    You may discover that you need to add more fields from the data source after the report was created. You can add fields to an existing report without creating an entirely new report.

    To add fields to an already created report, select the main node of the report from the outline view (in this example is named "coffee"). From the properties tab press the button Edit query, filter and sort option, as shown in the following figure:

    9.png.73ca9071cd470973edcbc071e7abed6e.png

    In the dialog that appears, you can specify a new query and by clicking the button Read Fields and then "OK". All the fields discovered are added as new fields in the report. Note that if the new query discovers fewer fields than the ones used in the existing report, the fields that are not included the new query will be removed from the report. An example can be seen in the following figure:

    10.png.73450f55a7bc82192c9e9e1b9577e0f4.png

    As you can see, we selected only the fields ORDERID and CUSTOMERID, so any field previously added to the report that is not included in these two will be removed. This dialog is very powerful - from here, you can change the data source, add, remove and reorder the fields discovered by the new query.

    Create a Project Folder

    From JasperSoft Studio, open the menu File, select New and then Others.... 

    11(3).png.5161de0bbeec640667300db26d81dbb2.png

    In the dialog that will appear type "Jasper" in the Wizards bar, this will filter all the actions showing only the ones related to JasperSoft Studio, and from this filtered list select "JasperReports Project". Then hit Next.

    12(5).png.4338b9f48a57126d2d287ff14b041fa9.png

    At this point you need only to insert the name of the folder and hit Finish.

    13(2).png.a4e12626f69de4cdf8250027c109d7e6.png


    See Also:

    1(9).png.c43076a8f406367bee640f43f4418eb1.png

    2(6).png.69a22937e7a5ba34184da053a2e21262.png

    3(3).png.1ec2a50e47f169950875f4ec692e037b.png

    4(3).png.91a18c8be1c6c0ed05ee029752b91647.png

    5(2).png.ad28fda950dc15f5a9f89cef886a8a69.png

    6(2).png.93cd40651823034663880873d5a1e199.png

    7.png.450527431b5e060b5f094266676f1178.png

    8.png.73f30ad759c2ed6778b57878095b3945.png

    9.png.495881b40a769b19134a4d4b859b6d52.png

    10.png.04294c548cf125ddb66fc1034bf0fab6.png

    11(3).png.b899f6052e33418f0f4595c324c2821f.png

    12(5).png.5ff9037d1ac533ca915526110104e62c.png

    13(2).png.ab28bc68be8d58c80a0afe6a38823878.png

    • Like 1

    User Feedback

    Recommended Comments

    In the wizard, the screen shots show 5 fields selected and the text doesn't indicate to select all fields.  Yet, two screen shots later, it shows more fields selected, without explaining how to get SHIPCOUNTRY in the Fields outline if someone followed the screen shots as examples and didn't select it in the wizard.  The simplest way to resolve this is to add a comment: "Use >> to move all the fields to the right side of the screen.  After selecting the fields click Next to proceed with the wizard."

    Link to comment
    Share on other sites



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...