LineChart JavaFX - 保存到 png - 如何控制大小

LineChart JavaFX -saving to png - How to control the size

我不知道如何控制保存为 png 的折线图的大小。图表在屏幕上看起来很棒,在它自己的 window 中,但在保存时它会垂直扭曲。我试图弄清楚如何控制大小,但无济于事。这一定是一个简单的设置,但是...

如有任何帮助,我们将不胜感激。

使用以下代码,不包括设置数据值的代码:

LineChart<Number, Number> xyChart = new LineChart<>(xAxis, yAxis);

StackPane layout = new StackPane();
layout.setPadding(new Insets(10, 10, 10, 10));
xyChart.prefWidthProperty().bind(window.widthProperty());
xyChart.prefHeightProperty().bind(window.heightProperty());
layout.getChildren().add(xyChart);

VBox mainVBox = new VBox();
mainVBox.getChildren().addAll(menuHBox, layout);

Scene scene = new Scene(mainVBox, 800, 600);
window.setScene(scene);
xyChart.setAnimated(false);
WritableImage snapShot = scene.snapshot(null);
try {
ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test3.png"));
} catch (IOException e) {
}

屏幕上显示的图表是 window grab

而写入的png文件是 upload of the png file

尝试在 显示 window 后保存图像,因为布局和尺寸是根据需要计算的,可能不会给出与之前相同的值。

另一种解决方案当然是明确设置节点的首选大小。

xyChart.setPrefWidth(...);
xyChart.setPrefHeight(...);

绑定不起作用的原因是因为 window 本身还没有计算出的大小,如前所述。