先看展示效果
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 ID24 {25 get { return this.ID; }26 //set { this.ID = value; }27 }28 29 public int Number30 {31 get { return this.number; }32 }33 34 public string name35 {36 get { return this.name; }37 //set { this.name = value; }38 }39 40 public string Type41 {42 get { return this.type; }43 //set { this.type = value; }44 }45 public bool Selected46 {47 get { return this.selected; }48 set { this.selected = value; }49 }50 public color linecolor51 {52 get { return this.linecolor; }53 set { this.linecolor = value; }54 }55 public int linewidth56 {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;//单位s32 33 34 35 36 myPane.XAxis.Scale.Min = 0; //X轴最小值037 myPane.XAxis.Scale.Max = 30; //X轴最大3038 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
总结以上是内存溢出为你收集整理的C# ZedGraph实时多条曲线数据更新实例全部内容,希望文章能够帮你解决C# ZedGraph实时多条曲线数据更新实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)