Recently, I want to use real-time curve charts. I have found it online. There are two ways to implement it. One is the official instance of JFreeChart MemoryUsageDemo.java. Through an internal class that implements java.Swing.Timer, the real-time data is added to TimeSeries in its listener. Since Timer will execute in real-time, there is no problem with this method. You can refer to the code.
Another way is to implement the Runnable interface of the real-time class. In its run() method, real-time data is added to TimeSeries through infinite loops. The following is the simpler implementation code:
//RealTimeChart.java 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.Millisecond; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; public class RealTimeChart extends ChartPanel implements Runnable { private static TimeSeries timeSeries; private long value=0; public RealTimeChart(String chartContent,String title,String yaxisName) { super(createChart(chartContent, title,yaxisName)); } private static JFreeChart createChart(String chartContent,String title,String yaxisName){ //Create the time series object timeSeries = new TimeSeries(chartContent,Millisecond.class); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeSeries); JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,"TimeSeriesCollection(seconds),yaxisName,timeseriescollection,true,true,false); XYPlot xyplot = jfreechart.getXYPlot(); //Onorthogonal coordinate setting ValueAxis valueaxis = xyplot.getDomainAxis(); //Onormal setting data range valueaxis.setAutoRange(true); //Onormal setting data range valueaxis.setAutoRange(true); //Fixed data range of data axis for 30 seconds valueaxis.setFixedAutoRange(30000D); valueaxis = xyplot.getRangeAxis(); //valueaxis.setRange(0.0D,200D); return jfreechart; } public void run() { while(true) { try { timeSeries.add(new Millisecond(), randomNum()); Thread.sleep(300); } catch (InterruptedException e) { } } } private long randomNum() { System.out.println((Math.random()*20+80)); return (long)(Math.random()*20+80); } } //Test.java import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; public class Test { /** * @param args */ public static void main(String[] args) { JFrame frame=new JFrame("Test Chart"); RealTimeChart rtcp=new RealTimeChart("Random Data","random number","numeric value"); frame.getContentPane().add(rtcp,new BorderLayout().CENTER); frame.pack(); frame.setVisible(true); (new Thread(rtcp)).start(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowevent) { System.exit(0); } }); } }There is a problem with both methods, that is, every time a graph is implemented, it must be rewrited, because real-time data cannot be transmitted through parameters. I wonder if it is possible to pass real-time data through setXXX() method, so that the real-time curve drawing class can be encapsulated, and just pass some parameters, or who has a better way?
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.