2016-11-11 64 views
3

我正在编写一个测试程序,您必须获得70%以上才能通过测试。 我已经设置了一个LineChart和一个系列,它将百分比值和日期作为坐标。我想为大于或等于70%绿色的节点着色,其余为红色。这里是一个代码片段:为一个系列中的特定节点设置样式 - JavaFX

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

问题是我无法为一个系列中的特定节点着色。 我希望有人能够帮助我。

+0

Css是执行此操作的最佳方法。你在使用Scenebuilder吗? – Sedrick

+0

如果您的工具提示正在使用鼠标,我很好奇吗?如果不看我在这里做了什么。 http://stackoverflow.com/questions/14615590/javafx-linechart-hover-values/40431880#40431880 – Sedrick

回答

1

你必须从这样的事情开始。

//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. 
}); 
相关问题