您的位置:首页 > 编程语言 > C#

C# 封装完成的坐标轴及画折线类

2013-10-10 17:24 471 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Drawing.Drawing2D;

namespace Model1

{

    class DrawImage

    {

        private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法

        private Bitmap objBitmap; //位图对象

        private float fltWidth = 900; //图像宽度

        private float fltHeight = 500; //图像高度

        private float fltXSlice = 30; //X轴刻度宽度

        private float fltYSlice = 30; //Y轴刻度宽度

        private float fltYSliceValue = 700; //Y轴刻度的数值宽度

        private float fltYSliceValue2 = 10; //Y轴2刻度的数值宽度

        private float fltYSliceBegin = 0; //Y轴刻度开始值

        private string[] strTitle; //标题

        private string strXAxisText = "时间"; //X轴说明文字

        private string strYAxisText = "流量"; //Y轴说明文字

        private string strYAxisText2 = "速度"; //Y轴说明文字2

        private string[] strsKeys; //键

        private float[] fltsValues; //值

        private float[] fltsValues2; //值2

        private Color clrBgColor = Color.Snow; //背景色

        private Color clrTextColor = Color.Black; //文字颜色

        private Color clrBorderColor = Color.Black; //整体边框颜色

        private Color clrAxisColor = Color.Black; //轴线颜色

        private Color clrAxisTextColor = Color.Black; //轴说明文字颜色

        private Color clrSliceTextColor = Color.Black; //刻度文字颜色

        private Color clrSliceColor = Color.Black; //刻度颜色

        private Color[] clrsCurveColors = new Color[] { Color.Red, Color.Blue, Color.Purple, Color.Green }; //曲线颜色

        private float fltXSpace = 100f; //图像左右距离边缘距离

        private float fltYSpace = 100f; //图像上下距离边缘距离

        private int intFontSize = 9; //字体大小号数

        private float fltXRotateAngle = 30f; //X轴文字旋转角度

        private float fltYRotateAngle = 0f; //Y轴文字旋转角度

        private int intCurveSize = 2; //曲线线条大小

        private int intFontSpace = 0; //intFontSpace 是字体大小和距离调整出来的一个比较适合的数字

          

        public DrawImage(string[] title, string[] strsKeys, float[] fltsValues, float[] fltsValues2)

        {

            strTitle = title;

            this.strsKeys = strsKeys;

            this.fltsValues = fltsValues;

            this.fltsValues2 = fltsValues2;

        }

        /*public Bitmap Draw()

        {

            gph.DrawString(title, new Font("宋体", 14), Brushes.Black, new PointF(cpt.X + 60, cpt.X));//图表标题

            //画x轴

            gph.DrawLine(pen, cpt.X, cpt.Y, cpt.Y, cpt.Y);

            //x轴三角形

            gph.DrawPolygon(pen, xpt);

            gph.FillPolygon(new SolidBrush(Color.Black), xpt);

            gph.DrawString(strXAxisText, new Font("宋体", 12), Brushes.Black, new PointF(cpt.Y + 10, cpt.Y + 10));

            //画y轴

            gph.DrawLine(pen, cpt.X, cpt.Y, cpt.X, cpt.X);

            //y轴三角形

            gph.DrawPolygon(pen, ypt);

            gph.FillPolygon(new SolidBrush(Color.Black), ypt);

            gph.DrawString(strYAxisText, new Font("宋体", 12), Brushes.Black, new PointF(0, 7));

            for (int i = 1; i < 11; i++)

            {

                //画y轴刻度

                gph.DrawString((400*i).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - 30, cpt.Y - i * 30 - 6));

                gph.DrawLine(pen, cpt.X - 3, cpt.Y - i * 30, cpt.X, cpt.Y - i * 30);

                

            }

            for(int i=0;i<strsKeys.Length;i++)

            {

                //画x轴项目

                gph.DrawString(strsKeys[i], new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 5));

            }

            for (int i = 1; i <= fltsValues1.Length; i++)

            {

                //画点

                gph.DrawEllipse(pen, cpt.X + i * 30 - 1.5f, cpt.Y - fltsValues1[i - 1] * 3 - 1.5f, 3, 3);

                gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * 30 - 1.5f, cpt.Y - fltsValues1[i - 1] * 3 - 1.5f, 3, 3);

                //画数值

                gph.DrawString(fltsValues1[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30, cpt.Y - fltsValues1[i - 1] * 3));

                //画折线

                if (i > 1)

                    gph.DrawLine(Pens.Red, cpt.X + (i - 1) * 30, cpt.Y - fltsValues1[i - 2] * 3, cpt.X + i * 30, cpt.Y - fltsValues1[i - 1] * 3);

            }

            for (int i = 1; i <= fltsValues2.Length; i++)

            {

                //画点

                gph.DrawEllipse(pen, cpt.X + i * 30 - 1.5f, cpt.Y - fltsValues2[i - 1] * 3 - 1.5f, 3, 3);

                gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * 30 - 1.5f, cpt.Y - fltsValues2[i - 1] * 3 - 1.5f, 3, 3);

                //画数值

                gph.DrawString(fltsValues2[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30, cpt.Y - fltsValues2[i - 1] * 3));

                //画折线

                if (i > 1)

                    gph.DrawLine(Pens.Red, cpt.X + (i - 1) * 30, cpt.Y - fltsValues2[i - 2] * 3, cpt.X + i * 30, cpt.Y - fltsValues2[i - 1] * 3);

            }

            //保存输出图片

            //bmap.Save(Response.OutputStream, ImageFormat.Gif);

            return bmap;

        }*/

        #region 公共属性

        /// <summary>

        /// 图像的宽度

        /// </summary>

        public float Width

        {

            set

            {

                if (value < 100)

                {

                    fltWidth = 100;

                }

                else

                {

                    fltWidth = value;

                }

            }

            get

            {

                if (fltWidth <= 100)

                {

                    return 100;

                }

                else

                {

                    return fltWidth;

                }

            }

        }

        /// <summary>

        /// 图像的高度

        /// </summary>

        public float Height

        {

            set

            {

                if (value < 100)

                {

                    fltHeight = 100;

                }

                else

                {

                    fltHeight = value;

                }

            }

            get

            {

                if (fltHeight <= 100)

4000

                {

                    return 100;

                }

                else

                {

                    return fltHeight;

                }

            }

        }

        /// <summary>

        /// X轴刻度宽度

        /// </summary>

        public float XSlice

        {

            set { fltXSlice = value; }

            get { return fltXSlice; }

        }

        /// <summary>

        /// Y轴刻度宽度

        /// </summary>

        public float YSlice

        {

            set { fltYSlice = value; }

            get { return fltYSlice; }

        }

        /// <summary>

        /// Y轴刻度的数值宽度

        /// </summary>

        public float YSliceValue

        {

            set { fltYSliceValue = value; }

            get { return fltYSliceValue; }

        }

        /// <summary>

        /// Y轴刻度的数值宽度2

        /// </summary>

        public float YSliceValue2

        {

            set { fltYSliceValue2 = value; }

            get { return fltYSliceValue2; }

        }

        /// <summary>

        /// Y轴刻度开始值

        /// </summary>

        public float YSliceBegin

        {

            set { fltYSliceBegin = value; }

            get { return fltYSliceBegin; }

        }

        /// <summary>

        /// 标题

        /// </summary>

        public string[] Title

        {

            set { strTitle = value; }

            get { return strTitle; }

        }

        /// <summary>

        /// 键,X轴数据

        /// </summary>

        public string[] Keys

        {

            set { strsKeys = value; }

            get { return strsKeys; }

        }

        /// <summary>

        /// 值,Y轴数据

        /// </summary>

        public float[] Values

        {

            set { fltsValues = value; }

            get { return fltsValues; }

        }

        /// <summary>

        /// 值2,Y轴2数据

        /// </summary>

        public float[] Values2

        {

            set { fltsValues2 = value; }

            get { return fltsValues2; }

        }

        /// <summary>

        /// 背景色

        /// </summary>

        public Color BgColor

        {

            set { clrBgColor = value; }

            get { return clrBgColor; }

        }

        /// <summary>

        /// 文字颜色

        /// </summary>

        public Color TextColor

        {

            set { clrTextColor = value; }

            get { return clrTextColor; }

        }

        /// <summary>

        /// 整体边框颜色

        /// </summary>

        public Color BorderColor

        {

            set { clrBorderColor = value; }

            get { return clrBorderColor; }

        }

        /// <summary>

        /// 轴线颜色

        /// </summary>

        public Color AxisColor

        {

            set { clrAxisColor = value; }

            get { return clrAxisColor; }

        }

        /// <summary>

        /// X轴说明文字

        /// </summary>

        public string XAxisText

        {

            set { strXAxisText = value; }

            get { return strXAxisText; }

        }

        /// <summary>

        /// Y轴说明文字

        /// </summary>

        public string YAxisText

        {

            set { strYAxisText = value; }

            get { return strYAxisText; }

        }

        /// <summary>

        /// Y轴说明文字2

        /// </summary>

        public string YAxisText2

        {

            set { strYAxisText2 = value; }

            get { return strYAxisText2; }

        }

        /// <summary>

        /// 轴说明文字颜色

        /// </summary>

        public Color AxisTextColor

        {

            set { clrAxisTextColor = value; }

            get { return clrAxisTextColor; }

        }

        /// <summary>

        /// 刻度文字颜色

        /// </summary>

        public Color SliceTextColor

        {

            set { clrSliceTextColor = value; }

            get { return clrSliceTextColor; }

        }

        /// <summary>

        /// 刻度颜色

        /// </summary>

        public Color SliceColor

        {

            set { clrSliceColor = value; }

            get { return clrSliceColor; }

        }

        /// <summary>

        /// 曲线颜色

        /// </summary>

        public Color[] CurveColors

        {

            set { clrsCurveColors = value; }

            get { return clrsCurveColors; }

        }

        /// <summary>

        /// X轴文字旋转角度

        /// </summary>

        public float XRotateAngle

        {

            get { return fltXRotateAngle; }

            set { fltXRotateAngle = value; }

        }

        /// <summary>

        /// Y轴文字旋转角度

        /// </summary>

        public float YRotateAngle

        {

            get { return fltYRotateAngle; }

            set { fltYRotateAngle = value; }

        }

        /// <summary>

        /// 图像左右距离边缘距离

        /// </summary>

        public float XSpace

        {

            get { return fltXSpace; }

            set { fltXSpace = value; }

        }

        /// <summary>

        /// 图像上下距离边缘距离

        /// </summary>

        public float YSpace

        {

            get { return fltYSpace; }

            set { fltYSpace = value; }

        }

        /// <summary>

        /// 字体大小号数

        /// </summary>

        public int FontSize

        {

            get { return intFontSize; }

            set { intFontSize = value; }

        }

        /// <summary>

        /// 曲线线条大小

        /// </summary>

        public int CurveSize

        {

            get { return intCurveSize; }

            set { intCurveSize = value; }

        }

        #endregion

        /// <summary>

        /// 自动根据参数调整图像大小

        /// </summary>

        public void Fit()

        {

            //计算字体距离

            intFontSpace = FontSize + 5;

            //计算图像边距

            float fltSpace = Math.Min(Width / 6, Height / 6);

            XSpace = fltSpace;

            YSpace = fltSpace;

            //计算X轴刻度宽度

            XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);

            //计算Y轴刻度宽度和Y轴刻度开始值

            float fltMinValue = 0;

            float fltMaxValue = 0;

            for (int i = 0; i < Values.Length; i++)

            {

                if (Values[i] < fltMinValue)

                {

                    fltMinValue = Values[i];

                }

                else if (Values[i] > fltMaxValue)

                {

                    fltMaxValue = Values[i];

                }

            }

            if (YSliceBegin > fltMinValue)

            {

                YSliceBegin = fltMinValue;

            }

            int intYSliceCount = (int)(fltMaxValue / YSliceValue);

            if (fltMaxValue % YSliceValue != 0)

            {

                intYSliceCount++;

            }

            YSlice = (Height - 2 * YSpace) / intYSliceCount;

        }

        /// <summary>

        /// 生成图像并返回bmp图像对象

        /// </summary>

        /// <returns></returns>

        public Bitmap CreateImage()

        {

            InitializeGraph();

            int intKeysCount = Keys.Length;

            int intValuesCount = Values.Length;

            if (intValuesCount % intKeysCount == 0)

            {

                int intCurvesCount = intValuesCount / intKeysCount;

                for (int i = 0; i < intCurvesCount; i++)

                {

                    float[] fltCurrentValues = new float[intKeysCount];

                    for (int j = 0; j < intKeysCount; j++)

                    {

                        fltCurrentValues[j] = Values[i * intKeysCount + j];

                    }

                    DrawBorkenLine(ref objGraphics, fltCurrentValues, clrsCurveColors[i]);

                }

            }

            else

            {

                objGraphics.DrawString("发生错误,Values的长度必须是Keys的整数倍!", new Font("宋体", FontSize + 5), new SolidBrush(TextColor), new Point((int)XSpace, (int)(Height / 2)));

            }

            //画速度折线

            intValuesCount = Values2.Length;

            if (intValuesCount % intKeysCount == 0)

            {

                int intCurvesCount = intValuesCount / intKeysCount;

                for (int i = 0; i < intCurvesCount; i++)

                {

                    float[] fltCurrentValues = new float[intKeysCount];

                    for (int j = 0; j < intKeysCount; j++)

                    {

                        fltCurrentValues[j] = Values2[i * intKeysCount + j];

                    }

                    DrawBorkenLine(ref objGraphics, fltCurrentValues, clrsCurveColors[i], DashStyle.Dot);

                }

            }

            else

            {

                objGraphics.DrawString("发生错误,Values的长度必须是Keys的整数倍!", new Font("宋体", FontSize + 5), new SolidBrush(TextColor), new Point((int)XSpace, (int)(Height / 2)));

            }

            return objBitmap;

        }

        /// <summary>

        /// 初始化和填充图像区域,画出边框,初始标题

        /// </summary>

        private void InitializeGraph()

        {

            //根据给定的高度和宽度创建一个位图图像

            objBitmap = new Bitmap((int)Width, (int)Height);

            //从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)

            objGraphics = Graphics.FromImage(objBitmap);

            //根据给定颜色(LightGray)填充图像的矩形区域 (背景)

            objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width - 1, Height - 1); //画边框

            objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2); //填充边框

            //画X轴,注意图像的原始X轴和Y轴计算是以左上角为原点,向右和向下计算的

            float fltX1 = XSpace;

            float fltY1 = Height - YSpace;

            float fltX2 = Width - XSpace + XSlice / 2;

            float fltY2 = fltY1;

            objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);

            //画Y轴

            fltX1 = XSpace;

            fltY1 = Height - YSpace;

            fltX2 = XSpace;

            fltY2 = YSpace - YSlice / 2;

            objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);

            //画Y轴2

            fltX1 = Width - XSpace + XSlice / 2;

            fltY1 = Height - YSpace;

            fltX2 = Width - XSpace + XSlice / 2;

            fltY2 = YSpace - YSlice / 2;

            objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);

            //初始化轴线说明文字

            SetAxisText(ref objGraphics);

            //初始化X轴上的刻度和文字

            SetXAxis(ref objGraphics);

            //初始化Y轴上的刻度和文字

            SetYAxis(ref objGraphics);

            //初始化Y轴2上的刻度和文字

            SetYAxis2(ref objGraphics);

            //初始化标题

            CreateTitle(ref objGraphics);

        }

        /// <summary>

        /// 初始化轴线说明文字

        /// </summary>

        /// <param name="objGraphics"></param>

        private void SetAxisText(ref Graphics objGraphics)

        {

            float fltX = Width - XSpace + XSlice * 2 - (XAxisText.Length - 1) * intFontSpace;

            float fltY = Height - YSpace - intFontSpace;

            objGraphics.DrawString(XAxisText, new Font("宋体", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);

            fltX = XSpace + 5;

            fltY = YSpace - YSlice / 2 - intFontSpace;

            for (int i = 0; i < YAxisText.Length; i++)

            {

                objGraphics.DrawString(YAxisText[i].ToString(), new Font("宋体", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);

                fltY += intFontSpace + intFontSize * 2; //字体上下距离

            }

            fltX = Width - XSpace;//Width - XSpace + XSlice / 2 - (XAxisText.Length - 1) * intFontSpace;

            fltY = YSpace - YSlice / 2 - intFontSpace;

            for (int i = 0; i < YAxisText2.Length; i++)

            {

                objGraphics.DrawString(YAxisText2[i].ToString(), new Font("宋体", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);

                fltY += intFontSpace + intFontSize * 2; //字体上下距离

            }

        }

        /// <summary>

        /// 初始化X轴上的刻度和文字

        /// </summary>

        /// <param name="objGraphics"></param>

        private void SetXAxis(ref Graphics objGraphics)

        {

            float fltX1 = XSpace;

            float fltY1 = Height - YSpace;

            float fltX2 = XSpace;

            float fltY2 = Height - YSpace;

            int iCount = 0;

            int iSliceCount = 1;

            float Scale = 0;

            float iWidth = ((Width - 2 * XSpace) / XSlice) * 50; //将要画刻度的长度分段,并乘以50,以10为单位画刻度线。

            float fltSliceHeight = XSlice / 10; //刻度线的高度

            objGraphics.TranslateTransform(fltX1, fltY1); //平移图像(原点)

            objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend); //旋转图像

            //objGraphics.DrawString(Keys[0].ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);

            objGraphics.ResetTransform(); //重置图像

            for (int i = 0; i <= iWidth; i += 10) //以10为单位

            {

                Scale = i * XSlice / 50;//即(i / 10) * (XSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10

                if (iCount == 5)

                {

                    objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 + Scale, fltY1 + fltSliceHeight * 1.5f, fltX2 + Scale, fltY2 - fltSliceHeight * 1.5f);

                    //画网格虚线

                    Pen penDashed = new Pen(new SolidBrush(AxisColor));

                    penDashed.DashStyle = DashStyle.Dash;

                    objGraphics.DrawLine(penDashed, fltX1 + Scale, fltY1, fltX2 + Scale, YSpace - YSlice / 2);

                    //这里显示X轴刻度

                    if (iSliceCount <= Keys.Length - 1)

                    {

                        objGraphics.TranslateTransform(fltX1 + Scale, fltY1);

                        objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend);

                        objGraphics.DrawString(Keys[iSliceCount].ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);

                        objGraphics.ResetTransform();

                    }

                    else

                    {

                        //超过范围,不画任何刻度文字

                    }

                    iCount = 0;

                    iSliceCount++;

                    if (fltX1 + Scale > Width - XSpace)

                    {

                        break;

                    }

                }

                else

                {

                    objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 + Scale, fltY1 + fltSliceHeight, fltX2 + Scale, fltY2 - fltSliceHeight);

                }

                iCount++;

            }

        }

        /// <summary>

        /// 初始化Y轴上的刻度和文字

        /// </summary>

        /// <param name="objGraphics"></param>

        private void SetYAxis(ref Graphics objGraphics)

        {

            float fltX1 = XSpace;

            float fltY1 = Height - YSpace;

            float fltX2 = XSpace;

            float fltY2 = Height - YSpace;

            int iCount = 0;

            float Scale = 0;

            int iSliceCount = 1;

            float iHeight = ((Height - 2 * YSpace) / YSlice) * 50; //将要画刻度的长度分段,并乘以50,以10为单位画刻度线。

            float fltSliceWidth = YSlice / 10; //刻度线的宽度

            string strSliceText = string.Empty;

            objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.ToString().Length, Height - YSpace); //平移图像(原点)

            objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转图像

            objGraphics.DrawString(YSliceBegin.ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);

            objGraphics.ResetTransform(); //重置图像

            for (int i = 0; i < iHeight; i += 10)

            {

                Scale = i * YSlice / 50; //即(i / 10) * (YSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10

                if (iCount == 5)

                {

                    objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 - fltSliceWidth * 1.5f, fltY1 - Scale, fltX2 + fltSliceWidth * 1.5f, fltY2 - Scale);

                    //画网格虚线

                    Pen penDashed = new Pen(new SolidBrush(AxisColor));

                    penDashed.DashStyle = DashStyle.Dash;

                    objGraphics.DrawLine(penDashed, XSpace, fltY1 - Scale, Width - XSpace + XSlice / 2, fltY2 - Scale);

                    //这里显示Y轴刻度

                    strSliceText = Convert.ToString(YSliceValue * iSliceCount + YSliceBegin);

                    objGraphics.TranslateTransform(XSpace - intFontSize * strSliceText.Length, fltY1 - Scale); //平移图像(原点)

                    objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转图像

                    objGraphics.DrawString(strSliceText, new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);

                    objGraphics.ResetTransform(); //重置图像

                    iCount = 0;

                    iSliceCount++;

                }

                else

                {

                    objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 - fltSliceWidth, fltY1 - Scale, fltX2 + fltSliceWidth, fltY2 - Scale);

                }

                iCount++;

            }

        }

        /// <summary>

        /// 初始化Y轴2上的刻度和文字

        /// </summary>

        /// <param name="objGraphics"></param>

        private void SetYAxis2(ref Graphics objGraphics)

        {

            float fltX1 = Width - XSpace + XSlice / 2;

            float fltY1 = Height - YSpace;

            float fltX2 = Width - XSpace + XSlice / 2;

            float fltY2 = Height - YSpace;

            int iCount = 0;

            float Scale = 0;

            int iSliceCount = 1;

            float iHeight = ((Height - 2 * YSpace) / YSlice) * 50; //将要画刻度的长度分段,并乘以50,以10为单位画刻度线。

            float fltSliceWidth = YSlice / 10; //刻度线的宽度

            string strSliceText = string.Empty;

            objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.ToString().Length, Height - YSpace); //平移图像(原点)

            objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转图像

            objGraphics.DrawString(YSliceBegin.ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), Width - 2 * XSpace + XSlice / 2, 0);

            objGraphics.ResetTransform(); //重置图像

            for (int i = 0; i < iHeight; i += 10)

            {

                Scale = i * YSlice / 50; //即(i / 10) * (YSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10

                if (iCount == 5)

                {

                    objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 - fltSliceWidth * 1.5f, fltY1 - Scale, fltX2 + fltSliceWidth * 1.5f, fltY2 - Scale);

                    

                    //这里显示Y轴刻度

                    strSliceText = Convert.ToString(YSliceValue2 * iSliceCount + YSliceBegin);

                    objGraphics.TranslateTransform(XSpace - intFontSize * strSliceText.Length, fltY1 - Scale); //平移图像(原点)

                    objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转图像

                    objGraphics.DrawString(strSliceText, new Font("宋体", FontSize), new SolidBrush(SliceTextColor), Width - 2 * XSpace + XSlice * 2, 0);

                    objGraphics.ResetTransform(); //重置图像

                    iCount = 0;

                    iSliceCount++;

                }

                else

                {

                    objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 - fltSliceWidth, fltY1 - Scale, fltX2 + fltSliceWidth, fltY2 - Scale);

                }

                iCount++;

            }

        }

        /// <summary>

        /// 画折线

        /// </summary>

        /// <param name="objGraphics"></param>

        private void DrawBorkenLine(ref Graphics objGraphics, float[] fltCurrentValues, Color clrCurrentColor)

        {

            Pen CurvePen = new Pen(clrCurrentColor, CurveSize);

            PointF[] CurvePointF = new PointF[Keys.Length];

            float keys = 0;

            float values = 0;

            for (int i = 0; i < Keys.Length; i++)

            {

                keys = XSlice * i + XSpace;

                values = (Height - YSpace) + YSliceBegin - YSlice * (fltCurrentValues[i] / YSliceValue);

                CurvePointF[i] = new PointF(keys, values);

                //画数值

                //objGraphics.DrawString(fltCurrentValues[i].ToString(), new Font("宋体", 11), new SolidBrush(CurvePen.Color), CurvePointF[i]);

            }

            objGraphics.DrawLines(CurvePen, CurvePointF);

        }

        /// <summary>

        /// 画折线2

        /// </summary>

        /// <param name="objGraphics"></param>

        private void DrawBorkenLine(ref Graphics objGraphics, float[] fltCurrentValues, Color clrCurrentColor, DashStyle myDashStyle)

        {

            Pen CurvePen = new Pen(clrCurrentColor, CurveSize);

            CurvePen.DashStyle = myDashStyle;

            PointF[] CurvePointF = new PointF[Keys.Length];

            float keys = 0;

            float values = 0;

            for (int i = 0; i < Keys.Length; i++)

            {

                keys = XSlice * i + XSpace;

                values = (Height - YSpace) + YSliceBegin - YSlice * (fltCurrentValues[i] / YSliceValue2);

                CurvePointF[i] = new PointF(keys, values);

                //画数值

                //objGraphics.DrawString(fltCurrentValues[i].ToString(), new Font("宋体", 11), new SolidBrush(CurvePen.Color), CurvePointF[i]);

            }

            objGraphics.DrawLines(CurvePen, CurvePointF);

        }

        /// <summary>

        /// 初始化标题

        /// </summary>

        /// <param name="objGraphics"></param>

        private void CreateTitle(ref Graphics objGraphics)

        {

            for (int i = 0; i < Title.Length; i++)

            {

                objGraphics.DrawString(Title[i], new Font("宋体", FontSize), new SolidBrush(clrsCurveColors[i]), new Point((int)(Width - 2 * XSpace) - intFontSize * Title.Length, (int)(YSpace - YSlice / 2 - intFontSize * i - 30)));

            }

        }

    }

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 绘图