Platinum Partner
java,reporting

Java Reporting – Part 4

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

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}