jFreeChart: How to hide items from legend?
Asked Answered
C

2

7

I need to hide every second/third/forth item from the legend. IS there a way to achieve this in jFreeChart? thanks!

Carothers answered 27/7, 2010 at 9:52 Comment(0)
C
4

okay, just did it myself. This way I remove every second item 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));
Carothers answered 27/7, 2010 at 11:8 Comment(1)
That's good for a dynamic solution. It's also possible to use renderer.getLegendItems() and plot.setFixedLegendItems(). This yields a simpler solution, but is only good if your legend won't be changing after you set the fixed legend. I don't have time to craft the details of the solution, but look for getLegendItems and setFixedLegendItems in AbstractXYItemRenderer and XYPlot.By
B
8

I have tried the above suggestion but it didn't seem to work for me. If you just want to remove series from the legend you can do it with the setSeriesVisibleInLegend() method. My scenario was that some of my series do not have a legend key. If they don't have a legend key then the series shouldn't 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);
        }
    }

The seriesList is a list of seriesData pojo's that I created that holds all of the graph data to create the graph. If the seriesData object's key value is null or = "" then the series will not be visible in the legend.

By answered 11/12, 2012 at 9:48 Comment(0)
C
4

okay, just did it myself. This way I remove every second item 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));
Carothers answered 27/7, 2010 at 11:8 Comment(1)
That's good for a dynamic solution. It's also possible to use renderer.getLegendItems() and plot.setFixedLegendItems(). This yields a simpler solution, but is only good if your legend won't be changing after you set the fixed legend. I don't have time to craft the details of the solution, but look for getLegendItems and setFixedLegendItems in AbstractXYItemRenderer and XYPlot.By

© 2022 - 2024 — McMap. All rights reserved.