2

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();


        } 
    }
jzd
  • 23,473
  • 9
  • 54
  • 76
jorgen
  • 1,217
  • 6
  • 22
  • 41

1 Answers1

4

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.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • Yes, this is almost working. The problem when using removeAll() is that the state of the checkbox(checked/unchecked) is deleted. How can I only delete/update the dataset? – jorgen Mar 28 '11 at 12:46
  • 3
    Instead of creating a new dataset each time, create it once and then clear and add the new values to it. – jzd Mar 28 '11 at 12:48
  • Yes! Can't belive I didn't though of that earlier! Thanks a lot! – jorgen Mar 28 '11 at 12:55
  • Can you paste your final code? I can use it for my problem too. – AloneInTheDark Feb 06 '14 at 08:30