xAxis Barchart中的所有标签位于相同位置 - JavaFx

时间:2018-03-31 16:18:29

标签: java javafx label bar-chart axis

我有一个刷新按钮,当我第一次点击它时,所有标签位于同一位置:labels with false position 当我第二次点击标签时,标签的位置正确: labels with correct position

initialize-method不包含条形图。

这里是控制器类的相关代码片段:

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

2 个答案:

答案 0 :(得分:1)

我通过在填充数据图表之前关闭动画来解决它

barChart.setAnimated(false);

答案 1 :(得分:0)

setAnimated(false);

过程数据...

Platform.runLater(new Runnable() {

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