gpt4 book ai didi

java - 动态更新 JavaFX BarChart

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:15 26 4
gpt4 key购买 nike

正如标题所说。基本上我有一个 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"));

这一切都有效,尽管我更喜欢非工厂方法。但是我不知道如何与条形图进行相同类型的链接。

我的条形图有一个 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 轴为滚动次数。

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

编辑2:

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

我尝试按照克里斯的建议执行此操作,但程序没有打印任何内容。 observableArrayList() 可能会返回一个新列表,而不是包装现有引用,但这意味着我的表也无法工作。可能出了什么问题?

编辑3:

错误在于,当引用的值发生变化时,可观察量会“变化”。可观察列表的值是其他引用,在我的例子中,这些引用被添加一次并且再也不会被触及。这意味着它在程序启动后的生命周期内实际上永远不会改变。我发现我可以将监听器附加到 DiceRoll 中的 IntegerProperties,当其中一个发生更改时,我可以做任何我想做的事,这很棒,并且给了我急需的控制权。

编辑4

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));

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

关于java - 动态更新 JavaFX BarChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com