C#繪製即時曲線

來源:互聯網
上載者:User

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.