Tuesday 7 April 2015

Tip : JFree Bar Chart Customization in Jasper Reports: Moving Bar Itemlabels ( some times values will not fully apper for -ve highest bars)

This post will provide you the code snippets to write in customizer method for JFree Bar Chart in
Jasper Reports

1) Remove space between 2 or  more series within a category.
2) Remove space between 2 or more categories for chart
3)  Remove horizontal and vertical grid lines for bar chart.
4) Fix overlapping of the highest -Ve bar value with category label. 
5) Fixed width for the bars

Problem Image : 


Solution Image :



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author SADAKAR POCHAMPALLI
 */

package com.sadakar.jfree;
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;
import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.ui.TextAnchor;

import org.jfree.util.UnitType;

public class JFree extends JRAbstractChartCustomizer{
@Override
public void customize(JFreeChart chart, JRChart jasperChart){
CategoryPlot categoryPlot=chart.getCategoryPlot();
BarRenderer renderer=(BarRenderer)categoryPlot.getRenderer();

//The space between 2 series within a category is controlled by the attribute itemMargin
renderer.setItemMargin(-0.5);
categoryPlot.setRenderer(renderer);

//The space between 2 categories is controlled by the categoryMargin attribute of the CategoryAxis //for the plot
 CategoryAxis domainAxis = categoryPlot.getDomainAxis();
 domainAxis.setCategoryMargin(0.3f);


// -Ve bar Item Labels positions - Below lines of code will not cut down the item label for the biggest //value of bar
//renderer.setSeriesNegativeItemLabelPosition(0, new //ItemLabelPosition(ItemLabelAnchor.OUTSIDE11,TextAnchor.BASELINE_CENTER));

renderer.setItemLabelAnchorOffset(0.1);
renderer.setBaseItemLabelsVisible(true);
renderer.setMaximumBarWidth(0.01);


categoryPlot.setRangeGridlinesVisible(false);
categoryPlot.setDomainGridlinesVisible(false);


}
}

You may be interested in similar posts in this site. 

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

2) JFree Line Chart Cusomization in Jasper iReport. Line to Dashed-line, line width,gap in the dashed lines

3) JFree Meter chart customization in Jasper iReport-- Removing values from the meter

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


No comments:

Post a Comment