NET簡單的一個畫圖程序

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;
    }

}

而後這裏是 調用的方法:數組

        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生成成功!");

不過要注意的是  須要創建文件夾  框架

出處:http://blog.csdn.net/jacky4955/article/details/4061658函數

相關文章
相關標籤/搜索