gpt4 book ai didi

java - JFreeChart - 如何在 TimeSeries 图表的 X 轴上显示实时

转载 作者:行者123 更新时间:2023-11-30 07:11:25 32 4
gpt4 key购买 nike

我想在 TimeSeries 图表上显示实时数据,在 x 轴上显示实时时间(或者至少时间速度与实时时间相同)。

这是以随机数作为实时输入的问题的 SSCCE。 x轴显示的时间比实时快很多(假设以hh:mm:ss格式显示):

public class DynamicTimeSeriesChart extends JPanel {

private DynamicTimeSeriesCollection dataset;
private JFreeChart chart = null;

public DynamicTimeSeriesChart(final String title) {

dataset = new DynamicTimeSeriesCollection(1, 2000, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 1990)); // date 1st jan 0 mins 0 secs

dataset.addSeries(new float[1], 0, title);
chart = ChartFactory.createTimeSeriesChart(
title, "Time", title, dataset, true,
true, false);
final XYPlot plot = chart.getXYPlot();

ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(200000); // proportional to scroll speed
axis = plot.getRangeAxis();

final ChartPanel chartPanel = new ChartPanel(chart);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
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) {
JFrame frame = new JFrame("testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final DynamicTimeSeriesChart chart = new DynamicTimeSeriesChart("random numbers");
frame.add(chart);
frame.pack();
frame.setVisible(true);
Timer timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
chart.update((float) (Math.random() * 10));
}
});
}
});
timer.start();
}
}

最佳答案

虽然在 initial threadsleep() 是可以接受的, Swing GUI 对象应该在事件调度线程上构造和操作。相反,使用 javax.swing.Timer 来调整更新速度,如图所示 here . 100 毫秒的延迟 将以大约 10 Hz 的频率更新。

如果漂移 Not Acceptable ,则以标称速率的一半从另一个线程轮询主机时钟,并使用 EventQueue.invokeLater() 更新数据集。确保主机与 NTP 服务器同步。

附录:根据您的更新,请注意所有 Swing GUI 对象必须在事件派发线程上构造和操作,而不仅仅是javax.swing.Timer。 . Swing Timer 的优点是它在 EDT 时触发。下面的变体显示了大约实时的 10 秒 1 Hz 数据。

附录:您可以调整传递给 setTimeBase() 的时间,如图所示 here .

image

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 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, 23, 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 - JFreeChart - 如何在 TimeSeries 图表的 X 轴上显示实时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21299096/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com