Reputation: 107
I'm writing a program in javafx where I'm comparing different companies and products to each other using bubble charts, line charts, scatter charts and pie charts. I would like one certain company and all products belonging to it to always have the color green when appearing in the charts. Right now I'm using css to style the charts and I've tried these different alternatives for setting a color for a specific series (in this case it's for the bubble charts):
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");
}
All these alternatives give NullPointerExceptions. Why is that? Is there any other way I can set the color the way I would like to?
Thanks!
Upvotes: 1
Views: 441
Reputation: 107
I posted the question, but I found an answer myself. Thought I could post it here if anyone else has the same problem. I used this code to solve the problem:
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 is a Set where I have stored all indexes of the series that belong to myCompany.
Upvotes: 3