How to wrap text inside of a label

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
darrellu
Posts: 3
Joined: Wed Jan 25, 2017 8:45 pm
antibot: No, of course not.

How to wrap text inside of a label

Post by darrellu » Sat Jan 28, 2017 12:18 am

I have created a label for the following marker and need to wrap the text inside of the label. Can someone send me a snippet of code which shows me how to accomplish this?

Code: Select all

CategoryMarker target = new CategoryMarker("A", Color.red.darker(), new BasicStroke(2.0F));
target.setDrawAsLine(true); 
target.setLabel("Target Primary Completion Date"); 
target.setLabelFont(new Font("SansSerif", 0, 8));
target.setLabelPaint(Color.red.darker());
target.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
target.setLabelTextAnchor(TextAnchor.TOP_LEFT);
plot.addDomainMarker(target);

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

Re: How to wrap text inside of a label

Post by paradoxoff » Sat Jan 28, 2017 11:44 am

There is no easy way to achieve this.
Markers do not have a custom draw-method, but are drawn by the renderer. In order to change anything regarding the rendering of a marker, you will have to overwrite the drawDomainMarker-method of all the renderers that you want to use. In order to avoid this, I have written some annotation classes that can be used instead of markers (link to thread).
The equivalent of a CategoryMarker (a CategoryDomainAnnotation) still does not support mutiline text. I am currently working on a hack to integrate that.
[Edit] Done. The annotation classes have a common superclass (AxisAnnotation) that draws the label. I have uploaded a new version of AxisAnnotation to the patch tracker that is using a TextBlock instead of a plain String for the label and thus supports mutiline labels.
link to patch.

Locked