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

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

问题描述

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

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

我有一个带有图例的 LineChart,而且 Series 太多,所以你不能很好地读出信息.我想知道是否有可能使用图例来切换系列以显示/隐藏?

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?

我的Series的大部分名字都很长,如果它们在图例中写两次看起来很奇怪所以你知道哪种颜色属于哪个Series和第二次除了 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 which color belongs to which Series and a second time besides a CheckBox to toggle them.

Edit1:也许我不清楚,即使没有为此内置函数,我也可以使用一些输入来了解解决方法的外观,因为我想不出任何东西.

Maybe I was unclear, even if there is no built in function for this, I could use some input for how a workaround 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,但您可能可以将其调整到任何其他图表).我找到 Legend 子项,然后遍历它的所有项.我根据名称将图例项目与正确的系列相匹配——根据我的经验,它们总是匹配的,我找不到更好的方法来匹配它们.然后只需将正确的事件处理程序添加到该特定图例项即可.

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天全站免登陆