//用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#"
< %@import namespace="System.Data"%>
< %@import namespace="System.Data.OleDb"%>
< %@import namespace="System.Drawing"%>
< %@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
24Font fontlegend=new Font("verdana",9),fonttitle=new Font("verdana",10,FontStyle.Bold);//设置字体
25//fonttitle为主标题的字体
26
27int width=230;//白色背景宽
28const int bufferspace=15;
29int legendheight=fontlegend.Height*(ds.Tables[0].Rows.Count+1)+bufferspace;
30int titleheight = fonttitle.Height + bufferspace;
31int height = width + legendheight + titleheight + bufferspace;//白色背景高
32int pieheight = width;
33Rectangle pierect=new Rectangle(0,titleheight,width,pieheight);
34
35//加上各种随机色
36ArrayList colors = new ArrayList();
37Random rnd = new Random();
38for (iloop = 0; iloop < ds.Tables[0].Rows.Count; iloop++)
39colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
40
41Bitmap objbitmap=new Bitmap(width,height);//创建一个bitmap实例
42//Bitmap objbitmap=new Bitmap(230,500);//创建一个bitmap实例
43Graphics objgraphics=Graphics.FromImage(objbitmap);
44
45objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);//画一个白色背景
46objgraphics.FillRectangle(new SolidBrush(Color.LightYellow), pierect);//画一个亮黄色背景
47//以下为画饼图(有几行row画几个)
48float currentdegree=0.0f;
49for (iloop = 0; iloop < ds.Tables[0].Rows.Count; iloop++)
50{
51objgraphics.FillPie((SolidBrush) colors[iloop], pierect, currentdegree,
52Convert.ToSingle(ds.Tables[0].Rows[iloop]["point"]) / total * 360);
53currentdegree += Convert.ToSingle(ds.Tables[0].Rows[iloop]["point"]) / total * 360;
54}
55//---以下为生成主标题
56SolidBrush blackbrush=new SolidBrush(Color.Black);
57string title="本次《程序员大本营》推出了配套的专刊,您对专刊满意吗?";
58StringFormat stringFormat = new StringFormat();
59stringFormat.Alignment = StringAlignment.Center;
60stringFormat.LineAlignment = StringAlignment.Center;
61
62objgraphics.DrawString(title, fonttitle, blackbrush,
63new Rectangle(0, 0, width, titleheight), stringFormat);
64//列出各字段与得票数
65objgraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height - legendheight, width, legendheight);
66for (iloop = 0; iloop < ds.Tables[0].Rows.Count; iloop++)
67{
68objgraphics.FillRectangle((SolidBrush) colors[iloop], 5, height - legendheight + fontlegend.Height * iloop + 5, 10, 10);
69objgraphics.DrawString(((String) ds.Tables[0].Rows[iloop]["title"]) + " - " + Convert.ToString(ds.Tables[0].Rows[iloop]["point"]), fontlegend, blackbrush,
7020, height - legendheight + fontlegend.Height * iloop + 1);
71}
72//
73objgraphics.DrawString("总票数是:"+Convert.ToString(total),fontlegend,blackbrush, 5, height - fontlegend.Height );
74//图像总的高度-一行字体的高度,即是最底行的一行字体高度(height - fontlegend.Height )
75Response.ContentType="image/jpeg";
76objbitmap.Save(Response.OutputStream,ImageFormat.Jpeg);
77//objbitmap.Save("myyyyyyyyyyy.jpg", ImageFormat.Jpeg);//输出到文件
78
79objgraphics.Dispose();
80objbitmap.Dispose();
81}
82</script>