我正在尝试设置我的JavaFX Area-Chart,但找不到任何方法将颜色设置为特定系列。我知道,我可以通过CSS设置样式,但我也想在代码中进行设置。
我该怎么做? 1.)如何使用内联样式将颜色设置为面积图?
感谢您的帮助。
@Jose:
我之前就这样做过,但它对我不起作用!
@Override
public void start(Stage primaryStage)
{
final AreaChart<String, Number> areaChart = new AreaChart<>(new CategoryAxis(), new NumberAxis());
ObservableList<XYChart.Data<String, Integer>> xyList = FXCollections.observableArrayList(
new XYChart.Data<>("P1", 30),
new XYChart.Data<>("P2", 40),
new XYChart.Data<>("P3", 30));
XYChart.Series series = new XYChart.Series(xyList);
areaChart.getData().addAll(series);
Button button = new Button("Change style");
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent arg0)
{
int redColor = 0, greenColor = 127, blueColor = 195;
double opacity = 0.3;
areaChart.setStyle("CHART_COLOR_1: rgb(" + redColor + "," + greenColor + "," + blueColor + "); "
+ "CHART_COLOR_1_TRANS_20: rgba(" + redColor + "," + greenColor + "," + blueColor + "," + opacity + ");");
}
});
VBox root = new VBox();
root.getChildren().addAll(button, areaChart);
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
答案 0 :(得分:2)
这个answer在Java 7中不起作用,因为默认的CSS样式表是Caspian,而不是Modena。
在Caspian中,未定义主色调的常量:CHART_COLOR_1
,CHART_COLOR_1_TRANS_20
,...
因此,如果您想要应用内联样式,可以通过查找具有查找的系列并根据其CSS选择器将样式应用于符号,线条和/或区域来实现。例如:
@Override
public void start(Stage primaryStage) {
final AreaChart<String, Number> areaChart = new AreaChart<>(new CategoryAxis(), new NumberAxis());
ObservableList<XYChart.Data<String, Integer>> xyList = FXCollections.observableArrayList(
new XYChart.Data<>("P1", 30),
new XYChart.Data<>("P2", 40),
new XYChart.Data<>("P3", 30));
XYChart.Series series = new XYChart.Series(xyList);
areaChart.getData().addAll(series);
Scene scene = new Scene(areaChart, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
int redColor = 0, greenColor = 127, blueColor = 195;
double opacity = 0.3;
for(Node symbol : areaChart.lookupAll(".default-color0.chart-area-symbol")){
symbol.setStyle("-fx-background-color: rgb(" + redColor + "," + greenColor + "," + blueColor + "), white; ");
}
Node line = areaChart.lookup(".default-color0.chart-series-area-line");
line.setStyle("-fx-stroke: rgb(" + redColor + "," + greenColor + "," + blueColor + "); ");
Node area = areaChart.lookup(".default-color0.chart-series-area-fill");
area.setStyle("-fx-fill: rgba(" + redColor + "," + greenColor + "," + blueColor + "," + opacity + "); ");
}
修改强>
上述解决方案最多可达8个系列。
对于任何数量的系列,其他方法也适用:
int numSeries=10;
final int[] redColor = new int[numSeries];
Arrays.fill(redColor, 0);
final int[] greenColor =new int[numSeries];
Arrays.fill(greenColor, 127);
final int[] blueColor = new int[numSeries];
Arrays.fill(blueColor, 195);
double opacity = 0.3;
for(int i=0; i<numSeries; i++){
for(Node n : areaChart.lookupAll(".series"+i)){
n.setStyle("-fx-background-color: rgb(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "), white; "
+ "-fx-stroke: rgb(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "); "
+ "-fx-fill: rgba(" + redColor[i] + "," + greenColor[i] + "," + blueColor[i] + "," + opacity + "); ");
}
}
答案 1 :(得分:1)
您可以使用:
chart-series-area-line series<i> default-color<j>
系列的索引在哪里,是系列的颜色索引
在css参考this中具体说明,你可以使用areaChart的setStyle函数
你可以用这个:
Node line = areaChart.lookup(".default-color0.chart-series-area-line");
line.setStyle("-fx-stroke: rgb(" + redColor + "," + greenColor + "," + blueColor + "); ");