JFreeChart实现实时曲线图

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 final long serialVersionUID = 1L;
private static TimeSeries timeSeries;

public RealTimeChart(String chartContent, String title, String yaxisName) {
super(createChart(chartContent, title, yaxisName));
}

@SuppressWarnings("deprecation")
private static JFreeChart createChart(String chartContent, String title,
String yaxisName) {
//创建时序图对象
timeSeries = new TimeSeries(chartContent, Millisecond.class);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(
timeSeries);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,
"时间(秒)", yaxisName, timeseriescollection, true, true, false);
XYPlot xyplot = jfreechart.getXYPlot();
//纵坐标设定
ValueAxis valueaxis = xyplot.getDomainAxis();
//自动设置数据轴数据范围
valueaxis.setAutoRange(true);
//数据轴固定数据范围 30s
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);
}
}


import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class Test {

public static void main(String[] args) {
JFrame frame = new JFrame("Test Chart");
RealTimeChart rtcp = new RealTimeChart("Random Data", "随机数", "数值");
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);
}

});
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值