DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Java Reporting – Part 4

Java Reporting – Part 4

Hossam Sadik user avatar by
Hossam Sadik
·
Jun. 26, 08 · Interview
Like (1)
Save
Tweet
Share
39.32K Views

Join the DZone community and get the full member experience.

Join For Free

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

Java (programming language) Chart Open source

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Full Lifecycle API Management Is Dead
  • Reliability Is Slowing You Down
  • What Are the Benefits of Java Module With Example

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: