Within my application, using JDK version 1.8u51, I need to assign specific colors to different data categories in a StackedBarChart. To accomplish this, I have created the following CSS:
.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;
}
This CSS is then utilized in my code as shown below:
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 contains the link from the alarm type (minor, major....) to the CSS style (minorChartbar, majorChartbar, ...)
});
});
The resulting StackedBarChart can be viewed here.
In the displayed chart, there seems to be a discrepancy in colors between the chart areas and the legend. For instance, "Critical" should appear as blue and "Major" as red.
I am uncertain whether this inconsistency is due to a JavaFX bug or an issue within my code.
I apologize for the length of this post, but I wanted to provide as much detail as possible.