Align Rectangles to bars of BarChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rhobit
Posts: 9
Joined: Wed Apr 02, 2014 8:43 am
antibot: No, of course not.

Align Rectangles to bars of BarChart

Post by rhobit » Wed Apr 02, 2014 8:54 am

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.

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: Align Rectangles to bars of BarChart

Post by remiohead » Wed Apr 02, 2014 12:58 pm

Have you considered Annotations?

http://www.jfree.org/jfreechart/api/jav ... ation.html

You could override the draw method to do additional formatting.

rhobit
Posts: 9
Joined: Wed Apr 02, 2014 8:43 am
antibot: No, of course not.

Re: Align Rectangles to bars of BarChart

Post by rhobit » Wed Apr 02, 2014 1:51 pm

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()

rhobit
Posts: 9
Joined: Wed Apr 02, 2014 8:43 am
antibot: No, of course not.

Re: Align Rectangles to bars of BarChart

Post by rhobit » Wed Apr 02, 2014 2:49 pm

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:

Code: Select all

|||
000
000
Where "|" is a bar and "0" is a rectangle (two lines with rectangle under the bars).

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: Align Rectangles to bars of BarChart

Post by remiohead » Wed Apr 02, 2014 3:46 pm

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

rhobit
Posts: 9
Joined: Wed Apr 02, 2014 8:43 am
antibot: No, of course not.

Re: Align Rectangles to bars of BarChart

Post by rhobit » Thu Apr 03, 2014 1:44 pm

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:

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;
			 
			}
}
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?

rhobit
Posts: 9
Joined: Wed Apr 02, 2014 8:43 am
antibot: No, of course not.

Re: Align Rectangles to bars of BarChart

Post by rhobit » Sun Apr 06, 2014 8:55 am

I'm moving the last question to another topic. Thanks for the help so far. :-)

Locked