KunLun
KunLun

Reputation: 3225

JavaFX LineChart - change color of legend which have changed location

I have this code:

@Override
public void start(Stage primaryStage){

    NumberAxis xAxis = new NumberAxis();
    xAxis.setLabel("XX");

    NumberAxis yAxis = new NumberAxis();
    yAxis.setLabel("YY");

    LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);

    XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
    series1.setName("Loooooooooooooongest");
    series1.getData().addAll(new XYChart.Data<>(1, 1));

    XYChart.Series<Number, Number> series2 = new XYChart.Series<>();
    series2.setName("Short");
    series2.getData().addAll(new XYChart.Data<>(2, 2));

    XYChart.Series<Number, Number> series3 = new XYChart.Series<>();
    series3.setName("Loooooong");
    series3.getData().addAll(new XYChart.Data<>(3, 3));

    lineChart.getData().addAll(series1, series2, series3);
    lineChart.setLegendVisible(false); //make default legend invisible

    //change color of series
    final AtomicInteger index = new AtomicInteger(0);
    for(int i = 0; i < 3; i++){

        int color = (i*60) + 70;
        Platform.runLater(() ->

                lineChart.lookupAll(".series" + index.getAndIncrement())
                        .forEach(e1 -> e1.setStyle("-fx-stroke: rgb(0, 0, " + color + ");" +
                                                    "-fx-background-color: rgb(0, 0, " + color + "), white;"))

        );

    }

    //create an hbox for legends
    HBox legendBox = new HBox();
    legendBox.setStyle("-fx-border-style: solid;" +
                    "-fx-border-color: lightgray;" +
                    "-fx-border-width: 1;" +
                    "-fx-padding: 10;" +
                    "-fx-background-color: white;" +
                    "-fx-alignment: center;");

    //take all legends and put in hbox
    var insets = new Insets(0, 10, 0, 10);
    for(Node node : lineChart.lookupAll("Label.chart-legend-item")){

        legendBox.getChildren().add(node);
        HBox.setMargin(node, insets);

    }

    //place chart and hbox-legends in borderpane
    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(lineChart);
    borderPane.setBottom(legendBox);

    primaryStage.setScene(new Scene(borderPane));
    primaryStage.sizeToScene();
    primaryStage.centerOnScreen();

    primaryStage.show();

}

This looks like this:

Color of legends are not changed.

If I remove that part with HBox and let legends into original Node their color are change.

Why the color are no longer change if I move legends in other node and how I can change color of legends which are placed in other Node than original?

Upvotes: 1

Views: 245

Answers (1)

KunLun
KunLun

Reputation: 3225

I found the solution.

In for where I add legends to HBox:

for(Node node : lineChart.lookupAll("Label.chart-legend-item")){

    legendBox.getChildren().add(node);
    HBox.setMargin(node, insets);

}

I should add:

Label label = (Label)node;
label.getGraphic().setStyle("-fx-background-color: " + mycolor + ", white");

Which will result:

for(Node node : lineChart.lookupAll("Label.chart-legend-item")){

    Label label = (Label)node;
    label.getGraphic().setStyle("-fx-background-color: " + mycolor + ", white");

    legendBox.getChildren().add(node);
    HBox.setMargin(node, insets);

}

Upvotes: 1

Related Questions