BarChart/LineChart - Only the label for the last data point showing

坚强是说给别人听的谎言 提交于 2019-12-03 20:11:41

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...

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

EDIT

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

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

This is a bug in j8_u20. With the help of the people on the Java Chat we have tested with u20,u25 and u11. This is the result:

  • U25 bug is visible
  • U20 bug is visible
  • U11 bug is not visible

Bug filed to JAVA jira.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!