如何将多个JavaFX图表保存到一个pdf文件中?

时间:2019-05-04 19:52:15

标签: java pdf javafx charts

我正在尝试从六(6)个不同的JAVAFX图表(饼图,条形图和折线图)生成一个pdf文件。

我只能以.png格式生成一张图表的一张图像。这些图表全部显示在一个AnchorPane上,因此一次只显示一张图表。

下面是用于将图表另存为图像的代码

private void chartToImage(final Node node, final File file, final int aWidth, final int aHeight, final Node childNode){

        final AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMinSize(aWidth, aHeight);
        anchorPane.setMaxSize(aWidth, aHeight);
        anchorPane.setPrefSize(aWidth, aHeight);

        final ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(anchorPane);

        final JFXPanel fxPanel = new JFXPanel();
        fxPanel.setScene(new Scene(scrollPane));

        final JFrame frame = new JFrame();
        final AnchorPane previousBaby = childNode;
        final AnchorPane previousParentPane = (AnchorPane)childNode.getParent();



        //Creating an invisible JFrame
        frame.setSize(new Dimension(64, 64));
        frame.setVisible(false);
        frame.add(fxPanel);

        anchorPane.getChildren().clear();
        AnchorPane.setLeftAnchor(node, 0.0);
        AnchorPane.setRightAnchor(node, 0.0);
        AnchorPane.setTopAnchor(node, 0.0);
        AnchorPane.setBottomAnchor(node, 0.0);
        anchorPane.getChildren().add(node);

        anchorPane.layout();

        /**
         * Handling the Snapshot operation
         */

        try {
            final SnapshotParameters snapshotParameters = new SnapshotParameters();
            snapshotParameters.setViewport(new Rectangle2D(0.0, 0.0, aWidth, aHeight));
            ImageIO.write(SwingFXUtils.fromFXImage(anchorPane.snapshot(snapshotParameters, new WritableImage(aWidth, aHeight)),
                    new BufferedImage(aWidth, aHeight, BufferedImage.TYPE_INT_ARGB)), "png", file);
            AlertMaker.showSimpleAlert("Success", "Chart image saved successfully");
        } catch (IOException ex) {
            Logger.getLogger(PerformanceAnalyzerController.class.getName()).log(Level.SEVERE, null, ex);
            AlertMaker.showErrorMessage(ex, "Error", "Failed to save the Chart as Image");
        }finally{
            Platform.runLater(() -> {
                //Return the node back into it's previous parent

                previousParentPane.getChildren().clear();
                AnchorPane.setLeftAnchor(childNode, 0.0);
                AnchorPane.setRightAnchor(childNode, 0.0);
                AnchorPane.setTopAnchor(childNode, 0.0);
                AnchorPane.setBottomAnchor(childNode, 0.0);

                previousParentPane.getChildren().add(childNode);

                frame.dispose();

            });
        }
    }

我尝试使用ExecutorService捕获不同时间的所有图像,但是所有这些都是徒然的返回异常,即“线程“ pool-2-thread-1”中的异常” Java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = pool-2-thread-1“。

下面是用于在不同时间捕获图像的代码。

    @FXML
    private void performAnalysisOperation(ActionEvent event) {
        //Initialing the date ranges for searching
        LocalDate dateA1 = endDatePicker.getValue();
        LocalDate dateB1 = startDatePicker.getValue();
        System.out.println(dateA1 + "\n" + dateB1);

        String dateA = dateA1.toString() + " 00:00:00.00000";
        String dateB = dateB1.toString() + " 00:00:00.00000";

            analysisItems.setDisable(true);

            clearChart();
            piechart.setAnimated(false);
            barchart.setAnimated(false);
            linechart.setAnimated(false);


            Task task1 = new Task() {
                @Override
                protected Object call() throws Exception  {
                    clearChart();
                    ChartsHandler.barChartHandler1(barchart, chartsHandler.getBookGraphStatics(dateA, dateB), graphHandler, 1);
                    chartToImage(graphHandler, new File("C:\\Users\\Garande\\Documents/testChartGATA031T.png"), 600, 600, analyzercontainer);
                    return true;
                }
            };


            Task task2 = new Task() {
                @Override
                protected Object call() throws Exception  {
                    clearChart();
                    ChartsHandler.pieChartHandler(piechart, handler.getBookGraphStatics(), graphHandler, 1);
                    chartToImage(graphHandler, new File("C:\\Users\\Garande\\Documents/testChartGATA032T.png"), 600, 600, analyzercontainer);
                    return true;
                }
            };


            Task task3 = new Task() {
                @Override
                protected Object call() throws Exception  {
                    clearChart();
                    ChartsHandler.lineChartHandler(linechart, chartsHandler.getBookGraphStatics(dateA, dateB), graphHandler, 1);
                    chartToImage(graphHandler, new File("C:\\Users\\Garande\\Documents/testChartGATA033T.png"), 600, 600, analyzercontainer);
                    return true;
                }
            };

            //More tasks here for all the charts


            ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
            service.schedule(task2, 10, TimeUnit.SECONDS);
            service.schedule(task1, 30, TimeUnit.SECONDS);
            service.schedule(task3, 50, TimeUnit.SECONDS);

        }
    }

我希望程序为所有图表生成图像并将它们保存到一个pdf文件中。 当前正在使用pdfbox-2.0.13.jar从我的程序创建pdf文件。 但是我仍然不知道如何一次将所有图表保存在一个pdf文件中。 谢谢。

0 个答案:

没有答案