C#怎么绘制实时曲线
这篇文章主要讲解了“C#怎么绘制实时曲线”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么绘制实时曲线”吧!
1.要做一个调试工具,采集传感器数据并显示。绘制曲线注意坐标反转,线条的张力即可。项目中的曲线是从右往左显示的,线条的坐标都放在list里了,效果如下图:
2.上代码
publicclassDrawingCurve{privateGraphicsgraphics;//Graphics类提供将对象绘制到显示设备的方法privateBitmapbitmap;//位图对象privateinttimeLine=60;//60sprivateintcanvasWidth=600;//画布长度privateintsliceCount=0;//刻度分段个数=timeLineprivateintxSlice=10;//X轴刻度分端宽度privateintxSliceHeight=10;//X轴刻度高度privatefloattension=0.5f;//张力系数privateboolshowX=true;privateboolshowY=true;privateboolshowZ=true;//Queue<PointF>que=newQueue<PointF>();//曲线fifo///<summary>///构造函数///</summary>publicDrawingCurve(){this.xSlice=this.canvasWidth/timeLine;}///<summary>///绘制画布///</summary>///<paramname="width"></param>///<paramname="height"></param>///<paramname="points"></param>///<returns></returns>publicBitmapDrawCanvas(intwidth,intheight,List<float>points){if(bitmap!=null){bitmap.Dispose();bitmap=null;}bitmap=newBitmap(width,height);graphics=Graphics.FromImage(bitmap);graphics.FillRectangle(Brushes.Black,newRectangle(0,0,width,height));graphics.Transform=newMatrix(1,0,0,-1,0,0);//Y轴向上为正,X向右为graphics.TranslateTransform(0,height/2,MatrixOrder.Append);Penpen=newPen(Color.Red,1);pen.DashStyle=DashStyle.Custom;pen.DashPattern=newfloat[]{2,2};graphics.DrawLine(pen,newPoint(0,height/4),newPoint(width,height/4));graphics.DrawLine(pen,newPoint(0,height/-4),newPoint(width,height/-4));graphics.DrawLine(newPen(Color.GreenYellow,1),newPoint(0,0),newPoint(width,0));graphics.DrawString("0",newFont("Vendara",10),Brushes.White,newPoint(0,-15));graphics.DrawString("+",newFont("Vendara",10),Brushes.White,newPoint(0,height/4));graphics.DrawString("-",newFont("Vendara",10),Brushes.White,newPoint(0,height/-4-15));graphics.Transform=newMatrix(1,0,0,1,0,0);//Y轴向上为正,X向右为graphics.TranslateTransform(0,height/2,MatrixOrder.Append);graphics.DrawString("-59s",newFont("Vendara",8),Brushes.White,newPoint(0,height/2-15));graphics.DrawString("0s",newFont("Vendara",8),Brushes.White,newPoint(width-20,height/2-15));for(inti=0;i<timeLine;i++){intscale=i*xSlice;graphics.DrawLine(newPen(newSolidBrush(Color.Blue)),0+scale,0+xSliceHeight*0.1f,0+scale,0-xSliceHeight*0.1f);}graphics.Transform=newMatrix(-1,0,0,-1,0,0);//Y轴向上为正,X向右为graphics.TranslateTransform(width,height/2,MatrixOrder.Append);if(showX)DrawX(graphics,points);if(showY)DrawY(graphics,points);if(showZ)DrawZ(graphics,points);graphics.Dispose();returnbitmap;}#region绘制曲线privatevoidDrawX(Graphicsgraphics,List<float>points){PenCurvePen=newPen(Color.Cyan,2);PointF[]CurvePointF=newPointF[points.Count];floatkeys=0;floatvalues=0;for(inti=0;i<points.Count;i++){keys=xSlice*i;values=10*(points[i]/10);CurvePointF[i]=newPointF(keys,values);}graphics.DrawCurve(CurvePen,CurvePointF,this.tension);}privatevoidDrawY(Graphicsgraphics,List<float>points){PenCurvePen=newPen(Color.Purple,2);PointF[]CurvePointF=newPointF[points.Count];floatkeys=0;floatvalues=0;for(inti=0;i<points.Count;i++){keys=xSlice*i;values=10*(points[i]/10);CurvePointF[i]=newPointF(keys,values);}graphics.DrawCurve(CurvePen,CurvePointF,this.tension);}privatevoidDrawZ(Graphicsgraphics,List<float>points){PenCurvePen=newPen(Color.OrangeRed,2);PointF[]CurvePointF=newPointF[points.Count];floatkeys=0;floatvalues=0;for(inti=0;i<points.Count;i++){keys=xSlice*i;values=10*(points[i]/10);CurvePointF[i]=newPointF(keys,values);}graphics.DrawCurve(CurvePen,CurvePointF,this.tension);}///<summary>///曲线开关///</summary>///<paramname="_xyz"></param>///<paramname="show"></param>publicvoidHideCurve(string_xyz,boolshow){switch(_xyz){case"x":showX=show;break;case"y":showY=show;break;case"z":showZ=show;break;default:break;}}#endregion}
3.UI上使用ThreadStart进行调用,根据需要设置休眠时间即可,同时设置pictureBox显示即可。
感谢各位的阅读,以上就是“C#怎么绘制实时曲线”的内容了,经过本文的学习后,相信大家对C#怎么绘制实时曲线这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。