jFreeChart reload dataset issue

独自空忆成欢 提交于 2019-12-10 23:15:52

问题


I use JFreeChart to display some simple data in a column chart. I have no problem in loading the dataset into the chart and display the data, but my issue come when I'm trying to use checkboxes to sort the data. When checked, the data from year2001 should be display, when uncheck, data from all years should be displayed. From performing a println I can see that the checkboxes is working and that the data from Persons.getPersons().getCountSuccessYesMale() is changed, but the chart is not reloading the new data. What's wrong here?

public class ChartColumn extends JPanel {

private JCheckBox year2001;
private JCheckBox year2002;
private EventListener eventListener;
private int successMale;

public ChartColumn(EventListener eventListener) {
    this.eventListener = eventListener;
}

public void update() {
    this.createChart(this.createDataset());
    this.showSelectionControl();
}

private CategoryDataset createDataset() {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.addValue(Persons.getPersons().getCountSuccessYesMale(), "Success", "MALE");
    dataset.addValue(Persons.getPersons().getCountSuccessNoMale(), "Fail", "MALE");
    dataset.addValue(Persons.getPersons().getCountSuccessYesFemale(), "Success", "FEMALE");
    dataset.addValue(Persons.getPersons().getCountSuccessNoFemale(), "Fail", "FEMALE");

    return dataset;

}

public void createChart(CategoryDataset dataset) {

    JFreeChart chart =
            ChartFactory.createBarChart3D(
            "Comparison between Male and Female in attending Meetings",
            "Person Comparisons",
            "No of Persons",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 300));

    this.add(chartPanel);

}

public void showSelectionControl() {

    year2001 = new JCheckBox("2001");
    year2001.setMnemonic(KeyEvent.VK_C);
    year2001.setSelected(true);
    year2001.setActionCommand("2001");
    year2001.addActionListener((ActionListener) this.eventListener);

    year2002 = new JCheckBox("2002");
    year2002.setMnemonic(KeyEvent.VK_G);
    year2002.setSelected(true);
    year2002.setActionCommand("2002");
    year2002.addActionListener((ActionListener) this.eventListener);

    this.add(year2001);
    this.add(year2002);
}
}

Eventlistener which call the update-method when checkbox has change status:

 class TabbedPaneEvent implements ActionListener {

    public void actionPerformed(ActionEvent actionEvent) {
         if (actionEvent.getActionCommand().equals("2001")) {
            AbstractButton abstractButton = (AbstractButton)actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();

            if(selected == true){
                Persons.setYear(1);
            } else if(selected == false){
                Persons.setYear(0);
            }


            Persons.calculateChartData();
            chartColumn.update();


        } 
    }

回答1:


Looks like you aren't removing the old chart. Either call removeAll() in your update method or just update the chart's dataset instead of rebuilding the chart again.



来源:https://stackoverflow.com/questions/5457859/jfreechart-reload-dataset-issue

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