如何在JavaFX BarChart中更改条形的颜色?
我找不到通过CSS setStyle方法更改颜色的方法.
解决方法:
您可以使用css设置条的颜色
.default-color0.chart-bar { -fx-bar-fill: ***** }
.default-color1.chart-bar { -fx-bar-fill: ***** }
...
使用setStyle方法:
在Node类中使用lookupAll方法,
Finds all Nodes, including this one and any children, which match the
given CSS selector. If no matches are found, an empty unmodifiable set
is returned. The set is explicitly unordered.
代码:
//set first bar color
for(Node n:barChart.lookupAll(".default-color0.chart-bar")) {
n.setStyle("-fx-bar-fill: red;");
}
//second bar color
for(Node n:barChart.lookupAll(".default-color1.chart-bar")) {
n.setStyle("-fx-bar-fill: green;");
}