import java.io.File; import java.io.IOException; import javafx.application.Application; import javafx.collections.*; import javafx.embed.swing.SwingFXUtils; import javafx.scene.*; import javafx.stage.Stage; import javafx.scene.chart.*; import javafx.scene.control.Label; import javafx.scene.image.*; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javax.imageio.ImageIO; public class LineChartExport extends Application { @Override public void start(Stage stage) throws IOException { LineChart snapshotChart = createChart(); LineChart sceneChart = createChart(); sceneChart.setStyle("-fx-background-color: antiquewhite;"); // render an image of the chart to a file. Scene snapshotScene = new Scene(snapshotChart); SnapshotParameters params = new SnapshotParameters(); params.setFill(Color.ALICEBLUE); Image image = snapshotChart.snapshot(params, null); File file = new File("chart.png"); ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file); // layout the scene. VBox layout = new VBox(5); layout.getChildren().addAll( new Label("Wrote: " + file.getCanonicalPath()), new Label("Snapshot Chart"), new ImageView(new Image(file.toURI().toString())), new Label("Scene Chart"), sceneChart ); layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;"); System.getProperties().list(System.out); // show the scene. stage.setScene(new Scene(layout)); stage.show(); } private LineChart createChart() { final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("Number of Month"); final LineChart chart = new LineChart<>(xAxis,yAxis); chart.setTitle("Stock Monitoring, 2010"); XYChart.Series series = new XYChart.Series( "My portfolio", FXCollections.observableArrayList( new XYChart.Data(1, 23), new XYChart.Data(2, 14), new XYChart.Data(3, 15), new XYChart.Data(4, 24), new XYChart.Data(5, 34), new XYChart.Data(6, 36), new XYChart.Data(7, 22), new XYChart.Data(8, 45), new XYChart.Data(9, 43), new XYChart.Data(10, 17), new XYChart.Data(11, 29), new XYChart.Data(12, 25) ) ); chart.getData().add(series); return chart; } public static void main(String[] args) { launch(args); } }