本文將介紹在.net中如何使用代碼畫圖表,就像用ms excel生成的圖表一樣。也可以畫像datagrid一樣的表格。
在.net中,微軟給我們提供了畫圖類(system.drawing.imaging),在該類中畫圖的基本功能都有。比如:直線、折線、矩形、多邊形、橢圓形、扇形、曲線等等,因此一般的圖形都可以直接通過代碼畫出來。接下來介紹一些畫圖函數(shù):
bitmap bmap=new bitmap(500,500) //定義圖像大小;
bmap.save(stream,imagecodecinfo) //將圖像保存到指定的輸出流;
graphics gph //定義或創(chuàng)建gdi繪圖對像;
pointf cpt //定義二維平面中x,y坐標(biāo);
drawstring(string,font,brush,ponitf) //用指定的brush和font對像在指定的矩形或點(diǎn)繪制指定的字符串;
drawline(pen,ponit,ponit) //用指定的筆(pen)對像繪制指定兩點(diǎn)之間直線;
drawpolygon(pen,ponit[]) //用指定的筆(pen)對像繪制指定多邊形,比如三角形,四邊形等等;
fillpolygon(brush,ponit[]) //用指定的刷子(brush)對像填充指定的多邊形;
drawellipse(pen,x,y,width,height) //用指定的筆繪制一個(gè)邊框定義的橢圓;
fillellipse(brush,x,y,width,height) //用指定的刷子填充一個(gè)邊框定義的橢圓;
drawrectangle(pen,x,y,width,height) //用指定的筆繪制一個(gè)指定坐標(biāo)點(diǎn)、寬度、高度的矩形;
drawpie(pen,x,y,width,height,startangle,sweepangle) //用指定的筆繪制一個(gè)指定坐標(biāo)點(diǎn)、寬度、高度以及兩條射線組成的扇形;
ok,就介紹這么多了,參數(shù)已簡寫。我相信在實(shí)際的使用過程中,大家會有更深刻的體會。最后,通過一個(gè)簡單的例子來看看如何使用這些畫圖函數(shù)(折線圖)。
畫上述折線圖的代碼如下:
//數(shù)據(jù)初始化
string[] month=new string[12]{"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
float[] d=new float[12]{20.5f,60,10.8f,15.6f,30,70.9f,50.3f,30.7f,70,50.4f,30.8f,20};
//畫圖初始化
bitmap bmap=new bitmap(500,500);
graphics gph=graphics.fromimage(bmap);
gph.clear(color.white);
pointf cpt=new pointf(40,420);//中心點(diǎn)
pointf[] xpt=new pointf[3]{new pointf(cpt.y+15,cpt.y),new pointf(cpt.y,cpt.y-8),new pointf(cpt.y,cpt.y+8)};//x軸三角形
pointf[] ypt=new pointf[3]{new pointf(cpt.x,cpt.x-15),new pointf(cpt.x-8,cpt.x),new pointf(cpt.x+8,cpt.x)};//y軸三角形
gph.drawstring("某工廠某產(chǎn)品月生產(chǎn)量圖表", new font("宋體", 14), brushes.black, new pointf(cpt.x+60, cpt.x));//圖表標(biāo)題
//畫x軸
gph.drawline(pens.black, cpt.x,cpt.y ,cpt.y,cpt.y);
gph.drawpolygon(pens.black,xpt);
gph.fillpolygon(new solidbrush(color.black),xpt);
gph.drawstring("月份", new font("宋體", 12), brushes.black, new pointf(cpt.y+10, cpt.y+10));
//畫y軸
gph.drawline(pens.black, cpt.x,cpt.y,cpt.x,cpt.x);
gph.drawpolygon(pens.black,ypt);
gph.fillpolygon(new solidbrush(color.black),ypt);
gph.drawstring("單位(萬)", new font("宋體", 12), brushes.black, new pointf(0, 7));
for(int i=1;i<=12;i++)
{
//畫y軸刻度
if (i<11)
{
gph.drawstring((i*10).tostring(), new font("宋體", 11), brushes.black, new pointf(cpt.x-30, cpt.y-i*30-6));
gph.drawline(pens.black, cpt.x-3,cpt.y-i*30 ,cpt.x,cpt.y-i*30);
}
//畫x軸項(xiàng)目
gph.drawstring(month[i-1].substring(0,1), new font("宋體", 11), brushes.black, new pointf(cpt.x+i*30-5, cpt.y+5));
gph.drawstring(month[i-1].substring(1,1), new font("宋體", 11), brushes.black, new pointf(cpt.x+i*30-5, cpt.y+20));
if(month[i-1].length>2) gph.drawstring(month[i-1].substring(2,1), new font("宋體", 11), brushes.black, new pointf(cpt.x+i*30-5, cpt.y+35));
//畫點(diǎn)
gph.drawellipse(pens.black,cpt.x+i*30-1.5f,cpt.y-d[i-1]*3-1.5f,3,3);
gph.fillellipse(new solidbrush(color.black),cpt.x+i*30-1.5f,cpt.y-d[i-1]*3-1.5f,3,3);
//畫數(shù)值
gph.drawstring(d[i-1].tostring(), new font("宋體", 11), brushes.black, new pointf(cpt.x+i*30,cpt.y-d[i-1]*3));
//畫折線
if(i>1) gph.drawline(pens.red,cpt.x+(i-1)*30,cpt.y-d[i-2]*3,cpt.x+i*30,cpt.y-d[i-1]*3);
}
//保存輸出圖片
bmap.save(response.outputstream, imageformat.gif);
新聞熱點(diǎn)
疑難解答
圖片精選