Marian Thiel Marian Thiel - 22 days ago 8
CSS Question

Set Style for a specific node in one series - JavaFX

I'm writing a test programm where you have to get over 70% to pass the test.
I have setup a LineChart and one series, which takes the percent value and the date as coordinates. I want to color the nodes which are >= 70% green and the rest red. Here is a code fragment:

for(final XYChart.Data<String, Number> data : series.getData()){
System.out.println(data.getXValue());
if(percent>=70){
data.getNode().setStyle("-fx-background-color: green;");
}else{
data.getNode().setStyle("-fx-background-color: red;");
}
data.getNode().addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {


@Override
public void handle(MouseEvent event) {
// TODO Auto-generated method stub
data.getNode().setCursor(Cursor.HAND);

Tooltip.install(data.getNode(), new Tooltip("Am: \n"+data.getXValue()+"\nZu: "+data.getYValue()+"%"));
}
});
}


The problem is that I'm not able to color a specific node in one series.
I hope someone is able to help me.

Answer

You have to start with something like this.

//If you only have one series all you need is this first block of code
Set<Node> node = lineChart.lookupAll(".default-color0.chart-line-symbol.series0.");                    
    node.forEach((element) -> {
      //do somthing to each node in series0
      System.out.println(element.toString());//don't know if this will work. If it does it will all you to see each node. At the very least the node address.
 });

//If you have two series you need this. If you have more thant two series you need to copy this and change node2 to node3 everywhere in your copy.
Set<Node> node2 = lineChart.lookupAll(".default-color1.chart-line-symbol.series1.");                    
    node2.forEach((element) -> {
      //do somthing to each node in series1
      System.out.println(element.toString());//don't know if this will work. If it does it will all you to see each node. At the very least the node address.
 });
Comments