I want to show live data on a TimeSeries chart with real time shown on the x-axis (or at least have the speed of the time the same as real-time).
Here is a SSCCE of the problem with random numbers as the live input. The time shown on the x-axis is much faster than real-time (assuming it is shown in hh:mm:ss format):
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();
}
}