基本信息
源码名称:用jfreechart实现的动态曲线
源码大小:0.01M
文件格式:.zip
开发语言:Java
更新时间:2019-07-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
用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;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RealTimeChart extends ChartPanel implements Runnable,ActionListener
{
    private static final long serialVersionUID = 1L;
    static JLabel show;//显示随机数
    private static TimeSeries timeSeries;
    public RealTimeChart(String chartContent,String title,String yaxisName)
    {   //构造函数,第一个参数为图表内容,第二个参数为图表标题,第三个参数为纵坐标标题
        super(createChart(chartContent,title,yaxisName)); //调用超类的方法createChart  
    }

    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,"Time",yaxisName,timeseriescollection,true,true,false);
        XYPlot xyplot = jfreechart.getXYPlot();  //设定坐标系

        //横坐标设定   
        ValueAxis valueaxis = xyplot.getDomainAxis();

        //横坐标自动设置数据轴数据范围   
        valueaxis.setAutoRange(true);

        //横坐标固定数据范围 30s   
        valueaxis.setFixedAutoRange(30000D);

        //纵坐标设定
        valueaxis = xyplot.getRangeAxis();

        return jfreechart;
    }

    public void run()
    {
        while(true)
        {
            try
            {
                timeSeries.add(new Millisecond(), randomNum()); //第一个参数(当前时间)为横坐标的值
                Thread.sleep(1000); //每隔一秒产生一个随机数
            }
            catch (InterruptedException e)  {}  //异常处理
        }
    }

    private long randomNum()  //产生随机数 
    {
        show.setText("随机数:" (Math.random()*20 80));
        return (long)(Math.random()*20 80);   //返回随机数
    }


    public static void main(String[] args)
    {
        JFrame frame=new JFrame("实时曲线");
        RealTimeChart rtcp=new RealTimeChart("Real-time curves","Random real-time curves","Random Number");
        frame.getContentPane().add(rtcp,BorderLayout.CENTER);
        show=new JLabel();
        frame.getContentPane().add(show,BorderLayout.SOUTH);
        frame.pack();//窗口大小可随意变化
        frame.setVisible(true);
        (new Thread(rtcp)).start();  //启动线程,并自动执行run()函数
        frame.addWindowListener(new WindowAdapter()  //关闭窗口事件
        {
            public void windowClosing(WindowEvent windowevent)
            {
                System.exit(0);
            }
        });
    }
}