Skip to main content

Integrate Charts in Jasper Reports +JSF 2.0

In this post we will walk through how to integrate Charts (JFreeChart) into Jasper Report framework. JasperReports is the world's most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Xlsx, Docx, Pptx, Odf

Check my Introduction to Jasper Reports and Sub-report with jasper Report posts.

2-ways to Integrate the Charts/Graphs into Jasper Reports
  1. Use JFreeChart API inside Jasper Report to generate the Charts while generating reports
  2. Use any 3rd party Java Libraries to create the chart and stored in In-memory Image object (or) stored in file. then pass the Image/file to Jasper Report. then embed the Image into Report. (check here)
In this post we going to use 1st option. In Jasper Report use JFreeChart Components to make charts. In my next post we will see the 2nd way to generate the charts in reports.

I ll create one sample application (Progress Report) to demonstrate the Integrate the charts in reports.

I have Student Bean contain name, roll No, image and List of Semester paper beans. In Semester paper bean contain name of the paper and mark(score).

Student.java

SemesterPaper.java


Create the Report
    we can use either iReport Standalone version or Jasper report Net-beans plugins to create the jasper Reports.

Create empty reports and create the fields and these fields are match with Student bean property variables. here name,rollNo and imagePath are String. so no need to change the data type of field. but listOfSemesterPaper is List. so change the data type of the field to List.

then drag Image component and static text component for making general report like this
here name and rollNo fields are dragged into report canvas, then drag the Image component and change the expression of Image component to imagePath field. Now we ready for bring the Chart component. But chart data are stored in listOfSemesterPaper field. Its List. So we can't use directly. so we going to create sub data set from main data set. so create sub dataset named "ChartDataset". and In sub data set create another 2 fields match with SemesterPaper bean. here name is String and mark is double.

Now drag the chart and choose the chart type. here i chosen Bar chart. its poped up the wizard.  we need to choose Data set. here we need to choose "ChartDataset" sub data set, which one we created. then select the Category and Value property of Charts. here name is category and mark is value. (see the screenshot)



and final step is we need to explicitly mention how main data set is divied into sub data set. In sub data set "ChartDataset" is take value of listOfSemesterPaper field. this fields is List. so we need to wrap into JRBeanCollectionDataSource.

that's it. now compile the report and generate the .jasper format. then integrate into Netbeans project.


DemoBean.java

index.xhtml
Download the Sample Project from my  GitHub

Watch in HD

Comments are Welcomed

Popular posts from this blog

Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

In this post i will show Tomcat Clustering in Docker Container. In  my previous post i discussed how to achieve tomcat clustering with Nginx Front end . Its almost same scenario, but this time we will achieve via docker container. Docker Docker  is an  open-source  project that automates the deployment of  applications  inside  software containers , by providing an additional layer of abstraction and automation of  operating-system-level virtualization  on  Linux . [4]  Docker uses resource isolation features of the Linux kernel  such as  cgroups  and kernel  namespaces  to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining  virtual machine   --Wikipedia

Understanding Virtual Host Concept in Tomcat

Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective. Scenario: I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com The following diagram is my outline. Outline structure of Virtual Host Concept in Tomcat Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address. How all domain names are pointing to my Tomcat?                   When we purchase the domain name we need to update the our tomcat IP address to it. like or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts Now How Setup...

Virtual Host + Apache httpd server + Tomcat + mod_jk connector

In my last post ( Virtual Host in Tomcat ) we discussed about how setup the virtual host in Tomcat. Its cost effective technique because only one public IP is enough to host multiple domain. If we have big organization and each department want to host their website in locally in different machine. then how to achieve the virtual host concept?. In this post we will see the how we do this. Update :   I posted  Virtual Host + Nginx + Tomcat  Its easy to configure, compare to Apache httpd server Problem Scenario:         In big organization they have multiple department, each department want to host their website in different machine. so these websites are accessed locally with different local IP address. When we mapping to public address then we face the problem. We have two choice either purchase as many public address or Put one server front  and delegate these request. ...