您的位置:首页 > 其它

ZedGraph控件能够显示动态或静态数据

2008-10-30 14:15 537 查看
ZedGraph控件能够显示动态或静态数据。
若要定时的向一个图表里添加数据,应按照如下顺序实现:
1.在GraphPane.CurveList集合中查找CurveItem
2.在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
3.调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
4.调用Form.Invalidate()方法更新图表
/*翻译的不准,大概意思就是找到数据集合后增加/修改数据->修改轴范围->更新图表*/

每个CurveItem里的数据点作为一个IPointList接口的引用存储在CurveItem.Points中.注意数据点列表能被任意一个实现了 IPointList的接口的类引用.当然,此类需要同时实现IPoiontListEdit(),IpointListEdit.Add()和 IPontListEdit.RemoveAt()方法.

The code sample is for a form that implements a ZedGraphControl with a Timer event to show dynamically updated data. You can download the complete project from the links below:
代码示例为一个ZedGraph控件在WinForm中的实现,用Timer事件动态显示更新数据.

以下是完整代码(没有VS设计资料).
此代码包括两个方法:Form_Load()方法用一条没有数据点的曲线初始化图表,Timer_Tick()方法响应timer事件增加数据点.

Visual C# Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace DynamicData
...{
public partial class Form1 : Form
...{
// Starting time in milliseconds
int tickStart = 0;

public Form1()
...{
InitializeComponent();
}

private void Form1_Load( object sender, EventArgs e )
...{
//获取引用
GraphPane myPane = zedGraphControl1.GraphPane;
//设置标题
myPane.Title.Text = "Test of Dynamic Data Update with ZedGraph " +
"(After 25 seconds the graph scrolls)";
//设置X轴说明文字
myPane.XAxis.Title.Text = "Time, Seconds";
//设置Y轴文字
myPane.YAxis.Title.Text = "Sample Potential, Volts";

// Save 1200 points. At 50 ms sample rate, this is one minute
// The RollingPointPairList is an efficient storage class that always
// keeps a rolling set of point data without needing to shift any data values
//设置1200个点,假设每50毫秒更新一次,刚好检测1分钟
//一旦构造后将不能更改这个值
RollingPointPairList list = new RollingPointPairList( 1200 );

// Initially, a curve is added with no data points (list is empty)
// Color is blue, and there will be no symbols
//开始,增加的线是没有数据点的(也就是list为空)
//增加一条名称:Voltage,颜色Color.Bule,无符号,无数据的空线条
LineItem curve = myPane.AddCurve( "Voltage", list, Color.Blue, SymbolType.None );

// Sample at 50ms intervals
//设置timer控件的间隔为50毫秒
timer1.Interval = 50;
//timer可用
timer1.Enabled = true;
//开始
timer1.Start();

// Just manually control the X axis range so it scrolls continuously
// instead of discrete step-sized jumps
//X轴最小值0
myPane.XAxis.Scale.Min = 0;
//X轴最大30
myPane.XAxis.Scale.Max = 30;
//X轴小步长1,也就是小间隔
myPane.XAxis.Scale.MinorStep = 1;
//X轴大步长为5,也就是显示文字的大间隔
myPane.XAxis.Scale.MajorStep = 5;

// Scale the axes
//改变轴的刻度
zedGraphControl1.AxisChange();

// Save the beginning time for reference
//保存开始时间
tickStart = Environment.TickCount;
}

private void timer1_Tick( object sender, EventArgs e )
...{
// Make sure that the curvelist has at least one curve
//确保CurveList不为空
if ( zedGraphControl1.GraphPane.CurveList.Count <= 0 )
return;

// Get the first CurveItem in the graph
//取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem
LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
if ( curve == null )
return;

// Get the PointPairList
//第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
IPointListEdit list = curve.Points as IPointListEdit;
// If this is null, it means the reference at curve.Points does not
// support IPointListEdit, so we won't be able to modify it
if ( list == null )
return;

// Time is measured in seconds
double time = (Environment.TickCount - tickStart) / 1000.0;

// 3 seconds per cycle
list.Add( time, Math.Sin( 2.0 * Math.PI * time / 3.0 ) );

// Keep the X scale at a rolling 30 second interval, with one
// major step between the max X value and the end of the axis
Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
if ( time > xScale.Max - xScale.MajorStep )
...{
xScale.Max = time + xScale.MajorStep;
xScale.Min = xScale.Max - 30.0;
}

// Make sure the Y axis is rescaled to accommodate actual data
//第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
zedGraphControl1.AxisChange();
// Force a redraw
//第四步:调用Form.Invalidate()方法更新图表
zedGraphControl1.Invalidate();
}

private void Form1_Resize( object sender, EventArgs e )
...{
SetSize();
}

// Set the size and location of the ZedGraphControl
private void SetSize()
...{
// Control is always 10 pixels inset from the client rectangle of the form
Rectangle formRect = this.ClientRectangle;
formRect.Inflate( -10, -10 );

if ( zedGraphControl1.Size != formRect.Size )
...{
zedGraphControl1.Location = formRect.Location;
zedGraphControl1.Size = formRect.Size;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: