Adding Text or Title to a Subchart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
cobragtk
Posts: 11
Joined: Fri Jan 23, 2009 7:07 pm

Adding Text or Title to a Subchart

Post by cobragtk » Thu Apr 30, 2009 6:40 pm

Hi all,
I am using a CombinedDomainXYPlot for my chart. I add 4 subplots to this. Can anyone tell me how I can add some text to the top left corner of each of the subplots? Essentially I want to have separate titles for each subplot. I have posted a picture below.
Thank you.

Image

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Adding Text or Title to a Subchart

Post by paradoxoff » Fri May 01, 2009 1:38 pm

You could try to add an XYTitleAnnotation to each of the subplots, using 0.0 for both the x and y coordinate and RectangleAnchor.TOP_LEFT as anchor. Though you won´t be able to set the XYCoordinateType (neither in the constructor nor by a setter), the default XYCoordinateType.RELATIVE should do what you want.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Adding Text or Title to a Subchart

Post by david.gilbert » Mon May 04, 2009 3:31 pm

paradoxoff wrote:You could try to add an XYTitleAnnotation to each of the subplots, using 0.0 for both the x and y coordinate and RectangleAnchor.TOP_LEFT as anchor. Though you won´t be able to set the XYCoordinateType (neither in the constructor nor by a setter), the default XYCoordinateType.RELATIVE should do what you want.
I think my original intention with the coordinate type was to enable annotations to be attached to "data" items, so that they would follow the data item if its value changed. I should go back and implement that some time.
David Gilbert
JFreeChart Project Leader

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

cobragtk
Posts: 11
Joined: Fri Jan 23, 2009 7:07 pm

Re: Adding Text or Title to a Subchart

Post by cobragtk » Mon May 04, 2009 11:04 pm

Hi guys,
Paradoxoff, thank you very much for your solution. It worked perfectly with the following tweak, basically the y coordinate I set to 1.0.

Code: Select all

public void addTitle(String text, Color color) {
TextTitle title = new TextTitle(text);
title.setPaint(color);
title.setPadding(2, 5, 0, 0);
XYTitleAnnotation ta = new XYTitleAnnotation(0.0, 1.0, title, RectangleAnchor.TOP_LEFT);
plot.addAnnotation(ta);
}
So Dave - does this mean that my code would not work if you implement the attaching of annotations to data items?

Thank you.
Simon

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Adding Text or Title to a Subchart

Post by david.gilbert » Fri May 08, 2009 7:47 am

Your code will still work. Attaching an annotation to a data item would be an additional option, not a replacement of the existing feature.
David Gilbert
JFreeChart Project Leader

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

Locked