3

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.

  • Css is the best way to do this. Are you using Scenebuilder? – SedJ601 Nov 11 '16 at 14:22
  • I am curious if your tool tip is working on mouse over? If not look at what I did here. http://stackoverflow.com/questions/14615590/javafx-linechart-hover-values/40431880#40431880 – SedJ601 Nov 11 '16 at 14:43

1 Answers1

1

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.
 });
SedJ601
  • 12,173
  • 3
  • 41
  • 59