日期:2010-11-19 浏览次数:20449 次
本文将介绍在.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.Y-i*30-6));
gph.DrawLine(Pens.Black, cPt.X-3,cPt.Y-i*30 ,cPt.X,cPt.Y-i*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.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);
//画数值
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);