legend -> visible/invisible

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

legend -> visible/invisible

Post by PollerJava » Fri Jan 16, 2009 11:01 am

Hello,

I have a problem with the legend in the JFreechart, so when I zoom into the chart I will
1. reload data from my database
2. series.clear(); (TimeSeries series) so the lines remove from the chart
3. clear the legend -> how can I do this -> if I invoke chart.removeLegend(); than the legend is removed but if I fill a series once again with data, the legend never appears (because it is removed)

How can I solve the problem?

Thank a lot,

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

Re: legend -> visible/invisible

Post by paradoxoff » Fri Jan 16, 2009 1:26 pm

PollerJava wrote: I have a problem with the legend in the JFreechart, so when I zoom into the chart I will
1. reload data from my database
2. series.clear(); (TimeSeries series) so the lines remove from the chart
3. clear the legend -> how can I do this -> if I invoke chart.removeLegend(); than the legend is removed but if I fill a series once again with data, the legend never appears (because it is removed)
How can I solve the problem?
I have run into a similar issue. Normally, the items in the legend reflect what is seen in the plot. If you are using the default legend (which is backed by the plot as LegendItemSource) you will get one legend symbol for every series in every dataset. Hiding of series is possible (either in the plot and the legend or in the legend only) but that needs to be done explicitly by the user.
If you remove a series from a dataset or a dataset from the plot, the legend symbols will also disappear.
However, if you are only removing the items from a series (or, generally speaking, when you have series in your dataset with an item count of 0) they of course will not show up in the plot (because there is no data to show) but they will appear in the legend (because the series itself is still there, though it is empty).
I have solved that by adding a flag emptySeriesVisibleInLegend to the XYPlot class that is checked in its getLegendItems() method. For every series, it is not only checked whether renderer.isSeriesVisible(i) is true and renderer.isSeriesVisibleInLegend(i) is true but also if emptySeriesVisibleInLegend is true or dataset.getItemCount(i) > 0.

Code: Select all

if (renderer.isSeriesVisible(i) && renderer.isSeriesVisibleInLegend(i)
&& (dataset.getItemCount(i) > 0 || emptySeriesVisibleInLegend) ) {
//construct legend item and append to LegendItemCollection
}
If you do so, you can keep the LegendTitle alive.

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Fri Jan 16, 2009 3:52 pm

Is there a way to simply toggle the Legend on or off? In my program the user needs to be able to show or hide the legend. Hiding can be accomplished by chart.removeLegend(); but that completely removes the legend. To get the legend back, you need to recreate it, which is what I'm doing now.

Code: Select all

public void setLegendVisible(boolean showLegend)
{
	boolean legendExists = (chart.getLegend() == null) ? false : true;
	if (!legendExists && showLegend)
	{
		// Recreate Legend
		// Copied from JFreeChart source
		LegendTitle legend = new LegendTitle(chart.getPlot());
		legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
		legend.setFrame(new LineBorder());
		legend.setBackgroundPaint(Color.white);
		legend.setPosition(RectangleEdge.BOTTOM);
		chart.addLegend(legend);
		// End copy
	}
	else if (legendExists && !showLegend)
	{
		chart.removeLegend();
	}
}

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

Post by paradoxoff » Fri Jan 16, 2009 5:58 pm

Try

Code: Select all

Title.setVisible(false)
Since LegendTitle extends Title this method is availabe for a legend as well.
This method was introduced in 1.0.11 (IIRC).

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Fri Jan 16, 2009 8:22 pm

Thank you paradoxoff! You're absolutely correct.

Locked