重新创建条形图而不记住数据 [英] Recreate bar chart without it remembering data

查看:167
本文介绍了重新创建条形图而不记住数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务

我有一个BarChart,我必须一遍又一遍地填充不同的数据。数据必须按预定义顺序排列。当然我只想更改数据而不是每次都创建条形图。

I have a BarChart which I have to fill with different data over and over again. The data must be in a predefined order. Of course I only want to change the data and not create a bar chart every time.

问题

如果您的某个类别具有已存在于早期数据中的给定名称,则订单错误。

Once you have a category with a given name that already existed in earlier data, the order is wrong.

我创建了一个例子。通过单击Insert Before按钮,可以添加给定数量的柱。之后添加静态栏。

I created an example. By clicking on the "Insert Before" button, a given number of bars is added. After that the "static" bar is added.

Insert After按钮首先添加静态栏,然后添加其他栏。

The "Insert After" button adds the "static" bar first and then the other bars.

public class BarChartSample extends Application {

    int count = 1;

    @Override
    public void start(Stage stage) {

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
        bc.setAnimated(false);

        // create new chart data where the data are inserted before the "static" bar
        Button insertBeforeButton = new Button("Insert Before");
        insertBeforeButton.setOnAction(e -> {

            XYChart.Series series1 = new XYChart.Series();

            for (int i = 0; i < count; i++) {
                series1.getData().add(new XYChart.Data("New " + i, 50));
            }

            series1.getData().add(new XYChart.Data("Static", 100));

            bc.getData().setAll(series1);

            count++;

        });

        // create new chart data where the data are inserted after the "static" bar
        Button insertAfterButton = new Button("Insert After");
        insertAfterButton.setOnAction(e -> {

            XYChart.Series series1 = new XYChart.Series();

            series1.getData().add(new XYChart.Data("Static", 100));

            for (int i = 0; i < count; i++) {
                series1.getData().add(new XYChart.Data("New " + i, 50));
            }

            bc.getData().setAll(series1);

            count++;

        });

        // borderpane for chart and toolbar
        BorderPane bp = new BorderPane();
        bp.setCenter(bc);

        // toolbar
        HBox hb = new HBox();
        hb.getChildren().addAll(insertBeforeButton, insertAfterButton);
        bp.setTop(hb);

        Scene scene = new Scene(bp, 400, 200);

        stage.setScene(scene);
        stage.show();
    }

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

启动程序并点击之前插入 ,你得到这个:

When you start the program and click on "Insert Before", you get this:

当你重新启动程序并点击Insert After时,你得到这个:

When you restart the program and click on "Insert After", you get this:

重新启动程序并单击在Insert Before然后在Insert After上,你得到这个:

When you restart the program and click on "Insert Before" and then on "Insert After", you get this:

这是错误的。它应该是这样的:

which is wrong. It should be this:

有没有办法清除条形图的记忆?显然setData是不够的。

Is there a way to clear the barchart's memory? Obviously setData isn't sufficient.

我怀疑它与条形图中的特殊删除方法有关,并且当新的时候数据没有完全删除数据被添加。在JavaFX的BarChart类的源代码中有一些可疑的方法,如seriesBeingRemovedIsAdded。

I suspect it has something to do with the special removal methods in the bar chart and that the data weren't removed totally when the new data are added. There are some dubious methods like "seriesBeingRemovedIsAdded" in the source code of the BarChart class in JavaFX.

非常感谢您的帮助!

推荐答案

嗯..再次调用 layout()似乎解决了问题

Well.. again calling the layout() seems to solve the problem

bc.getData().clear();
bc.layout();
bc.getData().addAll( series1 );

在Insert After第二个循环中。

in the "Insert After" second loop.

这篇关于重新创建条形图而不记住数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆