I am trying to create a line chart using JfreeChart API. The x axis receives data as long (milliseconds) and Y axis as a double. As I am using the dynamic template I am passing data to it via Add_Point method from separate thread and chart is constantly updated via this method. Data comes in irregularly without any periodicity which perhaps makes it even trickier.
I cannot find a way to convert long into digestible for Jfreechart format without working example of it. It seems that I have exhausted all options but my guess is that there are a lot of people out there who have already been through this and have a ready answer.
All I want to do is to reflect correct time on X axis labels every minute with data points correctly spaced over respective point in time. By correct time I mean long value passed to method and not current system time.
Here is the code:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
public class DynamicDataDemo extends ApplicationFrame {
/** The time series data. */
private static TimeSeries series;
public DynamicDataDemo(final String title) {
super(title);
this.series = new TimeSeries("Random Data", Millisecond.class);
final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(content);
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
"Dynamic Data Demo",
"Time",
"Value",
dataset,
true,
true,
false
);
final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0); // 60 seconds
axis = plot.getRangeAxis();
axis.setRange(9000.0, 11000.0);
return result;
}
public static void Add_Point(long x, double y){
series.add(new Millisecond(), y);
// series.add(new FixedMillisecond(x), y); }
}