欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#绘制实时曲线的方法

程序员文章站 2022-06-10 09:04:19
本文实例为大家分享了c#绘制实时曲线的具体代码,供大家参考,具体内容如下1.要做一个调试工具,采集传感器数据并显示。绘制曲线注意坐标反转,线条的张力即可。项目中的曲线是从右往左显示的,线条的坐标都放在...

本文实例为大家分享了c#绘制实时曲线的具体代码,供大家参考,具体内容如下

1.要做一个调试工具,采集传感器数据并显示。绘制曲线注意坐标反转,线条的张力即可。项目中的曲线是从右往左显示的,线条的坐标都放在list里了,效果如下图:

C#绘制实时曲线的方法

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.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<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 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<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) { 
                case "x":
                    showx = show;
                    break;
                case "y":
                    showy = show;
                    break;
                case "z":
                    showz = show;
                    break;
                default:
                    break;
            }
        }
 
        #endregion
    }

3.ui上使用threadstart进行调用,根据需要设置休眠时间即可,同时设置picturebox显示即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: C# 曲线