用asp.net画饼图

//用asp.net画饼图(可用于各种投票程序)
//和asp相比asp.net拥有更强大的功能,使用gdi+可以轻易实现以前很多不能办到的图形功能。
//首先在c:\中建库mess.mdb,并建表title.
//建二个字段,title(char型),point(int型)
//非常满意 281
//比较满意 297
//还凑合 166
//不满意 416
//我还写了画折线图和条形图的部分,目前正在把它们全部写进一个类中。需要的可以和我联系:mailto:[email protected]

1@ Page Language="C#" 
1@import namespace="System.Data"
1@import namespace="System.Data.OleDb"
1@import namespace="System.Drawing"
1@import namespace="System.Drawing.Imaging"
 1<script language="c#" runat="server">   
 2public void page_load(Object obj,EventArgs e)   
 3{   
 4//把连接字串指定为一个常量   
 5const String strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" +   
 6"Data Source=c:\\\mess.mdb";   
 7OleDbConnection conn=new OleDbConnection(strconn);   
 8conn.Open();   
 9string sql="select * from title";   
10OleDbCommand cmd=new OleDbCommand(sql,conn);   
11DataSet ds=new DataSet();   
12OleDbDataAdapter adapter1=new OleDbDataAdapter(cmd);   
13adapter1.Fill(ds);   
14conn.Close();   
15float total=0.0f,tmp;   
16int iloop;   
17for(iloop=0;iloop<ds.Tables[0].Rows.Count;iloop++)   
18{   
19tmp=Convert.ToSingle(ds.Tables[0].Rows[iloop]["point"]);//转换成单精度,投票不可能投半票。也可写成Convert.ToInt32   
20total+=tmp;   
21}   
22//Response.Write(Convert.ToString(total));   
23  
24  
25Font fontlegend=new Font("verdana",9),fonttitle=new Font("verdana",10,FontStyle.Bold);//设置字体   
26//fonttitle为主标题的字体   
27  
28  
29int width=230;//白色背景宽   
30const int bufferspace=15;   
31int legendheight=fontlegend.Height*(ds.Tables[0].Rows.Count+1)+bufferspace;   
32int titleheight = fonttitle.Height + bufferspace;   
33int height = width + legendheight + titleheight + bufferspace;//白色背景高   
34int pieheight = width;   
35Rectangle pierect=new Rectangle(0,titleheight,width,pieheight);   
36  
37  
38//加上各种随机色   
39ArrayList colors = new ArrayList();   
40Random rnd = new Random();   
41for (iloop = 0; iloop < ds.Tables[0].Rows.Count; iloop++)   
42colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));   
43  
44  
45Bitmap objbitmap=new Bitmap(width,height);//创建一个bitmap实例   
46//Bitmap objbitmap=new Bitmap(230,500);//创建一个bitmap实例   
47Graphics objgraphics=Graphics.FromImage(objbitmap);   
48  
49  
50objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);//画一个白色背景   
51objgraphics.FillRectangle(new SolidBrush(Color.LightYellow), pierect);//画一个亮黄色背景   
52//以下为画饼图(有几行row画几个)   
53float currentdegree=0.0f;   
54for (iloop = 0; iloop < ds.Tables[0].Rows.Count; iloop++)   
55{   
56objgraphics.FillPie((SolidBrush) colors[iloop], pierect, currentdegree,   
57Convert.ToSingle(ds.Tables[0].Rows[iloop]["point"]) / total * 360);   
58currentdegree += Convert.ToSingle(ds.Tables[0].Rows[iloop]["point"]) / total * 360;   
59}   
60//---以下为生成主标题   
61SolidBrush blackbrush=new SolidBrush(Color.Black);   
62string title="本次《程序员大本营》推出了配套的专刊,您对专刊满意吗?";   
63StringFormat stringFormat = new StringFormat();   
64stringFormat.Alignment = StringAlignment.Center;   
65stringFormat.LineAlignment = StringAlignment.Center;   
66  
67  
68objgraphics.DrawString(title, fonttitle, blackbrush,   
69new Rectangle(0, 0, width, titleheight), stringFormat);   
70//列出各字段与得票数   
71objgraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height - legendheight, width, legendheight);   
72for (iloop = 0; iloop < ds.Tables[0].Rows.Count; iloop++)   
73{   
74objgraphics.FillRectangle((SolidBrush) colors[iloop], 5, height - legendheight + fontlegend.Height * iloop + 5, 10, 10);   
75objgraphics.DrawString(((String) ds.Tables[0].Rows[iloop]["title"]) + " - " + Convert.ToString(ds.Tables[0].Rows[iloop]["point"]), fontlegend, blackbrush,   
7620, height - legendheight + fontlegend.Height * iloop + 1);   
77}   
78//   
79objgraphics.DrawString("总票数是:"+Convert.ToString(total),fontlegend,blackbrush, 5, height - fontlegend.Height );   
80//图像总的高度-一行字体的高度,即是最底行的一行字体高度(height - fontlegend.Height )   
81Response.ContentType="image/jpeg";   
82objbitmap.Save(Response.OutputStream,ImageFormat.Jpeg);   
83//objbitmap.Save("myyyyyyyyyyy.jpg", ImageFormat.Jpeg);//输出到文件   
84  
85  
86objgraphics.Dispose();   
87objbitmap.Dispose();   
88}   
89</script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus