JavaFX StackedBarChart legend color don't foll

2019-08-11 11:00发布

In my application (JDK 1.8u51) I want to set some specific colors to some data categories in a StackedBarChart. I made this with a CSS as bellow :

.root{
    -fx-ok-color: darkgreen;
    -fx-critical-color: darkblue;
    -fx-warning-color: gold;
    -fx-minor-color: orange;
    -fx-major-color: red;
    -fx-undefined-color: darkgrey;  
}
.okChartBar{
    -fx-bar-fill : -fx-ok-color;
}
.warnigChartBar{
    -fx-bar-fill : -fx-warning-color;
}
.minorChartBar{
    -fx-bar-fill : -fx-minor-color;
}
.majorChartbar{
    -fx-bar-fill : -fx-major-color;
}
.criticalChartBar{
    -fx-bar-fill : -fx-critical-color;
}
.undefinedChartBar{
    -fx-bar-fill : -fx-undefined-color;
}

I use this CSS in my code like this :

StackedBarChart barChart = new StackedBarChart(new CategoryAxis(), new NumberAxis());
barChart.setTitle("Title");
vBox.getChildren().add(1,barChart);
barChart.setAnimated(true);
barChart.getData().addAll(barChartData());
barChart.getData().forEach(data ->{
    XYChart.Series moduleSerie = (XYChart.Series)data;
    moduleSerie.getData().forEach(item ->{
        XYChart.Data item2 = (XYChart.Data)item;
        item2.getNode().getStyleClass().add(styleLink.get(moduleSerie.getName())); 
        // styleLink is a map which containt the link from the alarm type (minor, major....) to the CSS style   (minorChartbar, majorChartbar, ...)
    });
});

What I get as reslut to this is such a StackedBarChart : Example of not same color between chart and legend

As you can see, the colors between the chart areas and the legend aren't the same. "Critical" value must be Blue and "Major" must be Red.

Is it a JavaFX bug or is it just my code?

Sorry for the long post, I just want to be as complete as possible.

1条回答
放荡不羁爱自由
2楼-- · 2019-08-11 11:33

For those who search I found the answer on 2 other questions :

I also had to add .chart-legend-item-symbol into each style class in my CSS like this :

.okChartBar .chart-legend-item-symbol{ 
    -fx-background-color:-fx-ok-color;
 }
查看更多
登录 后发表回答