How to change color of a single bar java fx

前端 未结 1 1734
自闭症患者
自闭症患者 2020-12-03 19:39

Here is my code which generates bar chart of 10 values from 0 to 10 . i want to change the color of bars as follows

if i>5 color==red if i>8 color==blue

相关标签:
1条回答
  • 2020-12-03 20:15

    I created a sample solution.

    The solution works by setting the bar's -fx-bar-fill color to a different color based on the value of the bar's data.

    final XYChart.Data<String, Number> data = new XYChart.Data("Value " + i , i);
    data.nodeProperty().addListener(new ChangeListener<Node>() {
      @Override public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
        if (newNode != null) {
          if (data.getYValue().intValue() > 8 ) {
            newNode.setStyle("-fx-bar-fill: navy;");
          } else if (data.getYValue().intValue() > 5 ) {
            newNode.setStyle("-fx-bar-fill: firebrick;");
          }  
        }
      }
    });
    

    coloredbarchart

    0 讨论(0)
提交回复
热议问题