【问题标题】:How to create a image of a chart with Java如何使用 Java 创建图表的图像
【发布时间】:2018-07-14 17:45:26
【问题描述】:

我需要通过读取数据库中的一些条目来创建图表。我只想创建图表的图像并创建一个新的 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);
    }
}

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

【问题讨论】:

标签: java javafx charts


【解决方案1】:

以下是如何使用 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
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多