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