BarChart更新

时间:2013-11-02 21:13:01

标签: multithreading javafx bar-chart auto-update

现在又出现了另一个问题,似乎我正在使用的查询仅在第一次工作,但在按下下一个/上一个按钮后,它给了我别的东西!!

以下是我使用的查询:

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);
        }
    }

但是有一个查询工作正常一次然后它会给出错误。

度过愉快的一天

1 个答案:

答案 0 :(得分:0)

BuildData(顺便说一下,不遵循Java命名约定,更改其名称)方法中,您正在更新系列数据。在同一方法中,您将此系列添加到图表中。通过单击“下一步”按钮,将调用BuildData方法再次添加此图表,这是不必要的。删除

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

部分来自该方法并在开始时仅添加一次图表:

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

经过测试的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);
//            }
        }
    }
}