本文將介紹在. Net中如何使用程式碼畫圖表,就像用MS Excel產生的圖表一樣。也可以畫像DataGrid一樣的表格。
在. Net中,微軟為我們提供了畫圖類別(System.Drawing.Imaging),在該類別中畫圖的基本功能都有。例如:直線、折線、長方形、多邊形、橢圓形、扇形、曲線等等,因此一般的圖形都可以直接用程式碼畫出來。接下來介紹一些畫圖函數:
Bitmap bMap=new Bitmap(500,500) //定義圖片大小;
bMap.Save(Stream,ImageCodecInfo) //將映像儲存到指定的輸出流;
Graphics gph //定義或建立GDI繪圖物件;
PointF cPt //定義二維平面中x,y座標;
DrawString(string,Font,Brush,PonitF) //用指定的Brush和Font物件在指定的矩形或點繪製指定的字串;
DrawLine(Pen,Ponit,Ponit) //以指定的筆(Pen)物件繪製指定兩點之間直線;
DrawPolygon(Pen,Ponit[]) //用指定的筆(Pen)物件繪製指定多邊形,例如三角形,四邊形等等;
FillPolygon(Brush,Ponit[]) //用指定的刷子(Brush)物件填滿指定的多邊形;
DrawEllipse(Pen,x,y,Width,Height) //用指定的筆繪製一個邊框定義的橢圓;
FillEllipse(Brush,x,y,Width,Height) //用指定的刷子填滿一個邊框定義的橢圓形;
DrawRectangle(Pen,x,y,Width,Height) //用指定的筆繪製一個指定座標點、寬度、高度的矩形;
DrawPie(Pen,x,y,Width,Height,startAngle,sweepAngle) //用指定的筆繪製一個指定座標點、寬度、高度以及兩條射線組成的扇形;
OK,就介紹這麼多了,參數已簡寫。我相信在實際的使用過程中,大家會有更深刻的體會。最後,透過一個簡單的例子來看看如何使用這些畫圖函數(折線圖)。
畫出上述折線圖的程式碼如下:
//資料初始化
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);//中心點
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("某工廠某產品月生產量圖表", new Font("宋體", 14), Brushes.Black, new PointF(cPt.X+60, cPt.X));//圖表標題
//畫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.Yi*30-6));
gph.DrawLine(Pens.Black, cPt.X-3,cPt.Yi*30 ,cPt.X,cPt.Yi*30);
}
//畫X軸項目
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));
//畫點
gph.DrawEllipse(Pens.Black,cPt.X+i*30-1.5F,cPt.Yd[i-1]*3-1.5F,3,3);
gph.FillEllipse(new SolidBrush(Color.Black),cPt.X+i*30-1.5F,cPt.Yd[i-1]*3-1.5F,3,3);
//畫數值
gph.DrawString(d[i-1].ToString(), new Font("宋體", 11), Brushes.Black, new PointF(cPt.X+i*30,cPt.Yd[i-1]*3 ));
//畫折線
if(i>1) gph.DrawLine(Pens.Red,cPt.X+(i-1)*30,cPt.Yd[i-2]*3,cPt.X+i*30,cPt.Yd[i- 1]*3);
}
//儲存輸出圖片
bMap.Save(Response.OutputStream, ImageFormat.Gif);
http://www.cnblogs.com/172838427/archive/2006/09/19/508688.html