Legend alignment

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
guyom
Posts: 2
Joined: Mon Jan 19, 2015 12:06 pm
antibot: No, of course not.

Legend alignment

Post by guyom » Mon Jan 19, 2015 2:04 pm

Hi guys,

I try to generate a StackedBarChart, but the legend is too long to be written on just one line and I don't know why the second part of it isn't left aligned. An illustration :

Image

We can see that " souscrites en janvier" is centered although I set :
legendTitleNew.setHorizontalAlignment(HorizontalAlignment.LEFT);


I need you're help :cry:


Thanks !

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

Re: Legend alignment

Post by david.gilbert » Mon Jan 19, 2015 11:15 pm

I think there is no API method to fix this. The LegendTitle class is using a LabelBlock object to represent the label, and within this class a TextBlock is used. The TextBlock has a method setLineAlignment() which you could use...unfortunately it is not nicely exposed by the API. I suggest you try setting it within the LabelBlock constructor and see if it has the desired effect - something like this:

this.label = TextUtilities.createTextBlock(text, font, this.paint);
this.label.setLineAlignment(HorizontalAlignment.LEFT);

If that works, then the API needs changing to allow the LegendTitle to pass the required alignment down the chain. I don't have enough time to look at this more closely right now sorry.
David Gilbert
JFreeChart Project Leader

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

guyom
Posts: 2
Joined: Mon Jan 19, 2015 12:06 pm
antibot: No, of course not.

Re: Legend alignment

Post by guyom » Tue Jan 20, 2015 3:13 pm

Thank you David

greydad
Posts: 3
Joined: Wed Aug 08, 2007 11:52 pm

Re: Legend alignment

Post by greydad » Mon Apr 06, 2015 4:20 pm

Hi David,

I have been having the same problem as has many other developers with long legend text and having it centered as a default alignment. The patch to the LabelBlock class works as expected and will place the label text with left alignment within the block.

The company I work for, CA Technologies, would rather use a supplied jar maintained by the developer team than modify the existing API and maintain it ourselves.

Is there any time frame to provide a more general solution since this is an issue that has been around for quite some time? Perhaps a method in the LegendItem class that takes an Alignment parameter and passes it onto the LabelBlock class?

Regards,
Frank Goss

Locked