JFreeChart更改现有条形图中的数据

时间:2014-01-21 14:01:34

标签: java jfreechart bar-chart

我想在循环中更改条形图数据,我不知道该怎么做。我的代码:

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 个答案:

答案 0 :(得分:3)

更新模型,然后将听取视图。要在不阻止event dispatch thread的情况下为更新设置动画,请使用javax.swing.Timer,如图所示here;根据需要调用stop()

image

经测试:

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