Trimming long subtitles

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
eladmena
Posts: 9
Joined: Tue Sep 08, 2009 12:19 pm
antibot: No, of course not.

Trimming long subtitles

Post by eladmena » Tue Sep 08, 2009 12:30 pm

Hey
I have a problem with charts with legends, where at least one legend item is very long.
In that case the legends take too much space at the expense of the chart area.
I have tried limiting the legend size, but didn't manage to do it.

Is there a way to automatically cut long labels the same way it is done in axis labels?

example image:
Image

Thanks for your help
Elad

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Trimming long subtitles

Post by paradoxoff » Wed Sep 09, 2009 6:21 am

Text title are among the first things that are laid out and drawn during the rendering of a jJFreeChart. They are arranged in the "available space", and the available space for the first title (regardless of whether it is a LegendTitle or a TextTitle) is more or less the entire chart area. This is less obvious for TextTitles because these are rotated when they are positioned at the left or right edge, but place a TextTitle in a CompositeTitle and place the CompositeTitle at the left or right edge of the chart, and you will also squeeze the plot to a very small size.
A more subtle issue (that is related to your problem) is discussed here and in an earlier thread mentioned there.
The basic idea is to tell a block that it should not request more than x pixels or more than y % of the available space. There is a patch available to achieve this.

eladmena
Posts: 9
Joined: Tue Sep 08, 2009 12:19 pm
antibot: No, of course not.

Re: Trimming long subtitles

Post by eladmena » Wed Sep 09, 2009 2:15 pm

Thanks.
Will this fix be added to the next version?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Trimming long subtitles

Post by paradoxoff » Thu Sep 10, 2009 8:26 pm

I don´t know. At present, the patch is still not assigned.
The additions to the AbstractBlock and JFreeChart classes are trivial, and it should be possible to apply them to a local JFreeChart installation. If you need the functions, I would recommend to try them out!
The uploaded patch also contains changes to the Arrangement classes. The latter are more complex, and I would not classify them as "complete" at present (but I am working on them).
Though I haven´t tried that, I believe that the changes to the FlowArranagement and ColumnArrangement classes should lead to a wrapped LegendItem and not to a truncated one. I just wonder whether additional flags would be useful to indidcate whether a too long Title or, more general, Block should be wrapped or truncated.

eladmena
Posts: 9
Joined: Tue Sep 08, 2009 12:19 pm
antibot: No, of course not.

Re: Trimming long subtitles

Post by eladmena » Sun Sep 13, 2009 9:09 am

I believe truncating is a good way to show long legend labels, wrapping can exceed the height of the legend with long ones.
When truncating, tooltips would be a good idea, so the user can see the full label.

I'll add the changes you mentioned to FlowArranagement class, and add the following lines of code to constrain the legend area:

Code: Select all

  BlockContainer itemContainer = legend.getItemContainer();
  itemContainer.setWidth(0.33);
Thanks for all your help
Elad

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Trimming long subtitles

Post by paradoxoff » Sun Sep 13, 2009 10:01 pm

eladmena wrote:I believe truncating is a good way to show long legend labels, wrapping can exceed the height of the legend with long ones.
When truncating, tooltips would be a good idea, so the user can see the full label.

I'll add the changes you mentioned to FlowArranagement class, and add the following lines of code to constrain the legend area:

Code: Select all

  BlockContainer itemContainer = legend.getItemContainer();
  itemContainer.setWidth(0.33);
I do not believe that the width or height of a BlockContainer or of a Block in general are ever requested during the layout process. AFAIK, the layout process is built on the arrange()-methods.
So I would rather recommend to add the changes to the AbstractBlock and JFreeChart classes, and use a maxWidth of 0.33, a maxWidthUnitType of UnitType.RELATIVE, and set useMaxWidth to true.

The addition to the AbstractBlock and JFreeChart classes will help to restrict the size used for layouting a title to only a fraction of the available chart width and to leave sufficient space to render the plot.
The addition to FlowArrangement that I mentioned will only change how a Block is laid out within the availabe width.
BTW, a LegendTitle at the left or right edge of a chart uses a ColumnArrangement to arrange the legend items. Even if you changed the FlowArrangement class, I will not have an effect.

Concerning the tool tips: the tool tip for a legend item is created by the renderer. See AbstractCategoryItemRenderer.setLegendItemToolTipGenerator(CategorySeriesLabelGenerator generator). The default legendItemToolTipGenerator is null --> no tooltips.

eladmena
Posts: 9
Joined: Tue Sep 08, 2009 12:19 pm
antibot: No, of course not.

Re: Trimming long subtitles

Post by eladmena » Mon Sep 14, 2009 3:23 pm

Thanks a lot for all your help.
I have managed to solve this by creating a LegendItemLabelGenerator which trims the text as needed.

Elad

Locked