BarChart / LineChart - only label for last data point showing
I cannot get a bar or line chart to show all labels on the X axis. As you can see in the print screen, only the last datatop shows its label.
This is when the scene builder is used. Should I have an ObservableList with strings for CategoriesAxis?
I made 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);
source to share
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 will receive all the tags ...
Orther has only one series with corresponding labels, as you have already indicated, create a global one ObservableList
, add it at the beginning to the series, and then Initialize
add the series to the graph.
Then, if you want to handle click events, just add new values ββto the series and you will see new labels 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 episodes every time you click the button, you can do it as you suggested at the beginning.
But note that this seems to be a bug in the first series and only the last label is displayed if the chart is animated. To avoid this error (think about feeding it to JIRA), just disable animation and it will work.
If you need animation, you can add a listener to the diagram and set the animation if 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)));
}
source to share