JFree Bar Chart Customization in iReport.. Category axis labels overlapping is removed.

Posted on by By Nikhilesh, in Business Intelligence, Jaspersoft, Open Source Business Intelligence | 0

HI Folks,

Today we are going to learn how to customize BarChart Category axis.

Credit of developing code goes to Mr. Sharad Sinha who is my colleague. As part of sharing knowledge I’m gonna explain the steps.  I’m starting with the problem statement followed by the solution.

Prerequisites :

iReport : 5.0.4

PostgreSQL : 9.2

Jasper Server : 5.0

Problem :

* We have a bar chart having many values to display on the chart .. i.e, let us say 100+

* This is not an XY Chart … simply it is Vertical Bar Chart.

* Bars are coming up properly BUT the labels on the category axis are overlapped and coming in a line. or when the angle is set up to -70 the labels are coming as clumsy as shown in figure below.

* The values which we want to show on the category axis are : dates like 1 Jan 2013 and the next label we want to show 8 Jan 2013 and etc..

* Here is the snapshot of the problem statement.

 

Solution:

Our final out put should looks some thing like as follows.

What we have to do to get the above output?

We have to write Chart customizer class and have to call that class from iReport.

I’m going to explain step by step how to achieve this.

 

Read the points:

1.  Write chart customizer class java code in NetBeans.

2. Make a jar file.

3. Add that jar file in iReport as well in Jasper Server.

4. Call the class from the properties of Vertical Bar Chart in customizer class.

 

1.  Write chart customizer class java code in NetBeans.

package com.xyz.bar.chart.customizer;  //xyz is name of the company generally we give
import java.awt.Color;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

public class BarChartCustomizer

implements

net.sf.jasperreports.engine.JRChartCustomizer

{

private Number tickUnits;
public BarChartCustomizer() {

tickUnits = Integer.valueOf(0);

}
@Override

public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart) {

org.jfree.chart.renderer.category.BarRenderer renderer;

org.jfree.chart.plot.CategoryPlot plot;

org.jfree.chart.axis.NumberAxis rangeAxis;

org.jfree.chart.axis.CategoryAxis axis;

renderer = (org.jfree.chart.renderer.category.BarRenderer) chart.getCategoryPlot().getRenderer();

 

plot = chart.getCategoryPlot();

rangeAxis = (org.jfree.chart.axis.NumberAxis) plot.getRangeAxis();
axis = plot.getDomainAxis();
CategoryAxis domainAxis = plot.getDomainAxis();

CategoryLabelPositions pos = domainAxis.getCategoryLabelPositions();

for(int i=0; i< plot.getCategories().size()-1; i++)

{

if(i%6==0){

String cat_Name = (String) plot.getCategories().get(i);
}

else

{

String cat_Names = (String) plot.getCategories().get(i);

domainAxis.setTickLabelPaint(cat_Names, Color.white);

}

}

// plot.getDomainAxis().setLabel(“TEST “+plot.getCategories().size()+”  “+pos.);

}

}

2. Make a jar file.

* Right click on the project and click on clean and build

* Net beans will automatically create jar file.

* Find this jar file in “dest” folder of your project.

 

3. Add that jar file in iReport as well in Jasper Server.

Location to place this jar files in Jasepr server.

C:Program Filesjasperreports-server-5.0apache-tomcatwebappsjasperserver-proWEB-INFlib

Location to call the jar files in iReport.

* On the menu bar go to

Tools –> Option –> iReport –>Class Path–> Add jar

* Select the location of jar file.

 

4. Call the class from the properties of Vertical Bar Chart in customizer class.
* High light the chart (bar chart in our case)  that you want to apply the customizer class.

* Go to properties (right side appears) —> go to customizer –> in the blank space write the classname including the package.

* For example in our case:

package com.xyz.bar.chart.customizer.BarChartCustomizer

That’s it we have done with the customization of the chart.

 

NOTE: after adding jar file to the server, you must restart your server otherwise you can’t see the effect of added jar file in our report

NOTE: Make sure to import necessary library files in Netbens while developing the plugin.

* Your imported library files (nothing but jars) should compatible with the files that your jasper server is using otherwise you will get minor/higher version related error when you run your report.

* Find the image below.

Thanks for reading document 🙂

 

Queries are welcome @ sadakar@helicaltech.com

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments