Multiline XYTextAnnotation

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
truthsense
Posts: 21
Joined: Thu Sep 29, 2005 4:11 pm

Multiline XYTextAnnotation

Post by truthsense » Wed Jan 04, 2006 5:09 pm

I want to be able to use line wrapping/new lines in XYAnnotations. I want to add statistical annotation options for the user in our application that could be 5-6 lines; however, using html or newline characters in the text doesn't work.

Is adding multiline tooltips currently a possibility, or would I have to work out a way to add another XYAnnotation for each line while adding or subtracting a certain number of pixels?

Allamistakeo

Post by Allamistakeo » Thu Jan 05, 2006 9:46 am

Newline characters don't work indeed, but HTML code works fine with me:

Code: Select all

<html><body>first line<br>second line</body></html>

truthsense
Posts: 21
Joined: Thu Sep 29, 2005 4:11 pm

Post by truthsense » Thu Jan 05, 2006 9:05 pm

Thanks Allamistakeo.

I used the string you gave in order to test JFreeChart but when I add a new XYTextAnnotation("<html><body>first line<br>second line</body></html>", x, y); the string is placed exactly as stated on the chart. No formatting.

Is this version specific, or do you have to set a specific property? I am using 1.0 RC2

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

Post by david.gilbert » Fri Jan 06, 2006 5:35 pm

I think the HTML will work in a tooltip, but not the XYTextAnnotation. It probably wouldn't be too hard to modify XYTextAnnotation to use multiple lines. Patches are welcome, as long as they maintain backwards compatibility of course.
David Gilbert
JFreeChart Project Leader

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

snid3ly
Posts: 23
Joined: Wed Aug 07, 2013 4:21 am
antibot: No, of course not.

Re: Multiline XYTextAnnotation

Post by snid3ly » Sat Apr 19, 2014 8:43 pm

Did anyone ever fix this?

I need to pass in multiline Labels to XYPointerAnnotation.

gliesian
Posts: 6
Joined: Tue Dec 03, 2013 9:50 pm
antibot: No, of course not.

Re: Multiline XYTextAnnotation

Post by gliesian » Mon Jul 14, 2014 5:15 pm

I'm trying to do this too... any resolution or answers here?

Thanks,
Robert

dkrider
Posts: 1
Joined: Fri Dec 19, 2014 8:50 pm
antibot: No, of course not.

Re: Multiline XYTextAnnotation

Post by dkrider » Fri Dec 19, 2014 8:55 pm

I messed around with this for a while before I finally came across XYTitleAnnotation. This will place a multi-line annotation in the upper left corner of the plot.

Code: Select all

TextTitle textTitle = new TextTitle("first line\nsecond line", new Font(Font.SANS_SERIF, Font.PLAIN, 8), Color.BLACK, RectangleEdge.TOP, HorizontalAlignment.LEFT,
            		VerticalAlignment.TOP, new RectangleInsets(2, 2, 2, 2));
XYTitleAnnotation xyTitleAnnotation = new XYTitleAnnotation(0, 1, textTitle, RectangleAnchor.TOP_LEFT);
            
plot.addAnnotation(xyTitleAnnotation);

ricochet99
Posts: 8
Joined: Tue Jun 23, 2015 2:37 pm
antibot: No, of course not.

Re: Multiline XYTextAnnotation

Post by ricochet99 » Thu Jun 25, 2015 9:02 am

I found this class yesterday (https://github.com/JeremyMcCormick/jfre ... ation.java) and added TextAnchor support in a fork:

https://github.com/rivella50/jfreechart ... ation.java

Works perfectly for me:
Image

Slaysn
Posts: 1
Joined: Fri Jul 29, 2016 7:53 am
antibot: No, of course not.

Re: Multiline XYTextAnnotation

Post by Slaysn » Fri Jul 29, 2016 9:29 am

Hi,

had the same problem. Solved it by adding an annotation for each line manually. Here is a quick fix:

Code: Select all

private void addMultilineTextAnnotations(double x, double y, String label, Color color) {
        String[] lines = label.split("\n");
        for(int i=0; i < lines.length;  i++){
            XYTextAnnotation annotationLabel = new XYTextAnnotation(lines[i], 
                    x,  y - (i+1) * XYTextAnnotation.DEFAULT_FONT.getSize());
            annotationLabel.setPaint(color);
            plot.addAnnotation(annotationLabel);
        }
}
Seems to work fine for me.

Cheers,

Locked