Bar chart setBarWidth

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
utkarsh
Posts: 2
Joined: Fri Oct 03, 2003 8:29 pm
Location: Phoenix

Bar chart setBarWidth

Post by utkarsh » Mon Oct 20, 2003 10:39 pm

I was trying to control the bar width. But somehow I was unable to access setBarwidth() method. I made following changes so as to make use of that method in BarRenderer:

/**
* Updates the calculated bar width.
*
* @param width the new width.
*/
public void setBarWidth(double width)
{
this.barWidth = width;
}


public void initialise(Graphics2D g2,
Rectangle2D dataArea,
CategoryPlot plot,
Integer index,
PlotRenderingInfo info) {

super.initialise(g2, dataArea, plot, index, info);

// get the clipping values...
ValueAxis rangeAxis = getRangeAxis(plot, index);
this.lowerClip = rangeAxis.getRange().getLowerBound();
this.upperClip = rangeAxis.getRange().getUpperBound();

// calculate the bar width
CategoryAxis domainAxis = getDomainAxis(plot, index);
CategoryDataset dataset = getDataset(plot, index);
if (dataset != null && this.getBarWidth() == 0.0 ){

You can then use the setBarWidth method, eg:

BarRenderer renderer = new BarRenderer();
renderer.setBarWidth(8.0);

dbasten
Posts: 52
Joined: Fri Jul 25, 2003 4:29 pm

Post by dbasten » Tue Oct 21, 2003 6:48 pm

I was tackling this same problem today as well. As an alternative to the solution posted, I did not prevent the calculation of the bar width. Instead, I updated the section of code within the initalise method where the width is set:

Code: Select all

            if ((rows * columns) > 0) {
				double tempBarWidth = used / (dataset.getColumnCount() * dataset.getRowCount());
//                setBarWidth(used / (dataset.getColumnCount() * dataset.getRowCount()));
				if ( tempBarWidth < getBarWidth() )
				{
					setBarWidth( tempBarWidth );
				}
            }
            else {
//                setBarWidth(used);
				if ( used < getBarWidth() )
				{
					setBarWidth( used );
				}				
            }
As you can see in the snippet, if the calculated width is less than the user-specified width, it is used instead. Otherwise, the user-specified value is kept. It keeps the bars from being outrageously large, but also allows the tool to set the width necessary to fit in the space provided.

The code works in small trials I have been doing; if others have a different way through this, I'm always willing to learn!

Regards,
David

dbasten
Posts: 52
Joined: Fri Jul 25, 2003 4:29 pm

Post by dbasten » Wed Oct 22, 2003 6:40 pm

I was a little too hasty with the previous code version I posted. In that example, if a bar width is not set, the bar width is 0 and is not displayed.

Here is the corrected version:

Code: Select all

            if ((rows * columns) > 0) {
				double tempBarWidth = used / (dataset.getColumnCount() * dataset.getRowCount());
//                setBarWidth(used / (dataset.getColumnCount() * dataset.getRowCount()));
				if ( getBarWidth() == 0 || tempBarWidth < getBarWidth() )
				{
					setBarWidth( tempBarWidth );
				}
            }
            else {
//                setBarWidth(used);
				if ( getBarWidth() == 0 || used < getBarWidth() )
				{
					setBarWidth( used );
				}				
            }
I was addressing this problem from the Gantt chart perspective; if my series only had one value, it made one very big, unsightly bar. In order to get the bars to "line up" with the task name, you can play with the image size and the category margin on the category axis.

Regards,
David

Guest

Post by Guest » Mon Nov 03, 2003 8:25 pm

hi ,
i used your implementation . wen i have just one categroy item . for example data fro one country and for one year to be painted in the bar chart , the bar size doesnt decrease . it paints as one massive vertical bar . how do i over come this .

Thanks in anticipation,
Morgan

Kartik

Setting bar width in case of single bar

Post by Kartik » Fri Nov 07, 2003 8:15 am

Hi,

I am also facing a similar problem. If the dataset contains data only for a single category, then the bar occupies the whole graph. Is there any other way other than making the setBarWidth() method public?

thanks

Kartik

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 » Fri Nov 07, 2003 5:51 pm

The setBarWidth(...) method won't be made public. A better approach would be to add (to the BarRenderer class) an attribute 'maxBarWidthPercentage' that specifies the maximum width of a bar as a percentage of the plot width.
David Gilbert
JFreeChart Project Leader

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

dbasten
Posts: 52
Joined: Fri Jul 25, 2003 4:29 pm

Post by dbasten » Tue Nov 18, 2003 9:00 pm

I just noticed that the maxBarWidth attribute suggested by David Gilbert in his last post was added to the 0.9.14 release of JFreeChart. I have tried it out (again, from the gantt chart perspective) and it seems to work very well.

Thanks David!

fse
Posts: 25
Joined: Thu Mar 27, 2003 9:21 am
Location: cologne, germany
Contact:

Post by fse » Wed Dec 17, 2003 4:40 pm

hi folks,

i don't see any effect using
public void setMaxBarWidth(double percent)
and i tried out values between 500 and .005

this should narrow my bars am i right?

cu
frank

Locked