Search code examples
javadatabasejavafxbar-chartdynamic-data

Dynamic data in barchart not working properly javaFX


I am trying to load data from a result set but when the chart is displayed some of the bars are missing.

Following is my code:

        ResultSet rs = ps.executeQuery();

        XYChart.Series<String, Double> series = new XYChart.Series<>();
        while (rs.next()) {
            String name = rs.getString(1);
            Double no = rs.getDouble(2);
            series.getData().add(new XYChart.Data<>(name, no));
            barchart1.getData().add(series);
        }

Solution

  • You have to create a new series for each row-data,

    ResultSet rs = ps.executeQuery();
    
    while (rs.next()) {
         String name = rs.getString(1);
         Double no = rs.getDouble(2);
         XYChart.Series<String, Double> series = new XYChart.Series<>();
         series.getData().add(new XYChart.Data<>(name, no));
         barchart1.getData().add(new XYChart.Data<>);
    }