Font style : can JFreeChart display underlined font ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lbalogh
Posts: 4
Joined: Wed Jan 18, 2006 9:52 am

Font style : can JFreeChart display underlined font ?

Post by lbalogh » Wed Jan 18, 2006 9:57 am

Hello,

I need to underline the title of my charts, so I tried the following code :

Map m = new HashMap();
m.put(TextAttribute.SIZE, new Float(30));
m.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);

Font font = new Font(m);
myGraph.setTitleFont(font);

but it doesn't seem to work. The font has the right size, but is not underlined... is there a solution for this ?

Thanks

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 » Wed Jan 18, 2006 10:50 am

There's nothing in JFreeChart 1.0.0 that will support that. There is an AttributedTextTitle class in CVS that you could mess about with - I'm not even sure it will compile with 1.0.0, but it shouldn't be that hard to get working:

http://cvs.sourceforge.net/viewcvs.py/j ... iew=markup
David Gilbert
JFreeChart Project Leader

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

lbalogh
Posts: 4
Joined: Wed Jan 18, 2006 9:52 am

Post by lbalogh » Wed Jan 18, 2006 4:21 pm

Ok, I made a quick-and-dirty solution that works :

in TextUtilities.java, drawRotatedString method (line 539), I modified the code :

Code: Select all

if (useDrawRotatedStringWorkaround) {
  (...)
}
else
  g2.drawString(text, textX, textY);
to :

Code: Select all

if (useDrawRotatedStringWorkaround) {
  (...)
}
else {
  if (g2.getFont().getAttributes().get(TextAttribute.UNDERLINE) == TextAttribute.UNDERLINE_ON) {
    AttributedString as = new AttributedString(text);
    as.addAttribute(TextAttribute.FONT, g2.getFont());
    as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, text.length());

    g2.drawString(as.getIterator(), textX, textY);
  }
  else
    g2.drawString(text, textX, textY);
}

lbalogh
Posts: 4
Joined: Wed Jan 18, 2006 9:52 am

Post by lbalogh » Wed Jan 18, 2006 5:03 pm

Here is a better solution :

change

Code: Select all

if (useDrawRotatedStringWorkaround) {
  (...)
}
else
  g2.drawString(text, textX, textY);
to

Code: Select all

if (useDrawRotatedStringWorkaround) {
  (...)
}
else {
  AttributedString as = new AttributedString(text, g2.getFont().getAttributes());
  g2.drawString(as.getIterator(), textX, textY);
}
With this modification, fonts can have all supported styles (underline, strikeout...).

mickish
Posts: 29
Joined: Tue Jan 08, 2008 11:15 pm
Location: Venice, Florida

Post by mickish » Tue Jan 15, 2008 12:02 am

Thanks for the tip.

I have submitted an official patch for TextUtilities.java

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 » Tue Jan 15, 2008 11:33 am

That looks pretty good...thanks for digging it up!
David Gilbert
JFreeChart Project Leader

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

itai.marko
Posts: 10
Joined: Mon Jun 22, 2009 2:42 pm

Re: Font style : can JFreeChart display underlined font ?

Post by itai.marko » Sun Jul 05, 2009 8:12 am

Is there a reason why this was not included in the JCommon 1.0.16 release?
Will it be released in the next version?

mickish
Posts: 29
Joined: Tue Jan 08, 2008 11:15 pm
Location: Venice, Florida

Re: Font style : can JFreeChart display underlined font ?

Post by mickish » Sun Jul 26, 2009 6:37 pm

I have been using this patch continuously for a year and a half, and it seems solid.

It would be a big help to commit this patch to the main trunk and to the 1.0.x branch!

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

Re: Font style : can JFreeChart display underlined font ?

Post by david.gilbert » Mon Jul 27, 2009 10:51 am

OK, I've committed this to JCommon (for inclusion with the next release of JFreeChart, 1.0.14). And also to the trunk in Subversion for inclusion in JFreeChart 1.2.0.
David Gilbert
JFreeChart Project Leader

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

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

Re: Font style : can JFreeChart display underlined font ?

Post by david.gilbert » Thu Oct 24, 2013 9:54 am

It turns out that this incurs quite a high cost for JFreeChart output to SVG and PDF formats (using JFreeSVG and OrsonPDF), because all the strings are being pushed through TextLayout and rendered to SVG/PDF as vector output. So for JCommon 1.0.21 I will add a flag to enable/disable this feature, and default it to false.
David Gilbert
JFreeChart Project Leader

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

Locked