How to Write values on the bars in Bar charts?
How to Write values on the bars in Bar charts?
In bar chart, how do you write the actual values in the bar graphs? to write 50 on bar1, 20 on bar 2 etc.
th data it is representing?
th data it is representing?
Re: How to Write values on the bars in Bar charts?
There's no facility for this at present. It's pretty simple to add, but not so easy to make it foolproof...what should happen when there's not enough space to display the value? Suggestions are welcome...
Regards,
DG.
Regards,
DG.
Re: How to Write values on the bars in Bar charts?
This is absolutely necessary to write the values on the bar chart. Please let me know the changes I need to do.Thanks.
Re: How to Write values on the bars in Bar charts?
You need to add some code to the draw(...) method in the VerticalBarRenderer class (assuming you are using a vertical bar chart). You should be able to use g2.drawString(...) to draw whatever text you want, wherever you want it.
Regards,
DG.
Regards,
DG.
Re: How to Write values on the bars in Bar charts?
I meant the drawCategoryItem(...) method, not the draw(...) method.
Re: How to Write values on the bars in Bar charts?
Hi,
Can't you use tooltips to do this -- when you mouseover the bar a dwell label pops up with the value. I want to do something like this for html image maps, but time has been pretty scarce. Dave, any idea how to map pixels from java2D space (doubles) to image space (ints). It maybe straightforward but I'm no wiz with the java2D api (or much else, for that matter). If anyone knows how, I will create an HtmlImage adapter for DrawInfo.
Thanks,
Jim
Can't you use tooltips to do this -- when you mouseover the bar a dwell label pops up with the value. I want to do something like this for html image maps, but time has been pretty scarce. Dave, any idea how to map pixels from java2D space (doubles) to image space (ints). It maybe straightforward but I'm no wiz with the java2D api (or much else, for that matter). If anyone knows how, I will create an HtmlImage adapter for DrawInfo.
Thanks,
Jim
Re: How to Write values on the bars in Bar charts?
I agree that this is pretty important functionality. As for "what should happen when there's not enough space to display the value?", I don't think this should be an issue. If there is not enough space, make the space, i.e. scale the bars down enough to make room. I know this is probably not as easy as it seems, seeing as it will involve the renderer and the axis, but I feel like the struggle it will entail is worth it.
--jim
--jim
Re: How to Write values on the bars in Bar charts?
In addition, solving this problem may shed some light on another bug I have found. When drawing a stacked bar chart (vertical or horizontal), the maximumAxisValue is valculated incorrectly unless I do it manually as below:
double max = 0;
for (int cat = 0; cat < data.length; cat++) {
int mx = 0;
for (int series = 0; series < data.length; series++) {
mx+=data[series][cat].doubleValue();
}
max = Math.max(max, mx);
}
valueAxis.setMinimumAxisValue(0);
valueAxis.setMaximumAxisValue(max*1.1); // add 10 percent just to leave some headroom
The problem it seems is that the axis does not know anything about the renderer (in this case that this is a stacked renderer), but to accurately do its job, this is knowledge it needs. I think the axis should have some input from the renderer when auto-determining min and max axis values. This would solve both the stacked chart problem and the leaving room for labels problem. Adding this may involve some pretty heavy refactoring, but I think the changes will be worthwhile. Essentially if the renderer is doing the drawing, it knows better than anyone else how much space it needs, so it should really set the min and max axis values or at least tell the axis what its absolute min and max values are.
--jim
double max = 0;
for (int cat = 0; cat < data.length; cat++) {
int mx = 0;
for (int series = 0; series < data.length; series++) {
mx+=data[series][cat].doubleValue();
}
max = Math.max(max, mx);
}
valueAxis.setMinimumAxisValue(0);
valueAxis.setMaximumAxisValue(max*1.1); // add 10 percent just to leave some headroom
The problem it seems is that the axis does not know anything about the renderer (in this case that this is a stacked renderer), but to accurately do its job, this is knowledge it needs. I think the axis should have some input from the renderer when auto-determining min and max axis values. This would solve both the stacked chart problem and the leaving room for labels problem. Adding this may involve some pretty heavy refactoring, but I think the changes will be worthwhile. Essentially if the renderer is doing the drawing, it knows better than anyone else how much space it needs, so it should really set the min and max axis values or at least tell the axis what its absolute min and max values are.
--jim
Re: How to Write values on the bars in Bar charts?
Hi Jim,
It's pretty easy to make space vertically (for a vertical bar chart) by changing the range axis. The problem I was thinking of is the available space horizontally when there are a lot of bars...
Regards,
DG.
It's pretty easy to make space vertically (for a vertical bar chart) by changing the range axis. The problem I was thinking of is the available space horizontally when there are a lot of bars...
Regards,
DG.
Re: How to Write values on the bars in Bar charts?
As for horizontal space, I would almost argue that that issue is moot. If the chart is so tight that there isn't space for the values, then most likely the category labels will be overlapping as well, so even without the values, you are going to have a pretty messy looking chart.
The existing answer for getting the category labels not to overlap? Either print them vertically (for a vertical bar chart) or turn them off, both of which would work equally well for the value labels.... Basically allow the ability to view them or not (as with the pie chart--while we are speaking of pie charts, the ability to show percent labels, value labels or both would be nice), and also the ability to print the vertically or horizontally (as with the category labels). This way, if you have room for a category label, then you will definitely have room for a value label, even if it is 100000000000000000000000000000. Just scale the bar down to leave room for it and print it vertically.
--jim
The existing answer for getting the category labels not to overlap? Either print them vertically (for a vertical bar chart) or turn them off, both of which would work equally well for the value labels.... Basically allow the ability to view them or not (as with the pie chart--while we are speaking of pie charts, the ability to show percent labels, value labels or both would be nice), and also the ability to print the vertically or horizontally (as with the category labels). This way, if you have room for a category label, then you will definitely have room for a value label, even if it is 100000000000000000000000000000. Just scale the bar down to leave room for it and print it vertically.
--jim
Re: How to Write values on the bars in Bar charts?
Dave,
Where I can find "drawCategoryItem(...) method".
Would you explain little more detaily. Where I have to put drawString(...) method and which part I have to change for extra vertical space.
Thanks,
Seung
Where I can find "drawCategoryItem(...) method".
Would you explain little more detaily. Where I have to put drawString(...) method and which part I have to change for extra vertical space.
Thanks,
Seung
Re: How to Write values on the bars in Bar charts?
Finally, I put the value in the top of the bar vertically. It looks little busy, but better than seperate data table I used before.
Problem is I could not find the way to put extra space. Would you give me some tips for this.
Thanks,
Seung
Problem is I could not find the way to put extra space. Would you give me some tips for this.
Thanks,
Seung
Re: How to Write values on the bars in Bar charts?
Seung wrote:
> Problem is I could not find the way to put extra space. Would
> you give me some tips for this.
The easiest way would be to increase the upper margin for the range axis. There is a method setUpperMargin(double) in the NumberAxis class. By default, a margin of 5 percent (of the axis range) is maintained at the upper end of the axis (when auto range is TRUE) to ensure that the maximum data value isn't squeezed right up to the top of the chart.
It would be better, but much more difficult to code, to calculate the space required by the labels first and then automatically set the upper margin to an appropriate value.
Regards,
DG.
> Problem is I could not find the way to put extra space. Would
> you give me some tips for this.
The easiest way would be to increase the upper margin for the range axis. There is a method setUpperMargin(double) in the NumberAxis class. By default, a margin of 5 percent (of the axis range) is maintained at the upper end of the axis (when auto range is TRUE) to ensure that the maximum data value isn't squeezed right up to the top of the chart.
It would be better, but much more difficult to code, to calculate the space required by the labels first and then automatically set the upper margin to an appropriate value.
Regards,
DG.