首页 > 编程 > .NET > 正文

用asp.net画饼图(可用于各种投票程序)

2024-07-10 13:07:56
字体:
来源:转载
供稿:网友

//用asp.net画饼图(可用于各种投票程序)
//和asp相比asp.net拥有更强大的功能,使用gdi+可以轻易实现以前很多不能办到的图形功能。
//首先在c:/中建库mess.mdb,并建表title.
//建二个字段,title(char型),point(int型)
//非常满意     281
//比较满意     297
//还凑合         166
//不满意         416
//我还写了画折线图和条形图的部分,目前正在把它们全部写进一个类中。需要的可以和我联系:mailto:[email protected]
<%@ page language="c#" %>
<%@import namespace="system.data"%>
<%@import namespace="system.data.oledb"%>
<%@import namespace="system.drawing"%>
<%@import namespace="system.drawing.imaging"%>
<script language="c#" runat="server">
  public void page_load(object obj,eventargs e)
 {
//把连接字串指定为一个常量
const string strconn = "provider=microsoft.jet.oledb.4.0;" +
                             "data source=c://mess.mdb";
oledbconnection conn=new oledbconnection(strconn);
conn.open();
string sql="select *  from title";
oledbcommand cmd=new oledbcommand(sql,conn);
dataset ds=new dataset();
oledbdataadapter adapter1=new oledbdataadapter(cmd);
adapter1.fill(ds);
conn.close();
float total=0.0f,tmp;
int iloop;
    for(iloop=0;iloop<ds.tables[0].rows.count;iloop++)
     {
        tmp=convert.tosingle(ds.tables[0].rows[iloop]["point"]);//转换成单精度,投票不可能投半票。也可写成convert.toint32
        total+=tmp;
        }
//response.write(convert.tostring(total));

font fontlegend=new font("verdana",9),fonttitle=new font("verdana",10,fontstyle.bold);//设置字体
//fonttitle为主标题的字体

int width=230;//白色背景宽
const int bufferspace=15;
int legendheight=fontlegend.height*(ds.tables[0].rows.count+1)+bufferspace;
 int titleheight = fonttitle.height + bufferspace;
 int height = width + legendheight + titleheight + bufferspace;//白色背景高
 int pieheight = width;
rectangle pierect=new rectangle(0,titleheight,width,pieheight);

//加上各种随机色
 arraylist colors = new arraylist();
 random rnd = new random();
 for (iloop = 0; iloop < ds.tables[0].rows.count; iloop++)
  colors.add(new solidbrush(color.fromargb(rnd.next(255), rnd.next(255), rnd.next(255))));

bitmap objbitmap=new bitmap(width,height);//创建一个bitmap实例
//bitmap objbitmap=new bitmap(230,500);//创建一个bitmap实例
graphics objgraphics=graphics.fromimage(objbitmap);

objgraphics.fillrectangle(new solidbrush(color.white), 0, 0, width, height);//画一个白色背景
objgraphics.fillrectangle(new solidbrush(color.lightyellow), pierect);//画一个亮黄色背景
//以下为画饼图(有几行row画几个)
float currentdegree=0.0f;
 for (iloop = 0; iloop < ds.tables[0].rows.count; iloop++)
 {
  objgraphics.fillpie((solidbrush) colors[iloop], pierect, currentdegree,
    convert.tosingle(ds.tables[0].rows[iloop]["point"]) / total * 360);
  currentdegree += convert.tosingle(ds.tables[0].rows[iloop]["point"]) / total * 360;
 }
//---以下为生成主标题
solidbrush blackbrush=new solidbrush(color.black);
string title="本次《程序员大本营》推出了配套的专刊,您对专刊满意吗?";
 stringformat stringformat = new stringformat();
 stringformat.alignment = stringalignment.center;
 stringformat.linealignment = stringalignment.center;

 objgraphics.drawstring(title, fonttitle, blackbrush,
     new rectangle(0, 0, width, titleheight), stringformat);
//列出各字段与得票数
objgraphics.drawrectangle(new pen(color.black, 2), 0, height - legendheight, width, legendheight);
  for (iloop = 0; iloop < ds.tables[0].rows.count; iloop++)
    {
      objgraphics.fillrectangle((solidbrush) colors[iloop], 5, height - legendheight + fontlegend.height * iloop + 5, 10, 10);
      objgraphics.drawstring(((string) ds.tables[0].rows[iloop]["title"]) + " - " + convert.tostring(ds.tables[0].rows[iloop]["point"]), fontlegend, blackbrush,
   20, height - legendheight + fontlegend.height * iloop + 1);
        }
//
objgraphics.drawstring("总票数是:"+convert.tostring(total),fontlegend,blackbrush, 5, height - fontlegend.height );
//图像总的高度-一行字体的高度,即是最底行的一行字体高度(height - fontlegend.height )
response.contenttype="image/jpeg";
objbitmap.save(response.outputstream,imageformat.jpeg);
//objbitmap.save("myyyyyyyyyyy.jpg", imageformat.jpeg);//输出到文件

objgraphics.dispose();
objbitmap.dispose();
  }
</script>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表