Hossam has posted 5 posts at DZone. View Full User Profile

Java Reporting – Part 4

06.26.2008
| 31879 views |
  • submit to reddit

Today's article is the last one in the Java Reporting series. Through parts 1, 2, and 3 we have discovered JasperReports architecture, declarative file construction, integration with live parameters, and finally working with iReport as designing tool. Those topics are only the basic introduction to java reporting world and this series is not meant to cover the whole features of JasperReports, you can consider it as the kick start in the reporting field where every new report is supposed to contain new cases that formalize your experience.

There was a time when charts' reports were considered as one of the imaginary features that clients have dreamt with for a long nights, but they also were suffering from usual nightmares when dream reach to the payment point, that is a result of a quote says "When development is hard; the customer should pay for that too". Thanks to open-source community that age went and will not come back again, today as a result of cumulative work in open charts' libraries and engines we became able to build a graphical chart reports in a smooth and easy way without the usual headache of plotting and rendering the results, which is also –unfortunately- rescued the customer financial plans from the definite fate.

In today's article we are going to create a Line chart report as sample application of chart reports with JasperReport and iReport. Actually there are a lot of graphs you can work with through jasperReports, e.g. Pie, 3D Pie, Bar, 3D Bar, XY Bar, XY Line, XY Area, Time Series, Multi-axis, Meter, etc. I chose Line chart report for our sample as it is a good example to cover 3-variables graphs.

Dependencies


As we mentioned before, most of reporting tools depend mainly on the chart engines and frameworks for plotting and rendering the resulted graphs, our tool -JasperReports- depends on a very popular, powerful and flexible open source charting library called JFreeChart.
So to get ready you need to download JFreeChart Library from here: http://www.jfree.org/jfreechart/download.html .
Extract the compressed file and add these libraries to your project classpath:
jfreechart-1.0.xx.jar (jfreechart-1.0.10.jar)
jcommon-1.0.xx.jar (jcommon-1.0.13.jar)

Report Definition

 Database architecture:

-- ITEM
ITEM_ID NUMBER(5) NOT NULL
CATEOGRY_ID NUMBER(5) NOT NULL
ITEM_NAME VARCHAR2(50) NOT NULL
ITEM_DESCIPTION VARCHAR2(200)
ITEM_AMOUNT NUMBER(5) NOT NULL

--STOCK_OPERATIONS
ITEM_ID NUMBER(5) NOT NULL
OPERATION_TYPE NUMBER(1) NOT NULL
OPERATION_DATE DATE NOT NULL
AMOUNT NUMBER(10) NOT NULL

 

Report Result

Report should draw a graph line -for each item- that represents the total amount (on the y-axis) of in-operations divided by the days (on the x-axis).
 

Report Query:

[SELECT item.ITEM_NAME, to_date(OPERATION_DATE,'dd-mm-yyyy') operationdate, SUM (stock.AMOUNT) amount
FROM STOCK_OPERATIONS stock, item
WHERE stock.ITEM_ID = item.ITEM_ID and stock.OPERATION_TYPE =1
GROUP BY item.ITEM_NAME, to_date(OPERATION_DATE,'dd-mm-yyyy')
ORDER BY operationdate , item.ITEM_DATE ]

The above query will retrieve the graph result for all items in the stock; we can narrow it down by adding date period condition or specify item id in run-time search criteria as declared previously in part 3.

Step-by-Step

1- Open iReport.
2- From the toolbox choose ChartTool and draw your graph control in on of the bands that don't get repeated in the same page (background, pageHeader, ColumnHeader).
3- From the pop-up window choose your chart type (Line Chart) as shown in the image below.

4- Add the report query from Data -> Report query
5- Open Chart Properties (Right-click your chart and choose Chart Properties) and open Chart Data tab, then open the inside tab called Details and press the Add button, as shown:
6- The Expressions in the shown dialog are:
-Series Expression: represents the graph lines.
-Category Expression: represents X-axis values.
-Value Expression: represents Y-axis values.

7- Axis labels, lines colors, delimiters shapes, legends and other graph properties can be handled from the Chart properties dialog.

Now execute the report in JRViewer and watch the results:


That's all, you'r report is ready to get linked with application and exported to whatever format you want without any modification in your code –except the jrxml file name-.

Here we reach to the end of this series, it didn't cover everything in reporting field but I wish it could help as introduction for beginners and for those need kick start.

For detailed topics about JasperReports please refer to these books:
The Definitive Guide to JasperReports™ by Teodor Danciu and Lucian Chirita
JasperReports for Java Developers by David R. Heffelfinger

Note: this Article was first published in FCI-H Blog, here

Published at DZone with permission of its author, Hossam Sadik.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dino VV replied on Sat, 2008/06/28 - 10:11am

I've read the jasper articles and they give a decent guide to create basic reports.

Now, we have a requirement in our project that requires multiple tables with different data sets (about 15 different data sets) for a single report. How do you do this in jasper?

I see this question pop up in the forums and the standard answer is to use subreports which I think is a silly limitation. Does this mean I have to create 15 separate sub reports so it can be used in a master report? I want only one output file.

It is real easy to do this with Birt from eclipse. I'm hoping that I'm missing a key feature from jasper and need advice from an expert.

Hossam Sadik replied on Sun, 2008/06/29 - 4:16am

@Dino 

That's right, JasperReport's support for multiple datasets can only be done through sub-reborts -till now-, which is considered as limitations in such a large number of combinations, and it's true that Birt get over this problem by supporting data source mixing -although it is still tedious to maintain extra datasource-.

There isn't a perfect tool, each one has it's pros and cons. For example Birt has also limitaions lin WYSIWYG functionality, exporting formats, connection pool and HQL support.

Working with open source tools and engines makes you always in comparing situation as you can't make advantage of all tools together. You have to define your needs, evaluate the available tools and pick up the most suitable one.

Dino VV replied on Sun, 2008/06/29 - 12:32pm in response to: Hossam Sadik

Thanks Hossam,

You are right about the evaluation. But that limitation (requiring subreports) is so huge for us that we are either forced to write something ourselves or go to Birt and be content with their limitations.

I just can't believe that jasper doesn't support this. It just seems such a basic requirement. I just thought I missed something here.

Anyhow, I appreciate the response.

 

 

Alexis Lopez replied on Thu, 2008/10/09 - 11:39am

Hello. Excellent article. How ever I got a question. I'm making a report that use a JRDatasource to get the data for the Line chart. I dont know why, but the chart doesn't display any line...

I want to show in the X-axis the date, and in the Y-axis a number of sale products. In my DataSource, I group the count of products for each date.

Have you done your example using a JRDatasource instead a Connection to DB?

thanks 

Dumitru Vechiu replied on Tue, 2009/05/12 - 11:02am

Hello, thanks for best practices using Jasper Reports. Very useful information.

I need your professional help.

I have to generate a dynamic report. The ideea is that the repport contain a table that can have multiple columns (3 to 15) in dependance of the database content and it should be a A4 repport.

So, it is something like a split column repport.

Here are un exemple : Supose the database contain 3 active refs (it can contain till 15). Each ref.has the sum of phone calls per day (sum_ref). As you see in the example the dates must be ordered. The last column is the total sum per day.

A4 format, page 1

 ___________________________

|        Month repport     01           |

|                                                  |

|   |data  | sum_ref1 |sum_ref2|  |

|   |01.01| 1112        | 2323      |  |

|   |01.03|      12       |              |  |

|   |01.07|    114       |              |  |

|   |01.08|      12       | 22         |  | 

|   |01.09|                 |  33        |  | 

|_________________________p1|

 

A4 format, page 2

 ___________________________

|   Month repport     01                |

|                                                 |

|   |data  | sum_ref3| total       |  |

|   |01.01|                | 3435      |  |

|   |01.03|      1        |    13       |  |

|   |01.07|                |              |  |

|   |01.08|      4        |   38        |  | 

|   |01.09|                 |  33        |  | 

|_________________________p2|

 I have never worked with a report tool.

Can you answer please if it is possible to generate that kind of repports using jasperReport. Give me please some tips.

 

Thank you!

PS. Hope i gave a good explanation. If not please contact me.

Mihai Dinca - P... replied on Wed, 2010/02/24 - 9:40am

Your business must be adapted to your needs. NextReports (www.next-reports.com) is a simple and powerful reporting tool written in java. If you have a lot of reports, many of them will be easier to create with Next than Jasper or other reporting tools.

NextReports Server integrates more reporting engines (like Next and Jasper for now). So, you will be allowed to work faster with your favourite reporting engine. 

 

Ganeh Kc replied on Sun, 2010/09/26 - 11:23pm

Really this is an excellent article. I hope you got the chart line for making a report that use a JRDatasource to get the data for the Line chart. Though I have not done any example using this method, but hope you got it from  the reply comment of Vehicu. It contains dynamic contents as well as the report contain a table that can have multiple columns. Looking forward for further support.

 

Regards,

Multivariate Testing

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.