动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

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

作者来源: 
阅读 数 474 人次 , 2006-3-29 4:33:00 

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

 本文TagsC#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:C#版的网站新闻发布系统
· 下一篇:ASP.NET中动态修改web.config中的设置项目(前台代码)
· (ASP.NET)修改和删除DataGrid行——数据库访问
· 可拖动的无标题栏窗体
· UrlEncode 以及UrlDecode 方法
· 从 Visual Basic 6.0 到 Visual Basic.NET 的转换(1)
· ASP.NET升级能力探讨(一)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077