微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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",Color.Green,1);
 4             LineChannel lineChannel3 = new LineChannel(3333,3,"曲线 3",Color.Gray,1);
 5             LineChannel lineChannel4 = new LineChannel(4444,4,"曲线 4",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],data2);
25                 lineData.Add(channels[2],data3);
26                 lineData.Add(channels[3],data4);
27 
28                 //上报数据处理
29                 worker.ReportProgress(0,lineData);
30 
31                 Thread.Sleep(50);
32             }

4.数据接受更新曲线

 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();

程序运行效果

初始化曲线

分享图片

更换曲线颜色

分享图片

 

开始采集

分享图片

 

程序源代码工程下载

实现了比较基础简单的功能,提供思路,遇到bug不要惊讶,没有细调,需要源代码可以留言,给出邮箱,看到后会回复

 本程序参考

https://www.cnblogs.com/chuhj/p/7988404.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐