如何组合图表和TableView作为图像png格式输出?

时间:2015-09-21 20:42:41

标签: java charts javafx png tableview

我有这两个完全工作的分离代码用于相同的目的我想要组合在一起,并能够以相同的png文件格式输出值。 查看图片http://postimg.org/image/wtszfwq77/

第一个代码

import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import java

fx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class TableViewSample2 extends Application {

    private TableView<Metric> table = new TableView<Metric>();

    private final ObservableList<Metric> data =
        FXCollections.observableArrayList(
            new Metric("1","2","3","4","5","6","7","8"),
                 new Metric("2","2","2","2","2","2","2","2"),
                  new Metric("3","3","3","3","3","3","3","3"),
                   new Metric("4","4","4","4","4","4","4","4"),
                    new Metric("5","5","6","6","5","5","5","5")


        );


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

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Result of Java Metrics Analysis");
    stage.setWidth(700);
    stage.setHeight(500);

    final Label label = new Label("Result of Java Metrics Analysis");
    label.setFont(new Font("Arial", 20));

    table.setEditable(true);



    // WMCCol,DITCol,NOCCol,CBOCol,RFCCol,LCOMCol,CaCol,NPMCol
    TableColumn WMCCol = new TableColumn("WMC");
    WMCCol.setMinWidth(100);
    WMCCol.setCellValueFactory(          new PropertyValueFactory<Metric, String>("WMC"));

    TableColumn DITCol = new TableColumn("DIT");
    DITCol.setMinWidth(100);
    DITCol.setCellValueFactory(        new PropertyValueFactory<Metric, String>("DIT"));

    TableColumn NOCCol = new TableColumn("NOC");
    NOCCol.setMinWidth(200);
    NOCCol.setCellValueFactory(             new PropertyValueFactory<Metric, String>("NOC"));


    TableColumn CBOCol = new TableColumn("CBO");
    CBOCol.setMinWidth(100);
    CBOCol.setCellValueFactory(          new PropertyValueFactory<Metric, String>("CBO"));

    TableColumn RFCCol = new TableColumn("RFC");
    RFCCol.setMinWidth(100);
    RFCCol.setCellValueFactory(        new PropertyValueFactory<Metric, String>("RFC"));

    TableColumn LCOMCol = new TableColumn("LCOM");
    LCOMCol.setMinWidth(200);
    LCOMCol.setCellValueFactory(             new PropertyValueFactory<Metric, String>("LCOM"));



    TableColumn CaCol = new TableColumn("Ca");
    CaCol.setMinWidth(100);
    CaCol.setCellValueFactory(          new PropertyValueFactory<Metric, String>("Ca"));

    TableColumn NPMCol = new TableColumn("NPM");
    NPMCol.setMinWidth(100);
    NPMCol.setCellValueFactory(        new PropertyValueFactory<Metric, String>("NPM"));




    table.setItems(data);
    table.getColumns().addAll(WMCCol,DITCol,NOCCol,CBOCol,RFCCol,LCOMCol,CaCol,NPMCol);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();


     WritableImage image = table.snapshot(new SnapshotParameters(), null);

// TODO: probably use a file chooser here
File file = new File("C:\\Users\\acer\\Desktop\\tableChart.png");

try {
    ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
    // TODO: handle exception here
}


}

public static class Metric {

    private final SimpleStringProperty WMC;
    private final SimpleStringProperty DIT;
    private final SimpleStringProperty NOC;

    private final SimpleStringProperty CBO;
    private final SimpleStringProperty RFC;
    private final SimpleStringProperty LCOM;  

    private final SimpleStringProperty Ca;
    private final SimpleStringProperty NPM;



    private Metric(String vWMC,String vDIT, String vNOC, String vCBO,String vRFC,String vLCOM, String vCa, String vNPM) {


        //
        this.WMC = new SimpleStringProperty(vWMC);
        this.DIT = new SimpleStringProperty(vDIT);
        this.NOC = new SimpleStringProperty(vNOC);

        this.CBO = new SimpleStringProperty(vCBO);
        this.RFC = new SimpleStringProperty(vRFC);
        this.LCOM = new SimpleStringProperty(vLCOM);

        this.Ca = new SimpleStringProperty(vCa);
        this.NPM = new SimpleStringProperty(vNPM);

    }


    public String getWMC() {            return WMC.get();  }
    public void setWMC(String vWMC) {     WMC.set(vWMC); }

    public String getDIT() {            return DIT.get();     }
    public void setDIT(String vDIT) {       DIT.set(vDIT);   }

    public String getNOC() {        return NOC.get();       }
    public void setNOC(String vNOC) {   NOC.set(vNOC);    }


    public String getCBO() {            return CBO.get();  }
    public void setCBO(String vCBO) {     CBO.set(vCBO); }

    public String getRFC() {            return RFC.get();     }
    public void setRFC(String vRFC) {       RFC.set(vRFC);   }

    public String getLCOM() {        return LCOM.get();       }
    public void setLCOM(String vLCOM) {   LCOM.set(vLCOM);    }


    public String getCa() {            return Ca.get();  }
    public void setCa(String vCa) {     Ca.set(vCa); }

    public String getNPM() {            return NPM.get();     }
    public void setNPM(String vNPM) {       NPM.set(vNPM);   }





    }
} 

第二个代码

package javaapplication12;


import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javax.imageio.ImageIO;


public class JavaFX_Charts extends Application {


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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Result of Java Metrics Analysis");
        Group root = new Group();

        ObservableList<PieChart.Data> pieChartData = 
                FXCollections.observableArrayList(
                    new PieChart.Data("WMC", 100),
                    new PieChart.Data("DIT", 200),
                    new PieChart.Data("NOC", 50),
                    new PieChart.Data("CBO", 75),
                    new PieChart.Data("RFC", 110),
                    new PieChart.Data("LCOM", 300),
                    new PieChart.Data("Ca", 111),
                    new PieChart.Data("NPM", 30)

                );

        final PieChart pieChart = new PieChart(pieChartData);
        pieChart.setTitle("RESULT");

        root.getChildren().add(pieChart);

        primaryStage.setScene(new Scene(root, 500, 400));

        primaryStage.show();


                    WritableImage image = pieChart.snapshot(new SnapshotParameters(), null);

                    // TODO: probably use a file chooser here
                    File file = new File("C:\\Users\\acer\\Desktop\\pieChart.png");

                    try {
                        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);

                       }   catch (IOException e) {
                        // TODO: handle exception here
                    }







}


    }

1 个答案:

答案 0 :(得分:1)

将JavaFX图表和TableView放在一个应用程序和snapshot the entire scene中的同一场景中,而不仅仅是饼图。