在我们程序开发的过程中经常会需要绘制曲线图和柱状图等,尤其是在做统计功能时。但是有时候我们有觉得没有必要使用第三方控件(例如:ZedGraph等),这是我们可以自己编写代码来实现这些图形绘制的功能。以下是我在开发过程中所使用过的两段代码,现共享大家,希望能给大家带来一定的帮助,如有不妥敬请斧正!
1.柱状图,效果图如下
代码如下:
注意:请注意参数 chartTable 图形里的一些元素需要从chartTable里面取。具体请查看代码。
//Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集
publicImageRender(stringtitle,intwidth,intheight,DataTablechartTable) {
Bitmapbm=newBitmap(width,height);
Graphicsg=Graphics.FromImage(bm);
g.Clear(Color.White);
DataTabledt=chartTable;
constinttop=30;
constintleft=35;
if(width { g.DrawString("绘图区域太小",newFont("Tahoma",8), Brushes.Blue,newPointF(0,0)); returnbm; } //计算最高的点 floathighPoint=1; foreach(DataRowdrindt.Rows) { if(highPoint { highPoint=Convert.ToSingle(dr[0]); } if(highPoint { highPoint=Convert.ToSingle(dr[1]); } } try { //画大标题 g.DrawString(title,newFont("Tahoma",12),Brushes.Black,newPointF(2,2)); StringFormatdrawFormat=newStringFormat(); drawFormat.FormatFlags=StringFormatFlags.DirectionVertical; g.DrawString("[红 --"+dt.Columns[0].ToString()+"]",newFont("Tahoma",8), Brushes.Red,newPointF(2,top),drawFormat); g.DrawString("[蓝 --"+dt.Columns[1].ToString()+"]",newFont("Tahoma",8), Brushes.Blue,newPointF(17,top),drawFormat); //画条形图 floatbarWidth=(Convert.ToSingle(width)-left)/(dt.Rows.Count*3+1); PointFbarOrigin=newPointF(left+barWidth,0); floatbarHeight=dt.Rows.Count; floattopFontSize=(barWidth/highPoint.ToString().Length); if(topFontSize>2*top/3) { topFontSize=2*top/3; } if(topFontSize<5) { topFontSize=5; } for(inti=0;i { //底部字体的大小 floatbottomFontSize=(2*barWidth/dt.Rows[i][2].ToString().Length)+2; if(bottomFontSize>2*top/3) { bottomFontSize=2*top/3; } barHeight=Convert.ToSingle(dt.Rows[i][0])*(height-2*top)/highPoint*1; barOrigin.Y=height-barHeight-top; g.FillRectangle(newSolidBrush(Color.Red),barOrigin.X,barOrigin.Y,barW idth,barHeight); //柱状图底部 g.DrawString(dt.Rows[i][2].ToString(),newFont("Tahoma",bottomFontSize ),Brushes.Black, newPointF(barOrigin.X,height-top)); //柱状图顶部 g.DrawString(dt.Rows[i][0].ToString(),newFont("Tahoma",topFontSize),B rushes.Red, newPointF(barOrigin.X,barOrigin.Y-3*topFontSize/2)); barOrigin.X=barOrigin.X+barWidth; barHeight=Convert.ToSingle(dt.Rows[i][1])*(height-2*top)/highPoint*1; barOrigin.Y=height-barHeight-top; g.FillRectangle(newSolidBrush(Color.Blue),barOrigin.X,barOrigin.Y,bar Width, barHeight); //柱状图顶部 g.DrawString(dt.Rows[i][1].ToString(),newFont("Tahoma",topFontSize),B rushes.Blue, newPointF(barOrigin.X,barOrigin.Y-3*topFontSize/2)); barOrigin.X=barOrigin.X+(barWidth*2); } //设置边 g.DrawLine(newPen(Color.Blue,2),newPoint(left,top), newPoint(left,height-top)); g.DrawLine(newPen(Color.Blue,2),newPoint(left,height-top), newPoint(left+width,height-top)); g.Dispose(); returnbm; } catch { returnbm; } } 2.线状图,效果如下: 代码如下: 注意:请注意参数 chartTable 图形里的一些元素需要从chartTable里面取。具体请查看代码。 //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集 publicImageRender(stringtitle,intwidth,intheight,DataTablechartTable) { Bitmapbm=newBitmap(width,height); Graphicsg=Graphics.FromImage(bm); g.Clear(Color.White); constinttop=30; constintleft=35; if(width { g.DrawString("绘图区域太小",newFont("Tahoma",8), Brushes.Blue,newPointF(0,0)); returnbm; } if(chartTable==null) { g.DrawString("没有数据",newFont("Tahoma",7), Brushes.Blue,newPointF(0,0)); returnbm; } DataTabledt=chartTable; //计算最高的点 floathighPoint=1; foreach(DataRowdrindt.Rows) { if(highPoint { highPoint=Convert.ToSingle(dr[0]); } if(highPoint { highPoint=Convert.ToSingle(dr[1]); } } //建立一个Graphics对象实例 try { //画大标题 g.DrawString(title,newFont("Tahoma",12),Brushes.Black,newPointF(2,2)); StringFormatdrawFormat=newStringFormat(); drawFormat.FormatFlags=StringFormatFlags.DirectionVertical; g.DrawString("[红 --"+dt.Columns[0].ToString()+"]",newFont("Tahoma",8), Brushes.Red,newPointF(2,top),drawFormat); g.DrawString("[蓝 --"+dt.Columns[1].ToString()+"]",newFont("Tahoma",8), Brushes.Blue,newPointF(17,top),drawFormat); //画条形图 floatbarWidth=(Convert.ToSingle(width)-left)/(dt.Rows.Count+1); PointFbarOrigin=newPointF(left+barWidth,0); floatbarHeight=dt.Rows.Count; floattopFontSize=7; floatbottomFontSize=7; PointF[]pt1=newPointF[dt.Rows.Count]; PointF[]pt2=newPointF[dt.Rows.Count]; for(inti=0;i { //底部字体的大小 barHeight=Convert.ToSingle(dt.Rows[i][0])*(height-2*top)/highPoint*1; barOrigin.Y=height-barHeight-top; g.FillEllipse(newSolidBrush(Color.Red),barOrigin.X-3,barOrigin.Y-3,6, 6); pt1[i]=newPointF(barOrigin.X,barOrigin.Y); //顶部 g.DrawString(dt.Rows[i][0].ToString(),newFont("Tahoma",topFontSize),B rushes.Red, newPointF(barOrigin.X,barOrigin.Y-4*topFontSize/2)); barHeight=Convert.ToSingle(dt.Rows[i][1])*(height-2*top)/highPoint*1; barOrigin.Y=height-barHeight-top; g.FillEllipse(newSolidBrush(Color.Blue),barOrigin.X-3,barOrigin.Y-3,6 ,6); pt2[i]=newPointF(barOrigin.X,barOrigin.Y); //顶部 g.DrawString(dt.Rows[i][1].ToString(),newFont("Tahoma",topFontSize),B rushes.Blue, newPointF(barOrigin.X,barOrigin.Y-4*topFontSize/2)); barOrigin.X=barOrigin.X+barWidth; } if(dt.Rows.Count>10) { intdis=dt.Rows.Count/10; for(inti=0;i { if(i%dis==0) { g.DrawLine(newPen(Color.Blue,2),newPointF(left+(i+1)*barWidth,height- top+5), newPointF(left+(i+1)*barWidth,height-top-3)); //底部 g.DrawString(dt.Rows[i][2].ToString(),newFont("Tahoma",bottomFontSize ), Brushes.Black, newPointF(left+(i+1)*barWidth,height-top)); } else { g.DrawLine(newPen(Color.Gray,1),newPointF(left+(i+1)*barWidth,height- top+3), newPointF(left+(i+1)*barWidth,height-top-3)); } } else { for(inti=0;i { g.DrawLine(newPen(Color.Gray,1),newPointF(left+(i+1)*barWidth,height-top +3), newPointF(left+(i+1)*barWidth,height-top-3)); } } //绘制曲线 g.DrawLines(newPen(newSolidBrush(Color.Red),1),pt1); g.DrawLines(newPen(newSolidBrush(Color.Blue),1),pt2); //设置边 g.DrawLine(newPen(Color.Blue,2),newPoint(left,top), newPoint(left,height-top)); g.DrawLine(newPen(Color.Blue,2),newPoint(left,height-top), newPoint(left+width,height-top)); g.Dispose(); returnbm; } catch { returnbm; } }