JfreeChart - 如何隐藏图例中的项目 - 颜色问题

发布于 2024-11-03 08:53:42 字数 1703 浏览 1 评论 0原文

我想隐藏 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-11-10 08:53:42

基于此 线程,您可以尝试添加 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 the org.jfree.chart.demo classes may be a suitable starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文