C#绘制曲线图和柱状图的方法

 
  1. 在我们程序开发的过程中经常会需要绘制曲线图和柱状图等,尤其是在做统计功能时。但是有时候我们有觉得没有必要使用第三方控件(例如:ZedGraph等),这是我们可以自己编写代码来实现这些图形绘制的功能。以下是我在开发过程中所使用过的两段代码,现共享大家,希望能给大家带来一定的帮助,如有不妥敬请斧正!
  2. 1.柱状图,效果图如下
  3. 代码如下:
  4. 注意:请注意参数 chartTable 图形里的一些元素需要从chartTable里面取。具体请查看代码。
  5.         //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集  
  6.         public Image Render(string title, int width, int height, DataTable chartTable) 
  7.         { 
  8.             Bitmap bm = new Bitmap(width, height); 
  9.             Graphics g = Graphics.FromImage(bm); 
  10.             g.Clear(Color.White); 
  11.             DataTable dt = chartTable; 
  12.             const int top = 30; 
  13.             const int left = 35; 
  14.             if (width < left * 2 || height < top * 2) 
  15.             { 
  16.                 g.DrawString("绘图区域太小"new Font("Tahoma", 8), 
  17.                     Brushes.Blue, new PointF(0, 0)); 
  18.                 return bm; 
  19.             } 
  20.             //计算最高的点  
  21.             float highPoint = 1; 
  22.             foreach (DataRow dr in dt.Rows) 
  23.             { 
  24.                if (highPoint < Convert.ToSingle(dr[0])) 
  25.                 { 
  26.                     highPoint = Convert.ToSingle(dr[0]); 
  27.                 } 
  28.                 if (highPoint < Convert.ToSingle(dr[1])) 
  29.                 { 
  30.                     highPoint = Convert.ToSingle(dr[1]); 
  31.                 } 
  32.             } 
  33.             try 
  34.             { 
  35.                 //画大标题  
  36.                 g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(2, 2)); 
  37.                 StringFormat drawFormat = new StringFormat(); 
  38.                 drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; 
  39.                 g.DrawString("[红--" + dt.Columns[0].ToString() + "]"new Font("Tahoma", 8), 
  40.                     Brushes.Red, new PointF(2, top), drawFormat); 
  41.                 g.DrawString("[蓝--" + dt.Columns[1].ToString() + "]"new Font("Tahoma", 8), 
  42.                     Brushes.Blue, new PointF(17, top), drawFormat); 
  43.                 //画条形图  
  44.                 float barWidth = (Convert.ToSingle(width) - left) / (dt.Rows.Count * 3 + 1); 
  45.                 PointF barOrigin = new PointF(left + barWidth, 0); 
  46.                 float barHeight = dt.Rows.Count; 
  47.                 float topFontSize = (barWidth / highPoint.ToString().Length); 
  48.                  
  49.                 if (topFontSize > 2*top/3) 
  50.                 { 
  51.                     topFontSize = 2*top/3; 
  52.                 } 
  53.                 if (topFontSize < 5) 
  54.                 { 
  55.                     topFontSize = 5; 
  56.                 } 
  57.                 for (int i = 0; i < dt.Rows.Count; i++) 
  58.                 { 
  59.                     //底部字体的大小 
  60.                     float bottomFontSize = (2 * barWidth / dt.Rows[i][2].ToString().Length) + 2; 
  61.                     if (bottomFontSize > 2 * top / 3) 
  62.                     { 
  63.                         bottomFontSize = 2 * top / 3; 
  64.                     } 
  65.                     barHeight = Convert.ToSingle(dt.Rows[i][0]) * (height - 2 * top) / highPoint * 1; 
  66.                     barOrigin.Y = height - barHeight - top; 
  67.                     g.FillRectangle(new SolidBrush(Color.Red), barOrigin.X, barOrigin.Y, barWidth, barHeight); 
  68.                     //柱状图底部 
  69.                     g.DrawString(dt.Rows[i][2].ToString(), new Font("Tahoma", bottomFontSize), Brushes.Black, 
  70.                         new PointF(barOrigin.X, height - top)); 
  71.                     //柱状图顶部 
  72.                     g.DrawString(dt.Rows[i][0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red, 
  73.                         new PointF(barOrigin.X, barOrigin.Y - 3*topFontSize/2)); 
  74.                     barOrigin.X = barOrigin.X + barWidth; 
  75.                     barHeight = Convert.ToSingle(dt.Rows[i][1]) * (height - 2 * top) / highPoint * 1; 
  76.                     barOrigin.Y = height - barHeight - top; 
  77.                     g.FillRectangle(new SolidBrush(Color.Blue), barOrigin.X, barOrigin.Y, barWidth,  
  78. barHeight); 
  79.                     //柱状图顶部 
  80.                     g.DrawString(dt.Rows[i][1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue, 
  81.                         new PointF(barOrigin.X, barOrigin.Y - 3 * topFontSize/2)); 
  82.                     barOrigin.X = barOrigin.X + (barWidth * 2); 
  83.                 } 
  84.                 //设置边  
  85.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top), 
  86.                     new Point(left, height - top)); 
  87.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, height - top), 
  88.                     new Point(left + width, height - top)); 
  89.                 g.Dispose(); 
  90.                 return bm; 
  91.             } 
  92.             catch 
  93.             { 
  94.                 return bm; 
  95.             } 
  96.         }
  97. 2.线状图,效果如下:
  98. 代码如下: 
  99. 注意:请注意参数 chartTable 图形里的一些元素需要从chartTable里面取。具体请查看代码。
  100.         //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集  
  101.         public Image Render(string title, int width, int height, DataTable chartTable) 
  102.         { 
  103.             Bitmap bm = new Bitmap(width, height); 
  104.             Graphics g = Graphics.FromImage(bm); 
  105.             g.Clear(Color.White); 
  106.             const int top = 30; 
  107.             const int left = 35; 
  108.             if (width < left * 2 || height < top * 2) 
  109.             { 
  110.                 g.DrawString("绘图区域太小" ,new Font("Tahoma", 8), 
  111.                     Brushes.Blue, new PointF(0, 0)); 
  112.                 return bm; 
  113.             } 
  114.             if (chartTable == null
  115.             { 
  116.                 g.DrawString("没有数据"new Font("Tahoma", 7), 
  117.                     Brushes.Blue, new PointF(0, 0)); 
  118.                 return bm; 
  119.             } 
  120.             DataTable dt = chartTable; 
  121.             //计算最高的点  
  122.             float highPoint = 1; 
  123.             foreach (DataRow dr in dt.Rows) 
  124.             { 
  125.                 if (highPoint < Convert.ToSingle(dr[0])) 
  126.                 { 
  127.                     highPoint = Convert.ToSingle(dr[0]); 
  128.                 } 
  129.                 if (highPoint < Convert.ToSingle(dr[1])) 
  130.                 { 
  131.                     highPoint = Convert.ToSingle(dr[1]); 
  132.                 } 
  133.             } 
  134.             //建立一个Graphics对象实例  
  135.             try 
  136.             { 
  137.                 //画大标题  
  138.                 g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(2, 2)); 
  139.                 StringFormat drawFormat = new StringFormat(); 
  140.                 drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; 
  141.                 g.DrawString("[红--" + dt.Columns[0].ToString() + "]"new Font("Tahoma", 8), 
  142.                     Brushes.Red, new PointF(2, top), drawFormat); 
  143.                 g.DrawString("[蓝--" + dt.Columns[1].ToString() + "]"new Font("Tahoma", 8), 
  144.                     Brushes.Blue, new PointF(17, top), drawFormat); 
  145.                 //画条形图  
  146.                 float barWidth = (Convert.ToSingle(width) - left) / (dt.Rows.Count + 1); 
  147.                 PointF barOrigin = new PointF(left + barWidth , 0); 
  148.                 float barHeight = dt.Rows.Count; 
  149.                 float topFontSize = 7; 
  150.                 float bottomFontSize = 7; 
  151.                 PointF[] pt1 = new PointF[dt.Rows.Count]; 
  152.                 PointF[] pt2 = new PointF[dt.Rows.Count]; 
  153.                 for (int i = 0; i < dt.Rows.Count; i++) 
  154.                 { 
  155.                     //底部字体的大小 
  156.                     barHeight = Convert.ToSingle(dt.Rows[i][0]) * (height - 2 * top) / highPoint * 1; 
  157.                     barOrigin.Y = height - barHeight - top; 
  158.                     g.FillEllipse(new SolidBrush(Color.Red), barOrigin.X - 3, barOrigin.Y - 3, 6, 6); 
  159.                     pt1[i] = new PointF(barOrigin.X, barOrigin.Y); 
  160.                     //顶部 
  161.                     g.DrawString(dt.Rows[i][0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red, 
  162.                         new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2)); 
  163.                     barHeight = Convert.ToSingle(dt.Rows[i][1]) * (height - 2 * top) / highPoint * 1; 
  164.                     barOrigin.Y = height - barHeight - top; 
  165.                     g.FillEllipse(new SolidBrush(Color.Blue), barOrigin.X - 3, barOrigin.Y - 3, 6, 6); 
  166.                     pt2[i] = new PointF(barOrigin.X, barOrigin.Y); 
  167.                     //顶部 
  168.                     g.DrawString(dt.Rows[i][1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue, 
  169.                         new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2)); 
  170.                     barOrigin.X = barOrigin.X + barWidth; 
  171.                 } 
  172.                 if (dt.Rows.Count > 10) 
  173.                 { 
  174.                     int dis = dt.Rows.Count / 10; 
  175.                     for (int i = 0; i < dt.Rows.Count; i++) 
  176.                     { 
  177.                         if (i % dis == 0) 
  178.                         { 
  179.                             g.DrawLine(new Pen(Color.Blue, 2), new PointF(left + (i + 1) * barWidth, height -  
  180. top + 5), 
  181.                                 new PointF(left + (i + 1) * barWidth, height - top - 3)); 
  182.                             //底部 
  183.                             g.DrawString(dt.Rows[i][2].ToString(), new Font("Tahoma", bottomFontSize),  
  184. Brushes.Black, 
  185.                                 new PointF(left + (i + 1) * barWidth, height - top)); 
  186.                         } 
  187.                         else 
  188.                         { 
  189.                             g.DrawLine(new Pen(Color.Gray, 1), new PointF(left + (i+1) * barWidth, height -  
  190. top + 3), 
  191.                                 new PointF(left + (i+1) * barWidth, height - top - 3)); 
  192.                         } 
  193.                     } 
  194.                 } 
  195.                 else 
  196.                 { 
  197.                     for (int i = 0; i < dt.Rows.Count; i++) 
  198.                     { 
  199.                         g.DrawLine(new Pen(Color.Gray, 1), new PointF(left + (i + 1) * barWidth, height - top  
  200. + 3), 
  201.                             new PointF(left + (i + 1) * barWidth, height - top - 3)); 
  202.                     } 
  203.                 } 
  204.                 //绘制曲线 
  205.                 g.DrawLines(new Pen(new SolidBrush(Color.Red), 1), pt1); 
  206.                 g.DrawLines(new Pen(new SolidBrush(Color.Blue),1), pt2); 
  207.                 //设置边  
  208.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top), 
  209.                     new Point(left, height - top)); 
  210.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, height - top), 
  211.                     new Point(left + width, height - top)); 
  212.                 g.Dispose(); 
  213.                 return bm; 
  214.             } 
  215.             catch 
  216.             { 
  217.                 return bm; 
  218.             } 
  219.         }
  220. 杜思波的技术讨论帖子
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值