JavaFX LineChart传奇风格 [英] JavaFX LineChart legend style

查看:231
本文介绍了JavaFX LineChart传奇风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新LineChart图例的样式,我在具有correspoding系列类的节点上使用setStyle。

I want to update the style of LineChart legend, i using setStyle on the nodes with the correspoding series class.

String color = ....
XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
_chart.getData().add(series);

String seriesClass = null;
for(String styleClass : series.getNode().getStyleClass())
{
    if(styleClass.startsWith("series"))
    {
        seriesClass = styleClass;
        break;
    }
}
if(seriesClass != null)
{
    //
    // Customize the style.
    //
    StringBuilder sb = new StringBuilder();
    sb.append("-fx-stroke: ");
    sb.append(color);
    sb.append("; ");
    sb.append("-fx-background-color: ");
    sb.append(color);
    sb.append(", white;");
    if(doted)
    {
        sb.append("-fx-stroke-dash-array: 10 10");
    }
    _styles.put(seriesClass, sb.toString()); 
}

java.util.Set<javafx.scene.Node> nodes = _chart.lookupAll("." + seriesClass);
for(javafx.scene.Node n : nodes)
{
    n.setStyle(style);
}

事情是这只会影响路径的风格,传说风格不会改变。我已经打印了图表节点子节点,并且在添加系列调用返回后看到图例没有完全创建:

The thing is that this just affect the style of the path, the legend style doesn't change. I have printed the chart node children and see that legends are not fully created after add series call returns:

Legend@18e8627[styleClass=chart-legend]
    Label@1689c98[styleClass=label chart-legend-item]
    Label@100e4ce[styleClass=label chart-legend-item]
    Label@1adcb5e[styleClass=label chart-legend-item]
    Label@102a8fb[styleClass=label chart-legend-item]

稍后如果我再次打印孩子:

A bit later if i print the children again:

Legend@9a095[styleClass=chart-legend]
    Label[id=null, styleClass=label chart-legend-item]
        LabelSkin[id=null, styleClass=label chart-legend-item]
            Region@12acafc[styleClass=chart-legend-item-symbol chart-line-symbol series0 default-color0]
            LabeledText@749a47[styleClass=text]
    Label[id=null, styleClass=label chart-legend-item]
        LabelSkin[id=null, styleClass=label chart-legend-item]
            Region@3ca3a4[styleClass=chart-legend-item-symbol chart-line-symbol series1 default-color1]
            LabeledText@11b9972[styleClass=text]
    Label[id=null, styleClass=label chart-legend-item]
        LabelSkin[id=null, styleClass=label chart-legend-item]
            Region@57f433[styleClass=chart-legend-item-symbol chart-line-symbol series2 default-color2]
            LabeledText@6172b5[styleClass=text]
    Label[id=null, styleClass=label chart-legend-item]
        LabelSkin[id=null, styleClass=label chart-legend-item]
            Region@16458ed[styleClass=chart-legend-item-symbol chart-line-symbol series3 default-color3]
            LabeledText@10a68bd[styleClass=text]

如果我现在更新样式,图例样式会正确更新。

If i update the style now, the legend style update correctly.

我怎么知道当添加了设置样式所需的类的Region子元素时,我可以在该节点上设置setStyle吗?

How can i know when the Region child with classes needed for set the style were added, so i can setStyle on that nodes?

要更新的任何其他想法添加新系列时的图例样式?

any other ideas to update the legend style when a new series has been added?

推荐答案

我也遇到了这个问题。想出了一个解决方法,可以检测创建图例项目的时间,以便可以添加动态样式。

I ran into this issue as well. Came up with a work-around that detects when the legend items are created so that dynamic styling can be added to them.

我在图例的getChildrenUnmodifiable中添加了一个ListChangeListener( )ObservableList,它会在添加时为每个图例的子项添加一个ListChangeListener。在此侦听器中,我们可以判断何时将新项目添加到图例(或已删除)。这允许我们进行动态样式更改。

I added a ListChangeListener to the legend's "getChildrenUnmodifiable()" ObservableList, which in turn adds a ListChangeListener to each of the legend's children as they get added. From within this listener, we can tell when new items are being added to the legend (or removed). This allow us to then make the dynamic style changes.

for (Node n : lineChart.getChildrenUnmodifiable())
        {
            if (n instanceof Legend)
            {
                final Legend legend = (Legend) n;

                // remove the legend
                legend.getChildrenUnmodifiable().addListener(new ListChangeListener<Object>()
                {
                    @Override
                    public void onChanged(Change<?> arg0)
                    {
                        for (Node node : legend.getChildrenUnmodifiable())
                        {
                            if (node instanceof Label)
                            {
                                final Label label = (Label) node;
                                label.getChildrenUnmodifiable().addListener(new ListChangeListener<Object>()
                                {
                                    @Override
                                    public void onChanged(Change<?> arg0)
                                    {
                                        //make style changes here
                                    }

                                });
                            }
                        }
                    }
                });
            }
        }

这篇关于JavaFX LineChart传奇风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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