我想利用JavaFx工具生成图表,该图表将包含在报告生成模块中
我已经阅读了一些使用快照LineChart方法的代码段
// lineChart previously properly. It actually renders in a Window (on
// another JavaFX application). But this test case doesn't display it.
WritableImage wi = lineChart.snapshot(new SnapshotParameters(), new WritableImage(250, 250));
File file = new File("CanvasImage.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", file);
} catch (Exception s) {
}
但是当我显示图像时,它什么也没显示,在0,0位置上只有一点坐标轴。
答案 0 :(得分:1)
如果要获取节点的屏幕快照,首先必须在场景中进行渲染。您无需在任何阶段使用该场景。.只需创建一个场景->设置所需的节点->并调用该节点的applyCss方法。然后即可拍摄快照。
以下代码对我有用。我尚未在应用程序中呈现我的“ lineChart”。
注意:只需确保虚拟场景的大小与图像的大小相同。
LineChart<String, Number> lineChart= buildChart();
new Scene(lineChart,800, 600);
lineChart.applyCss();
WritableImage wi = lineChart.snapshot(new SnapshotParameters(), new WritableImage(800, 600));
File file = new File("CanvasImage.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", file);
} catch (Exception s) {
}