簡體   English   中英

動態更新JavaFX BarChart

[英]Update JavaFX BarChart dynamically

正如標題所說。 基本上我有一個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!"));

我按照Chris的建議嘗試這樣做,但程序沒有打印任何東西。 可能是observableArrayList()返回一個新列表而不是包裝現有引用,但這意味着我的表也無法工作。 可能有什么不對?

EDIT3:

錯誤的是,當參考值發生變化時,可觀察量會“改變”。 並且可觀察列表的值是其他引用,在我的情況下,它被添加一次並且再也不會被觸及。 這意味着它永遠不會在啟動后實際更改程序的生命周期。 我發現我可以將偵聽器附加到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變量,它支持開箱即用的偵聽器。 它改變的那一刻你可以運行一段代碼來更新你的圖表。

如果您使用的是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));

PS - 我省略了創建/刪除系列的步驟,因為你提到你已經可以做到這一點,如果你需要看到我也可以添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM