[英]How to create a image of a chart with Java
我需要通過讀取數據庫中的一些條目來創建圖表。 我只想創建圖表的圖像並創建一個新的png。 我更喜歡使用Java本機庫執行此操作,到目前為止,我已經能夠完成以下工作。
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
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.