Selectively hiding legends

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
anujcb
Posts: 8
Joined: Thu Nov 15, 2007 12:03 am

Selectively hiding legends

Post by anujcb » Thu Jan 03, 2008 9:39 pm

How can the legend be selectively hidden? I have a graph where i plot the same series as bar and as area chart, since both the series are the same i dont want the second series legend to show up is there a way we can make the legend of the second series disappear? Here is the key elements in my code.

Currently as a work around i have my bar chart drawn in white color, which is the background color of the chart, so the shape dont show up, then i set the label for the bar chart series to zero length string and the label dont show up. I wish there was a better way.



//renderer for area
XYAreaRenderer renderer =new XYAreaRenderer(XYAreaRenderer.AREA_AND_SHAPES, ttg, null);

//renderer for bar
XYItemRenderer dataPointLinesRenderer = new XYBarRenderer(0.01);

//add first series - area chart
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);

//add the second series - bar chart(same data)
plot.setDataset(1,dataPointLinesSeriesCollection);
plot.setRenderer(1,dataPointLinesRenderer);

//legend for both the series shows up we need only one
JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

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 Jan 03, 2008 11:05 pm

Yes, you can do this. The legend (an instance of LegendTitle) gets the items for display from a LegendItemSource (an interface that is implemented by all plots and all renderers). The default legend uses the plot as the LegendItemSource, and this fetches items from all the renderers that belong to the plot. But if you instead create a chart with no (default) legend, then you can manually add a legend that uses just one of the renderers as the LegendItemSource.

I hope that makes sense...
David Gilbert
JFreeChart Project Leader

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

Locked