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.         }
<script type=text/JavaScript> alimama_pid="mm_10249644_1605763_4930558"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=512; </script> <script src="http://a.alimama.cn/inf.js" type=text/javascript> </script>
书名 版权 前言 第一部分 C#的基本数据类型、数组类型和图形基础第1章 C#语言基础 1.1 数据类型 1.1.1 简单类型 1.1.2 结构类型 1.1.3 枚举类型 1.1.4 数组类型 1.1.5 类类型 1.1.6 类型转换 1.2 类 1.3 接口 1.4 委托与事件 第2章 图形基础 2.1 笔和画刷 2.1.1 Pen类 2.1.2 Brush类 2.2 基本图形形状 2.2.1 点 2.2.2 直线和曲线 2.2.3 矩形、椭圆形和圆弧形 2.2.4 多边形 2.3 颜色 2.4 双倍缓存 第3章 坐标系统和颜色变换 3.1 坐标系统 3.2 颜色变换 第二部分 二维图形的基本算法 第4章 二维矩阵和变换 4.1 矩阵基础和变换 4.2 齐次坐标 4.2.1 齐次坐标的缩放 4.2.2 齐次坐标的平移 4.2.3 齐次坐标的旋转 4.2.4 变换组合 4.2.5 C#矩阵的定义 4.2.6 C#的矩阵操作 4.2.7 C#基本的矩阵变换 4.3 C#图形对象的变换 基本变换 4.4 C#的多对象变换 4.5 文字变换 第5章 二维线形图形 5.1 序列化和反序列化及二维图形的基本框架 5.1.1 C#序列化和反序列化 5.1.2 二维图形的基本框架 5.2 二维图形 5.2.1 简单实例 5.2.2 图例 5.2.3 符号 5.2.4 对数比例 5.2.5 图形的修饰 5.3 阶梯状图 5.4 多Y轴图 第6章 特殊二维图形 6.1 创建柱状图 6.1.1 水平柱状图 6.1.2 垂直柱状图 6.1.3 图形充填柱状图 6.1.4 重叠柱状图 6.2 饼状图 6.3 误差图 6.4 股票图 6.4.1 最高最低收盘价股票图 6.4.2 最高最低开盘收盘价股票图 6.4.3 最高最低价股票图 6.4.4 K线图(阴阳烛图) 6.5 面积图 6.6 综合图 第三部分 三维图形的相关知识及三维图形的实现第7章 三维矩阵和变换 7.1 三维数学概念 7.1.1 操作三维对象 7.1.2 数学结构 7.2 三维的基本矩阵和变换 7.2.1 C#三维点和矩阵的操作 7.2.2 三维的基本变换 7.3 方位角和仰角 7.4 三维图形的特殊坐标系统 7.4.1 球坐标系统 7.4.2 圆柱坐标系统 7.5 特殊坐标的实际应用 7.5.1 球坐标示例 7.5.2 双缓存 第8章 三维图形 8.1 三维图形基础 8.1.1 Point3和Matrix3类 8.1.2 ChartStyle类 8.1.3 坐标轴 8.1.4 网格线 8.1.5 标签 8.2 三维折线图 8.3 三维图形函数包 8.3.1 ChartStyle2D类 8.3.2 Point4类 8.3.3 DataSeries类 8.3.4 ChartFunctions类 8.3.5 DrawChart类 8.4 曲面图的实现 8.4.1 网格图 8.4.2 幕布网格图 8.4.3 瀑布网格图 8.4.4 曲面图 8.5 X-Y平面色彩图 8.6 轮廓图 8.6.1 轮廓图的算法 8.6.2 轮廓图的实现 8.7 组合图 8.7.1 三维体系的X-Y色彩图 8.7.2 三维体系的轮廓图 8.7.3 网格-轮廓组合图 8.7.4 曲面-轮廓组合图 8.7.5 填充曲面-轮廓组合图 8.8 三维柱状图 实现柱状图 8.9 切片图 切片图的实现 第四部分 C#应用微软Office的Excel实现各种二维及三维图形第9章 应用 程序的Excel图表 9.1 Excel和C#间的互操作 9.2 C#应用程序的Excel图表示例 9.2.1 Excel图表对象模型 9.2.2 创建独立的Excel图表 9.2.3 创建嵌入式Excel图表 9.3 更多的Excel图表 9.3.1 柱状图 9.3.2 饼状图 9.3.3 面积图 9.3.4 圆环图 9.3.5 雷达图 9.3.6 股价图 9.3.7 曲面图 9.3.8 颜色映射 9.4 整合Excel图表到Windows Forms应用程序 9.4.1 Windows窗体上的独立Excel图表 9.4.2 Windows窗体上的嵌入式Excel图表 第五部分 实现文件的相关知识 第10章 文件的读/写 10.1 C#文件读/写常用类 10.1.1 System.IO.File类和System.IO.FileI nfo类 10.1.2 System.IO.Directory类和System.Dir ectoryInfo类 10.2 C#基于流的输入/输出 流的继承结构 10.3 文件读/写操作涉及的类 10.4 一些常见的问题及其解决方案 参考文献
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值