动态更新JavaFX BarChart

时间:2017-06-27 00:52:01

标签: java arraylist javafx bar-chart fxml

正如标题所说。基本上我有一个ArrayList,其中DiceRoll是一个在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"));

这一切都有效,尽管我更喜欢非工厂方法。但是,我无法弄清楚如何使用BarChart进行相同类型的链接。

我的BarChart将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基本上被初始化为可以滚动的数字。我已经在我的表x20中为所有20个可能的卷添加了这个,如果它被滚动则只添加它。在图表形式中,这意味着3个图表,在X轴上是滚动编号,Y轴是滚动的次数。

工厂评论与我的问题无关,但我并不喜欢使用此处使用的工厂模式。

EDIT2:

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

我尝试按照克里斯的建议这样做,但程序没有打印任何东西。可能是observableArrayList()返回一个新列表而不是包装现有引用,但这意味着我的表也不会工作。可能有什么不对?

EDIT3:

出现的问题是观察到的变化&#34;当引用的值发生变化时。并且可观察列表的值是其他引用,在我的情况下,它被添加一次并且再也不会被触及。这意味着它永远不会在启动后实际更改程序的生命周期。我发现我可以将侦听器连接到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)
        );
    });

这是我最后用来监控变化的内容。它需要的是你的类使用一个所谓的* Property变量,它支持开箱即用的侦听器。它改变的那一刻你可以运行一段代码来更新你的图表。

1 个答案:

答案 0 :(得分:0)

如果您使用ReactiveFormsModule,则可以添加type="button"并在列表更改时调用图表更新功能。

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 */

}

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