jfreechart时序图的模拟实现

真的好久好久不用jfreechart了。因为演示程序需要,花了点时间又重新回顾了一下。

一般时序图是嵌套在当前的窗口中,所以将其定义为JPanel的子类,只需在窗口待显示时序图的jpanel里自定义创建代码即可。

下面的类模拟实现了压力和温度的曲线。


package com.xd;

import java.awt.Dimension;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
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.Millisecond;
import org.jfree.data.time.TimeSeries;  
import org.jfree.data.time.TimeSeriesCollection;  



public class RealTimeChart extends JPanel   
{ 
    private  TimeSeries timeSeriesTemp;  
    private  TimeSeries timeSeriesPress;  
    private long value=0;  
    private Random tempseed=new Random(10);
    private Random pressseed=new Random(908765);
    ChartPanel chartpanel;
    JFreeChart jfreechart; 
    


      
    public RealTimeChart(String chartContent,String title,String yaxisName,int width,int height)  
    {  
        this.setPreferredSize(new Dimension(width, height));
        this.setLayout(null);
        chartpanel=new ChartPanel(createChart(chartContent,title,yaxisName));
        chartpanel.setLocation(10, 25);
        chartpanel.setSize(new Dimension(width -20, height-30));
        chartpanel.setVisible(true);
        this.add(chartpanel);
        this.updateUI();             

        init();
    }  
      
    private  JFreeChart createChart(String chartContent,String title,String yaxisName){  
        //创建时序图对象  
        timeSeriesTemp = new TimeSeries("Ttemperature","Y","long value");  
        timeSeriesPress = new TimeSeries("Pressure","Y","long value");  
        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();  
         timeseriescollection.addSeries(timeSeriesTemp);
        timeseriescollection.addSeries(timeSeriesPress);
        JFreeChart jfreechart;  
          
         jfreechart = ChartFactory.createTimeSeriesChart(title,"Time(second)",yaxisName,timeseriescollection,true,true,false);
      
        XYPlot xyplot = jfreechart.getXYPlot();  
        //纵坐标设定  
        ValueAxis valueaxis = xyplot.getDomainAxis();  
        //自动设置数据轴数据范围  
        valueaxis.setAutoRange(true);  
        //数据轴固定数据范围 300s  
        valueaxis.setFixedAutoRange(300000D);  
  
        valueaxis = xyplot.getRangeAxis();  
        //valueaxis.setRange(0.0D,200D);  
  
        return jfreechart;  
      }  
  
      

   public void init(){
       Thread t=new Thread(){
           @Override
           public void run(){
               while(true){
                   try {
                       Thread.sleep(1000L);
                       updateGraph();
                   } catch (InterruptedException ex) {
                       Logger.getLogger(RealTimeChart.class.getName()).log(Level.SEVERE, null, ex);
                   }
                   
               }
               
           }
       };
       t.start();
      
   } 

   
    public void updateGraph() {
        
        timeSeriesTemp.add(new Millisecond(), 120 + Math.abs(tempseed.nextInt())%5);
        timeSeriesPress.add(new Millisecond().next(), 90 + Math.abs(pressseed.nextInt())%5);
        System.out.println(120 + tempseed.nextInt()%10);

    }
    
}  
  




 因为是在jpanel中又添加了一个ChartPanel(也继承了JPanel类),显示的时候,稍微有点位置错误,就稍微通过位置修复了一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是由美联储经济数据库(FRED)托管的美联储数据集。有关每个文件的更多详细信息,请参见各个文件的说明。 industrial-production-business-equipment_metadata.json industrial-production-consumer-goods_metadata.json industrial-production-durable-consumer-goods_metadata.json industrial-production-durable-goods-raw-steel_metadata.json industrial-production-durable-manufacturing-motor-vehicles-and-parts_metadata.json industrial-production-durable-materials_metadata.json industrial-production-electric-and-gas-utilities_metadata.json industrial-production-electric-and-gas-utilities_metadata_1.json industrial-production-final-products-and-nonindustrial-supplies_metadata.json industrial-production-final-products-market-group_metadata.json industrial-production-fuels_metadata.json industrial-production-manufacturing-naics_metadata.json industrial-production-manufacturing-naics_metadata_1.json industrial-production-manufacturing-sic_metadata.json industrial-production-materials_metadata.json industrial-production-mining_metadata.json industrial-production-mining-crude-oil_metadata.json industrial-production-nondurable-consumer-goods_metadata.json industrial-production-nondurable-materials_metadata.json industrial-production-residential-utilities_metadata.json industrial-production-total-index_metadata.json IPB50001N.csv IPB51222S.csv IPBUSEQ.csv IPCONGD.csv IPDCONGD.csv IPDMAT.csv IPFINAL.csv IPFPNSS.csv IPFUELS.csv IPG2211A2N.csv IPG3361T3S.csv IPG211111CN.csv IPGMFN.csv IPMAN.csv IPMANSICS.csv IPMAT.csv IPMINE.csv IPN3311A2RN.csv IPNCONGD.csv IPNMAT.csv IPUTIL.csv

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值