国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用asp.net畫餅圖

2024-07-10 12:56: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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南开区| 象州县| 甘德县| 成武县| 扶余县| 无锡市| 乌拉特前旗| 漳平市| 固阳县| 桐柏县| 扶绥县| 务川| 和龙市| 沅陵县| 陆良县| 祥云县| 那曲县| 台前县| 寿光市| 南宁市| 响水县| 日照市| 永安市| 平阳县| 云龙县| 汽车| 犍为县| 南通市| 汾西县| 饶河县| 来凤县| 策勒县| 利辛县| 邹城市| 宜阳县| 洮南市| 定结县| 明水县| 铜梁县| 建湖县| 罗山县|