gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:08:32 25 4
gpt4 key购买 nike

我做了一个非常短的应用程序,它使用 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

我不知道我做错了什么。

最佳答案

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

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

关于场景的 Javafx 快照不显示值和系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35317932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com