How to change font of legend in bar chart?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
chausberger
Posts: 7
Joined: Sat Sep 01, 2007 4:19 pm

How to change font of legend in bar chart?

Post by chausberger » Mon May 19, 2008 11:59 am

Hello

I have a bar chart displaying five products and their market share.
Below the bars there is the legend with the product names (in black).

How can I change the color of the product names and the font size?
I searched the documentation and developer guide and couldn't find an easy solution.
Maybe I've just overlooked it.

Claus

kruthin
Posts: 5
Joined: Fri May 23, 2008 11:29 am

change legend font size and style

Post by kruthin » Fri May 23, 2008 11:36 am

create a legend object as shown
StandardLegend legend = (StandardLegend) chart.getLegend();
and create a font object as shown

Font nwfont=new Font("Arial",0,7);
and assign it to legend obj and add ito chart as shown
legend.setItemFont(nwfont);
chart.setLegend(legend);

the complete code below
StandardLegend legend = (StandardLegend) chart.getLegend();
Font nwfont=new Font("Arial",0,7);
legend.setItemFont(nwfont);
chart.setLegend(legend);
kruthin

samtuen
Posts: 5
Joined: Mon Jun 30, 2008 2:00 pm
Location: Germany

Post by samtuen » Thu Aug 14, 2008 11:01 am

hi kruthin,

there isn't the class StandardLegend. I can not found it in API.
I use the following code:

Code: Select all

LegendTitle legend = chart.getLegend();
Font labelFont = new Font("Arial", Font.BOLD, 12);
legend.setItemFont(labelFont);
It appears NullPointerException. Do you know why?

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 » Thu Aug 14, 2008 8:48 pm

samtuen wrote:It appears NullPointerException. Do you know why?
NullPointerExceptions are usually pretty easy to track down - does the full stack trace give you a line number and a source file name?

If I had to guess (and I do, because you didn't give enough information) I'd say that the getLegend() method is returning 'null' because you created a chart with no legend.
David Gilbert
JFreeChart Project Leader

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

leyla
Posts: 1
Joined: Thu Aug 14, 2008 10:47 pm

Unable to change the legend font on a bar chart

Post by leyla » Thu Aug 14, 2008 10:55 pm

Hi,
I am using jfreechart to create a bar chart. I am unable to change the font of the legend though. This is my code:

LegendTitle legend = new LegendTitle(plot);
Font font = new Font("Arial",0,12);
legend.setItemFont(font);
legend.setPosition(RectangleEdge.BOTTOM);
chart.addLegend(legend);

I looked under jre/lib/fonts and used the fonts that are in that directory and that didn't seem to work either. Is there anything wrong with the code, or is there any thing else I have to do like installing the fonts etc,..? thanks!

samtuen
Posts: 5
Joined: Mon Jun 30, 2008 2:00 pm
Location: Germany

Post by samtuen » Fri Aug 15, 2008 1:44 pm

Hi david,

you're right. Thank you!

Hi leyla,

try to change

Code: Select all

LegendTitle legend = new LegendTitle(plot.getRenderer()); 

Locked