|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
c++是语言,其实C++和java的应用范围根本就不一样的。在java应用的领域内,c++是不合适的。所以微软才搞了C#和Java对抗。饼图|折线图|柱形图 本文将先容在.Net中怎样利用代码绘图表,就像用MSExcel天生的图表一样。也能够画像DataGrid一样的表格。
在.Net中,微软给我们供应了绘图类(System.Drawing.Imaging),在该类中绘图的基础功效都有。好比:直线、折线、矩形、多边形、卵形、扇形、曲线等等,因而一样平常的图形都能够间接经由过程代码画出来。接上去先容一些绘图函数:
BitmapbMap=newBitmap(500,500) //界说图象巨细;
bMap.Save(Stream,ImageCodecInfo)//将图象保留到指定的输入流;
Graphicsgph//界说或创立GDI画图对像;
PointFcPt //界说二维立体中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=newstring[12]{"一月","仲春","三月","四月","蒲月","六月","七月","八月","玄月","十月","十一月","十仲春"};
float[]d=newfloat[12]{20.5F,60,10.8F,15.6F,30,70.9F,50.3F,30.7F,70,50.4F,30.8F,20};
//绘图初始化
BitmapbMap=newBitmap(500,500);
Graphicsgph=Graphics.FromImage(bMap);
gph.Clear(Color.White);
PointFcPt=newPointF(40,420);//中央点
PointF[]xPt=newPointF[3]{newPointF(cPt.Y+15,cPt.Y),newPointF(cPt.Y,cPt.Y-8),newPointF(cPt.Y,cPt.Y+8)};//X轴三角形
PointF[]yPt=newPointF[3]{newPointF(cPt.X,cPt.X-15),newPointF(cPt.X-8,cPt.X),newPointF(cPt.X+8,cPt.X)};//Y轴三角形
gph.DrawString("某工场某产物月临盆量图表",newFont("宋体",14),Brushes.Black,newPointF(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(newSolidBrush(Color.Black),xPt);
gph.DrawString("月份",newFont("宋体",12),Brushes.Black,newPointF(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(newSolidBrush(Color.Black),yPt);
gph.DrawString("单元(万)",newFont("宋体",12),Brushes.Black,newPointF(0,7));
for(inti=1;i<=12;i++)
{
//画Y轴刻度
if(i<11)
{
gph.DrawString((i*10).ToString(),newFont("宋体",11),Brushes.Black,newPointF(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),newFont("宋体",11),Brushes.Black,newPointF(cPt.X+i*30-5,cPt.Y+5));
gph.DrawString(month[i-1].Substring(1,1),newFont("宋体",11),Brushes.Black,newPointF(cPt.X+i*30-5,cPt.Y+20));
if(month[i-1].Length>2)gph.DrawString(month[i-1].Substring(2,1),newFont("宋体",11),Brushes.Black,newPointF(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(newSolidBrush(Color.Black),cPt.X+i*30-1.5F,cPt.Y-d[i-1]*3-1.5F,3,3);
//画数值
gph.DrawString(d[i-1].ToString(),newFont("宋体",11),Brushes.Black,newPointF(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);
实不相瞒,Java是我见过的执行效率最低的程序设计语言,前不久在CSDN论坛上有个评测,计算9999的阶乘,同样的循环算法,Java的耗时是.NET的5倍。 |
|