gpt4 book ai didi

javafx - 条形图/折线图 - 仅显示最后一个数据点的标签

转载 作者:行者123 更新时间:2023-12-02 11:51:53 25 4
gpt4 key购买 nike

我无法获取条形图或折线图来显示 X 轴上的所有标签。正如您在提供的打印屏幕中看到的,只有最新的数据点显示其标签。

这是使用场景生成器时的情况。我是否必须有一个带有用于 CategoriesAxis 的字符串的 ObservableList ?

enter image description here

我已经为我当前的代码创建了 MVCE:

@FXML
LineChart<String, Integer> lineChart;
@FXML
private Label label;

@FXML
private void handleButtonAction(ActionEvent event) {

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

series1.getData().add(new XYChart.Data<String, Integer>("austria", 300));
series1.getData().add(new XYChart.Data<String, Integer>("rr", 400));
series1.getData().add(new XYChart.Data<String, Integer>("qq", 3003));
lineChart.getData().addAll(series1);

最佳答案

按照您的方法,每次单击按钮时,都会将一个新系列添加到图表中,并且始终具有相同的数据。请注意,对于第二个系列,您将获得所有标签...

或者只有一个系列,并具有适当的标签,正如您已经指出的那样,创建一个全局 ObservableList,将其添加到系列的开头,然后添加到 初始化将系列添加到图表中。

然后,如果您想处理点击事件,只需向系列添加新值,您就会看到新标签也将被添加。

@FXML private LineChart<String, Integer> lineChart;

private final ObservableList<XYChart.Data<String,Integer>> xyList1 =
FXCollections.observableArrayList(
new XYChart.Data<>("austria", 300),
new XYChart.Data<>("rr", 400),
new XYChart.Data<>("qq", 3003));

private final XYChart.Series series1 = new XYChart.Series(xyList1);

@Override
public void initialize(URL url, ResourceBundle rb) {
series1.setName("Name");
lineChart.getData().addAll(series1);
}

@FXML
public void handleButtonAction(ActionEvent e){
// sample new points
series1.getData().add(new XYChart.Data<>("Point "+(series1.getData().size()+1),
new Random().nextInt(3000)));
}

编辑

如果您想在每次单击该按钮时添加新系列,您可以按照最初的建议进行操作。

但请注意,这似乎是第一个系列中的一个错误,如果图表是动画的,则仅显示最后一个标签。为了避免这个错误(考虑将其提交到 JIRA),只需禁用动画即可。

如果您想要动画,您可以向图表添加一个监听器,并在图表至少有一个系列时设置动画。这将起作用:

@FXML private LineChart<String, Integer> lineChart;

@Override
public void initialize(URL url, ResourceBundle rb) {
// Workaround to allow animation after series is added
lineChart.getData().addListener(
(ListChangeListener.Change<? extends XYChart.Series<String, Integer>> c) -> {
lineChart.setAnimated(c.getList().size()>1);
});
}

@FXML
public void handleButtonAction(ActionEvent e){

XYChart.Series series1 = new XYChart.Series();
series1.setName("Series "+lineChart.getData().size() );
lineChart.getData().addAll(series1);

series1.getData().add(new XYChart.Data<>("Point "+(series1.getData().size()+1),
new Random().nextInt(3000)));
series1.getData().add(new XYChart.Data<>("Point "+(series1.getData().size()+1),
new Random().nextInt(3000)));
series1.getData().add(new XYChart.Data<>("Point "+(series1.getData().size()+1),
new Random().nextInt(3000)));
}

关于javafx - 条形图/折线图 - 仅显示最后一个数据点的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362719/

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