JavaFX BarChart bar color

2019-02-25 03:27发布

How can you change the color of a bar in a JavaFX BarChart?

I couldn't find a way to change the color through the css setStyle Method.

2条回答
唯我独甜
2楼-- · 2019-02-25 03:52

From JavaFX8 you can simply use the .chart-bar selector:

.chart-bar {
    -fx-bar-fill: red; 
}
查看更多
闹够了就滚
3楼-- · 2019-02-25 03:54

you can set color of bar using css

.default-color0.chart-bar { -fx-bar-fill: ***** }
.default-color1.chart-bar { -fx-bar-fill: ***** }
...

Using setStyle Method :

use lookupAll method in Node class,

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.

code :

  //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;");
        }
查看更多
登录 后发表回答