C#画3D饼图(二)

C#画饼图一中介绍了小王利用C#画了一个平面饼状图,在小王画出来之后,小王开始想入非非了--要是能画出3D的饼状图该多好,于是小王就开始修改showImage.ashx页面了,果然功夫不负有心人,居然给小王做出来了,先看结果:

     

Yeh!看起来的确比原来帅多了,其实很简单,就是将原来的饼状图画成为椭圆,并在Y轴方向上向下多画了几个饼图,并将其重合在一起而已(这就是3D啦)!

修改后的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(0, 0, 200, 200);
 65                    int pointX = 0;
 66                    int pointY = 20;
 67                    gp.DrawLine(Pens.Black, 10204190204);
 68                    bool single = true;
 69                    int y = 210;
 70                    for (int i = 0; i < departments.Count; i++)
 71                    {
 72                        dp = (department)departments[i];
 73                        sb.Color = Color.FromArgb(rd.Next(255), rd.Next(255), rd.Next(255));
 74                        sweepAngle = Convert.ToInt32(360 * Convert.ToSingle(dp.number) / Convert.ToSingle(sum));
 75                        //gp.FillPie(sb, rect, startAngle, sweepAngle);
 76                        if (startAngle < 180)
 77                        {
 78                            for (int height = 0; height < 8; height++)
 79                            {
 80                                gp.FillPie(Brushes.Black, pointX, pointY + height, 200100, startAngle, sweepAngle);
 81                            }

 82                        }

 83                        gp.FillPie(sb, pointX, pointY, 200100, startAngle, sweepAngle);
 84                        startAngle += sweepAngle;
 85                        if (single)
 86                        {
 87                            gp.FillRectangle(sb, new Rectangle(10, y, 2015));
 88                            gp.DrawString(dp.name, new Font("Tahoma"8, FontStyle.Regular), Brushes.Black, new PointF(30, y));
 89                            single = false;
 90                        }

 91                        else
 92                        {
 93                            gp.FillRectangle(sb, new Rectangle(110, y, 2015));
 94                            gp.DrawString(dp.name, new Font("Tahoma"8, FontStyle.Regular), Brushes.Black, new PointF(130, y));
 95                            single = true;
 96                            y += 20;
 97                        }

 98                    }

 99                    //save the image in the page
100                    gp.DrawLine(Pens.Black, 10258190258);
101                    context.Response.ContentType = "Image/GIF";
102                    context.Response.Clear();
103                    context.Response.BufferOutput = true;
104                    bm.Save(context.Response.OutputStream, ImageFormat.Gif);
105                }

106            }

107        }

108    }

109 
110    public bool IsReusable
111    {
112        get
113        {
114            return false;
115        }

116    }

117
118}
原文地址:https://www.cnblogs.com/cdutedu/p/1288940.html