Whatever
Whatever

Reputation: 115

JavaFX BarChart bar color

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.

Upvotes: 3

Views: 14358

Answers (3)

FARS
FARS

Reputation: 457

Click in the Category axis or Number axis (which one you want to change color) > go the the editor (right side of scene builder) > choose color in tick label fill

enter image description here

Upvotes: 0

DVarga
DVarga

Reputation: 21799

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

.chart-bar {
    -fx-bar-fill: red; 
}

Upvotes: 1

invariant
invariant

Reputation: 8900

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;");
        }

Upvotes: 5

Related Questions