场景的Javafx快照不显示值和系列 [英] Javafx-snapshot of scene doesnt show values and series

查看:209
本文介绍了场景的Javafx快照不显示值和系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a really short app, which uses javafx to generate a chart. App shows the right values(chart) but when I do snapshot, image shows just an axis and labels, but not the series and values.

    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);      

这是保存的文件

这里是应用程序的截图

我不知道我做错了什么。

I have no idea what I am doing wrong.

推荐答案

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

By default, charts are animated. The snapshot is happening before the (brief) animation is complete, so the data don't appear. Call

lineChart.setAnimated(false);

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

It is probably also sensible to force layout and application of CSS (which determines how the lines and nodes for the data are drawn). Typically these are only done on the first scene rendering, so it is possible your snapshot takes place before these happen. You need to do this by calling

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);
    }
}

这篇关于场景的Javafx快照不显示值和系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆