Skip to main content

Integrate Chart Image into Jasper Report Part - 2

In my previous post we discussed how to Integrate the Charts into Jasper Reports using JFreeChart API in Jasper Report. In this post there is another way to embeds the charts into Jasper Reports. This method, we need to generate/create the chart using any 3rd party Java Lib and convert into Image Object or stored into File-system, then just insert the Image into Jasper Report. here no need to use chart functionality in Jasper Report. because these chart are limited functionality.


Steps:
  1. Create the chart using any Java libraries
  2. Convert the Chart into BufferedImage or Stored into files
  3. pass the Image into Jasper Report.

1. Create the chart using any Java libraries
     In this step we going to create the chart. here i m using jopenchart Java library to create the chart. In their website tutorial section contain some sample codes. so i used these codes. and download the jopenchart lib from here.

2. Convert the chart into BufferedImage or Stored into files
    In jopenchart have the in-build function for store chart into the file.
ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home") + "/chat.png"), c);
and if u want into in-memory Image object then use render() method.

In above code we create empty BufferedImage object. the get the Graphics2D object from the BufferedImage and pass the Graphics2D into chart render() method. so when render() method is called, the chart is write into BufferedImage object.

3. Pass the Image into Jasper Report
        We need to create the Jasper Report. this time we are not using chart function in Jasper reports. we using Image component of jasperreport. We have 2 options are there, pass the Image Object or Pass the Path String object where image is stored in File-system. here first i m using Image object.

we use parameter to pass into jasper report. so first create the new Parameter in Jasper Report and named "chartImage" and Parameter Class "java.lang.Object".  drag the Image Component and set the properties. Image Expression and Expression Class like
               Image Expression == >  $P{chartImage}
               Expression Class  == > java.awt.Image



If we use stored Image File then change the Parameter class to java.lang.String and Image Expression Class to java.lang.String.

here Image Expression class is decided the Jasper Report. how we add the Image. If u put "java.awt.Image" as a Expression class then we are passing the Image object. and jasper report can understand. If u mention "java.lang.String" then we pass as String that pointing to Image file. (png,jpg,gif,..).

Code:
DemoBean.java



Download the Sample Project from my  GitHub


Check the screen-cast for better understanding.

Screen cast:

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...

File Upload is Easy in JSF2.2

To bring the File Upload feature in Java based web application is one of the difficult and complex job, we need to dependent on 3rd party libraries like Apache Commons FileUpload  Libraries. These libraries codes are complex and most of them boilerplate code. If we are using Java Server Faces (JSF), we have the page with some fields and file upload menu the its add more complexity, fields are binded to backing bean but these file uploads components are need to tie up with some 3rd party file upload libraries. In Primefaces provide easy way to do the file upload in JSF web application, even though primefaces internally used the same Apache Commons FileUpload Libraries, but provide simple JSF tags. We need configure  some listeners.