C# 具有背景栅格的绘曲线类(一)
来源:互联网 发布:python logging 编辑:程序博客网 时间:2024/05/17 03:20
本文实现了用于绘制曲线的通用类,类实现对背景栅格的绘制,并将由外部传入到先进先出栈中的数据作为
曲线的关键点,返回一个位图的对象用于显示曲线。
using System;using System.Collections.Generic;using System.Collections;using System.Text;using System.Drawing;namespace PID_Control{ class Curve { public Queue KeyDots=new Queue(); //曲线关键点 private Graphics objGraphic;//提供绘制的方法 public Bitmap objBitmap;//位图对象 private Point CurrentPoint = new Point(0, 0); //当前点 private Point LastPoint = new Point(50, 635); //最后的点 private int m_Width = 900;// 图像宽度 private int m_Height = 650;//图像高度 private int m_XGrid = 50;//栅格X长度 private int m_YGrid = 50;//栅格Y长度 private Color m_BorderColor = Color.Blue; //边框颜色 private Color m_BgColor = Color.Black;//背景颜色 private Color m_GridColor = Color.Gray;//栅格颜色 public int Width { set { m_Width =value; } get { return m_Width; } } public int Height { set { m_Height = value; } get { return m_Height; } } public Color BorderColor { set { m_BorderColor = value; } get { return m_BorderColor; } } public Color BgColor { set { m_BgColor = value; } get { return m_BgColor; } } public Color GridColor { set { m_GridColor = value; } get { return m_GridColor; } } public int XGrid { set { m_XGrid = value; } get { return m_XGrid; } } public int YGrid { set { m_YGrid = value; } get { return m_YGrid; } } public Curve() { InitializeGraph(); DrawContent(); } public void InitializeGraph() { //创建指定高度和宽度的位图对象 objBitmap = new Bitmap(Width, Height); //从位图对象中创建Graphics对象 objGraphic = Graphics.FromImage(objBitmap); //创建指定颜色的矩形边框 objGraphic.DrawRectangle(new Pen(BorderColor, 1),0,0, Width, Height); objGraphic.FillRectangle(new SolidBrush(BgColor),1,1, Width, Height); //画X方向栅格并标注 int t = 0; for (int i = Height-15; i >= 0; i -= YGrid) { objGraphic.DrawLine(new Pen(GridColor, 1), 50, i, Width, i); objGraphic.DrawString(System.Convert.ToString(t*5000), new Font("宋体", 10), new SolidBrush(Color.White), 0, i); t++; } //画Y方向栅格并标注 t = 0; for (int i = 50; i < Width; i += XGrid) { objGraphic.DrawLine(new Pen(GridColor, 1), i, 0, i, Height-15); objGraphic.DrawString(System.Convert.ToString(t), new Font("宋体", 10), new SolidBrush(Color.White), i, Height-13); t++; } } public void DrawContent() { while (KeyDots.Count > 0) { //设置新点坐标 CurrentPoint.X = LastPoint.X + 2; CurrentPoint.Y = Height-System.Convert.ToInt16(KeyDots.Dequeue())-15; objGraphic.DrawLine(new Pen(Color.Red, 2),LastPoint,CurrentPoint); //重置最后点坐标 LastPoint = CurrentPoint; if (LastPoint.X > Width) { InitializeGraph(); LastPoint.X = 50; } } } }}