C# GDI產生餅圖

來源:互聯網
上載者:User

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
    private string[,] data = new string[6, 2];

    protected void Page_Load(object sender, EventArgs e)
    {
        DrawingAPic();
    }
    public void DrawingAPic()
    {
        int i;
        // 執行個體化Bitmap對象
        Bitmap objbitmap;
        objbitmap = new Bitmap(400, 300);
        Graphics objGraphics;
        // 執行個體化Graphics類
        objGraphics = Graphics.FromImage(objbitmap);

        // 填充背景色           
        objGraphics.Clear(Color.White);

        // 畫圓
        objGraphics.DrawRectangle(Pens.Black, 1, 1, 500, 298);
        objGraphics.DrawString("本公司上半年營業額統計圖", new Font("宋體", 16, FontStyle.Bold), Brushes.Black, new PointF(60, 5));

        // 擷取資料,這裡類比出6個月的公司業務資料,實際應用可以從資料庫讀取
        getdata();

        PointF monthcolor = new PointF(260, 40);
        PointF fontinfor = new PointF(285, 36);
        for (i = 0; i <=5; i++)
            {
                //  畫出填充矩形
                objGraphics.FillRectangle(new SolidBrush(getcolor(i)), monthcolor.X, monthcolor.Y, 20, 10);
 
                //畫出矩形邊框。
                objGraphics.DrawRectangle(Pens.Black, monthcolor.X, monthcolor.Y, 20, 10);
 
                //畫出圖例解說文字--data(i, 0)
                objGraphics.DrawString(data[i, 0], new Font("宋體", 10), Brushes.Black, fontinfor);
 
                //移動座標位置,只移動Y方向的值即可。
                monthcolor.Y += 15;
                fontinfor.Y += 15;
        }
                // 遍曆資料來源的每一項資料,並根據資料的大小畫出矩形圖(即直條圖的柱)。
                for (i = 0; i <= 5; i++)
                {
                    //畫出填充矩形。
                    objGraphics.FillRectangle(new SolidBrush(getcolor(i)), (i * 25) + 35, 270 - System.Convert.ToInt32(data[i, 1]), 15, System.Convert.ToInt32(data[i, 1]));

                    //'畫出矩形邊框線。
                    objGraphics.DrawRectangle(Pens.Black, (i * 25) + 35, 270 - System.Convert.ToInt32(data[i, 1]), 15, System.Convert.ToInt32(data[i, 1]));
                }

                //畫出示意座標
                objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, 0, 10, 320);
                objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, 270, 200, 270);
 
            // 在示意座標上添加數值標誌,注意座標的計算
            for (i = 0; i <= 5; i++)
            {
                objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, i * 50 + 20, 20, i * 50 + 20);
                objGraphics.DrawString((250 - i * 50).ToString(), new Font("宋體", 10), Brushes.Black, 12, i * 50 + 8);
            }
            //統計總銷售額
            float scount = 0;
            for (i = 0; i <= 5; i++)
            {
                scount += float.Parse((data[i, 1]));
            }
 
            //定義畫出扇形角度變數
            float scg = 0;
            float stg = 0;
            for (i = 0; i <= 5; i++)
            {
                //計算當前角度值:當月銷售額 / 總銷售額 * 360,得到餅圖中當月所佔的角度大小。
                float num = float.Parse(data[i, 1]);
                scg = (num / scount) * 360;
 
                //畫出填充圓弧。
                objGraphics.FillPie(new SolidBrush(getcolor(i)), 220, 150, 120, 120, stg, scg);
 
                //畫出圓弧線。
                objGraphics.DrawPie(Pens.Black, 220, 150, 120, 120, stg, scg);
 
                //  把當前圓弧角度加到總角度上。
                stg += scg;
            }
 
            // 畫出解說文字
            objGraphics.DrawString("柱狀圖", new Font("宋體", 15, FontStyle.Bold), Brushes.Blue, 50, 272);
            objGraphics.DrawString("餅狀圖", new Font("宋體", 15, FontStyle.Bold), Brushes.Blue, 250, 272);
 
            // 輸出到用戶端
            objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
 
        }
   
           private void getdata()
        {
            data[0, 0] = "一月份";
            data[1, 0] = "二月份";
            data[2, 0] = "三月份";
            data[3, 0] = "四月份";
            data[4, 0] = "五月份";
            data[5, 0] = "六月份";
            data[0, 1] = "85";
            data[1, 1] = "135";
            data[2, 1] = "85";
            data[3, 1] = "110";
            data[4, 1] = "130";
            data[5, 1] = "200";
        }
    // 產生色彩值,便於顯示區別
        private Color getcolor(int i)
        {
            Color newcolor;
            i += 1;
            if (i == 1)
            {
                newcolor = Color.Blue;
            }
            else if (i == 2)
            {
                newcolor = Color.ForestGreen;
            }
            else if (i == 3)
            {
                newcolor = Color.Gainsboro;
            }
            else if (i == 4)
            {
                newcolor = Color.Moccasin;
            }
            else if (i == 5)
            {
                newcolor = Color.Indigo;
            }
            else if (i == 6)
            {
                newcolor = Color.BurlyWood;
            }
            else
                newcolor = Color.Goldenrod;
            return newcolor;
        }
     }

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.