gpt4 book ai didi

JavaFX折线图改变颜色形状

转载 作者:行者123 更新时间:2023-12-01 19:38:37 26 4
gpt4 key购买 nike

我有这个窗口:

@Override
public void start(Stage primaryStage){

NumberAxis xAxis = new NumberAxis(1960, 2020, 10);
xAxis.setLabel("Years");

NumberAxis yAxis = new NumberAxis(0, 350, 50);
yAxis.setLabel("No.of schools");

LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Chart");

XYChart.Series<Number, Number> series = new XYChart.Series<>();
series.setName("No of schools in an year");
Platform.runLater(() ->
series.getNode().lookup(".chart-series-line").setStyle("-fx-stroke: black;")
);

series.getData().add(new XYChart.Data<>(1970, 15));
series.getData().add(new XYChart.Data<>(1980, 30));
series.getData().add(new XYChart.Data<>(1990, 60));
series.getData().add(new XYChart.Data<>(2000, 120));
series.getData().add(new XYChart.Data<>(2013, 240));
series.getData().add(new XYChart.Data<>(2014, 300));

lineChart.getData().add(series);

var scene = new Scene(lineChart);

primaryStage.setScene(scene);
primaryStage.show();

}

它看起来是这样的:

我想改变系列的颜色,但我能实现的是改变线条的颜色,但不能改变形状。

如何更改形状的颜色?

最佳答案

我建议使用CSS。这里的关键是查找与系列关联的所有节点并更改节点的颜色。第一个系列是 .series0

Key Code:

Set<Node> nodes = lineChart.lookupAll(".series" + 0);
for (Node n : nodes) {
n.setStyle("-fx-background-color: black, white;\n"
+ " -fx-background-insets: 0, 2;\n"
+ " -fx-background-radius: 5px;\n"
+ " -fx-padding: 5px;");
}

Full Code

import java.util.Set;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class ScatterChartSample extends Application {

@Override
public void start(Stage stage) {
NumberAxis xAxis = new NumberAxis(1960, 2020, 10);
xAxis.setLabel("Years");

NumberAxis yAxis = new NumberAxis(0, 350, 50);
yAxis.setLabel("No.of schools");

LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Chart");

XYChart.Series<Number, Number> series = new XYChart.Series<>();
series.setName("No of schools in an year");
Platform.runLater(()
-> {

Set<Node> nodes = lineChart.lookupAll(".series" + 0);
for (Node n : nodes) {
n.setStyle("-fx-background-color: black, white;\n"
+ " -fx-background-insets: 0, 2;\n"
+ " -fx-background-radius: 5px;\n"
+ " -fx-padding: 5px;");
}

series.getNode().lookup(".chart-series-line").setStyle("-fx-stroke: black;");
});

series.getData().add(new XYChart.Data<>(1970, 15));
series.getData().add(new XYChart.Data<>(1980, 30));
series.getData().add(new XYChart.Data<>(1990, 60));
series.getData().add(new XYChart.Data<>(2000, 120));
series.getData().add(new XYChart.Data<>(2013, 240));
series.getData().add(new XYChart.Data<>(2014, 300));

lineChart.getData().add(series);

var scene = new Scene(lineChart);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

Update: Try this!

for (Data<Number, Number> entry : series.getData()) {      
entry.getNode().setStyle("-fx-background-color: black, white;\n"
+ " -fx-background-insets: 0, 2;\n"
+ " -fx-background-radius: 5px;\n"
+ " -fx-padding: 5px;");
}

enter image description here

关于JavaFX折线图改变颜色形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56380403/

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