Monday, 8 September 2014

Add customizer class to remove Bar Gab between two series of Ba Chart

In This post I will see how to add a customizer class to remove space between two series of bar chart. 




Desired output after adding customizer class.



To get this output we have to create a executable jar file. Will use Netbeans to create .jar file.

Step 1:- Open NetBeans and create a new Java Application project.



Give a Project Name – customizerbargap   (you can give any other name also)
Here you can see the project location and class name, you can also change these parameters.
My Main class name CustomizerBarGap
and Finish

Copy below code in CustomizerBarGap.java

package customizerbargap;

import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import net.sf.jasperreports.engine.JRChart;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;


public class CustomizerBarGap extends JRAbstractChartCustomizer {

public void customize(JFreeChart chart, JRChart jasperChart) {

CategoryPlot categoryPlot = chart.getCategoryPlot();
BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();

//Spaces between bars
renderer.setItemMargin(0.00);      
}
}
     


Add required jars in Library folder like Jfree.jar, Jcommon.jar, Jasperreports.jar
Right click on the project  and click on Clean and Build.
In output window you can find the path of .jar file

In my case it is –
Building jar: C:\Users\sharad\Documents\NetBeansProjects\Project_Name \dist\ Package_Name(CustomizerBarGap).jar



Open iReport and add CustomizerBarGap.jar in the Classpath.
To go add go to Tools --> Options --> ClassPath --> Click on Add JAR.



After adding .jar file click on the chart and go to Bar Chart - Properties section and set Customizer Class property to PackageName.ClassName  i.e.

Customizer Class =  customizerbargap.CustomizerBarGap


Then click on the Preview tab.