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.
This is when using scene builder. Do I have to have an ObservableList with Strings for the CategoriesAxis ?
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...
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.
来源:https://stackoverflow.com/questions/26362719/barchart-linechart-only-the-label-for-the-last-data-point-showing