2

I want to change a bar chart data in a loop and I have no idea how to do that. My code:

DefaultCategoryDataset barChartData = new DefaultCategoryDataset();
        barChartData.setValue(0,  "Values","1");
        barChartData.setValue(0,  "Values","2");
        barChartData.setValue(0,  "Values","3");


        JFreeChart barChart = ChartFactory.createBarChart("Proxi", "Sensors", "Value", barChartData, PlotOrientation.VERTICAL, false, true, false);

        CategoryPlot barchrt = barChart.getCategoryPlot();

        barchrt.setRangeGridlinePaint(Color.ORANGE);

        ChartPanel barPanel = new ChartPanel(barChart);
        barPanel.setBounds(0, 0, 731, 456);
        contentPane.add(barPanel);

1 Answers1

4

Update the model and the listening view will follow. To animate the updates without blocking the event dispatch thread, use a javax.swing.Timer, as shown here; invoke stop() as needed.

image

As tested:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * @see https://stackoverflow.com/a/21267585/230513
 */
public class Test {

    private static final String ROW_KEY = "Values";
    private static final Random r = new Random();

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final DefaultCategoryDataset model = new DefaultCategoryDataset();
        model.setValue(1, ROW_KEY, "1");
        model.setValue(2, ROW_KEY, "2");
        model.setValue(3, ROW_KEY, "3");
        JFreeChart chart = ChartFactory.createBarChart("Proxi", "Sensors",
            "Value", model, PlotOrientation.VERTICAL, false, true, false);
        ChartPanel barPanel = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        f.add(barPanel);
        f.add(new JButton(new AbstractAction("Update") {

            @Override
            public void actionPerformed(ActionEvent e) {
                model.setValue(r.nextDouble() * 3, ROW_KEY, "2");
            }
        }), BorderLayout.SOUTH);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, I also need to send data for this chart from another object which changes it's values in the loop. So I need to create method updateChart in Test class that I would be able to transfer data in the loop, but if I create thread like that in run method I initialize object of Test class I can't access it in code below. Can you suggest how to realize that? – Arnas Ivanavičius Jan 22 '14 at 11:29
  • 1
    I'd use a `SwingWorker` to `publish()` in the background and update the model in `process()`. – trashgod Jan 22 '14 at 12:14