drawing Curve

来源:互联网 发布:mathematica 11 mac 编辑:程序博客网 时间:2024/05/17 04:46
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
 
namespace SarchPMS.Business.Draw
{
    publicclassDrawingCurve : DrawingChart
    {
        /// <summary>
        /// 画曲线图
        /// </summary>
        /// <param name="dsParameter"></param>
        /// <returns></returns>
        publicoverrideBitmap DrawImage(DataSet dsParameter)
        {
            Curve2D cuv2D =newCurve2D();
            cuv2D.Fit();
            returncuv2D.CreateImage();
        }
    }
 
    publicclassCurve2D
    {
        privateGraphics objGraphics;//Graphics 类提供将对象绘制到显示设备的方法
        privateBitmap objBitmap;//位图对象
 
        privatefloatfltWidth = 480; //图像宽度
        privatefloatfltHeight = 248; //图像高度
        privatefloatfltXSlice = 50; //X轴刻度宽度
        privatefloatfltYSlice = 50; //Y轴刻度宽度
        privatefloatfltYSliceValue = 20;//Y轴刻度的数值宽度
        privatefloatfltYSliceBegin = 0; //Y轴刻度开始值
        privatefloatfltTension = 0.5f;
        privatestringstrTitle = "曲线图";//标题
        privatestringstrXAxisText = "月份";//X轴说明文字
        privatestringstrYAxisText = "万元";//Y轴说明文字
        privatestring[] strsKeys =new string[] {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; //键
        privatefloat[] fltsValues =new float[] { 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f }; //值
        privateColor clrBgColor = Color.Snow;//背景色
        privateColor clrTextColor = Color.Black;//文字颜色
        privateColor clrBorderColor = Color.Black;//整体边框颜色
        privateColor clrAxisColor = Color.Black;//轴线颜色
        privateColor clrAxisTextColor = Color.Black;//轴说明文字颜色
        privateColor clrSliceTextColor = Color.Black;//刻度文字颜色
        privateColor clrSliceColor = Color.Black;//刻度颜色
        privateColor[] clrsCurveColors =newColor[] { Color.Red, Color.Blue };//曲线颜色
        privatefloatfltXSpace = 100f; //图像左右距离边缘距离
        privatefloatfltYSpace = 100f; //图像上下距离边缘距离
        privateintintFontSize = 9; //字体大小号数
        privatefloatfltXRotateAngle = 30f;//X轴文字旋转角度
        privatefloatfltYRotateAngle = 0f;//Y轴文字旋转角度
        privateintintCurveSize = 2; //曲线线条大小
        privateintintFontSpace = 0; //intFontSpace 是字体大小和距离调整出来的一个比较适合的数字
 
        #region 公共属性
 
        /// <summary>
        /// 图像的宽度
        /// </summary>
        publicfloatWidth
        {
            set
            {
                if(value < 100)
                {
                    fltWidth = 100;
                }
                else
                {
                    fltWidth = value;
                }
            }
            get
            {
                if(fltWidth <= 100)
                {
                    return100;
                }
                else
                {
                    returnfltWidth;
                }
            }
        }
 
        /// <summary>
        /// 图像的高度
        /// </summary>
        publicfloatHeight
        {
            set
            {
                if(value < 100)
                {
                    fltHeight = 100;
                }
                else
                {
                    fltHeight = value;
                }
            }
            get
            {
                if(fltHeight <= 100)
                {
                    return100;
                }
                else
                {
                    returnfltHeight;
                }
            }
        }
 
        /// <summary>
        /// X轴刻度宽度
        /// </summary>
        publicfloatXSlice
        {
            set{ fltXSlice = value; }
            get{return fltXSlice; }
        }
 
        /// <summary>
        /// Y轴刻度宽度
        /// </summary>
        publicfloatYSlice
        {
            set{ fltYSlice = value; }
            get{return fltYSlice; }
        }
 
        /// <summary>
        /// Y轴刻度的数值宽度
        /// </summary>
        publicfloatYSliceValue
        {
            set{ fltYSliceValue = value; }
            get{return fltYSliceValue; }
        }
 
        /// <summary>
        /// Y轴刻度开始值
        /// </summary>
        publicfloatYSliceBegin
        {
            set{ fltYSliceBegin = value; }
            get{return fltYSliceBegin; }
        }
 
        /// <summary>
        /// 张力系数
        /// </summary>
        publicfloatTension
        {
            set
            {
                if(value < 0.0f && value > 1.0f)
                {
                    fltTension = 0.5f;
                }
                else
                {
                    fltTension = value;
                }
            }
            get
            {
                returnfltTension;
            }
        }
 
        /// <summary>
        /// 标题
        /// </summary>
        publicstringTitle
        {
            set{ strTitle = value; }
            get{return strTitle; }
        }
 
        /// <summary>
        /// 键,X轴数据
        /// </summary>
        publicstring[] Keys
        {
            set{ strsKeys = value; }
            get{return strsKeys; }
        }
 
        /// <summary>
        /// 值,Y轴数据
        /// </summary>
        publicfloat[] Values
        {
            set{ fltsValues = value; }
            get{return fltsValues; }
        }
 
        /// <summary>
        /// 背景色
        /// </summary>
        publicColor BgColor
        {
            set{ clrBgColor = value; }
            get{return clrBgColor; }
        }
 
        /// <summary>
        /// 文字颜色
        /// </summary>
        publicColor TextColor
        {
            set{ clrTextColor = value; }
            get{return clrTextColor; }
        }
 
        /// <summary>
        /// 整体边框颜色
        /// </summary>
        publicColor BorderColor
        {
            set{ clrBorderColor = value; }
            get{return clrBorderColor; }
        }
 
        /// <summary>
        /// 轴线颜色
        /// </summary>
        publicColor AxisColor
        {
            set{ clrAxisColor = value; }
            get{return clrAxisColor; }
        }
 
        /// <summary>
        /// X轴说明文字
        /// </summary>
        publicstringXAxisText
        {
            set{ strXAxisText = value; }
            get{return strXAxisText; }
        }
 
        /// <summary>
        /// Y轴说明文字
        /// </summary>
        publicstringYAxisText
        {
            set{ strYAxisText = value; }
            get{return strYAxisText; }
        }
 
        /// <summary>
        /// 轴说明文字颜色
        /// </summary>
        publicColor AxisTextColor
        {
            set{ clrAxisTextColor = value; }
            get{return clrAxisTextColor; }
        }
 
        /// <summary>
        /// 刻度文字颜色
        /// </summary>
        publicColor SliceTextColor
        {
            set{ clrSliceTextColor = value; }
            get{return clrSliceTextColor; }
        }
 
        /// <summary>
        /// 刻度颜色
        /// </summary>
        publicColor SliceColor
        {
            set{ clrSliceColor = value; }
            get{return clrSliceColor; }
        }
 
        /// <summary>
        /// 曲线颜色
        /// </summary>
        publicColor[] CurveColors
        {
            set{ clrsCurveColors = value; }
            get{return clrsCurveColors; }
        }
 
        /// <summary>
        /// X轴文字旋转角度
        /// </summary>
        publicfloatXRotateAngle
        {
            get{return fltXRotateAngle; }
            set{ fltXRotateAngle = value; }
        }
 
        /// <summary>
        /// Y轴文字旋转角度
        /// </summary>
        publicfloatYRotateAngle
        {
            get{return fltYRotateAngle; }
            set{ fltYRotateAngle = value; }
        }
 
        /// <summary>
        /// 图像左右距离边缘距离
        /// </summary>
        publicfloatXSpace
        {
            get{return fltXSpace; }
            set{ fltXSpace = value; }
        }
 
        /// <summary>
        /// 图像上下距离边缘距离
        /// </summary>
        publicfloatYSpace
        {
            get{return fltYSpace; }
            set{ fltYSpace = value; }
        }
 
        /// <summary>
        /// 字体大小号数
        /// </summary>
        publicintFontSize
        {
            get{return intFontSize; }
            set{ intFontSize = value; }
        }
 
        /// <summary>
        /// 曲线线条大小
        /// </summary>
        publicintCurveSize
        {
            get{return intCurveSize; }
            set{ intCurveSize = value; }
        }
 
        #endregion
 
        /// <summary>
        /// 自动根据参数调整图像大小
        /// </summary>
        publicvoidFit()
        {
            //计算字体距离
            intFontSpace = FontSize + 5;
            //计算图像边距
            floatfltSpace = Math.Min(Width / 6, Height / 6);
            XSpace = fltSpace;
            YSpace = fltSpace;
            //计算X轴刻度宽度
            XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
            //计算Y轴刻度宽度和Y轴刻度开始值
            floatfltMinValue = 0;
            floatfltMaxValue = 0;
            for(inti = 0; i < Values.Length; i++)
            {
                if(Values[i] < fltMinValue)
                {
                    fltMinValue = Values[i];
                }
                elseif(Values[i] > fltMaxValue)
                {
                    fltMaxValue = Values[i];
                }
            }
            if(YSliceBegin > fltMinValue)
            {
                YSliceBegin = fltMinValue;
            }
            intintYSliceCount = (int)(fltMaxValue / YSliceValue);
            if(fltMaxValue % YSliceValue != 0)
            {
                intYSliceCount++;
            }
            YSlice = (Height - 2 * YSpace) / intYSliceCount;
        }
 
        /// <summary>
        /// 生成图像并返回bmp图像对象
        /// </summary>
        /// <returns></returns>
        publicBitmap CreateImage()
        {
            InitializeGraph();
 
            intintKeysCount = Keys.Length;
            intintValuesCount = Values.Length;
            if(intValuesCount % intKeysCount == 0)
            {
                intintCurvesCount = intValuesCount / intKeysCount;
                for(inti = 0; i < intCurvesCount; i++)
                {
                    float[] fltCurrentValues =newfloat[intKeysCount];
                    for(intj = 0; j < intKeysCount; j++)
                    {
                        fltCurrentValues[j] = Values[i * intKeysCount + j];
                    }
                    DrawContent(refobjGraphics, fltCurrentValues, clrsCurveColors[i]);
                }
            }
            else
            {
                objGraphics.DrawString("发生错误,Values的长度必须是Keys的整数倍!",newFont("宋体", FontSize + 5),newSolidBrush(TextColor),newPoint((int)XSpace, (int)(Height / 2)));
            }
 
            returnobjBitmap;
        }
 
        /// <summary>
        /// 初始化和填充图像区域,画出边框,初始标题
        /// </summary>
        privatevoidInitializeGraph()
        {
 
            //根据给定的高度和宽度创建一个位图图像
            objBitmap =newBitmap((int)Width, (int)Height);
 
            //从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
            objGraphics = Graphics.FromImage(objBitmap);
 
            //根据给定颜色(LightGray)填充图像的矩形区域 (背景)
            objGraphics.DrawRectangle(newPen(BorderColor, 1), 0, 0, Width - 1, Height - 1);//画边框
            objGraphics.FillRectangle(newSolidBrush(BgColor), 1, 1, Width - 2, Height - 2);//填充边框
 
            //画X轴,注意图像的原始X轴和Y轴计算是以左上角为原点,向右和向下计算的
            floatfltX1 = XSpace;
            floatfltY1 = Height - YSpace;
            floatfltX2 = Width - XSpace + XSlice / 2;
            floatfltY2 = fltY1;
            objGraphics.DrawLine(newPen(newSolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);
 
            //画Y轴
            fltX1 = XSpace;
            fltY1 = Height - YSpace;
            fltX2 = XSpace;
            fltY2 = YSpace - YSlice / 2;
            objGraphics.DrawLine(newPen(newSolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);
 
            //初始化轴线说明文字
            SetAxisText(refobjGraphics);
 
            //初始化X轴上的刻度和文字
            SetXAxis(refobjGraphics);
 
            //初始化Y轴上的刻度和文字
            SetYAxis(refobjGraphics);
 
            //初始化标题
            CreateTitle(refobjGraphics);
        }
 
        /// <summary>
        /// 初始化轴线说明文字
        /// </summary>
        /// <param name="objGraphics"></param>
        privatevoidSetAxisText(refGraphics objGraphics)
        {
            floatfltX = Width - XSpace + XSlice / 2 - (XAxisText.Length - 1) * intFontSpace;
            floatfltY = Height - YSpace - intFontSpace;
            objGraphics.DrawString(XAxisText,newFont("宋体", FontSize),newSolidBrush(AxisTextColor), fltX, fltY);
 
            fltX = XSpace + 5;
            fltY = YSpace - YSlice / 2 - intFontSpace;
            for(inti = 0; i < YAxisText.Length; i++)
            {
                objGraphics.DrawString(YAxisText[i].ToString(),newFont("宋体", FontSize),newSolidBrush(AxisTextColor), fltX, fltY);
                fltY += intFontSpace;//字体上下距离
            }
        }
 
        /// <summary>
        /// 初始化X轴上的刻度和文字
        /// </summary>
        /// <param name="objGraphics"></param>
        privatevoidSetXAxis(refGraphics objGraphics)
        {
            floatfltX1 = XSpace;
            floatfltY1 = Height - YSpace;
            floatfltX2 = XSpace;
            floatfltY2 = Height - YSpace;
            intiCount = 0;
            intiSliceCount = 1;
            floatScale = 0;
            floatiWidth = ((Width - 2 * XSpace) / XSlice) * 50;//将要画刻度的长度分段,并乘以50,以10为单位画刻度线。
            floatfltSliceHeight = XSlice / 10;//刻度线的高度
 
            objGraphics.TranslateTransform(fltX1, fltY1);//平移图像(原点)
            objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend);//旋转图像
            objGraphics.DrawString(Keys[0].ToString(),newFont("宋体", FontSize),newSolidBrush(SliceTextColor), 0, 0);
            objGraphics.ResetTransform();//重置图像
 
            for(inti = 0; i <= iWidth; i += 10) //以10为单位
            {
                Scale = i * XSlice / 50;//即(i / 10) * (XSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10
 
                if(iCount == 5)
                {
                    objGraphics.DrawLine(newPen(newSolidBrush(AxisColor)), fltX1 + Scale, fltY1 + fltSliceHeight * 1.5f, fltX2 + Scale, fltY2 - fltSliceHeight * 1.5f);
                    //画网格虚线
                    Pen penDashed =newPen(newSolidBrush(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(),newFont("宋体", FontSize),newSolidBrush(SliceTextColor), 0, 0);
                        objGraphics.ResetTransform();
                    }
                    else
                    {
                        //超过范围,不画任何刻度文字
                    }
                    iCount = 0;
                    iSliceCount++;
                    if(fltX1 + Scale > Width - XSpace)
                    {
                        break;
                    }
                }
                else
                {
                    objGraphics.DrawLine(newPen(newSolidBrush(SliceColor)), fltX1 + Scale, fltY1 + fltSliceHeight, fltX2 + Scale, fltY2 - fltSliceHeight);
                }
                iCount++;
            }
        }
 
        /// <summary>
        /// 初始化Y轴上的刻度和文字
        /// </summary>
        /// <param name="objGraphics"></param>
        privatevoidSetYAxis(refGraphics objGraphics)
        {
            floatfltX1 = XSpace;
            floatfltY1 = Height - YSpace;
            floatfltX2 = XSpace;
            floatfltY2 = Height - YSpace;
            intiCount = 0;
            floatScale = 0;
            intiSliceCount = 1;
            floatiHeight = ((Height - 2 * YSpace) / YSlice) * 50;//将要画刻度的长度分段,并乘以50,以10为单位画刻度线。
            floatfltSliceWidth = YSlice / 10;//刻度线的宽度
            stringstrSliceText =string.Empty;
 
            objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.ToString().Length, Height - YSpace);//平移图像(原点)
            objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend);//旋转图像
            objGraphics.DrawString(YSliceBegin.ToString(),newFont("宋体", FontSize),newSolidBrush(SliceTextColor), 0, 0);
            objGraphics.ResetTransform();//重置图像
 
            for(inti = 0; i < iHeight; i += 10)
            {
                Scale = i * YSlice / 50;//即(i / 10) * (YSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10
 
                if(iCount == 5)
                {
                    objGraphics.DrawLine(newPen(newSolidBrush(AxisColor)), fltX1 - fltSliceWidth * 1.5f, fltY1 - Scale, fltX2 + fltSliceWidth * 1.5f, fltY2 - Scale);
                    //画网格虚线
                    Pen penDashed =newPen(newSolidBrush(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,newFont("宋体", FontSize),newSolidBrush(SliceTextColor), 0, 0);
                    objGraphics.ResetTransform();//重置图像
 
                    iCount = 0;
                    iSliceCount++;
                }
                else
                {
                    objGraphics.DrawLine(newPen(newSolidBrush(SliceColor)), fltX1 - fltSliceWidth, fltY1 - Scale, fltX2 + fltSliceWidth, fltY2 - Scale);
                }
                iCount++;
            }
        }
 
        /// <summary>
        /// 画曲线
        /// </summary>
        /// <param name="objGraphics"></param>
        privatevoidDrawContent(refGraphics objGraphics,float[] fltCurrentValues, Color clrCurrentColor)
        {
            Pen CurvePen =newPen(clrCurrentColor, CurveSize);
            PointF[] CurvePointF =newPointF[Keys.Length];
            floatkeys = 0;
            floatvalues = 0;
 
            for(inti = 0; i < Keys.Length; i++)
            {
                keys = XSlice * i + XSpace;
                values = (Height - YSpace) + YSliceBegin - YSlice * (fltCurrentValues[i] / YSliceValue);
                CurvePointF[i] =newPointF(keys, values);
            }
            objGraphics.DrawCurve(CurvePen, CurvePointF, Tension);
        }
 
        /// <summary>
        /// 初始化标题
        /// </summary>
        /// <param name="objGraphics"></param>
        privatevoidCreateTitle(refGraphics objGraphics)
        {
            objGraphics.DrawString(Title,newFont("宋体", FontSize),newSolidBrush(TextColor),newPoint((int)(Width - XSpace) - intFontSize * Title.Length, (int)(YSpace - YSlice / 2 - intFontSpace)));
        }
    }
}
0 0
原创粉丝点击