当前位置: 首页 > 图文教程 > Java技术 > Web框架 > 浅谈JFreeChart实时曲线(代码)

Web框架
Web框架介绍---SpringMVC
基于Django框架的敏捷Web开发
驾驭新的Ruby Web框架Waves
JAVA脚本取得struts2 OgnlValueStack中的值
Struts中DownloadAction的使用
优化Hibernate性能的几点建议
Hibernate高级查询实战
对于Struts和Spring两种MVC框架的比较
JSF与Struts的比较 超易懂!
hibernate 主键生成方式
spring acegi 官方例子
获取ApplicationContext的几种方式
Spring与自动调度任务 基于Timer的任务调度器的应用
spring+hibernate 的包的详解,帮你了解每个包的作用以及是否必要导入工程
hibernate 一对一(one to one)级联保存
struts formbean 就是鸡肋
Struts中常用的几种Action
spring的应用事例
struts2(一)
struts2(二)

Web框架 中的 浅谈JFreeChart实时曲线(代码)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-23   浏览: 213 ::
收藏到网摘: n/a

package com.cityinforport.demo;

//导入java2d

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.io.PrintStream;

//导入jfreechart(chart)

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;

//导入jfreechart(data)

import org.jfree.data.time.*;

import org.jfree.data.xy.XYDataset;

//导入jfreechart(ui)

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;

 

public class TimeSeriesDemo1 extends JFrame implements Runnable,ActionListener{

  //时序图数据集

  private TimeSeries timeseries;

  //Value坐标轴初始值

  private double lastValue;

  static Class class$org$jfree$data$time$Millisecond;

  static Thread thread1;

 

  public static void main(String[] args){

    TimeSeriesDemo1 TimeSeriesDemo1 = new TimeSeriesDemo1();

    TimeSeriesDemo1.pack();

    RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo1);

    TimeSeriesDemo1.setVisible(true);

    startThread();

  }

 

  public void run(){

    while(true){

      try{

    //根据实际需要在此处加入需要执行的代码

    double d = 0.9D + 0.2D * Math.random();

    lastValue = lastValue * d;

    Millisecond millisecond = new Millisecond();

    System.out.println("Now=" + millisecond.toString());

    timeseries.add(millisecond, lastValue);

    Thread.sleep(300);

      }catch(InterruptedException e){}

    }

  }

 

  public static void startThread(){

    thread1.start();

  }

 

  public void actionPerformed(ActionEvent e){

    if(e.getActionCommand().equals("EXIT")){

      thread1.destroy();

      System.exit(0);

    }

  }

 

 

  public TimeSeriesDemo1(){

    //super(new BorderLayout());

    thread1 = new Thread(this);

    lastValue = 100D;

    //创建时序图对象

    timeseries = new TimeSeries("Random Data",TimeSeriesDemo1.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo1.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo1.class$org$jfree$data$time$Millisecond = TimeSeriesDemo1.getClass("org.jfree.data.time.Millisecond")));

    TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);

    //创建图表面板

    ChartPanel chartpanel = new ChartPanel(createChart(timeseriescollection));

    chartpanel.setPreferredSize(new Dimension(500,270));

 

    JPanel jpanel = new JPanel();

    jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//边距为4

    JButton jbutton = new JButton("退出");

    jbutton.setActionCommand("EXIT");

    jbutton.addActionListener(this);

    jpanel.add(jbutton);

 

    getContentPane().add(chartpanel);

    getContentPane().add(jpanel,"South");

  }

 

  private JFreeChart createChart(XYDataset xydataset){

    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图例子","时间","温度值",xydataset,true,true,false);

    XYPlot xyplot = jfreechart.getXYPlot();

    //纵坐标设定

    ValueAxis valueaxis = xyplot.getDomainAxis();

    valueaxis.setAutoRange(true);

    valueaxis.setFixedAutoRange(60000D);

 

    valueaxis = xyplot.getRangeAxis();

    valueaxis.setRange(0.0D,200D);

 

    return jfreechart;

  }

 

  static Class getClass(String s){

    Class cls = null;

    try{

      cls = Class.forName(s);

    }catch(ClassNotFoundException cnfe){

      throw new NoClassDefFoundError(cnfe.getMessage());

    }

    return cls;

  }

 

}

 

 

package com.cityinforport.demo;

 

//导入java2d

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.io.PrintStream;

//导入jfreechart(chart)

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.chart.renderer.xy.DefaultXYItemRenderer;

//导入jfreechart(data)

import org.jfree.data.time.*;

import org.jfree.data.xy.XYDataset;

//导入jfreechart(ui)

import org.jfree.ui.*;

 

public class TimeSeriesDemo2 extends JFrame implements Runnable,ActionListener{

 

  //申明实时曲线对象

  private TimeSeries timeseries1;

  private TimeSeries timeseries2;

 

  //Value坐标轴初始值

  private double lastValue1,lastValue2;

  private double originalValue1,originalValue2;

 

  static Class class$org$jfree$data$time$Millisecond;

  static Thread thread1;

 

  public static void main(String[] args){

    TimeSeriesDemo2 TimeSeriesDemo2 = new TimeSeriesDemo2();

    TimeSeriesDemo2.pack();

    RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo2);

    TimeSeriesDemo2.setVisible(true);

    startThread();

  }

 

  public void run(){

    while(true){

      try{

    //说明:在此处添加具体的业务数据

 

    //随机产生曲线1的数据

    double d1 = 2.0D * Math.random();

    lastValue1 = originalValue1 * d1;

    Millisecond millisecond1 = new Millisecond();

    System.out.println("Series1 Now=" + millisecond1.toString());

    timeseries1.add(millisecond1, lastValue1);

    //随机产生曲线2的数据

    double d2 = 2.0D * Math.random();

    lastValue2 = originalValue2 * d2;

    Millisecond millisecond2 = new Millisecond();

    System.out.println("Series2 Now=" + millisecond2.toString());

    timeseries2.add(millisecond2,lastValue2);

 

    Thread.sleep(500);

      }catch(InterruptedException e){}

    }

  }

 

  public static void startThread(){

    thread1.start();

  }

 

  public void actionPerformed(ActionEvent e){

    if(e.getActionCommand().equals("EXIT")){

      thread1.interrupt();

      System.exit(0);

    }

  }

 

 

  public TimeSeriesDemo2(){

    thread1 = new Thread(this);

    originalValue1 = 100D;

    originalValue2 = 100D;

    //创建时序图对象

    timeseries1 = new TimeSeries("热风温1",TimeSeriesDemo2.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo2.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo2.class$org$jfree$data$time$Millisecond = TimeSeriesDemo2.getClass("org.jfree.data.time.Millisecond")));

    timeseries2 = new TimeSeries("热风温2",TimeSeriesDemo2.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo2.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo2.class$org$jfree$data$time$Millisecond = TimeSeriesDemo2.getClass("org.jfree.data.time.Millisecond")));

    TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries1);

    TimeSeriesCollection timeseriescollection1 = new TimeSeriesCollection(timeseries2);

 

    //创建jfreechart对象

    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("RTU温度模拟量实时曲线图","Time","Value",

                            timeseriescollection,true,true,false);

    jfreechart.setBackgroundPaint(Color.white);

 

    //设定显示风格

    XYPlot xyplot = jfreechart.getXYPlot();

    xyplot.setBackgroundPaint(Color.lightGray);

    xyplot.setDomainGridlinePaint(Color.white);

    xyplot.setRangeGridlinePaint(Color.white);

    xyplot.setAxisOffset(new Spacer(1, 4D, 4D, 4D, 4D));

    ValueAxis valueaxis = xyplot.getDomainAxis();

    valueaxis.setAutoRange(true);

    valueaxis.setFixedAutoRange(60000D);

    //设定Value的范围

    valueaxis = xyplot.getRangeAxis();

    valueaxis.setRange(0.0D,200D);

    xyplot.setDataset(1, timeseriescollection1);

    xyplot.setRenderer(1,new DefaultXYItemRenderer());

 

    //创建图表面板

    ChartPanel chartpanel = new ChartPanel(jfreechart);

    getContentPane().add(chartpanel);

 

    //根据需要添加操作按钮

    this.setTitle("RTU实时曲线");

    JPanel jpanel = new JPanel();

    jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//边距为4

    JButton jbutton = new JButton("退出");

    jbutton.setActionCommand("EXIT");

    jbutton.addActionListener(this);

    jpanel.add(jbutton);

    getContentPane().add(jpanel,"South");

    chartpanel.setPreferredSize(new Dimension(500,270));

  }

 

  static Class getClass(String s){

    Class cls = null;

    try{

      cls = Class.forName(s);

    }catch(ClassNotFoundException cnfe){

      throw new NoClassDefFoundError(cnfe.getMessage());

    }

    return cls;

  }

 

}