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

后端 未结 4 1812
醉话见心
醉话见心 2021-01-18 09:00

I am trying to style my JavaFX linechart but I have some trouble with the legend.

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

4条回答
  •  伪装坚强ぢ
    2021-01-18 09:28

    This solution is based on @Chris' solution

     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;");
            });
     }
    

提交回复
热议问题