码迷,mamicode.com
首页 > Web开发 > 详细

NET简单的一个画图程序

时间:2014-05-10 07:36:33      阅读:491      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

bubuko.com,布布扣
using System;
using System.Drawing;
//HttpUtility.UrlEncode

/// <summary>
///Curve 的摘要说明
/// </summary>
public class Curve
{
    public Curve()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /*******************************************************属性设置**************************************************************/
    /// <summary>
    /// 房价数组
    /// </summary>
        private string[]  _CurveData;
    /// <summary>
    /// 房价时间
    /// </summary>
        //private string[] _CurveDate;
        public string[] CurveData
        {
        get { return _CurveData; }
        set { _CurveData = value; }
        }
        //public string[] CurveDate
        //{
        //    get { return _CurveDate; }
        //    set { _CurveDate = value; }
        //}

    /// <summary>
    /// 是否显示曲线
    /// </summary>
    private bool _IsDemonstrationCurve=true;
    public bool IsDemonstrationCurve
    {
        get { return _IsDemonstrationCurve;}
        set {
            if(value==false)
            {
                _IsDemonstrationCurve = false;
            }else
            {
                _IsDemonstrationCurve = true;
            }

        }
    }
    /// <summary>
    /// 是否显示 矩形图
    /// </summary>
    private bool _IsDemonstrationRectangle=true;
    public bool IsDemonstrationRectangle
    {
        get { return _IsDemonstrationRectangle; }
        set
        {
            if(value==false)
            {
                _IsDemonstrationRectangle = false;
            }else
            {
                _IsDemonstrationRectangle = true;
            }
        }


    }
    /*****************************************属性设置结束***********************************************************************************/

    

    public bool getCurve(string FileName)
    {

        try
        {
            /***********************大图的参数*****************************/
            /*自写编码*/
            int ImgWidth = 900; //1.[总宽度]            ***图宽度   
            int ImgHeight = 550; //2.[总高度]            ***图高度        
            int ItemNum = 1; //3.[项目数量]            ***图表划分的块     
            //int ChildSpace=15;        //6.[各块间的间距]
            int ChartLeft = 80; //7.[图表左边距]        ***图表距图的左边距离
            int ChartRight = 50; //8.[图表右边距]        ***图表距图的右边距离
            int ChartTop = 50; //9.[图表顶边距]        ***图表距图顶边距离
            int ChartBottom = 50; //10.[图表底边距]        ***图表距图底边距离

            /*************************坐标位置调整******************************************/
            int YMaxValue = 8000; //11.[纵坐标标尺最大值]    ***纵坐标标尺的最大值
            int YItemNum = 15; //12.[纵坐标标尺段数]    ***纵坐标标尺的段数
            int YTop = 15; //13.[距纵轴顶端间隔]
            int YStrStart = 35; //14.[纵坐标标尺文字起始X坐标]
            int XRight = 15; //15.[距横轴右端间隔]
            int XStrStart = 20; //16.[横坐标标尺文字起始Y坐标]


            /*************************小刻度位置调整*********************************/
            int ChildNum = 4; //4.[块数]                ***大块中划分的子项的数量
            float ChildRate = 0.6f; //5.[各块总占空间比率]
            int xiaoSpacing = 80; //小标示和小刻度之间的距离
            int Xxiaospace_between = 35; //矩形距离小刻度的位置
            int intjiange = 300; //顶部名称说明距离 左边的距离


            string[] strGIFName = new string[ItemNum]; //大标示  注意和项目个数有联系
            //string strGIFName = "2009年价格走势图";
            /*因为 最近要换 所以 只有今年的*/
            for (int intGIF = 1; intGIF <= ItemNum; intGIF++)
            {

                strGIFName[intGIF - 1] = 2008 + intGIF + "年价格走势图";
                // arrValueNames[1] = "2010年价格走势图";
                // arrValueNames[2] = "2011年价格走势图"; 
            }


            /*****************************各项数值的计算****************************************/
            //[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
            int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
            //[项目宽度]=[图表总宽度]/[项目数量]
            int itemwidth = chartwidth/ItemNum;
            //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
            int factwidth = Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
            //[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
            int rectanglewidth = factwidth/ChildNum;
            //[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
            int falge = ((itemwidth - factwidth)/(ChildNum + 1));
            decimal myFale = falge;
            int childspace = Convert.ToInt32(Math.Floor(myFale));
            //(itemwidth-factwidth)/(ChildNum+1)

            /***********************************开始画图******************************************************/
            Graphics objGps; //建立画板对象
            Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight); //建立位图对象
            objGps = Graphics.FromImage(objBitMap); //根据位图对象建立画板对象
            objGps.Clear(Color.White); //设置画板对象的背景色
            /***************************横坐标设置*************************************************/
            string[] strArrValueName = new string[ChildNum]; //标示名称(月份)         注意长度要和ChildNum 相同 //横坐标设置
            for (int intquarter = 1; intquarter <= strArrValueName.Length; intquarter++)
            {
                if (intquarter%4 == 1)
                {
                    strArrValueName[intquarter - 1] = "1~3月";
                }
                if (intquarter%4 == 2)
                {
                    strArrValueName[intquarter - 1] = "4~6月";
                }
                if (intquarter%4 == 3)
                {
                    strArrValueName[intquarter - 1] = "7~9月";
                }
                if (intquarter%4 == 0)
                {
                    strArrValueName[intquarter - 1] = "10~12月";
                }
            }

            /****************************数据的赋值*******************************/
            /*********************************************************************/
            //显示的数据
            int[] arrValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //数据数组
              //CurveData

            for (int inttheNum = 0; inttheNum <ChildNum; inttheNum++)
            {

                /*  (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i   纵坐标的计算方法 */
                //ImgHeight - ChartBottom - ChartTop - YTop 总高度-距离顶部高度-距离底部高度-y坐标距离顶部高度/标示个数个数=单个坐标的高度
                // ImgHeight - ChartBottom-单个坐标的高度     总高度-距离底部高度-单个项目的高度
                double dange = (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum; //单个高度的计算             
               
                double floatfalge = (double.Parse(CurveData[inttheNum])) * dange;
                // double floatfalge = (ImgHeight - ChartBottom - ChartTop - YTop)*(inttheNum + 1)*400/YMaxValue;
                arrValues[inttheNum] = Convert.ToInt32(Math.Floor(floatfalge));
            }


            /***************************************纵坐标显示标示设置*******************************************/
            //string[] arrValueNames = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}; 
            string[] arrValueNames = new string[YItemNum]; //这里表示所有的纵坐标的标示度名称
            for (int intValueName = 0; intValueName < arrValueNames.Length; intValueName++)
            {
                arrValueNames[intValueName] = ((intValueName*500) + 2000).ToString();
            }




            //得出矩形宽度,和画图X轴位置

            //[项目宽度]=[总宽度]/[项目数量]
            //======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
            //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
            //[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
            //[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2  

            System.Drawing.Point[] pi = new Point[arrValues.Length]; //定义折线点的对象数组
            System.Drawing.Point[] pit = new Point[3]; //定义坐标三角点的对象数组
            System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor(7)), 1f); //定义画直线的对象
            //画纵轴
            objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop));
            //画纵轴终点箭头
            pit[0].X = ImgWidth - ChartRight; //确定三角形三点的位置
            pit[0].Y = ImgHeight - ChartBottom - 4;
            pit[1].X = ImgWidth - ChartRight;
            pit[1].Y = ImgHeight - ChartBottom + 4;
            pit[2].X = ImgWidth - ChartRight + 10;
            pit[2].Y = ImgHeight - ChartBottom;
            objGps.FillPolygon(new SolidBrush(GetColor(7)), pit);
            //画纵轴标尺和标尺描述
            for (int i = 1; i <= YItemNum; i++)
            {
                //画标尺
                objGps.DrawLine(pe,
                                new PointF(ChartLeft,
                                           ImgHeight - ChartBottom -
                                           (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i),
                                new PointF(ChartLeft - 5,
                                           ImgHeight - ChartBottom -
                                           (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i));
                //画描述
                objGps.DrawString(arrValueNames[i - 1].ToString(), new Font("宋体", 10), Brushes.Black,
                                  new Point(YStrStart,
                                            ImgHeight - ChartBottom -
                                            (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i - 5));
            }
            //画横轴
            objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom),
                            new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));
            //画横轴终点箭头
            pit[0].X = ChartLeft - 3; //确定三角形三点的位置
            pit[0].Y = ChartTop;
            pit[1].X = ChartLeft + 3;
            pit[1].Y = ChartTop;
            pit[2].X = ChartLeft;
            pit[2].Y = ChartTop - 10;
            objGps.FillPolygon(new SolidBrush(GetColor(7)), pit);
            //画横轴标尺和标尺描述

            arrValueNames = strGIFName;


            for (int i = 1; i <= ItemNum; i++)
            {
                //i表示画的 第几个 ImgHeight - ChartBottom + 5 表示线的长度
                //pe 表示pen 定义的一只笔
                //  ChartLeft + itemwidth * i, ImgHeight - ChartBottom  横坐标=左边距+显示个数 ImgHeight - ChartBottom纵坐标=图片总高度-底边距离

                objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom),
                                new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom + 10)); //画横坐标大刻度

                //画小刻度和标示
                for (int j = 1; j <= ChildNum; j++)
                {
                    //
                    float widthX; //定义起点和重点的横坐标
                    widthX = ChartLeft + itemwidth*j/ChildNum + (i - 1)*itemwidth;
                    //画小的刻度
                    objGps.DrawLine(pe, new PointF(widthX, ImgHeight - ChartBottom),
                                    new PointF(widthX, ImgHeight - ChartBottom + 3));
                    //画小标示



                    //arrValueNames[]
                    //  ChartLeft + childspace + itemwidth * (i - 1)  横坐标=左边距+间隔+个数
                    int widthxiao;
                    widthxiao = ChartLeft + xiaoSpacing + itemwidth*(j - 1)/ChildNum + itemwidth*(i - 1);
                    objGps.DrawString(strArrValueName[j - 1].ToString(), new Font("宋体", 9), Brushes.Blue,
                                      new Point(widthxiao, ImgHeight - ChartBottom + XStrStart - 12));
                }



                objGps.DrawString(arrValueNames[i - 1].ToString(), new Font("宋体", 12), Brushes.Black,
                                  new Point(ChartLeft + intjiange + itemwidth*(i - 1),
                                            ImgHeight - ChartBottom + XStrStart));
            }



            //框架完成 开始画图
            for (int intfalge = 1; intfalge <= ItemNum; intfalge++)
            {

                for (int j = 0; j < ChildNum; j++) //画矩形图和折线图
                {
                    int theintwidth = 0;
                    //左边距+各块间距
                    theintwidth = ChartLeft + itemwidth * j / ChildNum + Xxiaospace_between + (intfalge - 1) * itemwidth;
                  
                    if(IsDemonstrationRectangle==true)
                    {
                     //画矩形                   
                     //5表示距离小刻度的位置
                    //j * (childspace + rectanglewidth)) + childspace + ChartLeft   
                    //画矩形里面的颜色
                    objGps.FillRectangle(new SolidBrush(GetColor(j)), theintwidth,
                                         ImgHeight - ChartBottom - arrValues[j],
                                         rectanglewidth, arrValues[j]);
                    //画矩形外框
                    objGps.DrawRectangle(Pens.Black, theintwidth, ImgHeight - ChartBottom - arrValues[j], rectanglewidth,
                                         arrValues[j]);
                    }
                    
                    //********************************画折线图****************************************************
                    if(IsDemonstrationCurve==true)
                    {
                        //   pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
                        pi[j].X = theintwidth; //定义折线的横坐标
                        pi[j].Y = ImgHeight - ChartBottom - arrValues[j]; //纵坐标=图片总高度-底部高度-实际数值

                        pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,
                                      System.Drawing.Drawing2D.LineCap.RoundAnchor,
                                      System.Drawing.Drawing2D.DashCap.Round);
                        if (j > 0) //进行连线
                        {
                            objGps.DrawLine(pe, pi[j - 1], pi[j]);
                        }
                    }                 
                }
            }

            //objBitMap.Save(Response.OutputStream, ImageFormat.Gif);//该位图对象以"GIF"格式输出
            //objBitMap.Save();//进行保存
            System.Web.HttpServerUtility myServer = System.Web.HttpContext.Current.Server;
            string strPath= myServer.MapPath("~/CurveGIF/");

            //string strPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.ToString() +
             //"/CurveGIF/"+FileName+".gif";

            objBitMap.Save(strPath+FileName+".gif");
            pe.Dispose();
            objBitMap.Dispose();
            GC.Collect(); //资源释放 垃圾回收
            return true;
        }catch(Exception ex)
        {
            return false;
        }
    }



    /**/
    /// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
    /// <returns></returns>
    public static Color GetColor(int itemIndex)
    {
        Color objColor = new Color();
        switch (itemIndex)
        {
            case 0:
                objColor = Color.Blue;
                break;
            case 1:
                objColor = Color.Yellow;
                break;
            case 2:
                objColor = Color.Red;
                break;
            case 3:
                objColor = Color.Orange;
                break;
            case 4:
                objColor = Color.Purple;
                break;
            case 5:
                objColor = Color.Brown;
                break;
            case 6:
                objColor = Color.Pink;
                break;
            case 7:
                objColor = Color.Peru;
                break;
            case 8:
                objColor = Color.PowderBlue;
                break;
            case 9:
                objColor = Color.RosyBrown;
                break;
            case 10:
                objColor = Color.RoyalBlue;
                break;
            case 11:
                objColor = Color.SaddleBrown;
                break;
            case 12:
                objColor = Color.SeaGreen;
                break;
            default:
                objColor = Color.Black;
                break;
        }
        return objColor;
    }



    /**/
    /// <param name="red">自定义颜色红色分量值,有效值0到255</param>
    /// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
    /// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
    /// <returns></returns>
    public static Color GetColor(int red, int green, int blue)
    {
        Color objColor = new Color();
        objColor = Color.FromArgb(red, green, blue);
        return objColor;
    }

}
bubuko.com,布布扣

然后这里是 调用的方法:

bubuko.com,布布扣
        Curve myCurve = new Curve();

        /**设置参数**/
        myCurve.IsDemonstrationCurve = true;  //是否显示曲折线
        myCurve.IsDemonstrationRectangle = false;//是否显示矩形
        string[] strValue = {"2","3","1","1.5"};//显示的数据  现在只能显示4 个
        myCurve.CurveData = strValue; //赋值
        myCurve.getCurve(Guid.NewGuid().ToString());//创建GIF 保存
        Response.Write("GIF生成成功!");
bubuko.com,布布扣

不过要注意的是  需要建立文件夹  

出处:http://blog.csdn.net/jacky4955/article/details/4061658

NET简单的一个画图程序,布布扣,bubuko.com

NET简单的一个画图程序

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/mq0036/p/3719827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!