【ZedGraph】控制元件的使用2_動態實時更新圖
轉載: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;
}