gpt4 book ai didi

java - 如何使用 Java 创建图表图像

转载 作者:行者123 更新时间:2023-11-30 06:05:59 25 4
gpt4 key购买 nike

我需要通过读取数据库中的一些条目来创建图表。我只想创建图表的图像并创建一个新的 png。我更喜欢使用 Java native 库来完成此操作,到目前为止我已经能够完成以下工作。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import javafx.scene.chart.*;
import javafx.scene.Group;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.ImageIO;

public class Test extends Application {

@Override public void start(Stage stage) {

Scene scene = new Scene(new Group());
stage.setTitle("Imported Fruits");
stage.setWidth(500);
stage.setHeight(500);

ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Imported Fruits");

((Group) scene.getRoot()).getChildren().add(chart);
stage.setScene(scene);
WritableImage img = new WritableImage(800, 800);
scene.snapshot(img);
writeImage(Paths.get("/Users/images"), "test.png", img);
}

public static void writeImage(Path path, String fileName, WritableImage image) {
File file = new File(Paths.get(path.toString(), fileName).toString());

try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}

}

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

在上面的示例中,我需要启动一个应用程序并创建图表。我只需要创建图表的图像并将其写入文件,有没有办法在不启动应用程序的情况下完成此操作?或者有更好的方法吗?

最佳答案

以下是如何使用 JavaFX 完成此操作,

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.imageio.ImageIO;

public class Test {

public static void main(String[] args) {

String chartGenLocation = "/Users/work/tmp";
new JFXPanel();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Failed", 10),
new PieChart.Data("Skipped", 20));
final PieChart chart = new PieChart(pieChartData);
chart.setAnimated(false);
Platform.runLater(() -> {
Stage stage = new Stage();
Scene scene = new Scene(chart, 500, 500);
stage.setScene(scene);
WritableImage img = new WritableImage(500, 500);
scene.snapshot(img);

File file = new File(Paths.get(chartGenLocation, "a.png").toString());
try {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", file);
} catch (IOException e) {
//logger.error("Error occurred while writing the chart image
}
});
}
}

关于java - 如何使用 Java 创建图表图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341920/

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