How to add a title to a Legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
opeongo
Posts: 10
Joined: Thu Oct 18, 2007 2:46 pm

How to add a title to a Legend

Post by opeongo » Tue Jan 24, 2017 9:27 pm

I would like to a a title to a legend. I have looked through the API but I cannot figure it out.

The LegendTitle class has a BlockContainer object to hold the legend items, and also possibly a BlockContainer wrapper. I could add a TextTitle to one of those BlockContainers, but how do I specifier that it should be centered at the top?

This answer on StackExchange gives a solution, but it seems a bit like a lucky guess. Is this really the way to do it?

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

Re: How to add a title to a Legend

Post by paradoxoff » Tue Jan 24, 2017 10:21 pm

You can extend LegendTitle and override the fetchLegendItems method. Modify the method that first a LabeBlock with the desired text is added, and then the regular item blocks.
The alignment might be harder to implement. The Arrangement classes FlowArrangement and ColumnArrangement contain a couple of ToDos, most of tem in relation to the horizontal and vertical alignment.

Locked