gpt4 book ai didi

JavaFX LineChart 图例样式

转载 作者:行者123 更新时间:2023-12-03 01:49:36 26 4
gpt4 key购买 nike

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

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);
}

问题是,这只会影响路径的样式,图例样式不会改变。我已经打印了图表节点子节点,并发现在添加系列调用返回后,图例尚未完全创建:

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]

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

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]

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

我如何知道何时添加了具有设置样式所需的类的 Region 子项,以便我可以在该节点上设置样式?

添加新系列时更新图例样式还有其他想法吗?

最佳答案

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

我向图例的“getChildrenUnmodifying()”ObservableList 添加了一个 ListChangeListener,这又在添加图例的每个子项时向其添加一个 ListChangeListener。从这个监听器中,我们可以知道何时将新项目添加到图例中(或删除)。这使我们能够进行动态样式更改。

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 图例样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12197877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com