2

I want to employ JavaFx facilities to generate charts which will be included in a report generation module

I've read some code snippets that use the snapshot LineChart's method

// 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) {
}

But when I display the image it shows nothing, only a little coordinate axis at 0,0 position.

1 Answers1

1

If you want to get the screenshot of a node, first you have to render it in a Scene. You dont need to use that Scene in any stage.. just create a scene ->set the node you desired -> and call the applyCss method of the node. Then you can take the snapshot.

Below code worked for me. I have not rendered my 'lineChart' in the application.

Note: Just ensure that the dummy scene size is same as the dimensions of your image.

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) {
}
Sai Dandem
  • 8,229
  • 11
  • 26