Jump to content
We've recently updated our Privacy Statement, available here ×
  • Creating Charts and Subreports with Jaspersoft Studio


    morlandin
    • Edited on:
    • Features: Charts Version: v8.0.0, v8 Product: Jaspersoft® Studio

    Overview

    JasperReports supports a large number of built-in charts created using the popular open source library JFreeChart. Chart types include Pie, Bar, Stacked Bar, Line, Area, Bubble, Gantt, Thermometer and Meter. In this tutorial we will learn how to add a multi-series chart to a report. This tutorial uses the same example explained in Designing a Report with Jaspersoft Studio. We suggest you complete that tutorial before beginning this one.

    The goal is to have the same report described in the tutorial Designing a Report with Jaspersoft Studio, with a bar chart showing the number of shipments for every city.

    Placing the Graph

    The report created in the earlier tutorial is composed of multiple bands, each with different print times. For example, the detail band is printed for every record read from the data source, the page header and footer are printed at the start and at the end of every page and so on. For this tutorial we will use the Summary band that is printed at the end of the report. Note that in this report the Summary band is not visible from the designer because its height is set to 0. It is, however, visible from the outline view, so select it from there and change its height in the property tab to 400 pixels, as shown in the following image:

    1(11).png.3ecf9fff811b74d76d30307cbe655fc3.png

    The Subreport

    A subreport is an element that includes a report inside another report. The idea in this tutorial is to have a subreport containing just the graph, and a main report that includes the subreport in the summary section.

    For the purposes of this tutorial, we want to use two reports because in the main report we need a list of records without any aggregation so we had used the query "select * from orders" and from all the fields read we had selected the order id, the shipment name, shipment address, shipment city, shipment region. But for the graph in the subreport we don't need a list of records, just a group of them. In other words, we need the records grouped by the shipment city. For this reason we need two queries, one that returns all the records  (for the main report) and one for the group (for the subreport). Because a report can have only one main query, we need to use another report for the additional query. Note that there are other ways to use more than a query on the report, but for now we choose this one to introduce the concept of subreport.

    Now drag the subreport element from the palette into the summary section and a wizard will appear:

    2(8).png.7a09c1f01bdc1d875bfa7ddebd843a04.png

    In the first step of the wizard we have three choices:

    1. Create a new report as subreport.
    2. Use an existing report as subreport.
    3. Put an empty report as subreport, as a placeholder.

    Since we are doing this from scratch select the first option, Create a new report, and click Next.

    3(4).png.d6d922fd0a0a26621396c71e1a6181ee.png

    Now we will seen a wizard almost identical to the one seen when we created the first report. This time, since we need only a blank paper with a graph select BlankA4 and hit Next.

    4(4).png.b03352fa9ef0afbd1cf5c9dee4a1ba26.png

    Now we need to choose the location and the filename of the new report, put it in the standard location MyReports and as name use "GraphReport" and click Next.

    5(3).png.1c5ea63957b4fadea2a21f4198db4bd1.png

    Now we have to select the data adapter. Since we need to use the same data source for both reports, select the same data adapter as used in the main report. We must also provide the query to retrive the fields, so enter the query that returns the records grouped by the shipment city. Every record read from the database with this query contains two fields: the number of elements for a city and the name of the city. After that we can click Next.

    6(3).png.e48430c9247b10f04a8c73d9cf3b0416.png

    At this point we can see the fields included by the second query. Click the add all button or double-click on each field until they are in the right list. After that click Next. The following step in the wizard is to create groups into the report, but this is unnecessary for our goal. Click Next again.

    7(1).png.00291784351b48b42a8185b8d1bc061d.png

    Now we have to select the connection to the database. Since we are using the same database as in the main report we can select the first option and click Next. In the following step in the wizard, we could create some parameters that can be passed from the main report to the subreport, but we don't need them so click the button Finish.

    Now we have a new report with all his bands, but we need only one of them. In fact, we need only a band for the graph, and the band must be printed only once, so the detail band is excluded. We can choose the Title or Summary band. If we decide to choose Title, we need to remove the other bands because otherwise they will take up space unnecessarily. So for each band except Title right-click on its element in the outline view and choose Delete. The name of a deleted band appears in light gray text in the outline view, so at the end of the process the only band with a black name must be Title.

    8(1).png.658baf5752021d94dfced5b7ac428593.png

    The Chart

    Now we can place the chart. Drag its element from the palette into the Title band to open the wizard, select the bar chart and click Next.

    9(1).png.0a64eab004760ce91605c6e9948a590d.png

    In the next step you can configure the series of the chart, as shown in the following image:

    10(1).png.1a62db74b57a7ef78a1663d69220b0bd.png

    Then click Finish to create the chart. Since we need space, resize the Title band with an height of 350 pixels. Right-click on the chart, select Size to Container and then Fill Both. The chart will take all the space in the band. Now we can switch to the Preview tab and if it is all correct, save this subreport and switch to the main report as shown in the following image:

    11.png.e28889d36e7707b6c58e59fc426762c1.png

    Now the only thing to do is to resize the subreport element to fill the Summary band, and compile the report. On the last page you will get a result like this:

    12.png.aeb63f543919b95652415b8307154ff4.png


    More on Complex Reporting

    Subreports in Jaspersoft Studio

    1(11).png.f75819f2c82ffe93f55fe9b1c518fc5a.png

    2(8).png.741a2ffd5a591d545ac7887db2bd3477.png

    3(4).png.3a8cf3ecdc1c122515966b302c176027.png

    4(4).png.62bffed8a47bb197ceccda01ee11d75f.png

    5(3).png.126bcadc44ad187660a1ca85250c0f0c.png

    6(3).png.2a1d644302fc0bc7d2824c2ae8bf7abf.png

    7(1).png.51ab1a9da349ed50ba893ede6d71fd22.png

    8(1).png.be5334ee06fa7473a95382215d108594.png

    9(1).png.d7f13acab6b1121b12de98b35c83691e.png

    10(1).png.3590a96959e5b44802af27a7b75cfc6a.png

    11.png.1c4bec3c1421b22dee71a472503fde59.png

    12.png.922890d16b1665ac92839c4b09ceabb2.png


    User Feedback

    Recommended Comments

    hey if a custom jr datasource class was created and the data is coming from the getDatasource function how would one go about populating the chart with the data?

    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...