gpt4 book ai didi

java - 由于类别数据太长,使用 JavaFx 使用 Slider 创建引人注目的 BarChart 来查看

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:29 26 4
gpt4 key购买 nike

我正在尝试使用 Javafx 制作条形图。然而它很小,很难看到。 enter image description here

我想让它变得更有吸引力

enter image description here

这是代码的程序

 public class Main extends Application {

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

@Override
public void start(Stage stage) throws Exception {
stage.setTitle("JavaFX Chart Demo");
StackPane pane = new StackPane();
pane.getChildren().add(createBarChart());
stage.setScene(new Scene(pane, 400, 200));
stage.show();
}

public ObservableList<XYChart.Series<String, Double>>
getDummyChartData() {
ObservableList<XYChart.Series<String, Double>> data =
FXCollections.observableArrayList();
Series<String, Double> as = new Series<>();
Series<String, Double> bs = new Series<>();
Series<String, Double> cs = new Series<>();
Series<String, Double> ds = new Series<>();
Series<String, Double> es = new Series<>();
Series<String, Double> fs = new Series<>();
as.setName("A-Series");
bs.setName("B-Series");
cs.setName("C-Series");
ds.setName("D-Series");
es.setName("E-Series");
fs.setName("F-Series");

Random r = new Random();

for (int i = 1900; i < 2017; i += 10) {

as.getData().add(new XYChart.Data<>
(Integer.toString(i), r.nextDouble()));
bs.getData().add(new XYChart.Data<>
(Integer.toString(i), r.nextDouble()));
cs.getData().add(new XYChart.Data<>
(Integer.toString(i), r.nextDouble()));
ds.getData().add(new XYChart.Data<>
(Integer.toString(i), r.nextDouble()));
es.getData().add(new XYChart.Data<>
(Integer.toString(i), r.nextDouble()));
fs.getData().add(new XYChart.Data<>
(Integer.toString(i), r.nextDouble()));
}
data.addAll(as, bs, cs, ds, es, fs);
return data;
}

public XYChart<CategoryAxis, NumberAxis>
createBarChart() {
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
BarChart bc = new BarChart<>(xAxis, yAxis);
bc.setData(getDummyChartData());
bc.setTitle("Bar Chart on Random Number");
return bc;
}
}

请帮助我如何通过Javafx获取它。我发现它可以通过 JFree Chart 解决,但是我不知道如何通过 BarChart javafX 来解决它。这对我来说真的是一个挑战。谢谢

最佳答案

只需在滚动 Pane 中制作图表即可。

public void start(Stage stage) throws Exception {
stage.setTitle("JavaFX Chart Demo");
StackPane stackPane = new StackPane();
stackPane.setPrefSize(1000, 200);
stackPane.getChildren().add(createBarChart());
ScrollPane scrollPane = new ScrollPane();
scrollPane.setPrefSize(400, 220);
scrollPane.setContent(stackPane);
stage.setScene(new Scene(scrollPane));
stage.show();
}

关于java - 由于类别数据太长,使用 JavaFx 使用 Slider 创建引人注目的 BarChart 来查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58143110/

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