饼图(附代码)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class Default5 : System.Web.UI.Page
{
    private int _pWidth = 500;//画布宽
    private int _pHeight = 300;//画布宽   
    private int total = 0;
    private int top = 20;//顶高
    private int startA = 0;
    private Hashtable list = new Hashtable();
    Color c = Color.FromArgb(128, 128, 128);//字体颜色
    Bitmap bmp;
    Graphics g;
    protected void Page_Load(object sender, EventArgs e)
    {
        initPanel();//初始化画布
        Random rnd = new Random();
        DrawHeader("调查统计结果一览图");//初始化标题
        AddItem("网易", rnd.Next(100,1000));
        AddItem("新浪", rnd.Next(100, 1000));
        AddItem("CSDN", rnd.Next(100, 1000));
        AddItem("blog.csdn.net/sunjay117", rnd.Next(100, 1000));
        AddItem("Google", rnd.Next(100, 1000));
        AddItem("百度", rnd.Next(100, 1000));
        AddItem("吕布", rnd.Next(100, 1000));
        ShowPanel();  
      
    }
  
    void AddItem(string text,int value)
    {
      
        total += value;
        list.Add(text,value);
        DrawRect(top + list.Count * 18, getColor(list.Count), text);      
    }
    void initPanel()//初始化画布
    {
        bmp = new Bitmap(this._pWidth, this._pHeight);
        g = Graphics.FromImage(bmp);
        g.Clear(Color.WhiteSmoke);
    }
    void DrawRect(int y, Color color, string s)
    {
        int x = 400;
        int w = 30;
        int h = 10;
        g.FillRectangle(new SolidBrush(c), x + 2, y + 2, w, h);
        g.DrawRectangle(new Pen(c), x, y, w, h);
        g.FillRectangle(new SolidBrush(color), x, y, w, h);
        g.DrawString(s, new Font("宋体", 9), new SolidBrush(c), x + w + 2, y);

    }
    void DrawHeader(string header)
    {
        g.DrawString(header, new Font("宋体", 17), new SolidBrush(c), 30, 10);
    }
    void ShowPanel()//显示图象
    {
        int oX=80;
        int oY=70;
        int R=100;
        int sweetA=0;
        g.DrawRectangle(new Pen(c), 1, 1, bmp.Width - 2, bmp.Height - 2);
        int i=0;
        foreach (string str in list.Keys)
        {
            i++;
            int offset = 10;
            sweetA = 360 * (int)list[str] / total;
            if (i == list.Count)
            {
                sweetA = 360 - startA;
            }
            double a=(startA+sweetA/2)*Math.PI/180.0f;
            int endX = Convert.ToInt32(Math.Cos(a) * (R + offset));
            int endY = Convert.ToInt32(Math.Sin(a) * (R + offset));
            int offsetX = Convert.ToInt32(Math.Cos(a) * (offset));
            int offsetY = Convert.ToInt32(Math.Sin(a) * (offset));
            g.DrawLine(new Pen(c), oX + R, oY + R, endX+R+oX, endY+R+oY);
         
            if (Math.Cos(a) > 0)
            {
                g.DrawLine(new Pen(c), oX + R + endX, oY + R + endY, endX + R + oX + offset, endY + R + oY);
                g.DrawString(str, new Font("宋体", 9), new SolidBrush(c), endX + R + oX + offset, endY + R + oY-6);
            }
            else
            {
                g.DrawLine(new Pen(c), oX + R + endX, oY + R + endY, endX + R + oX - offset, endY + R + oY);
                g.DrawString(str, new Font("宋体", 9), new SolidBrush(c), endX + R + oX - 15 - 6*System.Text.Encoding.Default.GetBytes(str).Length, endY + R + oY - 6);
            }
            g.DrawPie(new Pen(c,1), oX, oY, R * 2, R * 2, startA, sweetA);
            g.FillPie(new SolidBrush(getColor(i)), oX , oY , R * 2, R * 2, startA, sweetA);

           
            startA += sweetA;
        }
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
       
    }
    public Color getColor(int i)
    {
        switch (i)
        {
            case 0:
                return Color.Red;

            case 1:
                return Color.Blue;

            case 2:
                return Color.Yellow;
            case 3:
                return Color.Green;

            case 4:
                return Color.Pink;

            case 5:
                return Color.Plum;

            case 6:
                return Color.Gray;

            case 7:
                return Color.Salmon;

            case 8:
                return Color.RosyBrown;

            case 9:
                return Color.Teal;

            case 10:
                return Color.Orange;

            case 11:
                return Color.Thistle;

            case 12:
                return Color.Maroon;

            default:
                return Color.WhiteSmoke;


        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunjay117

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值