2

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); ///////////////////
Community
  • 1
  • 1
Johann
  • 447
  • 2
  • 8
  • 23
  • possible duplicate of [JFreeChart - Problem of colors with 2 legends](http://stackoverflow.com/questions/5800774/jfreechart-problem-of-colors-with-2-legends) – trashgod Apr 27 '11 at 17:30

1 Answers1

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Ok ! Sorry I am new on Stackoverflow ! ;) – Johann Apr 27 '11 at 15:03
  • No problem. Looking at your later [example](http://stackoverflow.com/q/5800774/713006), I don't see a way to make that approach work. It looks as if you're trying to specify the color in the data model. Instead, use `setSeriesPaint()` in the corresponding renderer. – trashgod Apr 27 '11 at 18:30
  • I tried to use `setSeriesPaint()`. But the colors in the graph take into consideration the color of the first legend. So, if you make the first legend invisible, the colors in the graph are chosen randomly even if you have added a new legend. How to manage that ? – Johann Apr 28 '11 at 06:39
  • It does not work. The two legends are in the same colors but do not respect the colors of the graph (colors in the graph are still chosen randomly) – Johann Apr 29 '11 at 06:33
  • 1
    Works for me; here's a `setFixedLegendItems()` [example](http://www.jroller.com/dgilbert/entry/ouch). – trashgod Apr 29 '11 at 10:01
  • Excellent. For reference, you might update your question to reflect your findings. Use `@trashgod` in a comment so I'll see it; I wouldn't mind up-voting it. See the [faq](http://stackoverflow.com/faq) for details. – trashgod Apr 29 '11 at 20:08