Bar chart setBarWidth
Bar chart setBarWidth
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);
/**
* 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);
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:
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
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 );
}
}
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
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:
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
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 );
}
}
Regards,
David
Setting bar width in case of single bar
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
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
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

