JavaFX StackedBarChart图例颜色不遵循图表颜色CSS样式 [英] JavaFX StackedBarChart legend color don't follow chart color CSS styled
问题描述
在我的应用程序(JDK 1.8u51)中,我想为StackedBarChart中的某些数据类别设置一些特定的颜色。我用CSS做了这个,如下:
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;
}
我在我的代码中使用这个CSS:
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, ...)
});
});
我得到的结果是这样的StackedBarChart:
What I get as reslut to this is such a StackedBarChart :
如您所见,图表区域与图例之间的颜色不同。 Critical值必须为Blue,Major必须为Red。
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.
是JavaFX错误还是只是我的代码?
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.
推荐答案
对于那些人谁搜索我找到了其他2个问题的答案:
For those who search I found the answer on 2 other questions :
- First
- Second
我还必须添加 .chart-legend-item-symbol
进入我的CSS中的每个样式类,如下所示:
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;
}
这篇关于JavaFX StackedBarChart图例颜色不遵循图表颜色CSS样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!