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<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显示即可。

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

(0)

相关推荐

  • C#画笔Pen绘制光滑模式曲线的方法

    本文实例讲述了C#画笔Pen绘制光滑模式曲线的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namesp

  • C#实现chart控件动态曲线绘制

    本文实例为大家分享了C#实现chart控件动态曲线绘制的具体代码,供大家参考,具体内容如下 思想 实验室要做一个动态曲线绘制,网上方法很多,但是缺乏完整代码和效果图的整合,往往总是缺少其一,因此整理如下,方便大家编程,节约时间.思路:新建一个队列,利用timer控件,动态的往队列中加入数据,每次触发事件,就相当于将队列中的值全部重新画一遍. 我的目的是做四个点的动态监测,所以代码重复了四次,其实应该用4个线程来做,思路就显得较为清晰了,这也是可以改进的地方. public partial cla

  • c# 实时曲线图示例代码

    示例目的:使用时间器添加曲线图的点,以达到实时描绘曲线图的效果.X轴显示时分,Y轴显示0-20的随机数 1. 必须安装DevExpress控件,没有安装的朋友可以使用下面的链接下载安装 https://www.jb51.net/softs/547627.html 2. 新建Windows窗体应用程序,直接在工具箱搜索ChartControl并使用改组件 3. 添加曲线图,并设置其属性 当拖拉该组件到窗体设计页面时,会弹出[Chart Designer],可在此处添加曲线图,并设置其属性 3.1.

  • C#实现贝塞尔曲线的方法

    本文实例为大家分享了C#实现贝塞尔曲线的具体代码,供大家参考,具体内容如下 话不多直接上代码 public Transform[] controlPoints; //曲线的控制点 ,最少三个,起点,弧度点,终点     public GameObject codeGameObject; //要动的物体     private int _segmentNum = 50; //运动物体过程分的段数     private int numIndex = 1;      void Start()    

  • C#动态绘制多条曲线的方法

    本文实例为大家分享了C#动态绘制多条曲线的具体代码,供大家参考,具体内容如下 实时绘制多条曲线,纵轴为数值,横轴为时间,精确到毫秒 实现效果如下: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Thr

  • C#画笔Pen绘制曲线的方法

    本文实例讲述了C#画笔Pen绘制曲线的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace

  • c# 曲线图生成代码

    复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Drawing.Imaging; using System.Collections; namespace Curve { public cl

  • C#使用Chart绘制曲线

    本文实例为大家分享了C#使用Chart绘制曲线的具体代码,供大家参考,具体内容如下 新建一个控制台应用程序,程序名:WindowsFormsApp2,将下面的代码拷贝进去即可 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using

  • C#绘制曲线图的方法

    本文实例讲述了C#绘制曲线图的方法.分享给大家供大家参考.具体如下: 1. 曲线图效果: 2. C#代码: /// <summary> /// 自动根据参数调整图像大小 /// </summary> public void Fit() { //计算字体距离 intFontSpace = FontSize + 5; //计算图像边距 float fltSpace = Math.Min(Width / 6, Height / 6); XSpace = fltSpace; YSpace

  • C#实现鼠标移动到曲线图上显示值的方法

    本文实例讲述了C#实现鼠标移动到曲线图上显示值的方法.分享给大家供大家参考.具体实现方法如下: 一.问题: 完成折线图报表后,产品经理要求把折线上的数值去掉,鼠标经过折线点时显示数值. 二.实现方法: 该方法针对dotnetcharting 下的charting折线图报表使用. 实现思路为,在该窗体上添加一个lable标签,当鼠标经过折线点时获取该点的x轴,y轴值和鼠标坐标值.然后将lable的坐标标记为鼠标所在坐标,并且给lable赋值,并且将lable显示出来.   具体实现代码如下: 复制

随机推荐