欢迎来到天天文库
浏览记录
ID:37909041
大小:47.58 KB
页数:8页
时间:2019-06-02
《C#绘制曲线图和柱状图》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、在我们程序开发的过程中经常会需要绘制曲线图和柱状图等,尤其是在做统计功能时。但是有时候我们有觉得没有必要使用第三方控件(例如:ZedGraph等),这是我们可以自己编写代码来实现这些图形绘制的功能。以下是我在开发过程中所使用过的两段代码,现共享大家,希望能给大家带来一定的帮助,如有不妥敬请斧正! 1.柱状图,效果图如下 代码如下: 注意:请注意参数chartTable图形里的一些元素需要从chartTable里面取。具体请查看代码。 //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集 p
2、ublicImageRender(stringtitle,intwidth,intheight,DataTablechartTable) { Bitmapbm=newBitmap(width,height); Graphicsg=Graphics.FromImage(bm); g.Clear(Color.White); DataTabledt=chartTable; constinttop=30; constintleft=35; if(width3、4、5、height6、 highPoint=Convert.ToSingle(dr[0]); } if(highPoint7、 StringFormatdrawFormat=newStringFormat(); drawFormat.FormatFlags=StringFormatFlags.DirectionVertical; g.DrawString("[红--"+dt.Columns[0].ToString()+"]",newFont("Tahoma",8), Brushes.Red,newPointF(2,top),drawFormat); g.DrawString("[蓝--"+dt.Co8、lumns[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;9、 floattopFontSize=(barWidth/highPoint.ToString().Length); if(topFontSize>2*top/3) { topFontSize=2*top/3; } if(topFontSize<5) { topFontSize=5; } for(inti=0;i10、大小 floatbottomFontSize=(2*barWidth/dt.Rows[i][2].ToString().Length
3、
4、
5、height6、 highPoint=Convert.ToSingle(dr[0]); } if(highPoint7、 StringFormatdrawFormat=newStringFormat(); drawFormat.FormatFlags=StringFormatFlags.DirectionVertical; g.DrawString("[红--"+dt.Columns[0].ToString()+"]",newFont("Tahoma",8), Brushes.Red,newPointF(2,top),drawFormat); g.DrawString("[蓝--"+dt.Co8、lumns[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;9、 floattopFontSize=(barWidth/highPoint.ToString().Length); if(topFontSize>2*top/3) { topFontSize=2*top/3; } if(topFontSize<5) { topFontSize=5; } for(inti=0;i10、大小 floatbottomFontSize=(2*barWidth/dt.Rows[i][2].ToString().Length
6、 highPoint=Convert.ToSingle(dr[0]); } if(highPoint7、 StringFormatdrawFormat=newStringFormat(); drawFormat.FormatFlags=StringFormatFlags.DirectionVertical; g.DrawString("[红--"+dt.Columns[0].ToString()+"]",newFont("Tahoma",8), Brushes.Red,newPointF(2,top),drawFormat); g.DrawString("[蓝--"+dt.Co8、lumns[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;9、 floattopFontSize=(barWidth/highPoint.ToString().Length); if(topFontSize>2*top/3) { topFontSize=2*top/3; } if(topFontSize<5) { topFontSize=5; } for(inti=0;i10、大小 floatbottomFontSize=(2*barWidth/dt.Rows[i][2].ToString().Length
7、 StringFormatdrawFormat=newStringFormat(); drawFormat.FormatFlags=StringFormatFlags.DirectionVertical; g.DrawString("[红--"+dt.Columns[0].ToString()+"]",newFont("Tahoma",8), Brushes.Red,newPointF(2,top),drawFormat); g.DrawString("[蓝--"+dt.Co
8、lumns[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;
9、 floattopFontSize=(barWidth/highPoint.ToString().Length); if(topFontSize>2*top/3) { topFontSize=2*top/3; } if(topFontSize<5) { topFontSize=5; } for(inti=0;i
10、大小 floatbottomFontSize=(2*barWidth/dt.Rows[i][2].ToString().Length
此文档下载收益归作者所有