服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#绘制实时曲线的方法

C#绘制实时曲线的方法

2023-02-07 14:31一点晴 C#

这篇文章主要为大家详细介绍了C#绘制实时曲线的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

C#绘制实时曲线的方法

2.上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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显示即可。

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

原文链接:https://blog.csdn.net/ikevin/article/details/49848645

延伸 · 阅读

精彩推荐
  • C#C# Button窗体常用属性及事件详解

    C# Button窗体常用属性及事件详解

    在本篇文章里小编给各位分享的是关于C# Button窗体常用属性及事件详解,需要的朋友们可以参考下。...

    微光9982022-08-30
  • C#C# List 并发丢数据问题原因及解决方案

    C# List 并发丢数据问题原因及解决方案

    这篇文章主要介绍了C# List 并发丢数据问题原因及解决方案,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    丹枫无迹11062022-11-02
  • C#c#中DataTable转List的2种方法示例

    c#中DataTable转List的2种方法示例

    这篇文章主要给大家介绍了关于c#中DataTable转List的2种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    chenweilong2138712022-11-12
  • C#C#数据结构之队列(Quene)实例详解

    C#数据结构之队列(Quene)实例详解

    这篇文章主要介绍了C#数据结构之队列(Quene),结合实例形式较为详细的讲述了队列的功能、原理与C#实现队列的相关技巧,需要的朋友可以参考下...

    Jimmy.Yang7692021-11-04
  • C#C#调用RabbitMQ实现消息队列的示例代码

    C#调用RabbitMQ实现消息队列的示例代码

    这篇文章主要介绍了C#调用RabbitMQ实现消息队列的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    kiba5185292022-08-15
  • C#C#读写指定编码格式的文本文件

    C#读写指定编码格式的文本文件

    这篇文章主要为大家详细介绍了C#读写指定编码格式文本文件的方法,感兴趣的小伙伴们可以参考一下...

    吴超10932021-11-15
  • C#C#递归算法之打靶算法分析

    C#递归算法之打靶算法分析

    这篇文章是对打靶算法分析,比较简单,但逻辑一定要清楚,分析问题的方法一定要准确,有需要的朋友可以参考一下。...

    Robin8462021-11-26
  • C#WPF应用启动慢的问题解决

    WPF应用启动慢的问题解决

    今天碰到一个奇怪的现象,在某些机器上,进行了系统还原后,WPF应用打开较慢,约有35s。本文先记录下该问题的解决方案,应用启动性能官方文档中有说...

    louzi6232022-11-20