如何从FXML中将数据从数据库获取到线图?

时间:2016-01-11 18:29:24

标签: java javafx javafx-2 javafx-8 fxml

我有问题如何从FXML中的表数据库Oracle到LineChart获取数据?我无法弄清楚我做错了什么。

这是FXML的一部分:

...
    <LineChart fx:id="graph" layoutX="365.0" layoutY="42.0" prefHeight="480.0" prefWidth="610.0">
        <xAxis>
            <CategoryAxis label="Datum" side="BOTTOM" />
        </xAxis>
        <yAxis>
            <NumberAxis label="Spotřaba v m3" side="LEFT" />
        </yAxis>
    </LineChart>
    ...

这是控制器部分:

...

public class PlynViewController implements Initializable {
    ....
    private ObservableList<StavPlynomeru> listSP = FXCollections.observableArrayList();

    @FXML 
    private LineChart<String, Integer> graph;    
    @FXML
    private NumberAxis yAxis;
    @FXML
    private CategoryAxis xAxis;
    @FXML
    private ObservableList<XYChart.Series<String, Integer>> lineChartData = FXCollections.observableArrayList();
    @FXML            
    private LineChart.Series<String, Integer> series1 = new LineChart.Series<String, Integer>();
    static ObservableList<StavPlynomeru> dataen =  FXCollections.observableArrayList();

    EnergiePlynSQL epsql = new EnergiePlynSQL();      

    @Override
    public void initialize(URL url, ResourceBundle rb) {          

        StavPlynomeru sp = new StavPlynomeru();

        for(StavPlynomeru record : dataen)
        {
            series1.getData().add(new XYChart.Data<String, Integer>(record.getDatum(),record.getSpotreba()));
        }

        lineChartData.add(series1);

        graph.setData(lineChartData);
        graph.createSymbolsProperty();
    }
}

这是GET和SET:

import java.io.Serializable;

public class StavPlynomeru implements Serializable {

    private String datum;
    private int spotreba;

    public StavPlynomeru() {
    }

    public StavPlynomeru(String datum, int spotreba) {
        this.datum = datum;
        this.spotreba = spotreba;
    }

    public String getDatum() {
        return datum;
    }

    public void setDatum(String datum) {
        this.datum = datum;
    }

    public int getSpotreba() {
        return spotreba;
    }

    public void setSpotreba(int spotreba) {
        this.spotreba = spotreba;
    }
}

这是SQL的一部分:

...
public ObservableList<StavPlynomeru> listStavPlynomeru(){
    try {
        connected();
        ObservableList<StavPlynomeru>list = FXCollections.observableArrayList();
        ResultSet rs = statement.executeQuery("SELECT TO_CHAR(datum, 'dd.mm.yyyy') AS datum,\n" +
                                              "  spotreba\n" +
                                              "FROM "+table_sp+"\n" +
                                              "WHERE archivace = 'N'\n" +
                                              "ORDER BY TO_DATE(datum, 'dd.mm.yyyy') DESC");
        while(rs.next()){
            StavPlynomeru sp = new StavPlynomeru();
            sp.setDatum(rs.getString(1));
            sp.setSpotreba(rs.getInt(2));
            list.add(sp);
        }
        return list;
    } catch (Exception e) {
        e.printStackTrace();
        Dialogs.create()
            .title("Exception Dialog")
            .showException(e);
        return null;
    }finally{
        closed();
    }        
}
...

有人可以帮助我吗?

0 个答案:

没有答案