Bar Graph and Bar Width

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mathematical
Posts: 13
Joined: Mon Apr 30, 2007 3:24 pm

Bar Graph and Bar Width

Post by mathematical » Wed May 02, 2007 9:02 pm

I have a horizontal bar graph and I need to keep the bar width the same regardless of how many categories are displayed. I understand that there is no method to directly set the width of a bar, and I have been trying to do it indirectly by setting all of the setXXXMargin() methods.

The setXXXMargin() methods (according to the api) receive parameters of type double that signify a percent of the DomainAxis length.

So here's what I tried to do:

Code: Select all

categoryPlot.getDomainAxis().setLowerMargin(0.05);    
categoryPlot.getDomainAxis().setCategoryMargin(0.05 * (numberOfCategories - 1));
barRenderer.setItemMargin(0.02 * numberOfCategories * (barsPerCategory - 1));
categoryPlot.getDomainAxis().setUpperMargin(1 - (categoryPlot.getDomainAxis().getLowerMargin() + categoryPlot.getDomainAxis().getCategoryMargin() + barRenderer.getItemMargin()));
This for some odd reason does not work, if anyone can see an error in this code or know another approach that works, please let me know. The extra space is supposed to go under the last category and why the setLowerMargin really means setUpperMargin in a horizontal bar graph...I dont know.

Thanks in advance.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu May 03, 2007 10:02 am

I'd recommend using setMaximumBarWidth() to cap the bar width. If you set this value small enough, the bars will always be the same width until such time as there are too many bars to display, and they'll be made thinner in order to fit them in.

The setLowerMargin()/setUpperMargin() methods are poorly named - setLeadingMargin()/setTrailingMargin() might have been better names. Anyway, the lower margin comes before the first category and the upper margin comes after the last category. It turns out that for a horizontal bar chart, this means the lower margin is at the top, and the upper margin is at the bottom. Sorry.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

mathematical
Posts: 13
Joined: Mon Apr 30, 2007 3:24 pm

Post by mathematical » Thu May 03, 2007 2:13 pm

Thank you for the setMaximumBarWidth() method it actually helped me out a lot. I had another issue but I figured it out, Thanks again.

Locked