Hi,
I have to draw a BarChart with a dynamically varying amount of bars (e.g. my CategoryDataset contains a varying amount of keys). In addition I have to place a filled rectangle (e.g. from Graphics2D) containing some numbers under each bar. Is there a simple way to to this? Labels must be Strings, so they can't be used apparently. Can I draw the chart, ask for the width and center coordinates of each bar, then draw some rectangles? How does this conflict with resizing? I tried for like two weeks to solve this, without success. Anybody got a clue? I would appreciate it.
Cheers.
Align Rectangles to bars of BarChart
Re: Align Rectangles to bars of BarChart
Have you considered Annotations?
http://www.jfree.org/jfreechart/api/jav ... ation.html
You could override the draw method to do additional formatting.
http://www.jfree.org/jfreechart/api/jav ... ation.html
You could override the draw method to do additional formatting.
Re: Align Rectangles to bars of BarChart
Hi,
thanks, I will try that. But to draw an appropriate sized rectangle I need to get the width of the bars somehow... Is that even possible? Can I deduct this information somehow?
*EDIT*
I got it; I can call getCategoryStart(...) and getCategoryEnd()
thanks, I will try that. But to draw an appropriate sized rectangle I need to get the width of the bars somehow... Is that even possible? Can I deduct this information somehow?
*EDIT*
I got it; I can call getCategoryStart(...) and getCategoryEnd()
Re: Align Rectangles to bars of BarChart
Remaining issue: I need to place these rectangles below my Bars. But as in topic
http://www.jfree.org/phpBB2/viewtopic.php?t=9029
one cannot annotate outside of a plot and my rectangles are cut of at the lower plot border. Can I extend the plot area at the lower end somehow?
I need something like:
Where "|" is a bar and "0" is a rectangle (two lines with rectangle under the bars).
http://www.jfree.org/phpBB2/viewtopic.php?t=9029
one cannot annotate outside of a plot and my rectangles are cut of at the lower plot border. Can I extend the plot area at the lower end somehow?
I need something like:
Code: Select all
|||
000
000
Re: Align Rectangles to bars of BarChart
As David said in the linked post, you'll most likely need to override the draw() method of the JFreeChart class to accomplish this.
Note that this method allows you to provide a ChartRenderingInfo object. You can use this to get the position of various entities drawn, including the bars. You'll need to iterate over the EntityCollection object to do this. For example, information about bars is contained within a CategoryItemEntity, created for each bar drawn.
http://www.jfree.org/jfreechart/api/jav ... gInfo.html
http://www.jfree.org/jfreechart/api/jav ... ction.html
http://www.jfree.org/jfreechart/api/jav ... ntity.html
Note that this method allows you to provide a ChartRenderingInfo object. You can use this to get the position of various entities drawn, including the bars. You'll need to iterate over the EntityCollection object to do this. For example, information about bars is contained within a CategoryItemEntity, created for each bar drawn.
http://www.jfree.org/jfreechart/api/jav ... gInfo.html
http://www.jfree.org/jfreechart/api/jav ... ction.html
http://www.jfree.org/jfreechart/api/jav ... ntity.html
Re: Align Rectangles to bars of BarChart
Apparently I found an easy solution to draw annotations outside of the data-area. I extended the CategoryPlot class and just removed
g2.clip(dataArea);
inside the draw(...) method. However, to use my new class (lets call it CategoryPlotExtension) I had to generate this Plot. I reused the createStackedBarChart-method from Chart-Factory:
But I can't change the background-color anymore - it is always grey with some horizontal dotted lines. Why? I only changed CategoryPlot to CategoryPlotExtension. Even if I change it back to CategoryPlot, it is the same situation. But I only copied from ChartFactory. Anybody got a clue?
g2.clip(dataArea);
inside the draw(...) method. However, to use my new class (lets call it CategoryPlotExtension) I had to generate this Plot. I reused the createStackedBarChart-method from Chart-Factory:
Code: Select all
public class ChartFactoryExtension {
public static JFreeChart createStackedBarChart(String title,
String domainAxisLabel,
String rangeAxisLabel,
CategoryDataset dataset,
PlotOrientation orientation,
boolean legend,
boolean tooltips,
boolean urls) {
if (orientation == null) {
throw new IllegalArgumentException("Null 'orientation' argument.");
}
CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel);
ValueAxis valueAxis = new NumberAxis(rangeAxisLabel);
StackedBarRenderer renderer = new StackedBarRenderer();
if (tooltips) {
renderer.setBaseToolTipGenerator(
new StandardCategoryToolTipGenerator());
}
if (urls) {
renderer.setBaseItemURLGenerator(
new StandardCategoryURLGenerator());
}
//CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,
//renderer);
CategoryPlotExtension plot = new CategoryPlotExtension (dataset, categoryAxis, valueAxis,
renderer);
plot.setOrientation(orientation);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
return chart;
}
}
Re: Align Rectangles to bars of BarChart
I'm moving the last question to another topic. Thanks for the help so far.