Javafx - Implementasi dari database ke BarChart

Selamat siang agan dan aganwati sekalian, kali ini ane akan beri tutor lagi, bagaimana mengimplementasikan BarChart di javafx dan tutor kali ini masih menyambung dari tutor sebelumnya yaitu CRUD JAVAFX . tool yang dibutuhkan yaitu:

Netbeans
Scene Builder
Paham MVC pattern
Gorengan dan kopi

Kira2 Penampakannya seperti Dibawah ini :
   
     1. yang ini adalah grafik chart tahun kelahiran. 

     2. yang ini dari tutor kemarin, tetapi ane telah beri tab yang ditandai merah..

  • Pertama - tama bikin file .fxml baru, desain dengan Scene Builder Seperti Dibawah ini :



  • modelGrafik.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.model;
     
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
     
    /**
     *
     * @author herudi-pc
     */
    public class modelGrafik {
        private final StringProperty tahun = new SimpleStringProperty();
        private final IntegerProperty jumlahNama = new SimpleIntegerProperty();
         
     
        public modelGrafik() {
        }
     
        public String getTahun() {
            return tahun.get();
        }
     
        public void setTahun(String value) {
            tahun.set(value);
        }
     
        public StringProperty tahunProperty() {
            return tahun;
        }
         
        public Integer getJumlahNama() {
            return jumlahNama.get();
        }
     
        public void setJumlahNama(Integer value) {
            jumlahNama.set(value);
        }
     
        public IntegerProperty jumlahNamaProperty() {
            return jumlahNama;
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.model;
    
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    
    /**
     *
     * @author herudi-pc
     */
    public class modelGrafik {
        private final StringProperty tahun = new SimpleStringProperty();
        private final IntegerProperty jumlahNama = new SimpleIntegerProperty();
        
    
        public modelGrafik() {
        }
    
        public String getTahun() {
            return tahun.get();
        }
    
        public void setTahun(String value) {
            tahun.set(value);
        }
    
        public StringProperty tahunProperty() {
            return tahun;
        }
        
        public Integer getJumlahNama() {
            return jumlahNama.get();
        }
    
        public void setJumlahNama(Integer value) {
            jumlahNama.set(value);
        }
    
        public IntegerProperty jumlahNamaProperty() {
            return jumlahNama;
        }
        
    }
    
  • interGrafik.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.interfaces;
     
    import biodata.model.modelGrafik;
    import javafx.collections.ObservableList;
     
    /**
     *
     * @author herudi-pc
     */
    public interface interGrafik {
        ObservableList<modelGrafik> getTahunKelahiran();
        ObservableList<Object> tahunKelahiranToGrafik();
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.interfaces;
    
    import biodata.model.modelGrafik;
    import javafx.collections.ObservableList;
    
    /**
     *
     * @author herudi-pc
     */
    public interface interGrafik {
        ObservableList<modelGrafik> getTahunKelahiran();
        ObservableList<Object> tahunKelahiranToGrafik();
    }
    
  • implGrafik.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.implement;
     
    import biodata.interfaces.interGrafik;
    import biodata.koneksi.koneksi;
    import biodata.model.modelGrafik;
    import java.sql.ResultSet;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.chart.XYChart;
     
     
    public class implGrafik implements interGrafik {
        koneksi k;
     
        @Override
        public ObservableList<modelGrafik> getTahunKelahiran() {
            k = new koneksi();
            ObservableList<modelGrafik> listData = FXCollections.observableArrayList();
            try {
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {      
                    modelGrafik m = new modelGrafik();
                    m.setTahun(rs.getString("tahun"));
                    m.setJumlahNama(rs.getInt("jumlahNama"));
                    listData.add(m);
                }
            } catch (Exception e) {
            }
            return listData;
        }
     
        @Override
        public ObservableList<Object> tahunKelahiranToGrafik() {
            ObservableList<Object> barCar = FXCollections.observableArrayList();
            try {
                k = new koneksi();
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {      
                    XYChart.Series<String, Integer> aSeries = new XYChart.Series<>();
                    aSeries.getData().add(new XYChart.Data(rs.getString("tahun"), rs.getInt("jumlahNama")));
                    barCar.add(aSeries);
                }
            } catch (Exception e) {
            }
            return barCar;
        }
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.implement;
    
    import biodata.interfaces.interGrafik;
    import biodata.koneksi.koneksi;
    import biodata.model.modelGrafik;
    import java.sql.ResultSet;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.chart.XYChart;
    
    
    public class implGrafik implements interGrafik {
        koneksi k;
    
        @Override
        public ObservableList<modelGrafik> getTahunKelahiran() {
            k = new koneksi();
            ObservableList<modelGrafik> listData = FXCollections.observableArrayList();
            try {
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {       
                    modelGrafik m = new modelGrafik();
                    m.setTahun(rs.getString("tahun"));
                    m.setJumlahNama(rs.getInt("jumlahNama"));
                    listData.add(m);
                }
            } catch (Exception e) {
            }
            return listData;
        }
    
        @Override
        public ObservableList<Object> tahunKelahiranToGrafik() {
            ObservableList<Object> barCar = FXCollections.observableArrayList();
            try {
                k = new koneksi();
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {       
                    XYChart.Series<String, Integer> aSeries = new XYChart.Series<>();
                    aSeries.getData().add(new XYChart.Data(rs.getString("tahun"), rs.getInt("jumlahNama")));
                    barCar.add(aSeries);
                }
            } catch (Exception e) {
            }
            return barCar;
        }
    }
    
  • grafikController.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.controller;
     
    import biodata.implement.implGrafik;
    import biodata.interfaces.interGrafik;
    import biodata.model.modelGrafik;
    import de.jensd.fx.fontawesome.AwesomeDude;
    import de.jensd.fx.fontawesome.AwesomeIcon;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.StackedBarChart;
    import javafx.scene.control.Button;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
     
    /**
     * FXML Controller class
     *
     * @author herudi-pc
     */
    public class grafikController implements Initializable {
        @FXML
        private TableView<modelGrafik> tableDetail;
        @FXML
        private TableColumn<modelGrafik, String> colDetailTahun;
        @FXML
        private TableColumn<modelGrafik, String> colDetailJumlah;
        @FXML
        private StackedBarChart bar;
        @FXML
        private NumberAxis barY;
        @FXML
        private CategoryAxis barX;
        @FXML
        private Button btnRefresh;
        ObservableList<Object> dataGrafik = FXCollections.observableArrayList();
        ObservableList<modelGrafik> dataDetail = FXCollections.observableArrayList();
        interGrafik crudGrafik = new implGrafik();
     
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            colDetailJumlah.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().jumlahNamaProperty().asString());
            colDetailTahun.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().tahunProperty());
            AwesomeDude.setIcon(btnRefresh, AwesomeIcon.CHAIN_BROKEN, "15px");
            tampilData();
            // TODO
        
         
        private void tampilData(){
            dataDetail = crudGrafik.getTahunKelahiran();
            dataGrafik = crudGrafik.tahunKelahiranToGrafik();
            bar.setData(dataGrafik);
            tableDetail.setItems(dataDetail);
        }
         
        @FXML
        private void aksiRefresh(ActionEvent e){
            bar.setAnimated(true);
            barY.setAnimated(true);
            barX.setAnimated(false);
            tampilData();
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.controller;
    
    import biodata.implement.implGrafik;
    import biodata.interfaces.interGrafik;
    import biodata.model.modelGrafik;
    import de.jensd.fx.fontawesome.AwesomeDude;
    import de.jensd.fx.fontawesome.AwesomeIcon;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.StackedBarChart;
    import javafx.scene.control.Button;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    
    /**
     * FXML Controller class
     *
     * @author herudi-pc
     */
    public class grafikController implements Initializable {
        @FXML
        private TableView<modelGrafik> tableDetail;
        @FXML
        private TableColumn<modelGrafik, String> colDetailTahun;
        @FXML
        private TableColumn<modelGrafik, String> colDetailJumlah;
        @FXML
        private StackedBarChart bar;
        @FXML
        private NumberAxis barY;
        @FXML
        private CategoryAxis barX;
        @FXML
        private Button btnRefresh;
        ObservableList<Object> dataGrafik = FXCollections.observableArrayList();
        ObservableList<modelGrafik> dataDetail = FXCollections.observableArrayList();
        interGrafik crudGrafik = new implGrafik();
    
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            colDetailJumlah.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().jumlahNamaProperty().asString());
            colDetailTahun.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().tahunProperty());
            AwesomeDude.setIcon(btnRefresh, AwesomeIcon.CHAIN_BROKEN, "15px");
            tampilData();
            // TODO
        }  
        
        private void tampilData(){
            dataDetail = crudGrafik.getTahunKelahiran();
            dataGrafik = crudGrafik.tahunKelahiranToGrafik();
            bar.setData(dataGrafik);
            tableDetail.setItems(dataDetail);
        }
        
        @FXML
        private void aksiRefresh(ActionEvent e){
            bar.setAnimated(true);
            barY.setAnimated(true);
            barX.setAnimated(false);
            tampilData();
        }
        
    }
    
  • di biodataController.java tambahkan source ini di awal inisialisasi
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    //tab biodata
    @FXML
    private Tab tabBiodata;
     
    //tab grafik
    @FXML
    private Tab tabGrafikKelahiran;
     
    //load anchorPane yang ada di tab ke 2
    @FXML
    private AnchorPane paneLoadGrafik;
    //tab biodata
    @FXML
    private Tab tabBiodata;
    
    //tab grafik
    @FXML
    private Tab tabGrafikKelahiran;
    
    //load anchorPane yang ada di tab ke 2
    @FXML
    private AnchorPane paneLoadGrafik;
    
  • masih di biodataController.java buat perintah ini untuk load grafik.fxml dan tampilkan di constructor
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    //load view grafik.fxml
    private void loadGrafik(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            Parent p = fxmlLoader.load(getClass().getResourceAsStream("/biodata/view/grafik.fxml"));
            paneLoadGrafik.getChildren().add(p);  
        } catch (IOException e) {
        }  
    }
    //load view grafik.fxml
    private void loadGrafik(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            Parent p = fxmlLoader.load(getClass().getResourceAsStream("/biodata/view/grafik.fxml"));
            paneLoadGrafik.getChildren().add(p);   
        } catch (IOException e) {
        }   
    }
    
    oke ane rasa sudah jelas, bila ada yang ditanyakan silahkan tanyakan agar tidak tersesat dikamar :D
    2 komentar :

    2 komentar :

    1. Mas untuk yang daftar baju, saya coba eror, di login user saya coba dalam combo box kosong juga kosong, tetap eror, kalau saya lihat dalam source code harusnya keluar jendela dialog, ini tidak sama sekali berbeda dengan biodata dimana erornya saya cek memberitahu belum connect, kalau daftar harga baju keluar erornya seperti ini :

      Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      ....................................................................

      sebenarnya banyak dibawahnya, tapi setahu saya baris pertama dan kedua yang penting, semoga ada pencerahanya. Terima kasih saya sudah belajar dari Mas Rudi, tapi saya penasaran untuk perpindahan scene terjadi eror umpama saya buat frame dengan menu bar dengan menu item informasi, waktu saya tekan pilihan menu informasi keluar eror yang sama seperti yang terjadi pada daftar harga baju.

      BalasHapus
    2. saya coba biodata untuk yang barchart sama sekali tidak ada respon, Mas, kosong juga tabel yang ada di sebelahnya. untuk tambah, edit, hapus berjalan lancar, mohon pencerahannya. Terima kasih.

      BalasHapus