C绘制曲线图和柱状图.docxVIP

  • 5
  • 0
  • 约7.67千字
  • 约 8页
  • 2016-09-21 发布于重庆
  • 举报
C绘制曲线图和柱状图

 在我们程序开发的过程中经常会需要绘制曲线图和柱状图等,尤其是在做统计功能时。但是有时候我们有觉得没有必要使用第三方控件(例如: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(widthleft*2||heighttop*2)      {        g.DrawString(绘图区域太小,newFont(Tahoma,8),          Brushes.Blue,newPointF(0,0));        returnbm;      }        //计算最高的点       floathighPoint=1;      foreach(DataRowdrindt.Rows)      {       if(highPointConvert.ToSingle(dr[0]))        {          highPoint=Convert.ToSingle(dr[0]);        }          if(highPointConvert.ToSingle(dr[1]))        {          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(topFontSize2*top/3)        {          topFontSize=2*top/3;        }        if(topFontSize5)        {          topFontSize=5;        }        for(inti=0;idt.Rows.Count;i++)        {          //底部字体的大小          floatbottomFontSize=(2*barWidth/dt.Rows[i][2].ToString().Length)+2;          if(botto

文档评论(0)

1亿VIP精品文档

相关文档