JavaFX使用图表图例切换显示/隐藏系列可能吗? [英] JavaFX Use Chart Legend to toggle show/hide Series possible?

查看:293
本文介绍了JavaFX使用图表图例切换显示/隐藏系列可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用图表的图例来切换显示/隐藏系列?

Is it possible to use a chart's legend to toggle show/hide a series?

我得到了 LineChart 有一个传奇,有太多系列,所以你不能很好地读出信息。我想知道是否有可能使用图例来切换系列以显示/隐藏?

I got a LineChart with a legend and there are too many Series so you can't read out the information well. I was wondering if there is a possibility to use the legend to toggle the series to show/hide?

我的系列的大部分名称非常长,看起来非常奇怪,如果它们在图例中被写了两次,那么你知道颜色属于系列并且第二次一个 CheckBox 来切换它们。

Most of the names of my Series are pretty long and it looks very weird if they are written twice once in the legend so you know wich color belongs to wich Seriesand a second time besides a CheckBox to toggle them.

Edit1:也许我不清楚,即使没有内置功能这个,我可以使用一些输入来看看workaroud的样子,因为我无法想出任何东西。

Maybe I was unclear, even if there is no built in function for this, I could use some input for how a workaroud could look like, because I can't come up with anything.

推荐答案

以下是我解决这个问题的方法 - 我不知道任何更简单的内置解决方案

Here is how I solved this - I am not aware of any simpler built-in solution

LineChart<Number, Number> chart;

for (Node n : chart.getChildrenUnmodifiable()) {
    if (n instanceof Legend) {
        Legend l = (Legend) n;
        for (Legend.LegendItem li : l.getItems()) {
            for (XYChart.Series<Number, Number> s : chart.getData()) {
                if (s.getName().equals(li.getText())) {
                    li.getSymbol().setCursor(Cursor.HAND); // Hint user that legend symbol is clickable
                    li.getSymbol().setOnMouseClicked(me -> {
                        if (me.getButton() == MouseButton.PRIMARY) {
                            s.getNode().setVisible(!s.getNode().isVisible()); // Toggle visibility of line
                            for (XYChart.Data<Number, Number> d : s.getData()) {
                                if (d.getNode() != null) {
                                    d.getNode().setVisible(s.getNode().isVisible()); // Toggle visibility of every node in the series
                                }
                            }
                        }
                    });
                    break;
                }
            }
        }
    }
}

您需要在图表上运行此代码一次(在此示例中为 LineChart ,但您可以将其调整为任何其他图表)。我找到图例子项,然后迭代它的所有项目。我根据名称将图例项目与正确的系列相匹配 - 根据我的经验,他们总是匹配,我找不到更好的方法来匹配它们。然后,只需将正确的事件处理程序添加到该特定图例项即可。

You need to run this code once on your chart (LineChart in this example, but you can probably adapt it to any other chart). I find the Legend child, and then iterate over all of its' items. I match the legend item to the correct series based on the name - from my experience they always match, and I couldn't find a better way to match them. Then it's just a matter of adding the correct event handler to that specific legend item.

这篇关于JavaFX使用图表图例切换显示/隐藏系列可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆