Steve2Fish
Steve2Fish

Reputation: 187

All labels at the same position in xAxis Barchart - JavaFx

I have an Refresh-Button and when I click it the first time all the labels are at the same position: labels with false position When I click it the second time the labels are positioned correct: labels with correct position

The initialize-method contains nothing about the barchart.

Here the relevant code-snippet of the controller-class:

@FXML
private NumberAxis barChart_yAxis;
@FXML
private BarChart<String, Number> barChart;

@FXML
void handleRefreshBarChart() {
barChart.getData().clear();
if (cbA.isSelected()) {
    XYChart.Series<String, Number> seriesA = new XYChart.Series<>();
    List<Integer> sales = statisticService.getSales('A');
    seriesA.setName("A");
    seriesA = createSerieBarChart(seriesA, sales);
    barChart.getData().add(seriesA);
}
if (cbB.isSelected()) {
    XYChart.Series<String, Number> seriesB = new XYChart.Series<>();
    List<Integer> sales = statisticService.getSales('B');
    seriesB.setName("B");
    seriesB = createSerieBarChart(seriesB, sales);
    barChart.getData().add(seriesB);
}
if (cbC.isSelected()) {
    XYChart.Series<String, Number> seriesC = new XYChart.Series<>();
    List<Integer> sales = statisticService.getSales('C');
    seriesC.setName("C");
    seriesC = createSerieBarChart(seriesC, sales);
    barChart.getData().add(seriesC);
}
}

private XYChart.Series<String, Number> createSerieBarChart(XYChart.Series<String, Number> serie, List<Integer> list) {
serie.getData().add(new XYChart.Data<>("Monday", list.get(0)));
serie.getData().add(new XYChart.Data<>("Tuesday", list.get(1)));
serie.getData().add(new XYChart.Data<>("Wednesday", list.get(2)));
serie.getData().add(new XYChart.Data<>("Thursday", list.get(3)));
serie.getData().add(new XYChart.Data<>("Friday", list.get(4)));
serie.getData().add(new XYChart.Data<>("Saturday", list.get(5)));
serie.getData().add(new XYChart.Data<>("Sunday", list.get(6)));
return serie;
}

Upvotes: 2

Views: 709

Answers (2)

Ser-Borovsky
Ser-Borovsky

Reputation: 1

setAnimated(false);

Process data...

Platform.runLater(new Runnable() {

        @Override
        public void run() {
            setAnimated(true);
        }
    });

Upvotes: 0

I solved it by turning off the animation before populating the data chart

barChart.setAnimated(false);

Upvotes: 2

Related Questions