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 toady'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.
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:
Extract the compressed file and add these libraries to your project classpath:
ITEM_ID NUMBER(5) NOT NULL
CATEOGRY_ID NUMBER(5) NOT NULL
ITEM_NAME VARCHAR2(50) NOT NULL
ITEM_AMOUNT NUMBER(5) NOT NULL
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).
[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.
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.
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.
Thanks and best regards,
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