setSeriesVisibleInLegend not working on a XY Area Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
hemanth228
Posts: 3
Joined: Thu Apr 09, 2015 10:10 pm
antibot: No, of course not.

setSeriesVisibleInLegend not working on a XY Area Chart

Post by hemanth228 » Thu Apr 09, 2015 10:30 pm

Hi Everyone,
I have a Multiaxis chart with one of them being a XY Line Chart with 5lines and the second being a XY Area chart with 2 areas. The requirement is to not show the legend on the XYArea chart and only show the legends coming from the XYline chart.

When I use the setSeriesVisibleInLegend(i, Boolean.FALSE) on the XY Area chart, it somehow still shows the legend for the area chart, but when I use the setSeriesVisibleInLegend(i, Boolean.FALSE) on the XYLine chart it hides that legend.

Below is sample code:
XYPlot plot = chart.getXYPlot();
XYItemRenderer renderer = chart.getXYPlot().getRenderer();
LegendItemCollection legendItemsOld = plot.getLegendItems();
---------- I put i=6 referring to the area chart and This doesn't work.
for(int i = 0; i< legendItemsOld.getItemCount(); i++)
{
if (i==6)
{ plot.getRenderer().setSeriesVisibleInLegend(i, Boolean.FALSE); }
}
---------- I put i = 0,1,2,3 or 4 referring to the line chart and it works and hides the legend for the XY Line chart
for(int i = 0; i< legendItemsOld.getItemCount(); i++){
if((i ==4 )){
plot.getRenderer().setSeriesVisibleInLegend(i, Boolean.FALSE);
}
}

Can anyone please suggest me on this issue? will the plot.getRenderer().setSeriesVisibleInLegend(i, Boolean.FALSE) work on a XY Area chart?

Thanks in advance.

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: setSeriesVisibleInLegend not working on a XY Area Chart

Post by Naxter » Fri Apr 10, 2015 8:46 am

Did you debug through and checked if your "if-statement" -> "if (i==6)" is true somewhen?

hemanth228
Posts: 3
Joined: Thu Apr 09, 2015 10:10 pm
antibot: No, of course not.

Re: setSeriesVisibleInLegend not working on a XY Area Chart

Post by hemanth228 » Fri Apr 10, 2015 2:16 pm

Yes Naxter, it goes into the loop for i==6 and even then doesn't work.

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

Re: setSeriesVisibleInLegend not working on a XY Area Chart

Post by paradoxoff » Fri Apr 10, 2015 5:34 pm

A look in the source of both XYAreaRenderer and XYAreaRenderer2 suggests that they honor the seriesVisible flag.
I assume the error has the following reason:
The return value of plot.getLegendItems() is normally the sum of all series in all datasets. When you have two datasets in your plot, each with three series, then plot.getLegendItems() should return 6.
If you want to hide the first series of the second dataset, then you need to change the visibility flag on the renderer that is responsible for the second dataset. You should get a reference to this renderer by calling

Code: Select all

plot.getRenderer(1)
To hide the first series of the second dataset, try

Code: Select all

plot.getRenderer(1).setSeriesVisibleInLegend(0, Boolean.FALSE)

hemanth228
Posts: 3
Joined: Thu Apr 09, 2015 10:10 pm
antibot: No, of course not.

Re: setSeriesVisibleInLegend not working on a XY Area Chart

Post by hemanth228 » Fri Apr 10, 2015 8:07 pm

Thanks alot paradoxoff, that worked for me. I didn't know that we can call plot.getRenderer(1).

Locked