Label word warp

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
tal

Label word warp

Post by tal » Wed Mar 31, 2004 8:00 pm

Hi,

First of all let me say great work on the new version, the pie labels are much better the the old ones. My problem now is that the label value is been wrapped because of it's length. Is there a way to control it? I mean I want it to be wrapped, but I want to mark where to break it, is it possible?

Thanks,

Tal

garv
Posts: 127
Joined: Wed Mar 31, 2004 3:44 pm
Location: Amsterdam, The Netherlands

Post by garv » Thu Apr 01, 2004 9:14 am

Have you tried just inserting a \n in the string?

I have no idea if this would work, but it's worth a try... :wink:

tal

Post by tal » Thu Apr 01, 2004 2:39 pm

Yes, I have... I've also tried \n\r and \r\n or just \r.... it doesn't help...

tal

Fixed!

Post by tal » Thu Apr 01, 2004 5:32 pm

Ok, I've found how to do it...

I've add a function to TextUtitlities.java:

public static TextBlock createTextBlock(String text, Font font, Paint paint,
float maxWidth, int maxLines, TextMeasurer measurer) {

TextBlock result = new TextBlock();
result.addLine(text, font, paint);
return result;
}

Then I've changed TextBox constructors to split the string by | char (but I guess some of you might want to use it as a parameter)

now it looks like :

public TextBox(String text) {
this((TextBlock) null);
if (text != null) {
this.textBlock = new TextBlock();

String[] test = text.split("[|]");
for(int i = 0 ; i < test.length ; i++)
this.textBlock.addLine(test, new Font("SansSerif", Font.PLAIN, 10), Color.black);

}
}

public TextBox(TextBlock block) {
java.util.List lines = block.getLines();
this.textBlock = new TextBlock();
for(int i = 0 ; i < lines.size() ; i++){
String text = ((TextLine)lines.get(i)).toString();
String[] test = text.split("[|]");
for(int j = 0 ; j < test.length ; j++)
if(test[j] != null) this.textBlock.addLine(test[j], new Font("SansSerif", Font.PLAIN, 10), Color.black);
}
this.outlinePaint = Color.black;
this.outlineStroke = new BasicStroke(1.0f);
this.interiorGap = new Spacer(Spacer.ABSOLUTE, 3.0, 1.0, 3.0, 1.0);
this.backgroundPaint = new Color(255, 255, 192);
this.shadowPaint = Color.gray;
this.shadowXOffset = 2.0;
this.shadowYOffset = 2.0;
}

Thats it, now you only need to put the name of the section with the | char where you would like it to break (take notice that you still have limitation on the textbox size).

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 Apr 02, 2004 6:07 am

I'd like to implement this by modifying the createTextBlock() method in the TextUtilities class and have it recognise '\n' characters.
David Gilbert
JFreeChart Project Leader

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

tal

Post by tal » Fri Apr 02, 2004 3:46 pm

This would be great! It much better then "|", more logic...

rkhlin
Posts: 10
Joined: Tue Apr 06, 2004 1:12 am

with no wrap version

Post by rkhlin » Tue Apr 06, 2004 1:49 am

Hi David,

It is good to have \n to control the wrapping.
Also if \n is not provided, please make it no wrap please.
Cause we are developer not designer. You know how wierd they are in insisting certain look and feels.

Cheers,
Richard :wink:

askex2056
Posts: 57
Joined: Tue Aug 05, 2003 3:16 pm
Location: Germany, Munich

Post by askex2056 » Thu Jul 01, 2004 3:25 pm

Hi, i would suggest:

in
org.jfree.text.TextUtilities

Code: Select all

    public static TextBlock createTextBlock(final String text,
                                            final Font font,
                                            final Paint paint,
                                            final float maxWidth,
                                            final int maxLines,
                                            final TextMeasurer measurer) {
        final TextBlock result = new TextBlock();
        if(text != null && text.indexOf('\n') >= 0)
        {
            int start = 0;
            int index = text.indexOf('\n');
            do
            {
                result.addLine(text.substring(start, index), font, paint);
                start = index +1;
                index = text.indexOf('\n', start);
            }
            while(index >= 0);
            
            // add the rest of the text behind the last \n as last line
            if(start < text.length())
                result.addLine(text.substring(start), font, paint);
        }
        else
        {
            final BreakIterator iterator = BreakIterator.getLineInstance();
            iterator.setText(text);
            int current = 0;
            int lines = 0;
            final int length = text.length();
            while (current < length && lines < maxLines) {
                final int next = nextLineBreak(text, current, maxWidth, iterator, measurer);
                if (next == BreakIterator.DONE) {
                    result.addLine(text.substring(current), font, paint); 
                    return result;
                }
                result.addLine(text.substring(current, next), font, paint);
                lines++;
                current = next;
            }
        }
        return result;
    }
instead of

Code: Select all

    public static TextBlock createTextBlock(final String text,
                                            final Font font,
                                            final Paint paint,
                                            final float maxWidth,
                                            final int maxLines,
                                            final TextMeasurer measurer) {
        final TextBlock result = new TextBlock();
        final BreakIterator iterator = BreakIterator.getLineInstance();
        iterator.setText(text);
        int current = 0;
        int lines = 0;
        final int length = text.length();
        while (current < length && lines < maxLines) {
            final int next = nextLineBreak(text, current, maxWidth, iterator, measurer);
            if (next == BreakIterator.DONE) {
                result.addLine(text.substring(current), font, paint); 
                return result;
            }
            result.addLine(text.substring(current, next), font, paint);
            lines++;
            current = next;
        }
        return result;
    }

Ok to go?
Regards,

Andreas Schroeder

Locked