动态更新 JavaFX BarChart

Update JavaFX BarChart dynamically

正如标题所说。基本上我有一个 ArrayList,其中 DiceRoll 是一个 class,它在 TableView 中成功用于在值发生变化时更新值。

public class DiceRoll {
private final SimpleIntegerProperty rollNumber = new SimpleIntegerProperty();
private final SimpleIntegerProperty cubeRoll = new SimpleIntegerProperty(0);
private final SimpleIntegerProperty icosahedron = new SimpleIntegerProperty(0);
private final SimpleIntegerProperty dodecahedron = new SimpleIntegerProperty(0);

public DiceRoll(int rollNumber) {
    this.rollNumber.set(rollNumber);
}

public void incrementRoll(DiceTypes type){
    switch(type){
        case CUBE:
            this.cubeRoll.set(this.cubeRoll.getValue() + 1);
            break;
        case DODECAHEDRON:
            this.dodecahedron.set(this.dodecahedron.getValue() + 1);
            break;
        case ICOSAHEDRON:
            this.icosahedron.set(this.icosahedron.getValue() + 1);
            break;
    }
}

public int getRollNumber() {
    return rollNumber.get();
}

public SimpleIntegerProperty rollNumberProperty() {
    return rollNumber;
}

public int getCubeRoll() {
    return cubeRoll.get();
}

public SimpleIntegerProperty cubeRollProperty() {
    return cubeRoll;
}

public int getIcosahedron() {
    return icosahedron.get();
}

public SimpleIntegerProperty icosahedronProperty() {
    return icosahedron;
}

public int getDodecahedron() {
    return dodecahedron.get();
}

public SimpleIntegerProperty dodecahedronProperty() {
    return dodecahedron;
}

public void reset(){
    cubeRoll.set(0);
    icosahedron.set(0);
    dodecahedron.set(0);
}

}

像这样:

rollNumberColumn.setCellValueFactory(new PropertyValueFactory<>("rollNumber"));

这一切都有效,尽管我更喜欢 non-factory 方法。但是我无法弄清楚如何与条形图进行相同类型的链接。

我的条形图有一个 CategoryAxis 作为 X 轴,一个 NumberAxis 作为 Y 轴。我可以使用 XYGraph.Series 和 XYGraph.Data 方法成功添加数据,但这些方法不会自动更新。我该怎么做?

编辑:

    TableColumn<DiceRoll, Number> rollNumberColumn = new TableColumn<>("Roll Number");        
    rollNumberColumn.setCellValueFactory(new PropertyValueFactory<>("rollNumber"));        
    tableView.setItems(FXCollections.observableArrayList(model.getDiceRolls())); //model.getDiceRolls() returns an ArrayList<DiceRoll>
    tableView.getColumns().setAll(rollNumberColumn, cubeRollNumberColumn, dodecahedronRollNumberColumn, icosahedronRollNumberColumn);

一个 DiceRoll 基本上被初始化为一个你可以滚动的数字。我已经在我的 table x20 中为所有 20 个可能的卷添加了这个,如果它被滚动就添加它。在图表形式中,这意味着 3 个图表,X 轴为滚动次数,Y 轴为滚动次数。

工厂评论与我的问题有点无关,但我真的不喜欢使用这里使用的工厂模式。

编辑2:

ObservableList<DiceRoll> testing = FXCollections.observableArrayList(model.getDiceRolls());
    testing.addListener((ListChangeListener)c -> System.out.println("I'm working!"));

我尝试按照 Chris 的建议执行此操作,但程序没有打印任何内容。 observableArrayList() returns 可能是一个新列表而不是包装现有引用,但这意味着我的 table 也不起作用。有什么问题吗?

编辑 3:

错误的是,当引用的值发生变化时,observables "change"。而 observablelist 的值是其他引用,在我的例子中,它们被添加一次并且再也不会被触及。这意味着它在启动后的程序生命周期中实际上永远不会改变。我发现我可以将侦听器附加到 DiceRoll 中的 IntegerProperties,以便在其中一个更改时做任何我想做的事,这太棒了,并且给了我非常需要的控制权。

edit4

this.model.getDiceRolls().parallelStream().forEach(diceRoll -> {
        diceRoll.cubeRollProperty().addListener((observable, oldValue, newValue) ->
                cubeSeries.getData().get(diceRoll.getRollNumber() - 1).setYValue(newValue)
        );
        diceRoll.dodecahedronRollProperty().addListener((observable, oldValue, newValue) ->
                dodecahedronSeries.getData().get(diceRoll.getRollNumber() - 1).setYValue(newValue)
        );
        diceRoll.icosahedronRollProperty().addListener((observable, oldValue, newValue) ->
                icosahedronSeries.getData().get(diceRoll.getRollNumber() - 1).setYValue(newValue)
        );
    });

这是我最后用来监视变化的。它所需要的只是您的 class 使用所谓的 *属性 变量,它支持开箱即用的侦听器。例如,当它发生变化时,您可以 运行 一段代码来更新您的图表。

如果您正在使用 ObservableList,您可以添加一个 ListChangeListener 并在列表更改时调用您的图表更新功能。

ObservableList<DiceRoll> myList = FxCollections.observableArrayList();
myList.addListener((ListChangeListener)(c -> { updateMyChart() }));

然后在 updateMyChart() 中迭代列表:

private void updateMyChart() {

    /* Clear series */

    for(int i = 0; i < myList.size(); i++ {
        /* Create new Series */
    }

    /* Add Series */

}

您只需将 DiceRoll 个对象添加到列表中即可。

myList.add(new DiceRoll(1));

P.S - 我省略了创建/删除系列的步骤,因为你提到你已经可以做到这一点,如果你也需要看到它,我可以添加它。