JavaFX折线图图例样式

JavaFX折线图图例样式,java,javafx-2,Java,Javafx 2,我想更新折线图图例的样式,我在具有相应编码序列类的节点上使用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().ge

我想更新折线图图例的样式,我在具有相应编码序列类的节点上使用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@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]
如果现在更新样式,图例样式将正确更新

如何知道何时添加了带有设置样式所需类的区域子节点,以便在该节点上设置样式


添加新系列后,是否还有其他更新图例样式的想法?

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

我在图例的“getChildrenUnmodifiable()”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
                                    }

                                });
                            }
                        }
                    }
                });
            }
        }
(节点n:lineChart.getChildrenUnmodifiable())的

{
if(图例的n个实例)
{
最终图例=(图例)n;
//删除图例
legend.getChildrenUnmodifiable().addListener(新ListChangeListener())
{
@凌驾
更改后的公共void(更改arg0)
{
for(节点:legend.getChildrenUnmodifiable())
{
if(标签的节点实例)
{
最终标签=(标签)节点;
label.getChildrenUnmodifiable().addListener(新ListChangeListener())
{
@凌驾
更改后的公共void(更改arg0)
{
//在此处更改样式
}
});
}
}
}
});
}
}

知道这已经很旧了,但也许有人仍然需要一个更平滑的解决方案。只需使用Platform.runLater函数:

int index = 2;
Platform.runLater(new Runnable() {
    @Override
    public void run() {
        myChart.lookupAll(".chart-legend-item-symbol").toArray()[index].setStyle("-fx-border-color: rgba(200,0,0,1)");
}});

所有这些对我都不起作用,所以我找到了另一个解决方案。答案和Platform.runLater方法都无效

XYChart.Series<Number,Number> value  //is our serie value.

for(int index = 0; index<value.getData().size(); index++){
    // we're looping for each data point, changing the color of line symbol
    XYChart.Data dataPoint = value.getData().get(index);
    Node lineSymbol = dataPoint.getNode().lookup(".chart-line-symbol");
    lineSymbol.setStyle("-fx-background-color: #0000FF, white;");
}
// and this is for the color of the line
value.getNode().setStyle("-fx-border-style: solid; -fx-stroke: #0000FF; -fx-background-color: #0000FF;");

希望这也适用于搜索此的任何人。

可能是一个愚蠢的问题,但我将添加样式更改?要标记吗?@JackStraw
XYChart.Series<Number,Number> value  //is our serie value.

for(int index = 0; index<value.getData().size(); index++){
    // we're looping for each data point, changing the color of line symbol
    XYChart.Data dataPoint = value.getData().get(index);
    Node lineSymbol = dataPoint.getNode().lookup(".chart-line-symbol");
    lineSymbol.setStyle("-fx-background-color: #0000FF, white;");
}
// and this is for the color of the line
value.getNode().setStyle("-fx-border-style: solid; -fx-stroke: #0000FF; -fx-background-color: #0000FF;");
for(Node n : chart.getChildrenUnmodifiable()){
   if(n instanceof Legend){
      for(Legend.LegendItem legendItem : ((Legend)n).getItems()){
        legendItem.getSymbol().setStyle("-fx-background-color: #0000ff, white;");
      }
   }
}