How to add a source to a legend?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
tedbyers
Posts: 50
Joined: Fri Oct 12, 2007 7:05 pm

How to add a source to a legend?

Post by tedbyers » Wed Jul 30, 2008 9:18 pm

I create my legend as follows:

Code: Select all

org.jfree.chart.title.LegendTitle aLegend = 
                    new org.jfree.chart.title.LegendTitle(ReturnsPlot);
And then I work with it.

This is for a combo lot, which is created as follows.

Code: Select all

            org.jfree.chart.plot.CombinedDomainXYPlot cplot = new org.jfree.chart.plot.CombinedDomainXYPlot(domainAxis);
            cplot.add(PortfolioValuePlot);
            cplot.add(ReturnsPlot);
ReturnsPlot uses two different renderers, if that matters.

Initially, the first of the plots had only one item, and a different perspective of it was provided in the second plot, along with several other series.

Now I need to add a second series to the first plot which does not get represented in the second plot. In fact, it is already added to the chart, but it looks odd because it is not represented in the legend (which ought to represent both charts). How do I manually add an item to the legend to represent this second series in the first plot?

Thanks

Ted

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 Jul 31, 2008 3:21 pm

You can only specify one LegendItemSource, which will be either one of the subplots, or the parent plot. If you want to include all of the series from one subplot PLUS a subset of the series from another subplot, then you'll have to either use the setSeriesVisibleInLegend() method to hide the series you don't want in the legend, or write a custom LegendItemSource that refers to the two subplots and picks out only the series names you want.
David Gilbert
JFreeChart Project Leader

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

tedbyers
Posts: 50
Joined: Fri Oct 12, 2007 7:05 pm

Post by tedbyers » Thu Jul 31, 2008 9:08 pm

Thanks Dave

Using "setSeriesVisibleInLegend()" on the renderer for the plot of interest did the trick.

If ever I get time, instead of writing a custom LegendItemSource that is specific to a given situation, like mine, I'd look at writing one for combination plots that look at the items in the plots to see if they have the same label, and if they do, make sure they have the same colour in each of the plots and that the label appears only once in the legend. That way, when one creates a combined plot, the user can ensure that a given label appears only once, even though the "object" is plotted in more than one of the sub-plots, albeit in a different perspective, simply by given the relevant series in the sub-plots the same name (so a combined plot that shows stock price in one sub plot and monthly rate of return for that stock in the other subplot, with S&P500 monthly rate of return in the second subplot, would have only one legend entry for the stock if the series added to the first subplot and that added to the second both bear the same name - in fact, that would happen automagically if a map was used to store the legend items text and color). Of course, there's more to it, but ... Alas I won't have the requisite time for a while. :(

But thanks again

Ted

Locked