本文实例为大家分享了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<pointf> que = new queue<pointf>();//曲线fifo /// <summary> /// 构造函数 /// </summary> public drawingcurve() { this.xslice = this.canvaswidth / timeline; } /// <summary> /// 绘制画布 /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="points"></param> /// <returns></returns> public bitmap drawcanvas(int width, int height,list<float> points) { if (bitmap != null) { bitmap.dispo(); 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.dispo(); return bitmap; } #region 绘制曲线 private void drawx(graphics graphics, list<float> 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<float> points) { pen cu刘长卿rvepen = 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<float> 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); } /// <summary> /// 曲线开关 /// </summary> /// <param name="_xyz"></param> /// <param name="show"></param> public void hidecurve(string _xyz,bool show) { switch (_xyz) { ca "x": showx = show; break; ca "y": showy = show; break; ca "z": showz = show; break; default: break; } } #endregion }
3.ui上使用threadstart进行调用,根据需要设置休眠时间即可,同时设置picturebox显示即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-05 01:24:15,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a555525fdb3dbfffec238b1404f00148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#绘制实时曲线的方法.doc
本文 PDF 下载地址:C#绘制实时曲线的方法.pdf
留言与评论(共有 0 条评论) |