首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 虚拟主机评测对比
如何在C#的WinForm中制作饼状图和柱状图-.NET教程,C#语言
作者:网友供稿 点击:37
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
当我们的软件需要各种饼状图和柱状图来表示数据时,我们或许会想到用offices中的图形控件或是第三方控件,但现在的第三方控件大都需要注册,有些免费的控件会有开发商的标记等。而对于使用offices的图形控件来说,并不能在程序中得于很好控制,其使用的简易程度也较低,所以在这我给出在c#中使用gdi+实现饼状图和柱状图跟数据库联接显示数据的方法。 
using system; 
using system.io;//用于文件存取 
using system.data;//用于数据访问 
using system.drawing;//提供画gdi+图形的基本功能 
using system.drawing.text;//提供画gdi+图形的高级功能 
using system.drawing.drawing2d;//提供画高级二维,矢量图形功能 
using system.drawing.imaging;//提供画gdi+图形的高级功能 
namespace baselayer

 public class piechart 
 { 
  public piechart() 
  { 
  } 
//render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集要表示出来的数据
  public image render(string title, string subtitle, int width, int height, dataset chartdata,int dataline) 
  { 
   const int side_length = 400; 
   const int pie_diameter = 200; 
   datatable dt = chartdata.tables[0]; 
   
   //通过输入参数,取得饼图中的总基数 
   float sumdata = 0; 
   foreach(datarow dr in dt.rows) 
   { 
    sumdata += convert.tosingle(dr[dataline]); 
   } 
   //产生一个image对象,并由此产生一个graphics对象 
   bitmap bm = new bitmap(width,height); 
   graphics g = graphics.fromimage(bm); 
   //设置对象g的属性 
   g.scaletransform((convert.tosingle(width))/side_length,(convert.tosingle(height))/side_length); 
   g.smoothingmode = smoothingmode.default; 
   g.textrenderinghint = textrenderinghint.antialias; 
   
   //画布和边的设定 
   g.clear(color.white); 
   g.drawrectangle(pens.black,0,0,side_length-1,side_length-1); 
   //画饼图标题 
   g.drawstring(title,new font("tahoma",14),brushes.black,new pointf(5,5)); 
   //画饼图的图例 
   g.drawstring(subtitle,new font("tahoma",12),brushes.black,new pointf(7,35)); 
   //画饼图 
   float curangle = 0; 
   float totalangle = 0; 
   for(int i=0;i<dt.rows.count;i++) 
   { 
    curangle = convert.tosingle(dt.rows[i][dataline]) / sumdata * 360; 
   
    g.fillpie(new solidbrush(chartutil.getchartitemcolor(i)),100,65,pie_diameter,pie_diameter,totalangle,curangle); 
    g.drawpie(pens.black,100,65,pie_diameter,pie_diameter,totalangle,curangle); 
    totalangle += curangle; 
   } 
   //画图例框及其文字 
   g.drawrectangle(pens.black,200,300,199,99); 
   g.drawstring("图表说明",new font("tahoma",12,fontstyle.bold),brushes.black,new pointf(200,300)); 
   
   //画图例各项 
   pointf boxorigin = new pointf(210,330); 
   pointf textorigin = new pointf(235,326); 
   float percent = 0; 
   for(int i=0;i<dt.rows.count;i++) 
   { 
    g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),boxorigin.x,boxorigin.y,20,10); 
    g.drawrectangle(pens.black,boxorigin.x,boxorigin.y,20,10); 
    percent = convert.tosingle(dt.rows[i][dataline]) / sumdata * 100; 
    g.drawstring(dt.rows[i][1].tostring() + " - " + dt.rows[i][0].tostring() + " (" + percent.tostring("0") + "%)",new font("tahoma",10),brushes.black,textorigin); 
    boxorigin.y += 15; 
    textorigin.y += 15; 
   } 
   //回收资源 
   g.dispose(); 
   return (image) bm; 
   
  } 
 } 
   
 //画条形图 
 public class barchart 
 { 
  public barchart() 
  { 
  } 
//render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集
  public image render(string title, string subtitle, int width, int height, dataset chartdata) 
  { 
   const int side_length = 400; 
   const int chart_top = 75; 
   const int chart_height = 200; 
   const int chart_left = 50; 
   const int chart_width = 300; 
   datatable dt = chartdata.tables[0]; 
   
   //计算最高的点 
   float highpoint = 0; 
   foreach(datarow dr in dt.rows) 
   { 
    if(highpoint<convert.tosingle(dr[0])) 
    { 
     highpoint = convert.tosingle(dr[0]); 
    } 
   } 
   //建立一个graphics对象实例 
   bitmap bm = new bitmap(width,height); 
   try
   {
    graphics g = graphics.fromimage(bm); 
    //设置条图图形和文字属性 
    g.scaletransform((convert.tosingle(width))/side_length,(convert.tosingle(height))/side_length); 
    g.smoothingmode = smoothingmode.default; 
    g.textrenderinghint = textrenderinghint.antialias; 
   
    //设定画布和边 
    g.clear(color.white); 
    g.drawrectangle(pens.black,0,0,side_length-1,side_length-1); 
    //画大标题 
    g.drawstring(title,new font("tahoma",14),brushes.black,new pointf(5,5)); 
    //画小标题 
    g.drawstring(subtitle,new font("tahoma",12),brushes.black,new pointf(7,35)); 
    //画条形图 
    float barwidth = chart_width / (dt.rows.count * 2); 
    pointf barorigin = new pointf(chart_left + (barwidth / 2),0); 
    float barheight = dt.rows.count; 
    for(int i=0;i<dt.rows.count;i++) 
    { 
     barheight = convert.tosingle(dt.rows[i][0]) * 200 / highpoint * 1; 
     barorigin.y = chart_top + chart_height - barheight; 
     g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),barorigin.x,barorigin.y,barwidth,barheight); 
     barorigin.x = barorigin.x + (barwidth * 2); 
    } 
    //设置边 
    g.drawline(new pen(color.black,2),new point(chart_left,chart_top),new point(chart_left,chart_top + chart_height)); 
    g.drawline(new pen(color.black,2),new point(chart_left,chart_top + chart_height),new point(chart_left + chart_width,chart_top + chart_height)); 
    //画图例框和文字 
    g.drawrectangle(new pen(color.black,1),200,300,199,99); 
    g.drawstring("图表说明",new font("tahoma",12,fontstyle.bold),brushes.black,new pointf(200,300)); 
   
    //画图例 
    pointf boxorigin = new pointf(210,330); 
    pointf textorigin = new pointf(235,326); 
    for(int i=0;i<dt.rows.count;i++) 
    { 
     g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),boxorigin.x,boxorigin.y,20,10); 
     g.drawrectangle(pens.black,boxorigin.x,boxorigin.y,20,10); 
     g.drawstring(dt.rows[i][1].tostring() + " - " + dt.rows[i][0].tostring(),new font("tahoma",10),brushes.black,textorigin); 
     boxorigin.y += 15; 
     textorigin.y += 15; 
    } 
    //输出图形 
    g.dispose(); 
    return bm;
   }
   catch
   {
    return bm;
   }   
  } 
 } 
 public class chartutil 
 { 
  public chartutil() 
  { 
  } 
  public static color getchartitemcolor(int itemindex) 
  { 
   color selectedcolor; 
   switch(itemindex) 
   { 
    case 0: 
     selectedcolor = color.blue; 
     break; 
    case 1: 
     selectedcolor = color.red; 
     break; 
    case 2: 
     selectedcolor = color.yellow; 
     break; 
    case 3: 
     selectedcolor = color.purple; 
     break; 
    default: 
     selectedcolor = color.green; 
     break; 
   } 
   return selectedcolor; 
  } 
 } 

以上是一个完整的winform中制作饼状图和柱状图源程序,大家可以通过以上程序的更改,做出能满足自己程序的需要。
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Asp.Net下导出/导入规则的Excel(.xls)文件-ASP教程,ASP应用
·中国万网,万网
·中资源
·上海火速
·加拿大中文网
·华夏名网
·世纪东方
·商务中国
·中国E动网
·神话网络
最新文章
·photoshop抠图合成特效:飞出照片相框_photoshop教程
·将升级的windows vista驱动还原到原来的版本_windows vista
·photoshop漫画笔刷绘制非主流照片漫画_photoshop教程
·看域名注册信息判断网赚公司真假_网赚技巧
·找坐标:对网站进行坐标定位_站长心得
·如何增加网站pv_站长心得
·google性格一:对活跃度高的网页收录很快_google推广
·googleadsense容易被k的可能性列表_google推广
·价值数万的googlepr作弊方_google推广
·如何抢占人家的“首要意念”_google推广
相关主题
  • 如何在C#中加载自己编写的动态链接库(DLL)-.NET教程,数据库应用
  • 如何在C#中读写INI文件-.NET教程,C#语言
  • 如何在C#中加载自己编写的动态链接库(DLL)-.NET教程,C#语言
  • 如何在C#中读写INI文件-.NET教程,C#语言
  • 如何在C#中实现图片缩放-.NET教程,C#语言
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭