How can I update JavaFX BarChart in Swing application?

折月煮酒 提交于 2019-12-11 12:05:58

问题


I want to update a JavaFX BarChart. I initialize my BarChart with this method:

private void initFxComponents(String questionName){

    Platform.runLater(() -> {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> barChart = new BarChart<>(xAxis,yAxis);
        barChart.setTitle(questionName);
        barChart.setLegendVisible(false);
        xAxis.setLabel("Answer");       
        yAxis.setLabel("Number of times answered");
        series = new Series();
        int nbAnswersForProposal;      
        String proposal;
        for(Proposal p : question.getListProposals()){
            nbAnswersForProposal = Answers.getInstance().getNbAnswersForProposal(quiz.getId(), question.getId(), p.getId());
            proposal = p.getProposal();                               
            StringBuilder sb = new StringBuilder(proposal);
            int i = 0;
            while ((i = sb.indexOf(" ", i + 30)) != -1)
                sb.replace(i, i + 1, "\n");                
            series.getData().add(new Data(sb.toString(), nbAnswersForProposal));
        } 
        Scene scene = new Scene(barChart,800,800);
        barChart.getData().addAll(series);
        fxPanel.setScene(scene);
    });

}

I use this method to update data:

public void updateData(){
    Platform.runLater(() -> {
        series.getData().clear();
        int nbAnswersForProposal;       
        String proposal; 
        for(Proposal p : question.getListProposals()){
            nbAnswersForProposal = Answers.getInstance().getNbAnswersForProposal(quiz.getId(), question.getId(), p.getId());
            proposal = p.getProposal();                               
            StringBuilder sb = new StringBuilder(proposal);
            int i = 0;
            while ((i = sb.indexOf(" ", i + 30)) != -1)
                sb.replace(i, i + 1, "\n");
            series.getData().add(new Data(sb.toString(), nbAnswersForProposal));
        }
    });
}

But when I update data, BarChart is wrong and doesn't display data correctly. I think I'm not doing the right thing to update my BarChart, can anyone help me ? Thanks !


回答1:


Ok, I found a solution to my problem. Apparently, the animation is causing the data to not being displayed correctly and to break auto resizing.

So just add: barChart.setAnimated(false);

You won't have animation anymore but it will solve the problem.


Changes

I also updated this:

private final Series series = new Series();
...
series.getData().add(new Data(sb.toString(), nbAnswersForProposal));
...
barChart.getData().addAll(series); 

by this:

private final XYChart.Series<String, Number> series = new XYChart.Series<>();
...
series.getData().add(new XYChart.Data(sb.toString(), nbAnswersForProposal));
...
barChart.setData(FXCollections.observableArrayList(series));

Update data - chart will be repaint automatically

public void updateData(){
    // You need to run it in thread
    Platform.runLater(() -> {
        // Update chart's name
        barChart.setTitle(questionName);
        // Remove all data
        series.getData().clear();
        // Add as many data as you want         
        series.getData().add(new XYChart.Data("xData", yValue));
    });
}

In my case I needed to delete data and add new ones. But you can also update values (y values) if your x values don't change.

Thanks to Roland which helps me with his answer on JavaFX Chart Auto-Scaling Wrong with Low Numbers



来源:https://stackoverflow.com/questions/34129970/how-can-i-update-javafx-barchart-in-swing-application

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