4 Replies Latest reply: Nov 1, 2012 9:37 PM by 971835 RSS

    How to save the chart to image file

    971835
      I try use JavaFx create a AreaChart and want to save as .png file. But the saved image is blank. Who can help me? Thanks.

      Below is my sample code.


      public class AreaChartSample extends Application {

           @Override
           public void start(Stage stage) {
                stage.setTitle("Area Chart Sample");
                final NumberAxis xAxis = new NumberAxis(1, 31, 1);
                final NumberAxis yAxis = new NumberAxis();
                final AreaChart<Number, Number> ac = new AreaChart<Number, Number>(
                          xAxis, yAxis);
                ac.setTitle("Temperature Monitoring (in Degrees C)");

                XYChart.Series seriesApril = new XYChart.Series();
                seriesApril.setName("April");
                seriesApril.getData().add(new XYChart.Data(1, 4));
                seriesApril.getData().add(new XYChart.Data(3, 10));
                seriesApril.getData().add(new XYChart.Data(6, 15));
                seriesApril.getData().add(new XYChart.Data(9, 8));
                seriesApril.getData().add(new XYChart.Data(12, 5));
                seriesApril.getData().add(new XYChart.Data(15, 18));
                seriesApril.getData().add(new XYChart.Data(18, 15));
                seriesApril.getData().add(new XYChart.Data(21, 13));
                seriesApril.getData().add(new XYChart.Data(24, 19));
                seriesApril.getData().add(new XYChart.Data(27, 21));
                seriesApril.getData().add(new XYChart.Data(30, 21));

                XYChart.Series seriesMay = new XYChart.Series();
                seriesMay.setName("May");
                seriesMay.getData().add(new XYChart.Data(1, 20));
                seriesMay.getData().add(new XYChart.Data(3, 15));
                seriesMay.getData().add(new XYChart.Data(6, 13));
                seriesMay.getData().add(new XYChart.Data(9, 12));
                seriesMay.getData().add(new XYChart.Data(12, 14));
                seriesMay.getData().add(new XYChart.Data(15, 18));
                seriesMay.getData().add(new XYChart.Data(18, 25));
                seriesMay.getData().add(new XYChart.Data(21, 25));
                seriesMay.getData().add(new XYChart.Data(24, 23));
                seriesMay.getData().add(new XYChart.Data(27, 26));
                seriesMay.getData().add(new XYChart.Data(31, 26));

                Scene scene = new Scene(ac, 800, 600);
                ac.getData().addAll(seriesApril, seriesMay);
                stage.setScene(scene);
                stage.show();

                WritableImage wim = new WritableImage((int) stage.getWidth(),
                          (int) stage.getHeight());
                scene.snapshot(wim);

                File file = new File("D:\\AreaChartImage.png");

                try {
                     ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
                } catch (Exception s) {
                }

                System.out.println("finished");

                // Platform.exit();
           }

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

      }
        • 1. Re: How to save the chart to image file
          James_D
          I got a blank chart, rather than a blank image (i.e. I saw axes but no data plotted).

          I think the problem is that you are snapshotting the image immediately, and by default the chart is animated (so it takes a short while for the data to appear visually once you display it).
          Add the line
          ac.setAnimated(false);
          before you add the data series to the chart, and it should fix it.
          • 2. Re: How to save the chart to image file
            shakir.gusaroff
            Hi. Create an image in a separate thread:

                import java.io.File;
            import javafx.application.Application;
            import javafx.application.Platform;
            import javafx.concurrent.Task;
            import javafx.embed.swing.SwingFXUtils;
            import javafx.event.ActionEvent;
            import javafx.event.EventHandler;
            import javafx.scene.Scene;
            import javafx.scene.chart.AreaChart;
            import javafx.scene.chart.NumberAxis;
            import javafx.scene.chart.XYChart;
            import javafx.scene.control.Button;
            import javafx.scene.image.WritableImage;
            import javafx.scene.layout.StackPane;
            import javafx.stage.Stage;
            import javax.imageio.ImageIO;
            
            public class AreaChartSample extends Application {
            
                @Override
                public void start(final Stage stage) {
                    stage.setTitle("Area Chart Sample");
                    final NumberAxis xAxis = new NumberAxis(1, 31, 1);
                    final NumberAxis yAxis = new NumberAxis();
                    final AreaChart<Number, Number> ac = new AreaChart<Number, Number>(
                            xAxis, yAxis);
                    ac.setAnimated(false);
                    ac.setTitle("Temperature Monitoring (in Degrees C)");
            
                    XYChart.Series seriesApril = new XYChart.Series();
                    seriesApril.setName("April");
                    seriesApril.getData().add(new XYChart.Data(1, 4));
                    seriesApril.getData().add(new XYChart.Data(3, 10));
                    seriesApril.getData().add(new XYChart.Data(6, 15));
                    seriesApril.getData().add(new XYChart.Data(9, 8));
                    seriesApril.getData().add(new XYChart.Data(12, 5));
                    seriesApril.getData().add(new XYChart.Data(15, 18));
                    seriesApril.getData().add(new XYChart.Data(18, 15));
                    seriesApril.getData().add(new XYChart.Data(21, 13));
                    seriesApril.getData().add(new XYChart.Data(24, 19));
                    seriesApril.getData().add(new XYChart.Data(27, 21));
                    seriesApril.getData().add(new XYChart.Data(30, 21));
            
                    XYChart.Series seriesMay = new XYChart.Series();
                    seriesMay.setName("May");
                    seriesMay.getData().add(new XYChart.Data(1, 20));
                    seriesMay.getData().add(new XYChart.Data(3, 15));
                    seriesMay.getData().add(new XYChart.Data(6, 13));
                    seriesMay.getData().add(new XYChart.Data(9, 12));
                    seriesMay.getData().add(new XYChart.Data(12, 14));
                    seriesMay.getData().add(new XYChart.Data(15, 18));
                    seriesMay.getData().add(new XYChart.Data(18, 25));
                    seriesMay.getData().add(new XYChart.Data(21, 25));
                    seriesMay.getData().add(new XYChart.Data(24, 23));
                    seriesMay.getData().add(new XYChart.Data(27, 26));
                    seriesMay.getData().add(new XYChart.Data(31, 26));
            
                    final Scene scene = new Scene(ac, 800, 600);
                    ac.getData().addAll(seriesApril, seriesMay);
            
            
                    stage.setScene(scene);
                    stage.show();
            
            
            
            
            
            
            
            
                    Task task = new Task<Void>() {
                        @Override
                        public Void call() {
            
                            Platform.runLater(
                                    new Runnable() {
                                        public void run() {
                                            try {
            
                                                WritableImage wim = new WritableImage(800, 600);
            
                                                scene.snapshot(wim);
                                                File file = new File("DAreaChartImage.png");
            
                                                ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
                                            } catch (Exception s) {
                                            }
            
                                            System.out.println("finished");
            
                                        }
                                    });
            
                            return null;
                        }
                    };
            
            
                    Thread th = new Thread(task);
                    th.start();
            
            
            
            // Platform.exit();
                }
            
                public static void main(String[] args) {
                    launch(args);
                }
            }
            • 3. Re: How to save the chart to image file
              971835
              Thanks for your help!
              • 4. Re: How to save the chart to image file
                971835
                Thanks for your help!