Monday 17 June 2013

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

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 then solution accordingly.
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 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 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 by 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 file in Jasepr server.
C:\Program Files\jasperreports-server-5.0\apache-tomcat\webapps\jasperserver-pro\WEB-INF\lib
Location to call the jar file 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 other wise we can see the effect of added jar file in our report
NOTE: Make sure to import necessory library files in Netbens while devloping the plugin.
* Your imported library files(nothing but jars) should compatable with the files that your jasper server is using other wise you will get minor/higher related error when you run your report.
* Find the image below.

Thanks for reading document