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.

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


Download the Sample Project from my  GitHub

Check the screen-cast for better understanding.

Screen cast: