Johann
Johann

Reputation: 447

JfreeChart - How to hide item from legend - Problem of colors

I would like to hide items from legend in Jfreechart and I've tried this code jFreeChart: How to hide items from legend?

It works but something strange happened : the colors of the legend items do not match to the right data anymore. In other words, in the graph, a piece of data appears in yellow for example but the legend corresponding to this item appears in another color. In fact, the colors in the legend have been mixed.

Besides, when I try to display both old and new legends, there is no problem of colors but when I make the old legend invisible, the problem of mixed color appears. Obviously, I would like not to display the old legend.

Working code =>

LegendItemCollection legendItemsOld = localCombinedDomainXYPlot.getLegendItems();
final LegendItemCollection legendItemsNew = new LegendItemCollection();

for(int i = 0; i<4; i++){
    legendItemsNew.add(legendItemsOld.get(i));
}
LegendItemSource source = new LegendItemSource() {
    LegendItemCollection lic = new LegendItemCollection();
    {lic.addAll(legendItemsNew);}
    public LegendItemCollection getLegendItems() {  
        return lic;
    }
};
localJFreeChart.addLegend(new LegendTitle(source));

ChartUtilities.applyCurrentTheme(localJFreeChart);
localJFreeChart.getLegend().setVisible(true); ///////////////////

Not working code =>

LegendItemCollection legendItemsOld = localCombinedDomainXYPlot.getLegendItems();
final LegendItemCollection legendItemsNew = new LegendItemCollection();

for(int i = 0; i<4; i++){
    legendItemsNew.add(legendItemsOld.get(i));
}
LegendItemSource source = new LegendItemSource() {
    LegendItemCollection lic = new LegendItemCollection();
    {lic.addAll(legendItemsNew);}
    public LegendItemCollection getLegendItems() {  
        return lic;
    }
};
localJFreeChart.addLegend(new LegendTitle(source));

ChartUtilities.applyCurrentTheme(localJFreeChart);
localJFreeChart.getLegend().setVisible(false); ///////////////////

Upvotes: 2

Views: 3637

Answers (1)

trashgod
trashgod

Reputation: 205785

Based on this thread, you might try adding a null element to replace the unwanted legend item. The other approach appears to elide unwanted items, but I'm not sure if you're doing the same. To clarify, consider posting an sscce that demonstrates the problem. One of the org.jfree.chart.demo classes may be a suitable starting point.

Upvotes: 1

Related Questions