我想更新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);
}
Run Code Online (Sandbox Code Playgroud)
事实是,这只会影响路径的样式,图例样式不会改变。我已经打印了图表节点的子级,并发现在添加系列调用返回后未完全创建图例:
Legend@18e8627[styleClass=chart-legend]
Label@1689c98[styleClass=label …Run Code Online (Sandbox Code Playgroud) 我正在尝试动态更改图例的颜色,因此我的 css 类中没有任何线条和符号的样式。我可以动态更改图表中的所有线条和符号,但遗憾的是无法更改图例。他们保持默认状态。有没有办法动态地做到这一点?
所以,我尝试过:
1)
for (int index = 0; index < series.getData().size(); index++) {
XYChart.Data dataPoint = series.getData().get(index);
Node lineSymbol = dataPoint.getNode().lookup(".chart-legend");
lineSymbol.setStyle("-fx-background-color: #00ff00, #000000; -fx-background-insets: 0, 2;\n" +
" -fx-background-radius: 3px;\n" +
" -fx-padding: 3px;");
}
Run Code Online (Sandbox Code Playgroud)
根据 caspian.css 和下面的链接问题,这应该可以工作,但它给了我NullPointerException因为无法找到 .chart-legend 即使它在那里。
2)
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 : …Run Code Online (Sandbox Code Playgroud)