C#怎么绘制实时曲线
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要讲解了"C#怎么绘制实时曲线",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#怎么绘制实时曲线"吧!1.要做一个调试工具,采集传感器数据并
千家信息网最后更新 2026年10月31日C#怎么绘制实时曲线
这篇文章主要讲解了"C#怎么绘制实时曲线",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#怎么绘制实时曲线"吧!
1.要做一个调试工具,采集传感器数据并显示。绘制曲线注意坐标反转,线条的张力即可。项目中的曲线是从右往左显示的,线条的坐标都放在list里了,效果如下图:
2.上代码
public class DrawingCurve { private Graphics graphics; //Graphics 类提供将对象绘制到显示设备的方法 private Bitmap bitmap; //位图对象 private int timeLine = 60;//60s private int canvasWidth = 600;//画布长度 private int sliceCount = 0;//刻度分段个数 = timeLine private int xSlice = 10;//X轴刻度分端宽度 private int xSliceHeight = 10;//X轴刻度高度 private float tension = 0.5f; //张力系数 private bool showX = true; private bool showY = true; private bool showZ = true; //Queue que = new Queue();//曲线fifo /// /// 构造函数 /// public DrawingCurve() { this.xSlice = this.canvasWidth / timeLine; } /// /// 绘制画布 /// /// /// /// /// public Bitmap DrawCanvas(int width, int height,List points) { if (bitmap != null) { bitmap.Dispose(); bitmap = null; } bitmap = new Bitmap(width, height); graphics = Graphics.FromImage(bitmap); graphics.FillRectangle(Brushes.Black, new Rectangle(0, 0, width, height)); graphics.Transform = new Matrix(1, 0, 0, -1, 0, 0);//Y轴向上为正,X向右为 graphics.TranslateTransform(0, height / 2, MatrixOrder.Append); Pen pen = new Pen(Color.Red, 1); pen.DashStyle = DashStyle.Custom; pen.DashPattern = new float[] { 2, 2 }; graphics.DrawLine(pen, new Point(0, height / 4), new Point(width, height / 4)); graphics.DrawLine(pen, new Point(0, height / -4), new Point(width, height / -4)); graphics.DrawLine(new Pen(Color.GreenYellow,1), new Point(0, 0), new Point(width, 0)); graphics.DrawString("0", new Font("Vendara",10), Brushes.White, new Point(0, -15)); graphics.DrawString("+", new Font("Vendara", 10), Brushes.White, new Point(0, height / 4)); graphics.DrawString("-", new Font("Vendara", 10), Brushes.White, new Point(0, height / -4-15)); graphics.Transform = new Matrix(1, 0, 0, 1, 0, 0);//Y轴向上为正,X向右为 graphics.TranslateTransform(0, height / 2, MatrixOrder.Append); graphics.DrawString("-59s", new Font("Vendara", 8), Brushes.White, new Point(0, height/2-15)); graphics.DrawString("0s", new Font("Vendara", 8), Brushes.White, new Point(width-20, height / 2 - 15)); for (int i = 0; i < timeLine; i++) { int scale = i * xSlice; graphics.DrawLine(new Pen(new SolidBrush(Color.Blue)), 0 + scale, 0 + xSliceHeight * 0.1f, 0 + scale, 0 - xSliceHeight * 0.1f); } graphics.Transform = new Matrix(-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(); return bitmap; } #region 绘制曲线 private void DrawX(Graphics graphics, List points) { Pen CurvePen = new Pen(Color.Cyan, 2); PointF[] CurvePointF = new PointF[points.Count]; float keys = 0; float values = 0; for (int i = 0; i < points.Count; i++) { keys = xSlice * i; values = 10 * (points[i] / 10); CurvePointF[i] = new PointF(keys, values); } graphics.DrawCurve(CurvePen, CurvePointF, this.tension); } private void DrawY(Graphics graphics, List points) { Pen CurvePen = new Pen(Color.Purple, 2); PointF[] CurvePointF = new PointF[points.Count]; float keys = 0; float values = 0; for (int i = 0; i < points.Count; i++) { keys = xSlice * i; values = 10 * (points[i] / 10); CurvePointF[i] = new PointF(keys, values); } graphics.DrawCurve(CurvePen, CurvePointF, this.tension); } private void DrawZ(Graphics graphics, List points) { Pen CurvePen = new Pen(Color.OrangeRed, 2); PointF[] CurvePointF = new PointF[points.Count]; float keys = 0; float values = 0; for (int i = 0; i < points.Count; i++) { keys = xSlice * i; values = 10 * (points[i] / 10); CurvePointF[i] = new PointF(keys, values); } graphics.DrawCurve(CurvePen, CurvePointF, this.tension); } /// /// 曲线开关 /// /// /// public void HideCurve(string _xyz,bool show) { 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#怎么绘制实时曲线这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
曲线
实时
C#
刻度
学习
内容
坐标
对象
张力
画布
线条
上代
个数
传感器
位图
函数
同时
宽度
就是
工具
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技海报
西安纸贵互联网科技有限公司
深圳互联网科技公司
北京互联网科技有限公司
互联网金融 金融科技
科技服务与互联网
丰台科技园 互联网
海南科技产业互联网平台
银川新华互联网科技学校地址
安控科技rtu产品工业互联网
栖莱科技互联网股份有限公司
佳云科技和互联网广告
科技互联网大厂
苏州五指互联网络科技有限
华夏恒生互联网科技013171
洛阳共同富裕互联网科技有限公司
半导体是互联网高科技吗
恒生科技指数 中概互联网
互联网科技创新方面的资料
知域互联网科技有限公司官网
新华互联网科技
科技企业和互联网企业
互联网科技社
互联网科技小贷
百单互联网科技有限公司
新华互联网科技校企合作
互联网企业和科技型企业
昭通互联网科技选哪家
朱啸虎曾投资哪些互联网科技企业
恒生互联网科技趋势