Friday 20 March 2015

JFree Bar Chart Customization in Jasper iReport - Decrease the size of bars & space between bars

This post will give you quick solution on customizing JFree Bar Chart .
Re-collected the year and half back workout again & here we go.

Software : 

1) Jasper iReport 5.6.1 community
2) Eclipse Luna

Problem Statement : Image-1

1) Decrease the width of the bars
2) Decrease the space b/w bars for a series bar Chart


Solution Image :Image-2


How to do ?

1) Develop your chart & have a preview which looks like image-1.

2) You need to customize the chart using JFree Java code written in Eclipse & get the output in Jar file.

3) Open Eclipse & create a project and then a class. You should import 3 jar files to your project.
   Those are : 1) jasper-reports-<version>.jar 2)jfreechart-<version>.jar & 3)jcommon-<version>.jar

  You can right click the project name and go Build path section and then Add jar files -> click on "Add external jar files " from the location where your jars are avaialbe.

How to get the jar files ?
You can get all the jar files in below jasper server installed location. (i.e., you should be installed jasper server in your local machine).

C:\Jaspersoft\jasperreports-server-6.0\apache-tomcat\webapps\jasperserver-pro\WEB-INF\lib

Alternatively, you can type the names of the jars and download directly from web.



4) Run the code & export to  a jar file. (for exporting Project Name -> Right click - > Export)

 5) Now the downloaded jar file code has to apply on the chart customization.
For that in iReport, Go to Tools-> Options and select the jar file from your system location.


6) Now select the chart to get it's properties on right side properties window.
   search for Customizer Class property. Provide the class name of the Java code along with it's package name.
i.e., for example : com.sadakar.jfree.JFreeBarChartCustomization1

That's all.. Save the report and have a preview. you will get the output as shown in Image-1

NOTE :
When you publish the report to the server you will not get the output as expected. B'z your server doesn't know about the changes you done to the report. For this you need to load your jar file in lib folder of jasper instance. i.e., place the jar file at
C:\Jaspersoft\jasperreports-server-6.0\apache-tomcat\webapps\jasperserver-pro\WEB-INF\lib 
and restart your server.

Exploring the same with Studio 6 community & professional and update this line :-)

 Interesting...!!! Have a question or suggestion ? Drop your feedback in comment box and help community to share knowledge... :-)

Cheers..!!!




No comments:

Post a Comment