Charts JavaFX我想完全保存图表图像

Charts JavaFX我想完全保存图表图像,charts,javafx,Charts,Javafx,这是我的代码: public class SceneToImageAndWrite extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { stage.setTitle("Primary Stage");

这是我的代码:

public class SceneToImageAndWrite extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Primary Stage");

        final CategoryAxis xAxis = new CategoryAxis();
        xAxis.setLabel("Animals");
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Number");

        final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("Line Chart");

        XYChart.Series<String, Number> series1 = new Series<String, Number>();
        series1.setName("Series");

        /** xAxis & yAxis Data */
        LinkedHashMap<String, Number> map = new LinkedHashMap<>();      
        map.put("dog", 12);
        map.put("cat", 3);
        map.put("bear", 8);
        map.put("tiger", 20);

        for (Entry<String, Number> entry : map.entrySet()) {
            series1.getData().add(new XYChart.Data<String, Number>(entry.getKey(), entry.getValue()));
        }

        lineChart.getData().add(series1);

        Scene scene = new Scene(lineChart);
        stage.setScene(scene);
        stage.show();

        WritableImage snapShot = scene.snapshot(null);
        ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test.png"));
    }
}
公共类SceneToImageAndWrite扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage)引发异常{
阶段。片名(“初级阶段”);
最终CategoryAxis xAxis=新CategoryAxis();
xAxis.setLabel(“动物”);
最终数字axis yAxis=新数字axis();
yAxis.setLabel(“编号”);
最终线形图线形图=新线形图(xAxis,yAxis);
线形图。设置标题(“线形图”);
XYChart.Series系列1=新系列();
系列1.集合名称(“系列”);
/**xAxis和yAxis数据*/
LinkedHashMap=新建LinkedHashMap();
地图.put(“狗”,12);
地图放置(“cat”,3);
地图放置(“熊”,8);
地图.put("老虎",20);;
for(条目:map.entrySet()){
series1.getData().add(新的XYChart.Data(entry.getKey(),entry.getValue());
}
lineChart.getData().add(series1);
场景=新场景(折线图);
舞台场景;
stage.show();
WritableImage snapShot=scene.snapShot(空);
write(SwingFXUtils.fromFXImage(快照,null),“png”,新文件(“test.png”);
}
}
框架很好,但保存的图像并没有它应该拥有的所有图表数据。 我想完全保存图表图像,怎么了?⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

在图表上关闭

public class SceneToImageAndWrite extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Primary Stage");

        final CategoryAxis xAxis = new CategoryAxis();
        xAxis.setLabel("Animals");
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Number");

        final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("Line Chart");

        XYChart.Series<String, Number> series1 = new Series<String, Number>();
        series1.setName("Series");

        /** xAxis & yAxis Data */
        LinkedHashMap<String, Number> map = new LinkedHashMap<>();      
        map.put("dog", 12);
        map.put("cat", 3);
        map.put("bear", 8);
        map.put("tiger", 20);

        for (Entry<String, Number> entry : map.entrySet()) {
            series1.getData().add(new XYChart.Data<String, Number>(entry.getKey(), entry.getValue()));
        }

        lineChart.getData().add(series1);

        Scene scene = new Scene(lineChart);
        stage.setScene(scene);
        stage.show();

        WritableImage snapShot = scene.snapshot(null);
        ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test.png"));
    }
}
从javadoc:

public class SceneToImageAndWrite extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Primary Stage");

        final CategoryAxis xAxis = new CategoryAxis();
        xAxis.setLabel("Animals");
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Number");

        final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("Line Chart");

        XYChart.Series<String, Number> series1 = new Series<String, Number>();
        series1.setName("Series");

        /** xAxis & yAxis Data */
        LinkedHashMap<String, Number> map = new LinkedHashMap<>();      
        map.put("dog", 12);
        map.put("cat", 3);
        map.put("bear", 8);
        map.put("tiger", 20);

        for (Entry<String, Number> entry : map.entrySet()) {
            series1.getData().add(new XYChart.Data<String, Number>(entry.getKey(), entry.getValue()));
        }

        lineChart.getData().add(series1);

        Scene scene = new Scene(lineChart);
        stage.setScene(scene);
        stage.show();

        WritableImage snapShot = scene.snapshot(null);
        ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test.png"));
    }
}
当通过应用程序显式或隐式(如图表动画)拍摄正在设置动画的场景的快照时,快照将基于拍摄快照时场景图的状态进行渲染,并且不会反映任何后续动画更改

public class SceneToImageAndWrite extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Primary Stage");

        final CategoryAxis xAxis = new CategoryAxis();
        xAxis.setLabel("Animals");
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Number");

        final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("Line Chart");

        XYChart.Series<String, Number> series1 = new Series<String, Number>();
        series1.setName("Series");

        /** xAxis & yAxis Data */
        LinkedHashMap<String, Number> map = new LinkedHashMap<>();      
        map.put("dog", 12);
        map.put("cat", 3);
        map.put("bear", 8);
        map.put("tiger", 20);

        for (Entry<String, Number> entry : map.entrySet()) {
            series1.getData().add(new XYChart.Data<String, Number>(entry.getKey(), entry.getValue()));
        }

        lineChart.getData().add(series1);

        Scene scene = new Scene(lineChart);
        stage.setScene(scene);
        stage.show();

        WritableImage snapShot = scene.snapshot(null);
        ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test.png"));
    }
}

非常感谢你。线形图。设置动画(假);