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