C#绘制曲线图和柱状图

在我们程序开发的过程中经常会需要绘制曲线图和柱状图等,尤其是在做统计功能时。但是有时候我们有觉得没有必要使用第三方控件(例如:ZedGraph等),这是我们可以自己编写代码来实现这些图形绘制的功能。以下是我在开发过程中所使用过的两段代码,现共享大家,希望能给大家带来一定的帮助,如有不妥敬请斧正!
1.柱状图,效果图如下

代码如下:

注意:请注意参数 chartTable 图形里的一些元素需要从chartTable里面取。具体请查看代码。

  1.         //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集 
  2.         public Image Render(string title, int width, int height, DataTable chartTable)
  3.         {
  4.             Bitmap bm = new Bitmap(width, height);
  5.             Graphics g = Graphics.FromImage(bm);
  6.             g.Clear(Color.White);
  7.             DataTable dt = chartTable;
  8.             const int top = 30;
  9.             const int left = 35;
  10.             if (width < left * 2 || height < top * 2)
  11.             {
  12.                 g.DrawString("绘图区域太小"new Font("Tahoma", 8),
  13.                     Brushes.Blue, new PointF(0, 0));
  14.                 return bm;
  15.             }
  16.             //计算最高的点 
  17.             float highPoint = 1;
  18.             foreach (DataRow dr in dt.Rows)
  19.             {
  20.                if (highPoint < Convert.ToSingle(dr[0]))
  21.                 {
  22.                     highPoint = Convert.ToSingle(dr[0]);
  23.                 }
  24.                 if (highPoint < Convert.ToSingle(dr[1]))
  25.                 {
  26.                     highPoint = Convert.ToSingle(dr[1]);
  27.                 }
  28.             }
  29.             try
  30.             {
  31.                 //画大标题 
  32.                 g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(2, 2));
  33.                 StringFormat drawFormat = new StringFormat();
  34.                 drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
  35.                 g.DrawString("[红--" + dt.Columns[0].ToString() + "]"new Font("Tahoma", 8),
  36.                     Brushes.Red, new PointF(2, top), drawFormat);
  37.                 g.DrawString("[蓝--" + dt.Columns[1].ToString() + "]"new Font("Tahoma", 8),
  38.                     Brushes.Blue, new PointF(17, top), drawFormat);
  39.                 //画条形图 
  40.                 float barWidth = (Convert.ToSingle(width) - left) / (dt.Rows.Count * 3 + 1);
  41.                 PointF barOrigin = new PointF(left + barWidth, 0);
  42.                 float barHeight = dt.Rows.Count;
  43.                 float topFontSize = (barWidth / highPoint.ToString().Length);
  44.                 
  45.                 if (topFontSize > 2*top/3)
  46.                 {
  47.                     topFontSize = 2*top/3;
  48.                 }
  49.                 if (topFontSize < 5)
  50.                 {
  51.                     topFontSize = 5;
  52.                 }
  53.                 for (int i = 0; i < dt.Rows.Count; i++)
  54.                 {
  55.                     //底部字体的大小
  56.                     float bottomFontSize = (2 * barWidth / dt.Rows[i][2].ToString().Length) + 2;
  57.                     if (bottomFontSize > 2 * top / 3)
  58.                     {
  59.                         bottomFontSize = 2 * top / 3;
  60.                     }
  61.                     barHeight = Convert.ToSingle(dt.Rows[i][0]) * (height - 2 * top) / highPoint * 1;
  62.                     barOrigin.Y = height - barHeight - top;
  63.                     g.FillRectangle(new SolidBrush(Color.Red), barOrigin.X, barOrigin.Y, barWidth, barHeight);
  64.                     //柱状图底部
  65.                     g.DrawString(dt.Rows[i][2].ToString(), new Font("Tahoma", bottomFontSize), Brushes.Black,
  66.                         new PointF(barOrigin.X, height - top));
  67.                     //柱状图顶部
  68.                     g.DrawString(dt.Rows[i][0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red,
  69.                         new PointF(barOrigin.X, barOrigin.Y - 3*topFontSize/2));
  70.                     barOrigin.X = barOrigin.X + barWidth;
  71.                     barHeight = Convert.ToSingle(dt.Rows[i][1]) * (height - 2 * top) / highPoint * 1;
  72.                     barOrigin.Y = height - barHeight - top;
  73.                     g.FillRectangle(new SolidBrush(Color.Blue), barOrigin.X, barOrigin.Y, barWidth, 
  74. barHeight);
  75.                     //柱状图顶部
  76.                     g.DrawString(dt.Rows[i][1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue,
  77.                         new PointF(barOrigin.X, barOrigin.Y - 3 * topFontSize/2));
  78.                     barOrigin.X = barOrigin.X + (barWidth * 2);
  79.                 }
  80.                 //设置边 
  81.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top),
  82.                     new Point(left, height - top));
  83.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, height - top),
  84.                     new Point(left + width, height - top));
  85.                 g.Dispose();
  86.                 return bm;
  87.             }
  88.             catch
  89.             {
  90.                 return bm;
  91.             }
  92.         }

2.线状图,效果如下:

代码如下:

注意:请注意参数 chartTable 图形里的一些元素需要从chartTable里面取。具体请查看代码。

  1.         //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集 
  2.         public Image Render(string title, int width, int height, DataTable chartTable)
  3.         {
  4.             Bitmap bm = new Bitmap(width, height);
  5.             Graphics g = Graphics.FromImage(bm);
  6.             g.Clear(Color.White);
  7.             const int top = 30;
  8.             const int left = 35;
  9.             if (width < left * 2 || height < top * 2)
  10.             {
  11.                 g.DrawString("绘图区域太小" ,new Font("Tahoma", 8),
  12.                     Brushes.Blue, new PointF(0, 0));
  13.                 return bm;
  14.             }
  15.             if (chartTable == null)
  16.             {
  17.                 g.DrawString("没有数据"new Font("Tahoma", 7),
  18.                     Brushes.Blue, new PointF(0, 0));
  19.                 return bm;
  20.             }
  21.             DataTable dt = chartTable;
  22.             //计算最高的点 
  23.             float highPoint = 1;
  24.             foreach (DataRow dr in dt.Rows)
  25.             {
  26.                 if (highPoint < Convert.ToSingle(dr[0]))
  27.                 {
  28.                     highPoint = Convert.ToSingle(dr[0]);
  29.                 }
  30.                 if (highPoint < Convert.ToSingle(dr[1]))
  31.                 {
  32.                     highPoint = Convert.ToSingle(dr[1]);
  33.                 }
  34.             }
  35.             //建立一个Graphics对象实例 
  36.             try
  37.             {
  38.                 //画大标题 
  39.                 g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(2, 2));
  40.                 StringFormat drawFormat = new StringFormat();
  41.                 drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
  42.                 g.DrawString("[红--" + dt.Columns[0].ToString() + "]"new Font("Tahoma", 8),
  43.                     Brushes.Red, new PointF(2, top), drawFormat);
  44.                 g.DrawString("[蓝--" + dt.Columns[1].ToString() + "]"new Font("Tahoma", 8),
  45.                     Brushes.Blue, new PointF(17, top), drawFormat);
  46.                 //画条形图 
  47.                 float barWidth = (Convert.ToSingle(width) - left) / (dt.Rows.Count + 1);
  48.                 PointF barOrigin = new PointF(left + barWidth , 0);
  49.                 float barHeight = dt.Rows.Count;
  50.                 float topFontSize = 7;
  51.                 float bottomFontSize = 7;
  52.                 PointF[] pt1 = new PointF[dt.Rows.Count];
  53.                 PointF[] pt2 = new PointF[dt.Rows.Count];
  54.                 for (int i = 0; i < dt.Rows.Count; i++)
  55.                 {
  56.                     //底部字体的大小
  57.                     barHeight = Convert.ToSingle(dt.Rows[i][0]) * (height - 2 * top) / highPoint * 1;
  58.                     barOrigin.Y = height - barHeight - top;
  59.                     g.FillEllipse(new SolidBrush(Color.Red), barOrigin.X - 3, barOrigin.Y - 3, 6, 6);
  60.                     pt1[i] = new PointF(barOrigin.X, barOrigin.Y);
  61.                     //顶部
  62.                     g.DrawString(dt.Rows[i][0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red,
  63.                         new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2));
  64.                     barHeight = Convert.ToSingle(dt.Rows[i][1]) * (height - 2 * top) / highPoint * 1;
  65.                     barOrigin.Y = height - barHeight - top;
  66.                     g.FillEllipse(new SolidBrush(Color.Blue), barOrigin.X - 3, barOrigin.Y - 3, 6, 6);
  67.                     pt2[i] = new PointF(barOrigin.X, barOrigin.Y);
  68.                     //顶部
  69.                     g.DrawString(dt.Rows[i][1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue,
  70.                         new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2));
  71.                     barOrigin.X = barOrigin.X + barWidth;
  72.                 }
  73.                 if (dt.Rows.Count > 10)
  74.                 {
  75.                     int dis = dt.Rows.Count / 10;
  76.                     for (int i = 0; i < dt.Rows.Count; i++)
  77.                     {
  78.                         if (i % dis == 0)
  79.                         {
  80.                             g.DrawLine(new Pen(Color.Blue, 2), new PointF(left + (i + 1) * barWidth, height - 
  81. top + 5),
  82.                                 new PointF(left + (i + 1) * barWidth, height - top - 3));
  83.                             //底部
  84.                             g.DrawString(dt.Rows[i][2].ToString(), new Font("Tahoma", bottomFontSize), 
  85. Brushes.Black,
  86.                                 new PointF(left + (i + 1) * barWidth, height - top));
  87.                         }
  88.                         else
  89.                         {
  90.                             g.DrawLine(new Pen(Color.Gray, 1), new PointF(left + (i+1) * barWidth, height - 
  91. top + 3),
  92.                                 new PointF(left + (i+1) * barWidth, height - top - 3));
  93.                         }
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     for (int i = 0; i < dt.Rows.Count; i++)
  99.                     {
  100.                         g.DrawLine(new Pen(Color.Gray, 1), new PointF(left + (i + 1) * barWidth, height - top 
  101. + 3),
  102.                             new PointF(left + (i + 1) * barWidth, height - top - 3));
  103.                     }
  104.                 }
  105.                 //绘制曲线
  106.                 g.DrawLines(new Pen(new SolidBrush(Color.Red), 1), pt1);
  107.                 g.DrawLines(new Pen(new SolidBrush(Color.Blue),1), pt2);
  108.                 //设置边 
  109.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top),
  110.                     new Point(left, height - top));
  111.                 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, height - top),
  112.                     new Point(left + width, height - top));
  113.                 g.Dispose();
  114.                 return bm;
  115.             }
  116.             catch
  117.             {
  118.                 return bm;
  119.             }
  120.         }

补充一句:转载的朋友请一定注明出处谢谢!半支烟阿杰
http://blog.csdn.net/gisfarmer/

你可能感兴趣的:(C#,null,图形,程序开发)