jFreeChart: How to hide items from legend?

倾然丶 夕夏残阳落幕 提交于 2020-03-18 12:31:33

问题


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


回答1:


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.




回答2:


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));


来源:https://stackoverflow.com/questions/3342406/jfreechart-how-to-hide-items-from-legend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!