2016-08-14 61 views
1

我正在用javafx編寫一個程序,我使用氣泡圖,折線圖,散點圖和餅圖將不同公司和產品彼此進行比較。我希望某個公司和屬於它的所有產品在出現在圖表中時始終具有綠色。現在,我使用的CSS樣式的圖表,我已經嘗試設置一種顏色通過一系列特定這些不同的選擇(在這種情況下,它的氣泡圖):在javafx中設置特定系列的顏色

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

所有這些替代給NullPointerException異常。這是爲什麼?有沒有其他方法可以按照我想要的方式設置顏色?

謝謝!

回答

2

我發佈了這個問題,但我自己找到了答案。以爲我可以在這裏發佈它,如果其他人有同樣的問題。我用這個代碼來解決這個問題:

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的該系列的所有索引。

相關問題