Customizer class to remove space between bars in a StackedBar


I created a StackedBar item and want to eliminate the space between the bars. I wrote the following customerize, but the bars are sill spaced, any idea ?

thanks in advnace.


import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.JRChart;

import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.*;

public class CustomizeBarChart extends JRAbstractChartCustomizer {
public void customize(JFreeChart chart, JRChart jasperChart) {
CategoryPlot categoryPlot = chart.getCategoryPlot();
StackedBarRenderer renderer = (StackedBarRenderer) categoryPlot.getRenderer();
renderer.setItemMargin(0.0); }}


Here is the scala version of the same code:

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;
class CustomizeBarChart extends JRAbstractChartCustomizer{
def customize(chart: JFreeChart , jasperChart:JRChart) =
val categoryPlot: CategoryPlot = chart.getCategoryPlot()
val renderer: BarRenderer = categoryPlot.getRenderer().asInstanceOf[BarRenderer]
//Spaces between bars
chart.setTitle("Hello From scala")

haidar_hadi_205's picture
Joined: May 14 2014 - 12:47pm
Last seen: 8 years 11 months ago

1 Answer:

Chech this post by Matt He has a chart customizer there that does exatly what you are looking for (scroll down to "Space between bars").

But answering your question the propertly you want to change is "setCategoryMargin" not setItemMargin this post on StackOverflow helped me understand where are all the margind in JFree Charts.

marianol's picture
Joined: Sep 13 2011 - 8:04am
Last seen: 4 years 8 months ago

@marianol, worked like a charm. Many thanks.

haidar_hadi_205 - 9 years 2 weeks ago