C#繪制實(shí)時(shí)曲線的方法
本文實(shí)例為大家分享了C#繪制實(shí)時(shí)曲線的具體代碼,供大家參考,具體內(nèi)容如下
1.要做一個(gè)調(diào)試工具,采集傳感器數(shù)據(jù)并顯示。繪制曲線注意坐標(biāo)反轉(zhuǎn),線條的張力即可。項(xiàng)目中的曲線是從右往左顯示的,線條的坐標(biāo)都放在list里了,效果如下圖:
2.上代碼
public class DrawingCurve ? ? { ? ? ? ? private Graphics graphics; //Graphics 類提供將對(duì)象繪制到顯示設(shè)備的方法 ? ? ? ? private Bitmap bitmap; //位圖對(duì)象 ? ? ? ? private int timeLine = 60;//60s ? ? ? ? private int canvasWidth = 600;//畫布長(zhǎng)度 ? ? ? ? private int sliceCount = 0;//刻度分段個(gè)數(shù) = timeLine ? ? ? ? private int xSlice = 10;//X軸刻度分端寬度 ? ? ? ? private int xSliceHeight = 10;//X軸刻度高度 ? ? ? ? private float tension = 0.5f; //張力系數(shù) ? ? ? ? private bool showX = true; ? ? ? ? private bool showY = true; ? ? ? ? private bool showZ = true; ? ? ? ? ? //Queue<PointF> que = new Queue<PointF>();//曲線fifo ? ? ? ? /// <summary> ? ? ? ? /// 構(gòu)造函數(shù) ? ? ? ? /// </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> ? ? ? ? /// 曲線開關(guān) ? ? ? ? /// </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進(jìn)行調(diào)用,根據(jù)需要設(shè)置休眠時(shí)間即可,同時(shí)設(shè)置pictureBox顯示即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)子類與父類的相互轉(zhuǎn)換
這篇文章主要介紹了C#實(shí)現(xiàn)子類與父類的相互轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05C#實(shí)現(xiàn)線程安全的簡(jiǎn)易日志記錄方法
這篇文章主要介紹了C#實(shí)現(xiàn)線程安全的簡(jiǎn)易日志記錄方法,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08Unity實(shí)現(xiàn)文本轉(zhuǎn)貼圖
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)文本轉(zhuǎn)貼圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05C#實(shí)現(xiàn)系統(tǒng)休眠或靜止休眠的方法
這篇文章主要介紹了C#實(shí)現(xiàn)系統(tǒng)休眠或靜止休眠的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05c#中SqlHelper封裝SqlDataReader的方法
這篇文章主要介紹了c#中SqlHelper封裝SqlDataReader的方法,涉及C#針對(duì)數(shù)據(jù)庫(kù)相關(guān)操作封裝與使用的技巧,需要的朋友可以參考下2015-05-05C#創(chuàng)建簡(jiǎn)單windows窗體應(yīng)用(加法器)
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建一個(gè)簡(jiǎn)單windows窗體應(yīng)用的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03