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,
legend -> visible/invisible
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: legend -> visible/invisible
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.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?
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
}
-
- Posts: 47
- Joined: Thu Dec 11, 2008 7:59 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();
}
}
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Try
Since LegendTitle extends Title this method is availabe for a legend as well.
This method was introduced in 1.0.11 (IIRC).
Code: Select all
Title.setVisible(false)
This method was introduced in 1.0.11 (IIRC).
-
- Posts: 47
- Joined: Thu Dec 11, 2008 7:59 pm