JavaFX 2.0 - How to change the color of a LineChart legend dynamically?

I am trying to create my JavaFX linear string, but I have some problems with the legend.

I know how to change the color of a line chart legend in a css file:

.default-color0.chart-series-line { -fx-stroke: #FF0000, white; }
.default-color1.chart-series-line { -fx-stroke: #00FF00, white; }
.default-color2.chart-series-line { -fx-stroke: #0000FF, white; }

.default-color0.chart-line-symbol { -fx-background-color: #FF0000, white; }
.default-color1.chart-line-symbol { -fx-background-color: #00FF00, white; }
.default-color2.chart-line-symbol { -fx-background-color: #0000FF, white; }

But this is not enough for my purposes. I have three or more color toggle buttons and a series of data for each button. The data should be displayed in the same color that the button has after I selected the button. This should be possible with multiple button selections, so that more than one series of data can be displayed at a time.

For the chart lines, I dealt with this by changing the style after clicking the button:

..
dataList.add(series);
..
series.getNode().setStyle("-fx-stroke: rgba(" + rgba + ")");

If I deselect a button, I will remove the data from the list.

dataList.remove(series);

This works great for punches, but how can I do the same for a legend?

. , ( -color0). . . , , color1, , .

enter image description here

+5
4

. , , , , N . , , , .

ListChangeListener "getChildrenUnmodifiable()" ObservableList, , , 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
                                }

                            });
                        }
                    }
                }
            });
        }
    }
+2

Platform.runLater(). :

LineChart<Number, Number> plot;
....
Platform.runLater(() -> {
    Node nl = plot.lookup(".default-color0.chart-series-line");
    Node ns = plot.lookup(".default-color0.chart-line-symbol");

    nl.setStyle("-fx-stroke: #333;");
    ns.setStyle("-fx-background-color: #333, white;");
}); 
+1

, , , , setStyle() . ( , )

0

@Chris

 if (checkCombo.getCheckModel().isChecked(0)) {
    lineChart.getData().add(seriesTest1);
    changeColorSeries(lineChart.getData().size() - 1, "darkgreen");
 }

 if (checkCombo.getCheckModel().isChecked(3)) {
    lineChart.getData().add(seriesTest2);
    changeColorSeries(lineChart.getData().size() - 1, "darkred");
 }

 private void changeColor(int position, String color) {
        Platform.runLater(() -> {
            Node nl = lineChart.lookup(".default-color" + position + ".chart-series-line");
            Node ns = lineChart.lookup(".default-color" + position + ".chart-line-symbol");
            Node nsl = lineChart.lookup(".default-color" + position + ".chart-legend-item-symbol");

            nl.setStyle("-fx-stroke: " + color + ";");
            ns.setStyle("-fx-background-color: " + color + ", white;");
            nsl.setStyle("-fx-background-color: " + color + ", white;");
        });
 }
0

All Articles