日期:2014-05-18 浏览次数:21515 次
public partial class FrmMain : Form { // 起始时间以毫秒为单位 int tickStart = 0; public FrmMain() { InitializeComponent(); } private void Form1_Load( object sender, EventArgs e ) { //获取引用 GraphPane myPane = zedGraphControl1.GraphPane; //设置标题 myPane.Title.Text = "实时曲线"; //设置X轴说明文字 myPane.XAxis.Title.Text = "时间"; //设置Y轴说明文字 myPane.YAxis.Title.Text = "温度"; //设置1200个点,假设每50毫秒更新一次,刚好检测1分钟,一旦构造后将不能更改这个值 RollingPointPairList list = new RollingPointPairList( 1200 ); //开始,增加的线是没有数据点的(也就是list为空) //增加一条名称:Voltage,颜色Color.Bule,无符号,无数据的空线条 LineItem curve = myPane.AddCurve("温度", list, Color.Blue, SymbolType.None); timeDraw.Interval = 10; //设置timer控件的间隔为50毫秒 timeDraw.Enabled = true; //timer可用 timeDraw.Start(); //开始 myPane.XAxis.Scale.Min = 0; //X轴最小值0 myPane.XAxis.Scale.Max = 30; //X轴最大30 myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1,也就是小间隔 myPane.XAxis.Scale.MajorStep = 5;//X轴大步长为5,也就是显示文字的大间隔 //改变轴的刻度 zedGraphControl1.AxisChange(); //保存开始时间 tickStart = Environment.TickCount; } private void timer1_Tick( object sender, EventArgs e ) { //确保CurveList不为空 if (zedGraphControl1.GraphPane.CurveList.Count <= 0) { return; } //取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem; if (curve == null) { return; } //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据 IPointListEdit list = curve.Points as IPointListEdit; if (list == null) { return; } // 时间用秒表示 double time = ( Environment.TickCount - tickStart ) / 1000.0; // 3秒循环 list.Add( time, Math.Sin( 2.0 * Math.PI * time / 3.0 ) ); Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale; if ( time > xScale.Max - xScale.MajorStep ) { xScale.Max = time + xScale.MajorStep; xScale.Min = xScale.Max - 30.0; } //第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围 zedGraphControl1.AxisChange(); //第四步:调用Form.Invalidate()方法更新图表 zedGraphControl1.Invalidate(); } private void Form1_Resize( object sender, EventArgs e ) { SetSize(); } private void SetSize() { // 控制始终是以10像素插入矩形从客户端的形 Rectangle formRect = this.ClientRectangle; formRect.Inflate( -10, -10 ); if ( zedGraphControl1.Size != formRect.Size ) { zedGraphControl1.Location = formRect.Location; zedGraphControl1.Size = formRect.Size; } }