JFreeChart AttributedString: how to get axis Label ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
AjitPS
Posts: 49
Joined: Tue Oct 22, 2013 10:08 am
antibot: No, of course not.
Location: UK

JFreeChart AttributedString: how to get axis Label ?

Post by AjitPS » Wed Jan 29, 2014 6:05 pm

Hi,
Does anyone know how to retrieve text content from an AttributedString ?

I'm using the new JFreeChart-FSE API and it uses AttributedString instead of String for the RangeAxis labels. I have set unique labels for the x and y-axis, e.g., my y-axis label is set to "Value (y)". How do I retrieve the text labels, i.e, the string "Value (y)" from the y-axis (i.e., the RangeAxis) ?

The plot.getRangeAxis().getLabel() method now returns an AttributedString and I do not know how to iterate through it and retrieve the label text.

Please help me out.
Thanks.

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

Re: JFreeChart AttributedString: how to get axis Label ?

Post by remiohead » Wed Jan 29, 2014 10:03 pm


AjitPS
Posts: 49
Joined: Tue Oct 22, 2013 10:08 am
antibot: No, of course not.
Location: UK

Re: JFreeChart AttributedString: how to get axis Label ?

Post by AjitPS » Thu Jan 30, 2014 10:23 am

Thanks for the docs.
However, I went through them before posting this question as I still cannot figure out how to get the exact String that is being used in my y-axis' Label. I can get the first character of the "text" attribute using the

Code: Select all

AttributedString.getIterator().current()
or get the begin and end indexes but do not know how to get the entire text attribute as a String.
If you have any information about this, do let me know.
Thanks.

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

Re: JFreeChart AttributedString: how to get axis Label ?

Post by david.gilbert » Thu Jan 30, 2014 10:45 am

It's an iterator, so use it to iterate over the AttributedString:

Code: Select all

import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.text.CharacterIterator;

public class AttributedStringTest {

    public static void main(String[] args) {
        AttributedString s = new AttributedString("Hello World");
        StringBuilder sb = new StringBuilder();
        AttributedCharacterIterator iterator = s.getIterator();
        char c = iterator.first();
        while (c != CharacterIterator.DONE) {
            sb.append(c);
            c = iterator.next();
        }
        System.out.println(sb.toString());
    }
}
David Gilbert
JFreeChart Project Leader

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

AjitPS
Posts: 49
Joined: Tue Oct 22, 2013 10:08 am
antibot: No, of course not.
Location: UK

Re: JFreeChart AttributedString: how to get axis Label ?

Post by AjitPS » Thu Jan 30, 2014 11:23 am

Thanks a lot David. That works perfectly.

I was trying to use the .getIterator().getAllAttributes() method of the AttributedString to try and and get all attributes back in a Map (of type <Attribute, Object>) to then iterate over it and get the value for the "text" attribute; but that didn't work.

This works perfectly though.
Thanks.
Last edited by AjitPS on Mon Jul 07, 2014 5:22 pm, edited 1 time in total.

AjitPS
Posts: 49
Joined: Tue Oct 22, 2013 10:08 am
antibot: No, of course not.
Location: UK

Re: JFreeChart AttributedString: how to get axis Label ?

Post by AjitPS » Mon Jul 07, 2014 5:21 pm

Hi David, I have a couple of queries around issues that I'm facing with the AttributedString labels of the domain & range axis. Any feedback from you on this would be very helpful. Thanks. The issues are described here:
http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=116966

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

Re: JFreeChart AttributedString: how to get axis Label ?

Post by david.gilbert » Tue Jul 08, 2014 6:10 am

There is an AttributedStringUtils class in JFreeChart FSE, and it has a toString() method that you can use (maybe I added that after your earlier question?).
David Gilbert
JFreeChart Project Leader

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

Locked