1

I'm using TIBCO Jaspersoft Studio version 6.8.0 and I'm creating a barChart as a subReport of a main report. The barChart takes data from a sub dataset. The report is creating quite well so no complaints about that.
The barChart is built dinamically setting the width of the bars based on the number of the elements. But I'd like to set statically the width of the bars, ignoring how many elements there are. So it would be a bar large 10 pixels (for example) both when there are 10 elements or 2 elements.

Here it is a picture showing the barChart with the correct bars' width (based on element number in it)

enter image description here


And here we have the same barChart but with less elements, so the bars' width would just resize dynamically (i want to set a static value to their width)

enter image description here

I followed some guide for creating and using a custom function for a barChart, but none of these seems to work (maybe I'm using a newer version and they were referring to an older version of Jasper Report)

I was using this, but I dont know if it's good and how to implement it in a report

package com.efarmgroup.sina.sios4.jasper;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.ui.TextAnchor;

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


public class customBarWidth implements JRChartCustomizer{

    @Override
    public void customize(JFreeChart chart, JRChart jasperChart){

        CategoryPlot categoryPlot = chart.getCategoryPlot();
        BarRenderer br = (BarRenderer) categoryPlot.getRenderer();
        br.setMaximumBarWidth(.10); // set maximum width to 35% of chart
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
alesssz
  • 384
  • 4
  • 18
  • 1
    Possible duplicate of [*Unable to custsomize the bar chart bars width*](https://stackoverflow.com/q/57050099/230513). – trashgod Jul 24 '19 at 02:31
  • Yes and no. I saw that question but that isn't resolved. And I'm asking one more question, how to create that custom function...I create it but I cannot import it as a Chart Customization inside the barChar in the subReport – alesssz Jul 24 '19 at 07:48
  • 1
    Also consider `setCategoryMargin()` _et al._, and as suggested [here](https://stackoverflow.com/search?tab=votes&q=%5bjfreechart%5d%20setMaximumBarWidth) – trashgod Jul 24 '19 at 16:18
  • I resolved my problem. I saw lot of errors 'cause I didnt build correctly the path to create the Jar in the correct folder, my bad. Thank you – alesssz Jul 25 '19 at 10:12
  • Glad you got it sorted. You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188) in the context of [tag:jasper-reports] or I can close as duplicate. – trashgod Jul 25 '19 at 13:00

1 Answers1

1

I resolved my problem (double problem).
Inside the function I wrote

br.setMaximumBarWidth(.01); // set maximum width to 1% of chart

And this worked fine for what I needed.
The other problem was about importing the function inside the report.
Through an ant file I managed to create the Jar that contains the compilated function (.class file).

<project name="projectName" default="createjar">    
   <target name="createjar">
      <jar destfile="output-jar/jarName.jar" basedir="./bin" />
   </target>    
</project>

After this, I had the .class file I needed to import and use the function in the report.

alesssz
  • 384
  • 4
  • 18