1. 程式人生 > 其它 >C# ZedGraph實時多條曲線資料更新例項

C# ZedGraph實時多條曲線資料更新例項

1.建立曲線例項新增必要的元素

 1 public class LineChannel
 2     {
 3         public LineChannel(int id,int number,string name,string type,bool selected,Color lineColor,int lineWidth)
 4         {
 5             this.id = id;
 6             this.number = number;
 7             this.name = name;
 8             this.type = type;
 9             this.selected = selected;
10             this.lineColor = lineColor;
11             this.lineWidth = lineWidth;
12         }
13 
14         private int id;
15         private int number;
16         private string name;
17         private string type;
18         private bool selected;
19         private Color lineColor;
20         private int lineWidth;
21 
22         
23         public int ID
24         {
25             get { return this.id; }
26             //set { this.id = value; }
27         }
28 
29         public int Number
30         {
31             get { return this.number; }
32         }
33 
34         public string Name
35         {
36             get { return this.name; }
37             //set { this.name = value; }
38         }
39 
40         public string Type
41         {
42             get { return this.type; }
43             //set { this.type = value; }
44         }
45         public bool Selected
46         {
47             get { return this.selected; }
48             set { this.selected = value; }
49         }
50         public Color LineColor
51         {
52             get { return this.lineColor; }
53             set { this.lineColor = value; }
54         }
55         public int LineWidth
56         {
57             get { return this.lineWidth; }
58             set { this.lineWidth = value; }
59         }
60 
61     }

2.初始化控制元件增加曲線

全域性曲線和其實時間儲存

   private List<LineChannel> lines;   //所有的曲線
   private int tickStart = 0;     // 起始時間以毫秒為單位

初始化曲線

 1   //載入建立曲線資料
 2             LineChannel lineChannel1 = new LineChannel(1111, 1, "曲線 1", "溫度", true, Color.Red, 1);
 3             LineChannel lineChannel2 = new LineChannel(2222, 2, "曲線 2", "溫度", true, Color.Green, 1);
 4             LineChannel lineChannel3 = new LineChannel(3333, 3, "曲線 3", "溫度", true, Color.Gray, 1);
 5             LineChannel lineChannel4 = new LineChannel(4444, 4, "曲線 4", "溫度", true, Color.Black, 1);
 6 
 7             this.lines.Add(lineChannel1);
 8             this.lines.Add(lineChannel2);
 9             this.lines.Add(lineChannel3);
10             this.lines.Add(lineChannel4);
11 
12 
13             //獲取引用
14             GraphPane myPane = zedGraphControl1.GraphPane;
15             //設定標題
16             myPane.Title.Text = "實時曲線";
17             //設定X軸說明文字
18             myPane.XAxis.Title.Text = "時間";
19             //設定Y軸說明文字
20             myPane.YAxis.Title.Text = "溫度";
21 
22 
23             //新增曲線
24             foreach (LineChannel channel in this.lines)
25             {
26                 RollingPointPairList list = new RollingPointPairList(1200);
27                 LineItem curve = myPane.AddCurve(channel.Name, list, channel.LineColor, SymbolType.None);
28             }
29 
30 
31             int increment = 30;//單位s
32 
33 
34 
35 
36             myPane.XAxis.Scale.Min = 0; //X軸最小值0
37             myPane.XAxis.Scale.Max = 30; //X軸最大30
38             myPane.XAxis.Scale.MinorStep = 1;//X軸小步長1,也就是小間隔
39             myPane.XAxis.Scale.MajorStep = 5;//X軸大步長為5,也就是顯示文字的大間隔
40 
41             //改變軸的刻度
42             zedGraphControl1.AxisChange();
43             this.zedGraphControl1.Invalidate();
44             this.dataGridViewLines.DataSource = this.lines;
45             this.dataGridViewLines.ClearSelection();

3.增加執行緒處理 模擬資料

 1   //儲存開始時間
 2             tickStart = Environment.TickCount;
 3             stop = false;
 4             BackgroundWorker worker = sender as BackgroundWorker;
 5 
 6             isSampling = true;
 7             List<LineChannel> channels = this.lines;
 8             //DoWork
 9             LineData lineData;
10             while (isSampling)
11             {
12                 lineData = new LineData();
13 
14 
15                 //獲取資料
16                 double time = (Environment.TickCount - tickStart) / 1000.0;
17                 double data = Math.Sin(2.0 * Math.PI * time / 3.0);
18                 double data2 = data - 0.4;
19                 double data3 = data - 0.8;
20                 double data4 = data - 1.2;
21 
22                 //新增資料
23                 lineData.Add(channels[0], time, data);
24                 lineData.Add(channels[1], time, data2);
25                 lineData.Add(channels[2], time, data3);
26                 lineData.Add(channels[3], time, data4);
27 
28                 //上報資料處理
29                 worker.ReportProgress(0, lineData);
30 
31                 Thread.Sleep(50);
32             }

4.資料接受更新曲線

//接收上報的資料LineData 資料處理如下
1 LineData lineData = e.Data; 2 3 double time = 0, data = 0; 4 5 foreach (LineChannel channel in lineData.Channels) 6 { 7 if (e.Data.GetData(channel, out time, out data)) 8 { 9 LineItem curve = null; 10 11 foreach (var item in zedGraphControl1.GraphPane.CurveList) 12 { 13 if (item.Label.Text == channel.Name) 14 curve = item as LineItem; 15 } 16 17 if (curve != null) 18 { 19 IPointListEdit list = curve.Points as IPointListEdit; 20 list.Add(time, data); 21 } 22 23 curve.IsVisible = channel.Selected; 24 curve.Line.Width = channel.LineWidth; 25 curve.Line.Color = channel.LineColor; 26 27 28 } 29 30 31 } 32 33 34 Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale; 35 if (time > xScale.Max - xScale.MajorStep) 36 { 37 xScale.Max = time + xScale.MajorStep; 38 xScale.Min = xScale.Max - 30.0; 39 } 40 41 42 //if (time > xScale.Max) 43 //{ 44 // xScale.Max = time; 45 // xScale.Min = xScale.Max - 30; 46 //} 47 48 49 //第三步:呼叫ZedGraphControl.AxisChange()方法更新X和Y軸的範圍 50 zedGraphControl1.AxisChange(); 51 52 //第四步:呼叫Form.Invalidate()方法更新圖表 53 zedGraphControl1.Invalidate();

程式執行效果

初始化曲線

更換曲線顏色

開始採集

JAVA&NET技術QQ群號:456257217有問題的可以在群裡面提問。