C#画饼状图(一)

中秋节到啦!公司准备给每个人发月饼,作为技术员的小王要求在公司网站上用饼图的形式将公司各个部门的月饼发放数量表示出来,小王想了半天,准备用C#来设计该图,以下是公司部门员工数量分布数据表:

最终饼图(图再大些可以把部门和部门员工数量一起写出来就好了):

步骤:

1.在VisualStudio2005中创建网站DrawPie

2.在默认首页Default.aspx上插入Image控件

3.插入一个Generic Handler新项,命名为showImage.ashx

4.将Default.aspx上的Image控件的ImageUrl属性设置为~/showImage.ashx

5.在showImage.ashx上输入以下代码:

  1<%@ WebHandler Language="C#" Class="showImage" %>
  2
  3using System;
  4using System.Web;
  5using System.Drawing;
  6using System.Collections;
  7using System.Drawing.Imaging;
  8using System.Drawing.Drawing2D;
  9using System.Data;
 10using System.Data.SqlClient;
 11using System.Web.Configuration;
 12
 13public class showImage : IHttpHandler
 14{
 15    struct department
 16    {
 17        public string name;
 18        public int number;
 19    }

 20    
 21    public void ProcessRequest (HttpContext context)
 22    {
 23        ArrayList departments = new ArrayList();
 24        department dp;
 25        int sum=0;
 26        SolidBrush sb = new SolidBrush(Color.Aqua);
 27        Random rd = new Random();
 28        string connstr = "server=(local);database=test;uid=sa;pwd=sa13";
 29        SqlConnection conn = new SqlConnection(connstr);
 30        int startAngle = 0;
 31        int sweepAngle = 45;
 32        //connect the database
 33        try
 34        {
 35            conn.Open();
 36            SqlCommand comm = new SqlCommand("select * from department", conn);
 37            SqlDataReader dr = comm.ExecuteReader();
 38            while (dr.Read())
 39            {
 40                dp.name = dr["departmentName"].ToString();
 41                dp.number = Convert.ToInt32(dr["departmentNum"]);
 42                sum += dp.number;
 43                departments.Add(dp);
 44            }

 45            dr.Close();
 46        }

 47        catch (Exception ex)
 48        {
 49            throw new Exception(ex.Message);
 50        }

 51        finally
 52        {
 53            conn.Close();
 54        }

 55        //Draw the pie of the every department
 56        if (departments.Count > 0)
 57        {
 58            using (Bitmap bm = new Bitmap(200,260))
 59            {
 60                using (Graphics gp = Graphics.FromImage(bm))
 61                {
 62                    gp.SmoothingMode = SmoothingMode.AntiAlias;
 63                    gp.Clear(Color.White);
 64                    Rectangle rect = new Rectangle(00200200);
 65                    gp.DrawLine(Pens.Black, 10204190204);
 66                    bool single = true;
 67                    int y = 210;
 68                    for (int i = 0; i < departments.Count; i++)
 69                    {
 70                        dp = (department)departments[i];
 71                        sb.Color = Color.FromArgb(rd.Next(255), rd.Next(255), rd.Next(255));
 72                        sweepAngle = Convert.ToInt32(360 * Convert.ToSingle(dp.number) / Convert.ToSingle(sum));
 73                        gp.FillPie(sb, rect, startAngle, sweepAngle);
 74                        startAngle += sweepAngle;
 75                        if (single)
 76                        {
 77                            gp.FillRectangle(sb, new Rectangle(10, y, 2015));
 78                            gp.DrawString(dp.name, new Font("Tahoma"8, FontStyle.Regular), Brushes.Black, new PointF(30, y));
 79                            single = false;
 80                        }

 81                        else
 82                        {
 83                            gp.FillRectangle(sb, new Rectangle(110, y, 2015));
 84                            gp.DrawString(dp.name, new Font("Tahoma"8, FontStyle.Regular), Brushes.Black, new PointF(130, y));
 85                            single = true;
 86                            y += 20;
 87                        }

 88                    }

 89                    //save the image in the page
 90                    gp.DrawLine(Pens.Black, 10258190258);
 91                    context.Response.ContentType = "Image/GIF";
 92                    context.Response.Clear();
 93                    context.Response.BufferOutput = true;
 94                    bm.Save(context.Response.OutputStream, ImageFormat.Gif);
 95                }

 96            }

 97        }

 98    }

 99 
100    public bool IsReusable
101    {
102        get
103        {
104            return false;
105        }

106    }

107
108}
原文地址:https://www.cnblogs.com/cdutedu/p/1288895.html