JavaFX BarChart bar color

前端 未结 2 1204
轮回少年
轮回少年 2021-01-19 09:12

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条回答
  • 2021-01-19 10:08

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

    .chart-bar {
        -fx-bar-fill: red; 
    }
    
    0 讨论(0)
  • 2021-01-19 10:14

    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;");
            }
    
    0 讨论(0)
提交回复
热议问题