【发布时间】:2021-10-27 10:59:38
【问题描述】:
用户可以选择自定义颜色,之后我需要以这种颜色显示折线图。这些行确实按照第二个链接中的描述工作:
String rgb = String.format("%d, %d, %d",
(int) (user.getColor().getRed() * 255),
(int) (user.getColor().getGreen() * 255),
(int) (user.getColor().getBlue() * 255));
series.nodeProperty().get().setStyle("-fx-stroke: rgba(" + rgb + ", 1);");
我无法更改用户图表的符号。使用 css 你可以这样做:
.default-color0.chart-line-symbol { -fx-background-color: #e9967a, white; }
.default-color1.chart-line-symbol { -fx-background-color: #f0e68c, white; }
.default-color2.chart-line-symbol { -fx-background-color: #dda0dd, white; }
我不能这样做,因为用户可以选择任何可能的颜色。有解决办法吗?
我试过这个,但这会抛出一个NullPointerException:
chart.lookup(".default-color0.chart-line-symbol")
.setStyle("-fx-background-color: rgba(" + rgb + ", 1), white;");
添加Platform.runLater(() -> chart.lo....) 会删除NullPointerException(我不明白为什么),但只会将一些点变成正确的颜色,而不是图例。
【问题讨论】:
-
CSS 查找仅在应用了 CSS 之后才起作用,默认情况下发生在第一个布局阶段;这解释了为什么您会得到一个空指针异常,该异常是通过使用
Platform.runLater()删除的。一种更稳健的方法是在执行查找之前在场景中调用applyCSS()。但我的答案中的解决方案完全避免了查找。