实时图形绘制开始时间

2024-02-23

这是基于@trashgod 的代码example https://stackoverflow.com/a/21307289/230513关于实时绘图:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;

/**
 * @see https://stackoverflow.com/a/21307289/230513
 */
public class DynamicTimeSeriesChart extends JPanel {

private static final long serialVersionUID = 5128935838291298041L;
private final DynamicTimeSeriesCollection dataset;
private final JFreeChart chart;

public DynamicTimeSeriesChart(final String title) {
    dataset = new DynamicTimeSeriesCollection(1, 1000, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2014));
    dataset.addSeries(new float[1], 0, title);
    chart = ChartFactory.createTimeSeriesChart(
        title, "Time", title, dataset, true, true, false);
    final XYPlot plot = chart.getXYPlot();
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setFixedAutoRange(10000);
    axis.setDateFormatOverride(new SimpleDateFormat("ss.SS"));
    final ChartPanel chartPanel = new ChartPanel(chart);
    add(chartPanel);
}

public void update(float value) {
    float[] newData = new float[1];
    newData[0] = value;
    dataset.advanceTime();
    dataset.appendData(newData);
}

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

        @Override
        public void run() {
            JFrame frame = new JFrame("testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final DynamicTimeSeriesChart chart
                = new DynamicTimeSeriesChart("Alternating data");
            frame.add(chart);
            frame.pack();
            Timer timer = new Timer(1000, new ActionListener() {
                private boolean b;

                @Override
                public void actionPerformed(ActionEvent e) {
                    chart.update(b ? 1 : 0);
                    b = !b;
                }
            });
            timer.start();
            frame.setVisible(true);
        }
    });
}
}

运行Java后,我仍然不明白为什么图表从40秒开始,尽管new Seconds从0开始? 运行代码;找不到任何 40 秒启动的设置。

另外,如何向后滚动图表以查看以前的数据。


该图表从用于构建该日期的午夜后 16 分 40 秒开始Second传递给setTimeBase()。这与 1000 个间隔相同,每个间隔一秒,在nMoments构造函数参数。如果时间设置为午夜,则可以使用一些可能的替代方案来获得从零开始的显示。

  1. Make nMoments的倍数60.

    dataset = new DynamicTimeSeriesCollection(1, 960, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2014));
    
  2. 减去nMoments从名义基准日算起。

    int nMoments = 1000;
    dataset = new DynamicTimeSeriesCollection(1, nMoments, new Second());
    Calendar c = Calendar.getInstance();
    c.setTime(new Date(0));
    c.add(Calendar.SECOND, -nMoments);
    dataset.setTimeBase(new Second(c.getTime()));
    

两种方法都会产生相同的显示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实时图形绘制开始时间 的相关文章

随机推荐