BarChart / LineChart - 仅显示最后一个数据点的标签 [英] BarChart/LineChart - Only the label for the last data point showing

查看:554
本文介绍了BarChart / LineChart - 仅显示最后一个数据点的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am not able to get a bar chart or a line chart to show all the labels on the X axis.As you can see in the provided print screen, only the latest datapoint shows its label.

这是在使用场景构建器时。我是否必须为CategoriesAxis提供带字符串的ObservableList?

This is when using scene builder. Do I have to have an ObservableList with Strings for the CategoriesAxis ?

我已经制作了当前代码的MVCE:

I have made a MVCE of my current code:

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


推荐答案

通过您的方法,每次点击按钮时新系列添加到图表中,始终使用相同的数据。请注意,对于第二个系列,您将获得所有标签...

With your approach, every time you click the button a new series is added to the chart, always with the same data. Note that for the second series you'll get all the labels...

仅仅有一个系列,有正确的标签,正如您已经指出的那样,创建一个全局 ObservableList ,在系列的开头添加它,然后在 Initialize 上添加系列到图表。

In orther to have just one series, with proper labels, as you've already pointed out, create a global ObservableList, add it at the beginning to the series, and then on Initialize add the series to the chart.

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

Then if you want to process click events, just add new values to the series, and you'll see the new labels will be added too.

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

编辑

如果你想在每次点击按钮时添加新系列,你可以按照你最初的建议进行。

If you want to add new series every time you click the button, you can do it as you proposed initially.

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

But note it seems to be a bug in the first series, and only the last label is shown, IF the chart is animated. To avoid this bug (consider filing it to JIRA), just disable animation and it will work.

如果你想要动画,可以在图表中添加一个监听器,一旦它至少有一个系列就设置动画。这将有效:

If you want animation, you can add a listener to the chart, and set animation once it has at least one series. This will work:

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

这篇关于BarChart / LineChart - 仅显示最后一个数据点的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆