如何在JavaFX BarChart中的sort方法中更改条形的位置?

时间:2018-02-13 21:42:13

标签: java sorting javafx charts

我需要显示一些排序方法(例如BubbleSort)工作并相互替换条形图。我想一步一步做。因此,在每个元素更改迭代中,我也在更改bar的位置并使用方法layout()来显示更改。但是,仅在此排序方法之后才会显示更改。我想逐步替换条形并显示可能有一点延迟(Thread.sleep(100);)。图表中的每个条形都与array_tmp中的元素相关联。

我该怎么做?

   public void sortBubble(){

    int n = array_tmp.length;
    String temp;
    XYChart.Data getChart1;
    XYChart.Data getChart2;
    for(int i=0; i < n; i++){
        for(int j=1; j < (n-i); j++){
            if(array_tmp[j-1].length() > array_tmp[j].length(){

                    getChart1 = barChart.getData().get(j - 1).getData().get(0);
                    getChart2 = barChart.getData().get(j).getData().get(0);
                    barChart.getData().get(j - 1).getData().remove(0);
                    barChart.getData().get(j).getData().remove(0);
                    barChart.layout();

                    barChart.getData().get(j - 1).getData().add(0, getChart2);
                    barChart.getData().get(j).getData().add(0, getChart1);

                temp = array_tmp[j-1];
                array_tmp[j-1] = array_tmp[j];
                array_tmp[j] = temp;

            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Animations,尤其是Transitions

以下是BarChart中移动条形图的简短示例。

public void start(Stage stage)
{
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    final BarChart<String, Number> barChart = new BarChart<String, Number>(xAxis, yAxis);

    XYChart.Series<String, Number> series1 = new XYChart.Series<String, Number>();
    series1.setName("Series One");
    XYChart.Data<String, Number> bar11 = new XYChart.Data<String, Number>("One", 10);
    series1.getData().add(bar11);
    XYChart.Data<String, Number> bar12 = new XYChart.Data<String, Number>("Two", 20);
    series1.getData().add(bar12);
    barChart.getData().add(series1);

    XYChart.Series<String, Number> series2 = new XYChart.Series<String, Number>();
    series2.setName("Series Two");
    XYChart.Data<String, Number> bar21 = new XYChart.Data<String, Number>("One", 5);
    series2.getData().add(bar21);
    XYChart.Data<String, Number> bar22 = new XYChart.Data<String, Number>("Two", 5);
    series2.getData().add(bar22);
    barChart.getData().add(series2);

    Scene scene = new Scene(barChart, 800, 600);
    stage.setScene(scene);
    stage.show();

    double pos12 = bar12.getNode().localToScene(bar12.getNode().getBoundsInLocal()).getMinX();
    double pos11 = bar11.getNode().localToScene(bar11.getNode().getBoundsInLocal()).getMinX();
    double diff = pos12 - pos11;

    TranslateTransition tt1 = new TranslateTransition(Duration.millis(2000), bar11.getNode());
    tt1.setByX(diff);
    tt1.setCycleCount(TranslateTransition.INDEFINITE);
    tt1.setAutoReverse(true);
    tt1.play();

    TranslateTransition tt2 = new TranslateTransition(Duration.millis(2000), bar12.getNode());
    tt2.setByX(-diff);
    tt2.setCycleCount(TranslateTransition.INDEFINITE);
    tt2.setAutoReverse(true);
    tt2.play();
}

如果您不喜欢动画,只需使用:bar.getNode().setTranslateX(xValueToGo);

PauseTransition对于非ui阻塞延迟非常有用:

    PauseTransition wait = new PauseTransition(Duration.millis(2000));
    wait.setOnFinished((e) ->
    {
        doThisLater();
    });
    wait.play();