سؤال

Now it is another problem that occurred, it seems that the query I'm using only works for the first time but after pressing the next/previous buttons, it is giving me something else !!

Here is the query I used:

for (int i = 0; i <= months.length + 1; i++) {
        try {
            String a;
            if (i < 9) {
                a = y + "0" + (i + 1);
            } else {
                a = y + "" + (i + 1);
            }
            System.out.println("Année Courante " + a);
            conn = DBConnection.connect();
            String sql = "select sum(montant_operation) from operations where (select Extract(YEAR_MONTH from date_operation)) = '" + a + "' and typ_operation ='Versement';";
            final ResultSet rs = conn.prepareStatement(sql).executeQuery();
            if (rs.next()) {
                System.out.println(series1.getData().toString());
                series1.getData().add(new XYChart.Data<>(months[i], rs.getFloat("sum(montant_operation)")));
            }
        } catch (SQLException e) {
            System.out.println(e);
        }
    }

But is there a query that works fine one time and then it gives error.

Have a nice day

هل كانت مفيدة؟

المحلول

In your BuildData (by the way, not following Java Naming Conventions, change its name) method you are updating the data of series. In the same method you are adding this series to the chart. By clicking the "next" button, BuildData method is invoked where this chart is added again which is unnecessary. Delete the

Platform.runLater(() -> {  
    barchart.getData().add(series1);  
});

part from the method and add the chart only once in start:

...
...
vbox.getChildren().addAll(box, barchart);
barchart.getData().add(series1);
pane.getChildren().add(vbox);
...
...

The tested SSCCE:

import java.util.Calendar;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class BarChartDemo extends Application {

    final String[] months = {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"};
    //Connection conn;
    final CategoryAxis month_axis = new CategoryAxis();
    final NumberAxis data_axis = new NumberAxis();
    final XYChart.Series<String, Number> series1 = new XYChart.Series();
    private final BarChart<String, Number> barchart = new BarChart(month_axis, data_axis);
    private Integer year = 0;

    @Override
    public void start(Stage primaryStage) {
        year = Calendar.getInstance().get(Calendar.YEAR);
        Button btn_next = new Button("NEXT");
        Button btn_previous = new Button("PREVIOUS");
        HBox box = new HBox(50);
        box.getChildren().addAll(btn_previous, btn_next);
        box.setAlignment(Pos.TOP_CENTER);
        VBox vbox = new VBox(25);
        box.setPadding(new Insets(10, 0, 10, 0));
        FlowPane pane = new FlowPane(Orientation.VERTICAL);
        vbox.getChildren().addAll(box, barchart);
        barchart.getData().add(series1);
        pane.getChildren().add(vbox);
        Scene scene = new Scene(pane);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
        BuildData(year);
        btn_next.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                year += 1;
                BuildData(year);
            }
        });
        btn_previous.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                year -= 1;
                BuildData(year);
            }
        });
    }

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

    private void BuildData(Integer y) {
        series1.setName("Versement");
        month_axis.setLabel("Mois de l'Année");
        month_axis.setStyle("-fx-font-weight:BOLD;" + "-fx-font-size:15");
        data_axis.setLabel("Valeur des Opérations Bancaires");
        data_axis.setStyle("-fx-font-weight:BOLD;" + "-fx-font-size:15");
        series1.getData().clear(); // clear old values
        for (int i = 0; i < months.length; i++) {
            series1.getData().add(new XYChart.Data(months[i], i * 10 * (y-2000)));

//            try {
//                String a;
//                if (i < 9) {
//                    a = y + "0" + (i + 1);
//                } else {
//                    a = y + "" + (i + 1);
//                }
//                conn = DBConnection.connect();
//                String sql = "select sum(montant_operation) from operations where (select Extract(YEAR_MONTH from date_operation)) = '" + a + "' and typ_operation ='Versement';";
//                final ResultSet rs = conn.prepareStatement(sql).executeQuery();
//                if (rs.next()) {
//                    series1.getData().add(new XYChart.Data<>(months[i], rs.getFloat("sum(montant_operation)")));
//                }
//            } catch (SQLException e) {
//                System.out.println(e);
//            }
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top