JavaFX LineChart传奇风格

时间:2012-08-30 13:22:45

标签: java javafx-2

我想更新LineChart图例的样式,我在具有correspoding系列类的节点上使用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子元素,所以我可以在那些节点上设置setStyle?

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

3 个答案:

答案 0 :(得分:4)

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

我在图例的“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
                                    }

                                });
                            }
                        }
                    }
                });
            }
        }

答案 1 :(得分:3)

这对我没有任何影响,我找到了另一个解决方案。无论是答案还是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;");

对于图例颜色更改:

for(Node n : chart.getChildrenUnmodifiable()){
   if(n instanceof Legend){
      for(Legend.LegendItem legendItem : ((Legend)n).getItems()){
        legendItem.getSymbol().setStyle("-fx-background-color: #0000ff, white;");
      }
   }
}

希望这适用于任何寻找此事的人。

答案 2 :(得分:0)

知道这已经过时了,但也许还有人需要更顺畅的解决方案。只需使用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)");
}});