使用计时器更新JFreechart数据集

时间:2014-02-25 08:41:51

标签: java timer jfreechart

我找到了一个关于多个图表here的示例,并尝试对其进行一些改进。我希望随着时间的推移改变这些值。

以下是代码:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimePeriod;
import org.jfree.ui.RefineryUtilities;

/**
 *
 * @author AloneInTheDark
 */
public class ThermometerDemo extends JPanel {

    private static final int W = 200;
    private static final int H = 2 * W;
private static Timer timer;
int artan=0;
double VALUE;
int FAST=1000;
  DefaultValueDataset dataset ;
    public ThermometerDemo(double value) {
        this.setLayout(new GridLayout());
        VALUE=value;   


            timer = new Timer(FAST, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
        artan++;
            }});

           dataset = new DefaultValueDataset(VALUE+artan);
        ThermometerPlot plot = new ThermometerPlot(dataset);
        plot.setSubrangePaint(0, Color.green.darker());
        plot.setSubrangePaint(1, Color.orange);
        plot.setSubrangePaint(2, Color.red.darker());
        JFreeChart chart = new JFreeChart("Demo",
            JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        this.add(new ChartPanel(chart, W, H, W, H, W, H,
            false, true, true, true, true, true));
    }
   public static void start() {
        timer.start();
    }


    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                 final JFrame f = new JFrame("Demo");            
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new GridLayout(1, 0));
                f.add(new ThermometerDemo(40));
                f.add(new ThermometerDemo(60));
                f.add(new ThermometerDemo(80));
                timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                f.repaint();
            }});
                f.pack();
                f.setVisible(true);

        start();

            }
        });

    }

}

当我调试这个项目时,计时器工作,我的“artan”变量每秒都在增长,但图表中没有任何变化。温度计中的值不会改变。我该如何解决这个问题?

编辑:当我将此行放入计时器时,图表中没有值:     dataset = new DefaultValueDataset(VALUE + artan);

1 个答案:

答案 0 :(得分:0)

我找到了更新它们的方法。它运行良好,但界面有一些问题。温度计颜色随机变化。这是代码:

public class ThermometerDemo extends JPanel {

    private static final int W = 200;
    private static final int H = 2 * W;
    private static Timer timer;
    int artan = 0;

    double VALUE;
    int FAST = 1000;
    DefaultValueDataset dataset;

    public ThermometerDemo(double value) {

        this.setLayout(new GridLayout());

        VALUE = value;

        timer = new Timer(FAST, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VALUE++;
                dataset.setValue(VALUE);
            }
        });

        dataset = new DefaultValueDataset(VALUE);
        ThermometerPlot plot = new ThermometerPlot(dataset);
        plot.setSubrangePaint(0, Color.green.darker());
        plot.setSubrangePaint(1, Color.orange);
        plot.setSubrangePaint(2, Color.red.darker());
        JFreeChart chart = new JFreeChart("Demo",
                JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        this.add(new ChartPanel(chart, W, H, W, H, W, H,
                false, true, true, true, true, true));
        start();
    }

    public static void start() {
        timer.start();
    }

    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                final JFrame f = new JFrame("Demo");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new GridLayout(1, 0));
                f.add(new ThermometerDemo(40));
                f.add(new ThermometerDemo(60));
                f.add(new ThermometerDemo(80));
                f.pack();
                f.setVisible(true);

            }
        });

    }

}