JfreeChart - 如何隐藏图例中的项目 - 颜色问题
我想隐藏 Jfreechart 中图例中的项目,我已经尝试过这段代码 :如何隐藏图例中的项目?
jFreeChart 有效,但发生了一些奇怪的事情:图例项的颜色不再与正确的数据匹配。换句话说,在图表中,一条数据例如以黄色显示,但与该项目对应的图例以另一种颜色显示。事实上,传说中的颜色已经混合了。
此外,当我尝试同时显示旧图例和新图例时,没有颜色问题,但是当我使旧图例不可见时,会出现混合颜色的问题。显然,我不想展示旧的传说。
工作代码 =>
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); ///////////////////
无效代码=>
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); ///////////////////
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); ///////////////////
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于此 线程,您可以尝试添加
null
元素来替换不需要的图例项。另一种方法似乎可以删除不需要的项目,但是我不确定你是否也在做同样的事情。为了澄清这一点,请考虑发布一个 sscce 来演示该问题。org. jfree.chart.demo
类可能是一个合适的起点。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 theorg.jfree.chart.demo
classes may be a suitable starting point.