gpt4 book ai didi

JavaFX 着色 XYChart.Series

转载 作者:行者123 更新时间:2023-11-29 03:14:39 30 4
gpt4 key购买 nike

我正在尝试设置我的 JavaFX 面积图的样式,但找不到任何方法来为特定系列设置颜色。我知道,我可以通过 CSS 设置样式,但我也想在代码中这样做。

我该怎么做?1.) 如何使用内联样式为 Area-Chart 设置颜色?

感谢您的帮助。

@何塞:

我以前是这样操作的,但是对我不起作用!

@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();
}

最佳答案

answer在 Java 7 中不起作用,因为默认的 CSS 样式表是 Caspian,而不是 Modena。

在里海中,主调色板的这些常量未定义:CHART_COLOR_1CHART_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 + "); ");
}
}

关于JavaFX 着色 XYChart.Series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420115/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com