用ASP.NET with C# 绘制曲线图(Curve图)

转载 2012年03月23日 15:42:16


文件描述#region 文件描述
// -------------------------------------------------------------------------------------------------
// 描述:绘画曲线图
// 作者:郭锋
// 日期:2007年3月 日
// -------------------------------------------------------------------------------------------------
#endregion

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ColorFul.Infor.wwwroot.Class
{
    
public class Curve
    
{
        
private // 私有成员数据区#region private // 私有成员数据区
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// X轴长度
        
/// </summary>

        private int _xWidth;            // X轴长度

        
/**//// <summary>
        
/// Y轴长度
        
/// </summary>

        private int _yHeight;           // Y轴长度

        
/**//// <summary>
        
/// X轴大刻度间隔长度
        
/// </summary>

        private int _xBigSpace;         // X轴大刻度间隔长度

        
/**//// <summary>
        
/// X轴小刻度间隔长度
        
/// </summary>

        private int _xSmallSpace;       // X轴小刻度间隔长度
        /**//// <summary>
        
/// Y轴刻度间隔长度
        
/// </summary>

        private int _ySpace;            // Y轴刻度间隔长度
        
        
/**//// <summary>
        
/// 曲线值间隔
        
/// </summary>

        private decimal _ySpaceValue;       // 曲线值间隔
        
        
/**//// <summary>
        
/// 原点X轴坐标
        
/// </summary>

        private int Origin_x;           // 原点X轴坐标
        /**//// <summary>
        
/// 原点Y轴坐标
        
/// </summary>

        private int Origin_y;           // 原点Y轴坐标
        
        
/**//// <summary>
        
/// X轴大刻度数目
        
/// </summary>

        private int _xMaxScaleCount;    // X轴大刻度数目

        
/**//// <summary>
        
/// Y轴输入的最大值
        
/// </summary>

        private float _yMaxValue;          // Y轴输入的最大值

        
/**//// <summary>
        
/// Y轴输入的最小值
        
/// </summary>

        private float _yMinValue;          //Y轴输入的最小值

        
/**//// <summary>
        
/// Y轴刻度值预留长度
        
/// </summary>

        private int _yTextLength = 10;      //Y轴刻度值预留长度

        
/**//// <summary>
        
/// X轴刻度值预留高度
        
/// </summary>

        private int _xTextHeight = 10;      //X轴刻度值预留高度

        
/**//// <summary>
        
/// 标题行高
        
/// </summary>

        private int TitleHeight = 0;        //标题行高
        
// ---------------------------------------------------------------------------------------------
        #endregion


        
public //公共属性区域#region public //公共属性区域
        
// -----------------------------------------------------------------------------------------
        public string ImgTitle  // 图片标题#region public string ImgTitle  // 图片标题
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 图片标题
        
/// </summary>

        private string _imgTitle;          // 图片标题
        /**//// <summary>
        
/// 图片标题
        
/// </summary>

        public string ImgTitle
        
{
            
set { _imgTitle = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public float[] Values //曲线数值#region public float[] Values //曲线数值
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 曲线数值
        
/// </summary>

        private float[] _values;            // 曲线数值
        /**//// <summary>
        
/// 曲线数值
        
/// </summary>

        public float[] DataValues
        
{
            
set { _values = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int XMinScaleCount    //X轴的最大范围#region public int XMinScaleCount    //X轴的最大范围
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// X轴小刻度数目;
        
/// </summary>

        private int _xMinScaleCount = 0;    // X轴小刻度数目;
        /**//// <summary>
        
/// X轴的最大范围
        
/// </summary>

        public int XMinScaleCount
        
{
            
set { _xMinScaleCount = value; }
        }
 
        
// -----------------------------------------------------------------------------------------
        #endregion


        
public string XTitle    // X轴坐标标题#region public string XTitle    // X轴坐标标题
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// X轴文字
        
/// </summary>

        private string _xTitle="X轴标题";            // X轴文字
        /**//// <summary>
        
/// X轴坐标标题
        
/// </summary>

        public string XTitle
        
{
            
get return _xTitle; }
            
set { _xTitle = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public string YTitle    // Y轴坐标标题#region public string YTitle    // Y轴坐标标题
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// Y轴文字
        
/// </summary>

        private string _yTitle="Y轴标题";            // Y轴文字
        /**//// <summary>
        
/// Y轴坐标标题
        
/// </summary>

        public string YTitle
        
{
            
get return _yTitle; }
            
set { _yTitle = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int ImgWidth //图片宽度#region public int ImgWidth //图片宽度
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 图片宽度
        
/// </summary>

        private int _imgWidth = 500;          // 图片宽度
        /**//// <summary>
        
/// 图片宽度
        
/// </summary>

        public int ImgWidth
        
{
            
get return _imgWidth; }
            
set { _imgWidth = value; }

        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int ImgHeight    //图片高度#region public int ImgHeight    //图片高度
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 图片高度
        
/// </summary>

        private int _imgHeight = 500;         // 图片高度
        /**//// <summary>
        
/// 图片高度
        
/// </summary>

        public int ImgHeight
        
{
            
get return _imgHeight; }
            
set { _imgHeight = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color ImgBgColor //图片背景颜色,默认白色#region public Color ImgBgColor //图片背景颜色,默认白色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 图片背景颜色,默认白色
        
/// </summary>

        private Color _imgBgColor = Color.White;        // 图片背景颜色,默认白色
        /**//// <summary>
        
/// 图片背景颜色,默认白色
        
/// </summary>

        public Color ImgBgColor
        
{
            
set { _imgBgColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color ImgBorderColor //图片背景边框颜色,默认黑色#region public Color ImgBorderColor //图片背景边框颜色,默认黑色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 图片背景边框颜色,默认黑色
        
/// </summary>

        private Color _imgBorderColor = Color.Black;    // 图片背景边框颜色,默认黑色
        /**//// <summary>
        
/// 图片背景边框颜色,默认黑色
        
/// </summary>

        public Color ImgBorderColor
        
{
            
set { _imgBorderColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color AxisColor  //刻度线颜色,默认黑色#region public Color AxisColor  //刻度线颜色,默认黑色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 刻度线颜色,默认黑色
        
/// </summary>

        private Color _axisColor = Color.Black;         // 刻度线颜色
        /**//// <summary>
        
/// 刻度线颜色,默认黑色
        
/// </summary>

        public Color AxisColor
        
{
            
set { _axisColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color SliceTextColor //刻度文字颜色,默认黑色#region public Color SliceTextColor //刻度文字颜色,默认黑色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 刻度文字颜色,默认黑色
        
/// </summary>

        private Color _sliceTextColor = Color.Black;    // 刻度文字颜色,默认黑色
        /**//// <summary>
        
/// 刻度文字颜色,默认黑色
        
/// </summary>

        public Color SliceTextColor
        
{
            
set { _sliceTextColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color AxisTextColor  //绘制轴标题的颜色,默认黑色#region public Color AxisTextColor  //绘制轴标题的颜色,默认黑色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制轴标题的颜色,默认黑色
        
/// </summary>

        private Color _axisTextColor = Color.Black;     // 绘制轴标题的颜色,默认黑色
        /**//// <summary>
        
/// 绘制轴标题的颜色,默认黑色
        
/// </summary>

        public Color AxisTextColor
        
{
            
set { _axisTextColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int PaddingWidth //内容边界宽度,默认值=ImgWidth*0.05#region public int PaddingWidth //内容边界宽度,默认值=ImgWidth*0.05
        
// -----------------------------------------------------------------------------------------
        private int paddingwidth = 0;
        
/**//// <summary>
        
/// 内容边界宽度,默认值=ImgWidth*0.05
        
/// </summary>

        public int PaddingWidth
        
{
            
get return paddingwidth; }
            
set { paddingwidth = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int PaddingHeight //内容边界高度,默认值=ImgHeight*0.05#region public int PaddingHeight //内容边界高度,默认值=ImgHeight*0.05
        
// -----------------------------------------------------------------------------------------
        private int paddingheight = 0;
        
/**//// <summary>
        
/// 内容边界高度,默认值=ImgWidth*0.05
        
/// </summary>

        public int PaddingHeight
        
{
            
get return paddingheight; }
            
set { paddingheight = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int XScaleMultiple    //大小刻度的倍数#region public int XScaleMultiple    //大小刻度的倍数
        
// -----------------------------------------------------------------------------------------
        private int Xscalemultiple = 3;
        
/**//// <summary>
        
/// 大小刻度的倍数,即几个小刻度一个大刻度,默认为3
        
/// </summary>

        public int XScaleMultiple
        
{
            
get return Xscalemultiple; }
            
set { Xscalemultiple = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int XBegin  //X轴开始坐标#region public int XBegin  //X轴开始坐标
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// X轴开始位置
        
/// </summary>

        private int _beginX = 0;            // X轴开始位置
        /**//// <summary>
        
/// X轴开始坐标
        
/// </summary>

        public int XBegin
        
{
            
get return _beginX; }
            
set { _beginX = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int YBegin   //Y轴开始坐标#region public int YBegin   //Y轴开始坐标
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// Y轴开始位置
        
/// </summary>

        private int _beginY = 0;            // Y轴开始位置
        /**//// <summary>
        
/// Y轴开始坐标
        
/// </summary>

        public int YBegin
        
{
            
get return _beginY; }
            
set { _beginY = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion
]

        
public int YScaleCount  //Y轴刻度份数,将Y轴评分成几分,默认为10#region public int YScaleCount  //Y轴刻度份数,将Y轴评分成几分,默认为10
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// Y轴刻度份数,将Y轴评分成几分,默认为10
        
/// </summary>

        private int Yscalecount = 10;
        
/**//// <summary>
        
/// Y轴刻度份数,将Y轴评分成几分,默认为10
        
/// </summary>

        public int YScaleCount
        
{
            
get return Yscalecount; }
            
set { Yscalecount = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public int YScaleBaseValue  //Y轴刻度间隔数值#region public int YScaleBaseValue  //Y轴刻度间隔数值
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// Y轴刻度间隔数值
        
/// </summary>

        private int _yScaleBaseValue;   //Y轴刻度间隔数值
        /**//// <summary>
        
/// Y轴刻度间隔数值
        
/// </summary>

        public int YScaleBaseValue
        
{
            
set { _yScaleBaseValue = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public string[] XScaleValue //X轴刻度值#region public string[] XScaleValue //X轴刻度值
        
// -----------------------------------------------------------------------------------------
        private string[] _xScalueValue;
        
/**//// <summary>
        
/// X轴刻度值
        
/// </summary>

        public string[] XScaleValue
        
{
            
get return _xScalueValue; }
            
set { _xScalueValue = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Font TitleFont   //标题字体设置#region public Font TitleFont   //标题字体设置
        
// -----------------------------------------------------------------------------------------
        private Font titlefont = new Font("宋体"15, FontStyle.Regular, GraphicsUnit.Pixel);
        
/**//// <summary>
        
/// 标题字体设置
        
/// </summary>

        public Font TitleFont
        
{
            
set { titlefont = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color ImgTitleColor  //图片标题文字颜色,默认红色#region public Color ImgTitleColor  //图片标题文字颜色,默认红色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 图片标题文字颜色,默认红色
        
/// </summary>

        private Color _imgTitleColor = Color.Red;     // 图片标题文字颜色,默认红色
        /**//// <summary>
        
/// 图片标题文字颜色,默认红色
        
/// </summary>

        public Color ImgTitleColor
        
{
            
set { _imgTitleColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Font Font    //绘制文字的字体,默认宋体12px#region public Font Font    //绘制文字的字体,默认宋体12px
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制文字的字体,默认宋体12px
        
/// </summary>

        private Font _font = new Font("宋体"12, FontStyle.Regular, GraphicsUnit.Pixel);              // 绘制文字的字体
        /**//// <summary>
        
/// 绘制文字的字体,默认宋体12px
        
/// </summary>

        public Font Font
        
{
            
set { _font = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public float Tension    //曲线的圆滑度#region public float Tension    //曲线的圆滑度
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 曲线的圆滑度,默认 0.45f;
        
/// </summary>

        private float _tension = 0.45f;          // 曲线的圆滑度默认 0.45f;
        /**//// <summary>
        
/// 曲线的圆滑度,默认 0.45f;
        
/// </summary>

        public float Tension
        
{
            
set { _tension = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public Color CurverColor       // 曲线颜色,默认黑色#region public Color CurverColor       // 曲线颜色,默认黑色
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 曲线颜色,默认黑色
        
/// </summary>

        private Color _curveColor = Color.Black;        // 曲线颜色,默认黑色
        /**//// <summary>
        
/// 曲线颜色,默认黑色
        
/// </summary>

        public Color CurverColor
        
{
            
set { _curveColor = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public float YScaleMaxValue  //Y轴刻度最大值#region public float YScaleMaxValue  //Y轴刻度最大值
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// Y轴刻度最大值
        
/// </summary>

        private int _yScaleMaxValue = 0;
        
/**//// <summary>
        
/// Y轴刻度最大值
        
/// </summary>

        public int YScaleMaxValue
        
{
            
set { _yScaleMaxValue = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion


        
public float YScaleMinValue  //Y轴刻度最小值#region public float YScaleMinValue  //Y轴刻度最小值
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// Y轴刻度最小值
        
/// </summary>

        private int _yScaleMinValue = 0;
        
/**//// <summary>
        
/// Y轴刻度最小值
        
/// </summary>

        public int YScaleMinValue
        
{
            
set { _yScaleMinValue = value; }
        }

        
// -----------------------------------------------------------------------------------------
        #endregion
 
        
// -----------------------------------------------------------------------------------------
        #endregion


        
private void InitValue() // 初始化所有数据数值#region private void InitValue() // 初始化所有数据数值
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 初始化所有数据数值
        
/// </summary>

        private void InitValue()
        
{
            
//_values = new float[30];
            
//Random random = new Random();
            
//for (int i = 0; i < 30; i++)
            
//{
            
//    _values[i] = Convert.ToSingle(random.Next(0, 1000));
            
//}
            
            
//{ 0, 120, 300, 90, 100, 134, 130, 160, 0, 70, 100, 120, 130, 120, 90, 12, 66, 78, 300, 400, 89, 65, 34, 23, 34, 25, 67, 23, 45, 78, 120 };

            
//取得最大与最小值
            _yMaxValue = 1f;
            _yMinValue 
= 0f;
            
for (int i = 0; i < _values.Length; i++)
            
{
                _yMaxValue 
= Math.Max(_yMaxValue, _values[i]);
                _yMinValue 
= Math.Min(_yMinValue, _values[i]);
            }

            
//取得刻度文字的预留长度和高度
            _yTextLength = TextImgSize.GetSize(_yMaxValue.ToString(), _font).Width;
            _xTextHeight 
= TextImgSize.GetSize(_yMaxValue.ToString(), _font).Height;
            GetTopAndEnd(_yMaxValue, _yMinValue, 
out _yScaleMaxValue, out _yScaleMinValue);

            
//取得标题行高
            TitleHeight = TextImgSize.GetSize(_imgTitle, titlefont).Height;
            
//设置作图区域边界
            paddingwidth = paddingwidth == 0 ? Convert.ToInt32(_imgWidth * 0.05) : paddingwidth;
            paddingheight 
= paddingheight == 0 ? Convert.ToInt32(_imgHeight * 0.05) : paddingheight;

            
//设置X轴小刻度数目
            
//_xMinScaleCount = _values.Length;
            
//设置X轴大刻度数目
            _xMaxScaleCount = _xScalueValue.Length-1;
            
//X轴宽度=图片宽度-边界宽*2-上下文字与刻度预留宽度
            _xWidth = _imgWidth - paddingwidth * 2 - _yTextLength;
            
//小刻度宽度
            _xSmallSpace = _xWidth / _xMinScaleCount;
            
//大刻度宽度=小刻度宽度*倍数
            _xBigSpace = _xWidth/_xMaxScaleCount;

            
            _yScaleBaseValue 
= (_yScaleMaxValue - _yScaleMinValue) / Yscalecount;

            
            
//Y轴高度=图片高度-边界高度*2-Y轴下方刻度文字行高-标题高度
            _yHeight = _imgHeight - paddingheight * 2 - _xTextHeight - TitleHeight;
            _ySpace 
= _yHeight / Yscalecount;

            
if (_yMaxValue >= Yscalecount)
            
{
                _ySpaceValue 
= Convert.ToDecimal(_yMaxValue / Yscalecount);
            }

            
else
            
{
                _ySpaceValue 
= Math.Round((decimal)_yMaxValue / (decimal)101);
            }


            Origin_x 
=  _yTextLength+paddingwidth;
            Origin_y 
= _imgHeight - _xTextHeight - paddingheight;
        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
public byte[] DrawCurveGraph() // 绘制曲线图#region public byte[] DrawCurveGraph() // 绘制曲线图
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制整个曲线图
        
/// </summary>

        public byte[] DrawCurveGraph()
        
{
            InitValue();

            Bitmap objBitmap 
= new Bitmap(_imgWidth, _imgHeight, PixelFormat.Format16bppRgb565);
            Graphics objGraphics 
= Graphics.FromImage(objBitmap);
            Pen axisPen 
= new Pen(new SolidBrush(_axisColor), 1);
            
float FirstPoint_X = 0f;
            
float FirstPoint_Y = 0f;
            
float SecondPoint_X = 0f;
            
float SecondPoint_Y = 0f;

            
// 绘制背景及边框
            
//objGraphics.DrawRectangle(new Pen(_imgBorderColor, 1), 0, 0, _imgWidth, _imgHeight);
            objGraphics.FillRectangle(new SolidBrush(_imgBgColor), 00, _imgWidth, _imgHeight);

            
//画Y轴直线
            FirstPoint_X = Convert.ToSingle(Origin_x);
            FirstPoint_Y 
= Convert.ToSingle(paddingheight + TitleHeight - 5);
            
//FirstPoint_Y = Convert.ToSingle(_imgHeight-paddingheight);
            SecondPoint_X = Convert.ToSingle(Origin_x);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头左半边
            FirstPoint_X = Convert.ToSingle(Origin_x - 5);
            FirstPoint_Y 
= Convert.ToSingle(paddingheight + 5 + TitleHeight - 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x);
            SecondPoint_Y 
= Convert.ToSingle(paddingheight + TitleHeight - 5);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头右半边
            FirstPoint_X = Convert.ToSingle(Origin_x);
            FirstPoint_Y 
= Convert.ToSingle(paddingheight + TitleHeight - 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + 5);
            SecondPoint_Y 
= Convert.ToSingle(paddingheight + 5 + TitleHeight - 5);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);

            
//画X轴直线
            FirstPoint_X = Convert.ToSingle(Origin_x);
            FirstPoint_Y 
= Convert.ToSingle(Origin_y);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + _xWidth);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头上半边
            FirstPoint_X = Convert.ToSingle(Origin_x + _xWidth - 5);
            FirstPoint_Y 
= Convert.ToSingle(Origin_y - 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + _xWidth);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头下半边
            FirstPoint_X = Convert.ToSingle(Origin_x + _xWidth - 5);
            FirstPoint_Y 
= Convert.ToSingle(Origin_y + 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + _xWidth);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);

            
// 绘制Y轴上的刻度和文字
            DrawYAxis(objGraphics);

            
// 绘制X轴上的刻度和文字
            DrawXAxis(objGraphics);

            
// 绘制标题
            DrawTitle(objGraphics);

            
// 绘制数据曲线
            DrawCurve(objGraphics);

            
/**/////擦除Y轴画超出的部分

            
//objGraphics.FillRectangle(new SolidBrush(_imgBgColor), _beginX, _beginY, _imgWidth - _beginX - 2, _imgHeight - _beginY - 2);

            
// 绘制XY轴向说明文字
            DrawAxisText(objGraphics);

            MemoryStream ms 
= new MemoryStream();
            objBitmap.Save(ms, ImageFormat.Jpeg);
            
return ms.ToArray();
        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
public void DrawCurveGraphPutOut(System.Web.HttpResponse response)  //画图并直接输出到制定页面#region public void DrawCurveGraphPutOut(System.Web.HttpResponse response)  //画图并直接输出到制定页面
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 画图并直接输出到制定页面
        
/// </summary>
        
/// <param name="response">指定页面的输出流</param>

        public void DrawCurveGraphPutOut(System.Web.HttpResponse response)
        
{
            InitValue();

            Bitmap objBitmap 
= new Bitmap(_imgWidth, _imgHeight, PixelFormat.Format16bppRgb565);
            Graphics objGraphics 
= Graphics.FromImage(objBitmap);
            Pen axisPen 
= new Pen(new SolidBrush(_axisColor), 1);
            
float FirstPoint_X = 0f;
            
float FirstPoint_Y = 0f;
            
float SecondPoint_X = 0f;
            
float SecondPoint_Y = 0f;

            
// 绘制背景及边框
            
//objGraphics.DrawRectangle(new Pen(_imgBorderColor, 1), 0, 0, _imgWidth, _imgHeight);
            objGraphics.FillRectangle(new SolidBrush(_imgBgColor), 00, _imgWidth , _imgHeight );

            
//画Y轴直线
            FirstPoint_X = Convert.ToSingle(Origin_x);
            FirstPoint_Y 
= Convert.ToSingle(paddingheight + TitleHeight - 5);
            
//FirstPoint_Y = Convert.ToSingle(_imgHeight-paddingheight);
            SecondPoint_X = Convert.ToSingle(Origin_x);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头左半边
            FirstPoint_X = Convert.ToSingle(Origin_x - 5);
            FirstPoint_Y 
= Convert.ToSingle(paddingheight + 5 + TitleHeight - 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x);
            SecondPoint_Y 
= Convert.ToSingle(paddingheight + TitleHeight - 5);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头右半边
            FirstPoint_X = Convert.ToSingle(Origin_x);
            FirstPoint_Y 
= Convert.ToSingle(paddingheight + TitleHeight - 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + 5);
            SecondPoint_Y 
= Convert.ToSingle(paddingheight + 5 + TitleHeight - 5);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);

            
//画X轴直线
            FirstPoint_X = Convert.ToSingle(Origin_x);
            FirstPoint_Y 
= Convert.ToSingle(Origin_y);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + _xWidth);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头上半边
            FirstPoint_X = Convert.ToSingle(Origin_x + _xWidth - 5);
            FirstPoint_Y 
= Convert.ToSingle(Origin_y - 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + _xWidth);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);
            
//画箭头下半边
            FirstPoint_X = Convert.ToSingle(Origin_x + _xWidth - 5);
            FirstPoint_Y 
= Convert.ToSingle(Origin_y + 5);
            SecondPoint_X 
= Convert.ToSingle(Origin_x + _xWidth);
            SecondPoint_Y 
= Convert.ToSingle(Origin_y);
            objGraphics.DrawLine(axisPen, FirstPoint_X, FirstPoint_Y, SecondPoint_X, SecondPoint_Y);

            
// 绘制Y轴上的刻度和文字
            DrawYAxis(objGraphics);

            
// 绘制X轴上的刻度和文字
            DrawXAxis(objGraphics);

            
// 绘制标题
            DrawTitle(objGraphics);

            
// 绘制数据曲线
            DrawCurve(objGraphics);

            
/**/////擦除Y轴画超出的部分

            
//objGraphics.FillRectangle(new SolidBrush(_imgBgColor), _beginX, _beginY, _imgWidth - _beginX - 2, _imgHeight - _beginY - 2);

            
// 绘制XY轴向说明文字
            DrawAxisText(objGraphics);

            objBitmap.Save(response.OutputStream, ImageFormat.Gif);


            objGraphics.Dispose();
            objBitmap.Dispose();
            response.End();


        }


        
// -----------------------------------------------------------------------------------------
        #endregion


        
private void DrawAxisText(Graphics objGraphics) // 绘制XY轴向说明文字#region private void DrawAxisText(Graphics objGraphics) // 绘制XY轴向说明文字
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制XY轴向说明文字
        
/// </summary>
        
/// <param name="objGraphics">绘图对象</param>

        private void DrawAxisText(Graphics objGraphics)
        
{
            SolidBrush brush 
= new SolidBrush(_axisTextColor);
            Size xTextSize 
= TextImgSize.GetSize(_xTitle, _font);
            Size yTextSize 
= TextImgSize.GetSize(_yTitle, _font);

            objGraphics.DrawString(_xTitle, _font, brush, Origin_x 
+ _xWidth - xTextSize.Width - 2, Origin_y + Convert.ToInt32(_imgHeight * 0.01));
            objGraphics.DrawString(_yTitle, _font, brush, Origin_x 
+ 5, paddingheight+TitleHeight-5);
        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
private void DrawXAxis(Graphics objGraphics) // 绘制X轴刻度和文字#region private void DrawXAxis(Graphics objGraphics) // 绘制X轴刻度和文字
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制X轴刻度和文字
        
/// </summary>
        
/// <param name="objGraphics">绘图对象</param>

        private void DrawXAxis(Graphics objGraphics)
        
{
            
int startY = Origin_y - 2;
            
int endY = Origin_y + 2;
            
//文字位置Y轴方向开始坐标=图片高-边界高-刻度预留+文字高
            int textStartY = _imgHeight - paddingheight - 4 + _yTextLength;
            Pen pen 
= new Pen(new SolidBrush(_axisColor));
            SolidBrush brush 
= new SolidBrush(_sliceTextColor);
            Size textSize;
            
//遍历所有大刻度数目
            for (int i = 0; i < _xMaxScaleCount; i++)
            
{
                
//绘制X轴上的刻度
                objGraphics.DrawLine(pen, Origin_x + i * _xBigSpace, Origin_y - 2, Origin_x + i * _xBigSpace, Origin_y + 2);
                textSize 
= TextImgSize.GetSize(_xScalueValue[i], _font);
                
//绘制X轴上的值
                objGraphics.DrawString(_xScalueValue[i], _font, brush, Origin_x + i * _xBigSpace, Origin_y+Convert.ToInt32(_imgHeight*0.01));
            }

        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
private void DrawYAxis(Graphics objGraphics) // 绘制Y轴刻度和文字#region private void DrawYAxis(Graphics objGraphics) // 绘制Y轴刻度和文字
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制Y轴刻度和文字
        
/// </summary>
        
/// <param name="objGraphics">绘图对象</param>

        private void DrawYAxis(Graphics objGraphics)
        
{
            SolidBrush brush 
= new SolidBrush(_sliceTextColor);
            Pen pen 
= new Pen(new SolidBrush(_axisColor));
            Size textSize;
            
decimal value = Convert.ToDecimal(_yMinValue);
            
string text;
            
//遍历
            for (int i = 0; i < Yscalecount+1; i++)
            
{
                
if (i > 0)
                
{

                    objGraphics.DrawLine(pen, Origin_x 
- 4, Origin_y - i * _ySpace, Origin_x, Origin_y - i * _ySpace);
                    value 
+= Convert.ToDecimal(_yScaleBaseValue);
                }

                text 
= value.ToString();
                textSize 
= TextImgSize.GetSize(text, _font);
                objGraphics.DrawString(text, _font, brush, Origin_x 
- textSize.Width - 2, Origin_y - i * _ySpace - textSize.Height / 2);
            }

        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
private void DrawTitle(Graphics objGraphics) // 绘制标题#region private void DrawTitle(Graphics objGraphics) // 绘制标题
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制标题
        
/// </summary>
        
/// <param name="objGraphics">绘图对象</param>

        private void DrawTitle(Graphics objGraphics)
        
{
            SolidBrush brush 
= new SolidBrush(_imgTitleColor);
            Size textSize 
= TextImgSize.GetSize(_imgTitle, _font);
            objGraphics.DrawString(_imgTitle, titlefont, brush, (_imgWidth 
- textSize.Width-paddingwidth*2)/25);
        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
private void DrawCurve(Graphics objGraphics) // 绘制数据曲线#region private void DrawCurve(Graphics objGraphics) // 绘制数据曲线
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 绘制数据曲线
        
/// </summary>
        
/// <param name="objGraphics">绘图对象</param>

        private void DrawCurve(Graphics objGraphics)
        
{
            Pen curvePen 
= new Pen(_curveColor, 1);
            PointF[] curvePointF 
= null;
            
if (_values.Length > 1)
            
{
                curvePointF 
= new PointF[_values.Length];
                
for (int i = 0; i < _values.Length; i++)
                
{
                    curvePointF[i] 
= new PointF(Origin_x + i * _xSmallSpace, Origin_y - _yHeight * _values[i] / _yScaleMaxValue);
                }

                objGraphics.DrawCurve(curvePen, curvePointF, _tension);
            }

            
        }

        
// ---------------------------------------------------------------------------------------------
        #endregion


        
private static void GetTopAndEnd(float maxvalue, float minvalue, out int topvalue, out int bottomvalue) //输入最大最小值,返回最高坐标与最低坐标#region private static void GetTopAndEnd(float maxvalue, float minvalue, out int topvalue, out int bottomvalue) //输入最大最小值,返回最高坐标与最低坐标
        
// -----------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 输入最大最小值,返回最高坐标与最低坐标
        
/// </summary>
        
/// <param name="maxvalue">最大值</param>
        
/// <param name="minvalue">最小值</param>
        
/// <param name="topvalue">最高坐标</param>
        
/// <param name="bottomvalue">最低坐标</param>

        private static void GetTopAndEnd(float maxvalue, float minvalue, out int topvalue, out int bottomvalue)
        
{
            
int MaxDigit = 1;    //最大数的位数
            
//MaxDigit = Convert.ToInt32(Math.Log10(Convert.ToDouble(maxvalue)));
            MaxDigit = maxvalue.ToString().Length;
            
//int MinDigit = 1;   //最小数的位数
            
//MinDigit = Math.Log10(Convert.ToDouble(minvalue)) + 1;

            
int BaseValue = Convert.ToInt32(Math.Pow(10, MaxDigit - 1));    //基数
            string temp = maxvalue.ToString();
            temp 
= temp.Substring(01);
            
int BigFirstValue = Convert.ToInt32(temp);    //大数中最高位的数值
            BigFirstValue = BigFirstValue + 1;
            
//最高坐标=大数中最高位的数值*基数
            topvalue = BigFirstValue * BaseValue;
            
//如果最小值大于基数
            if (minvalue > BaseValue)
            
{
                temp 
= minvalue.ToString();
                temp 
= temp.Substring(01);
                
int SmallFirstValue = Convert.ToInt32(temp);  //小数中最高位的数值
                bottomvalue = SmallFirstValue * BaseValue;
                
            }

            
else
            
{
                bottomvalue 
= 0;
                
            }

        }
 
        
// -----------------------------------------------------------------------------------------
        #endregion

    }


    
public class TextImgSize    //文字区域的大小#region public class TextImgSize    //文字区域的大小
    
// -----------------------------------------------------------------------------------------
    /**//// <summary>
    
/// 文字区域的大小
    
/// </summary>

    public class TextImgSize
    
{
        
private static // 静态成员数据区#region private static // 静态成员数据区
        
// ---------------------------------------------------------------------------------------------
        private static Graphics _graph = Graphics.FromImage(new Bitmap(11));
        
// ---------------------------------------------------------------------------------------------
        #endregion


        
public static TextImgSize GetSize(string text, Font font) // 获取文字图像的大小#region public static TextImgSize GetSize(string text, Font font) // 获取文字图像的大小
        
// ---------------------------------------------------------------------------------------------
        /**//// <summary>
        
/// 获取文字图像的大小
        
/// </summary>
        
/// <param name="text">文字内容</param>
        
/// <param name="font">字体</param>
        
/// <returns>返回文字图像的大小</returns>

        public static Size GetSize(string text, Font font)
        
{
            
return _graph.MeasureString(text, font).ToSize();
        }

        
// ---------------------------------------------------------------------------------------------
        #endregion

    }
 
    
// -----------------------------------------------------------------------------------------
    #endregion

}

相关文章推荐

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

ASP.NET中实现动态曲线图的视频教程

点击下面的入口可以在线观看视频  最近在项目中有个需求,把从数据库中读到的数据输出折线图并且是随着时间的推移动态的显示不同位置的数据这就要用到了动态折线图。刚开始项目中大部分的折线图都是用的MS...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

jQueryValidate 插件(表单验证功能)

validate表单验证插件

LINQ 的查询执行何时是延迟执行,何时是立即执行,以及查询的复用

延迟执行的经典例子:我们用 select ++i 就可以看到在foreach 时候,查询才被执行。public static void Linq99(){    int[] numbers = new...
  • ghj1976
  • ghj1976
  • 2007年07月03日 16:22
  • 5823

Upload files in ASP.NET MVC with JavaScript and C#

GOOGLE的地址被封了,转过来,方便需要的人 https://cmatskas.com/upload-files-in-asp-net-mvc-with-javascript-and-c/ ...
  • Ani
  • Ani
  • 2016年05月18日 21:05
  • 439

jQuery Autocomplete with multiple fileds in Asp.Net C#

 This  article explains jQuery autocomplete with  multiple fields using Asp.Net C#.Scenario :  Listi...

自己写的一个asp.net的生成曲线图的过程

这里是从Dataset里的数据生成曲线图. 我的Dataset是从表Sendrec里读取的数据,分别有Id,Sendid(订单号),Sendtime(记录时间),Sendnum(单位时间发送量/我这...

asp.net下使用C#实现动态绘制图表

最近一直在做毕业设计,大体上是一个信息管理系统。关键的功能是实现一些简单的统计分析,所以要在网页中加一些图表显得清晰直观一些。那么既然完成了,那就简单总结一下,也算对得起这个项目了。估计上去以后也很少...
  • DAsama
  • DAsama
  • 2012年05月18日 15:59
  • 1696
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:用ASP.NET with C# 绘制曲线图(Curve图)
举报原因:
原因补充:

(最多只允许输入30个字)