本文将介绍在.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);
新闻热点
疑难解答