【问题标题】:Setting a color for a specific series in javafx在 javafx 中为特定系列设置颜色
【发布时间】:2016-08-14 16:48:02
【问题描述】:

我正在用 javafx 编写一个程序,我在其中使用气泡图、折线图、散点图和饼图来比较不同的公司和产品。我希望某家公司和属于它的所有产品在图表中出现时始终具有绿色。现在我正在使用 css 来设置图表的样式,并且我已经尝试了这些不同的替代方法来为特定系列设置颜色(在本例中是气泡图):

if(company.equals("MyCompany")) {
     //alternative 1
     Set<Node> nodes = series.getNode().lookupAll("chart-bubble");
     for(Node node : nodes) {
        node.getStyleClass().add("myCompany-chart-bubble");
     }

     //alternative 2
     series.nodeProperty().get().setStyle("-fx-bubble-fill:  #7fff00;");

     //alternative 3
     series.getData().get(0).getNode().getStyleClass().add("myCompany-chart-bubble");

     //alternative 4
     series.getNode().getStyleClass().add("myCompany-chart-bubble");
}

所有这些选择都会产生 NullPointerExceptions。这是为什么?有没有其他方法可以设置我想要的颜色?

谢谢!

【问题讨论】:

    标签: javafx bubble-chart


    【解决方案1】:

    我发布了这个问题,但我自己找到了答案。如果其他人有同样的问题,我想我可以在这里发布。我用这段代码解决了这个问题:

     for(Integer position : myCompanyPositions) {
            Set<Node> nodes = chart.lookupAll(".series" + position);
            for (Node n : nodes) {
                n.setStyle("-fx-bubble-fill:  #7fff00aa; "
                        + "-fx-background-color: radial-gradient(center 50% 50%, radius 80%, "
                        + "derive(-fx-bubble-fill,20%), derive(-fx-bubble-fill,-30%));");
             }
        }
        Set<Node> items = chart.lookupAll("Label.chart-legend-item");
        for (Node item : items) {
             Label label = (Label) item;
             if(label.getText().equals("myCompany")) {
                 label.getGraphic().setStyle("-fx-bubble-fill:  #7fff00aa; "
                        + "-fx-background-color: radial-gradient(center 50% 50%, radius 80%, "
                        + "derive(-fx-bubble-fill,20%), derive(-fx-bubble-fill,-30%));");
             }
        }
    

    myCompanyPositions 是一个集合,我在其中存储了属于 myCompany 的系列的所有索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2017-12-02
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多