场景的Javafx快照不显示值和系列

时间:2016-02-10 14:30:48

标签: java javafx charts screenshot snapshot

我做了一个非常简短的应用程序,它使用javafx生成图表。应用程序显示正确的值(图表),但是当我执行快照时,图像仅显示轴和标签,但不显示系列和值。

    stage.setTitle("Line Chart Sample");        
    final DateAxis xAxis = new DateAxis();
    final NumberAxis yAxis = new NumberAxis();

    xAxis.setLabel("Number of Month");
    yAxis.setLabel("Count");
    final LineChart<Date, Number> lineChart =  new LineChart<>(xAxis,yAxis);       
    Scene scene  = new Scene(lineChart,1000,700);

    lineChart.setTitle("Stock Monitoring, 2010");
    XYChart.Series series = new XYChart.Series();
    series.setName("My portfolio");

    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 15).getTime(), 23));
    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 16).getTime(), 14));
    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 17).getTime(), 15));
    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 18).getTime(), 24));
    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 19).getTime(), 34));
    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 20).getTime(), 36));
    series.getData().add(new XYChart.Data(new GregorianCalendar(2012, 11, 21).getTime(), 22));
    lineChart.getData().add(series);        
    stage.setScene(scene);
    stage.show();
    WritableImage img = new WritableImage(1000, 700); 
    File file = new File("saved.png");      
    scene.snapshot(img);        
    RenderedImage renderedImage = SwingFXUtils.fromFXImage(img, null);        
    ImageIO.write(renderedImage,"png", file);      

here is a saved file

here is a screenshot from app

我不知道我做错了什么。

1 个答案:

答案 0 :(得分:5)

默认情况下,图表是动画的。快照在(简短)动画完成之前发生,因此数据不会出现。呼叫

lineChart.setAnimated(false);

强制CSS的布局和应用(确定如何绘制数据的线和节点)也是明智的。通常这些仅在第一个场景渲染时完成,因此您可以在这些快照发生之前进行快照。你需要通过调用

来做到这一点
lineChart.applyCss();
lineChart.layout();
将图表添加到场景后,以及将数据添加到图表后

这是一个SSCCE:

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;

public class SnapshotChart extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        stage.setTitle("Line Chart Sample");        
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("Number of Month");
        yAxis.setLabel("Count");
        final LineChart<Number, Number> lineChart =  new LineChart<>(xAxis,yAxis); 
        lineChart.setAnimated(false);
        Scene scene  = new Scene(lineChart,1000,700);


        lineChart.setTitle("Stock Monitoring, 2010");
        XYChart.Series<Number, Number> series = new XYChart.Series<>();
        series.setName("My portfolio");

        series.getData().add(new XYChart.Data<>(1, 23));
        series.getData().add(new XYChart.Data<>(2, 14));
        series.getData().add(new XYChart.Data<>(3, 15));
        series.getData().add(new XYChart.Data<>(4, 24));
        series.getData().add(new XYChart.Data<>(5, 34));
        series.getData().add(new XYChart.Data<>(6, 36));
        series.getData().add(new XYChart.Data<>(7, 22));
        lineChart.getData().add(series);        

        lineChart.applyCss();
        lineChart.layout();         

        stage.setScene(scene);
        stage.show();
        WritableImage img = new WritableImage(1000, 700); 
        File file = new File("saved.png");      
        scene.snapshot(img);        
        RenderedImage renderedImage = SwingFXUtils.fromFXImage(img, null);        
        ImageIO.write(renderedImage,"png", file);  
    }

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