原文链接 http://www.csharpwin.com/csharpspace/3701r5418.shtml
用定时器超时来刷新数据,实现“连续”的图形输出。
1. 新建图像面板
GraphPane myPane = zedGraphControl1.GraphPane;
2. 初始化图像、坐标轴标题
myPane.Title.Text = “”;
myPane.XAxis.Title.Text = "";
myPane.YAxis.Title.Text = "";
3. 新建RollingPointPairList滚动数据对用于存放实时显示的数据(应大于滚动区域内所有采样点数之和,尽量稍微偏大些)
RollingPointPairList list = new RollingPointPairList( 1200 );
4. 新建并初始化曲线
LineItem curve = myPane.AddCurve(string label, IPointList points, Color color, SymbolType symbolType);
5. 设置定时器,并启动
timer1.Interval =1;
timer1.Enabled = true;
timer1.Start();
6. 设定面板X轴的范围和主标、副标(同时也可以设定Y轴)
myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 30;
myPane.XAxis.Scale.MinorStep = 1;
myPane.XAxis.Scale.MajorStep = 5;
7. 用zedGraphControl1.AxisChange();来调整X轴
8. 获取当前环境tick数
tickStart = Environment.TickCount;
定时器超时函数:
1. 如果当前面板曲线数为零,返回
zedGraphControl1.GraphPane.CurveList.Count <= 0
2. 新建曲线,如果面板曲线为空,则返回
LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
as运算符将类型检查和强制转换合二为一,妙!
3. 获取曲线上的点集合,如果没有则返回
IPointListEdit list = curve.Points as IPointListEdit;
4. 获取当前时间,向刚才的点集合中添加当前点
double time = ( Environment.TickCount - tickStart ) / 1000.0;
list.Add( time, Math.Sin( 2.0 * Math.PI * time / 3.0 ) );
5. 新建坐标尺度
Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
6. 保持X轴上25秒的显示范围,总共标注范围是30秒
if ( time > xScale.Max - xScale.MajorStep )
{
xScale.Max = time + xScale.MajorStep;
xScale.Min = xScale.Max - 30.0;
}
7. 调整坐标轴
zedGraphControl1.AxisChange();
8. 使控件的整个图面无效并导致重绘控件
zedGraphControl1.Invalidate();
当调整窗口时:
1. 新建一个矩形,并指向当前矩形
Rectangle formRect = this.ClientRectangle;
2. 指定放大量
formRect.Inflate( 0, 0);
3. 调整大小
if ( zedGraphControl1.Size != formRect.Size )
{
zedGraphControl1.Location = formRect.Location;
zedGraphControl1.Size = formRect.Size;
}