JFreeChart: How to hide items from a legend?

I need to hide every second / third / fourth element from the legend. Is there any way to achieve this in jFreeChart? thanks!

+6
jfreechart
source share
2 answers

ok, just did it myself. Thus, I remove every second element from the legend. please leave comments!

LegendItemCollection legendItemsOld = plot.getLegendItems(); final LegendItemCollection legendItemsNew = new LegendItemCollection(); for(int i = 0; i< legendItemsOld.getItemCount(); i++){ if(!(i%2 == 0)){ legendItemsNew.add(legendItemsOld.get(i)); } } LegendItemSource source = new LegendItemSource() { LegendItemCollection lic = new LegendItemCollection(); {lic.addAll(legendItemsNew);} public LegendItemCollection getLegendItems() { return lic; } }; chart.addLegend(new LegendTitle(source)); 
+4
source share

I tried the above suggestion, but it didn't seem to work for me. If you just want to remove a series from a legend, you can do this using the setSeriesVisibleInLegend() method. My scenario was that some of my episodes do not have a legend key. If they do not have a legend key, the series should not be visible in the legend. I implemented this with the following code:

  for(int i = 0; i < seriesList.size(); i++){ if(seriesList.get(i).getKey() == null || seriesList.get(i).getKey().equals("")){ graph.getXYPlot().getRenderer().setSeriesVisibleInLegend(i, Boolean.FALSE); } } 

seriesList is a list of the seriesData pojo I created that contains all the graph data for creating a graph. If the value of the seriesData key of the object is null or = "" , then the series will not be visible in the legend.

+9
source share

All Articles