当前位置: 代码迷 >> ASP >> 浅谈ASP.NET表格控件
  详细解决方案

浅谈ASP.NET表格控件

热度:171   发布时间:2012-11-05 09:35:12.0
浅谈ASP.NET报表控件

浅谈ASP.NET报表控件

作者:管理员 来源:51CTO 浏览:360 发布时间:2010-6-19 0:26:07

OWC似乎使用者居多,但看见有网友在帖中抱怨OWC在使用时需要许可证书,于是将其排除,我可不想BOSS在看报表时弹出一个“没有许可证书”的窗口。

接着找到了ComponentOne的Web chart做出的各种效果图,效果极佳。我一下子被迷住了,决定就是它,于是马不停蹄的下了最新版100多M的安装文件,又花一上午找了个可以用的注册码,再找了篇教程,OK万事俱备,兴匆匆的开始写代码。需求不复杂,在一个页面上画出多条折线图即可,根据以往经验,这种大型商业共享软件在官方站点上看两个Example顶多一小时绝对能搞定的。但结果让我非常的恼火,官方的Example简单的它偏不提供,弄些什么AJAX的,一大堆实现辅助效果的代码,你要找的核心的那三四行打死也找不到,好像就怕你一下子把它的Example看明白了。历来讨厌这种软件。于是卸载、删除......等全部弄完,一天过去了。

还好,后来找到了Web Chart。代码超简洁,效果也还不错,写些简单的ASP.NET报表控件示例供大家参考:

ASP.NET报表控件一、折线图

折线图

  1. //引用命名空间 ?
  2. using?WebChart; ?
  3. ?
  4. //定义一个颜色数组,供循环时为不同的记录填充不同的颜色 ?
  5. private?string[]?myColor?=?new?string[] ?
  6. { ?
  7. ????"Tomato",//西红柿 ?
  8. ????"Black", ?
  9. ????"Gold", ?
  10. ????"Blue", ?
  11. ????"Green", ?
  12. ????"Orange", ?
  13. ????"Pink",//粉红 ?
  14. ????"Violet",//紫罗兰 ?
  15. ????"Orchid",//淡紫色 ?
  16. ????"Lime",//亮绿 ?
  17. ????"Tan",//茶色 ?
  18. ????"Red", ?
  19. ????"Navy"//橘红 ?
  20. }; ?
  21. ?
  22. //用静态方式示例了画出一条两个点的最简单折线.实际项目据此做循环而以. ?
  23. private?void?doIt() ?
  24. { ?
  25. ????//创建折线对象 ?
  26. ????LineChart?myChart?=?new?LineChart(); ?
  27. ????//为折线填充颜色 ?
  28. ????myChart.Line.Color?=?Color.FromName(myColor[0]); ?
  29. ????myChart.Fill.Color?=?Color.FromName(myColor[0]); ?
  30. ????myChart.LineMarker?=?new?DiamondLineMarker(8,?Color.FromName(myColor[0]),?Color.FromName(myColor[0])); ?
  31. ????//图例说明 ?
  32. ????myChart.Legend?=?"折线一"; ?
  33. ????//添加第一个点,参数一为x座标上的名称,参数二为y座标上的值 ?
  34. ????myChart.Data.Add(new?ChartPoint("一",?float.Parse("100"))); ?
  35. ????//添加第二个点 ?
  36. ????myChart.Data.Add(new?ChartPoint("二",?float.Parse("200"))); ?
  37. ????//chart为控件ID ?
  38. ????this.chart.Charts.Add(myChart); ?
  39. ????this.chart.RedrawChart(); ?
  40. }?

ASP.NET报表控件二、柱状图

柱状图

  1. //颜色数组 ?
  2. private?string[]?myColor?=?new?string[] ?
  3. { ?
  4. ????"Fuchsia", ?
  5. ????"Black", ?
  6. ????"Gold", ?
  7. ????"Blue", ?
  8. ????"HotPink", ?
  9. ????"Orange", ?
  10. ????"Peru", ?
  11. ????"DodgerBlue", ?
  12. ????"Lime", ?
  13. ????"Tan", ?
  14. ????"Red", ?
  15. ????"GreenYellow", ?
  16. ????"DarkGreen", ?
  17. ????"DimGray", ?
  18. ????"Orchid" ?
  19. }; ?
  20. ?
  21. //调用该方法生成柱状图 ?
  22. private?void?bindchart() ?
  23. { ?
  24. ????//获取一个DataTable,具体函数略... ?
  25. ????DataTable?dt?=?this.getdt(); ?
  26. ????if?(dt?!=?null) ?
  27. ????{ ?
  28. ????????if?(dt.Rows.Count?>?0) ?
  29. ????????{ ?
  30. ????????????//遍历DataTable为每条记录生成一个柱状 ?
  31. ????????????for?(int?i?=?0;?i?<?dt.Rows.Count;?i++) ?
  32. ????????????{ ?
  33. ????????????????//创建对象 ?
  34. ????????????????ColumnChart?mychart?=?new?ColumnChart(); ?
  35. ????????????????//设置柱子宽度 ?
  36. ????????????????mychart.MaxColumnWidth?=?48; ?
  37. ????????????????//颜色 ?
  38. ????????????????mychart.Fill.Color?=?Color.FromName(this.myColor[i]); ?
  39. ????????????????//在柱子上显示数量 ?
  40. ????????????????mychart.DataLabels.Visible?=?true; ?
  41. ????????????????//数量的字体 ?
  42. ????????????????mychart.DataLabels.Font?=?new?Font("Verdana",?14); ?
  43. ????????????????//添加 ?
  44. ????????????????mychart.Data.Add(new?ChartPoint("",?float.Parse(dt.Rows[i]["num"].ToString()))); ?
  45. ????????????????//备注 ?
  46. ????????????????mychart.Legend?=?dt.Rows[i]["name"].ToString(); ?
  47. ????????????????this.chart.Charts.Add(mychart); ?
  48. ????????????} ?
  49. ????????????//辅助设置 ?
  50. ????????????//背景色 ?
  51. ????????????chart.Background.Color?=?Color.FromArgb(165,?0,?16); ?
  52. ????????????chart.YAxisFont.ForeColor?=?Color.FromArgb(165,?0,?16); ?
  53. ????????????chart.XAxisFont.ForeColor?=?Color.FromArgb(165,?0,?16); ?
  54. ????????????//内部线条 ?
  55. ????????????chart.Border.Color?=?Color.FromArgb(200,?200,?200); ?
  56. ????????????//边框样式 ?
  57. ????????????chart.BorderStyle?=?BorderStyle.None; ?
  58. ????????????//y最大值 ?
  59. ????????????double?max?=?double.Parse(dt.Compute("MAX(num)","").ToString()); ?
  60. ????????????//递增值 ?
  61. ????????????int?intv?=?2; ?
  62. ?
  63. ????????????//数量小于16的情况 ?
  64. ????????????if?(max?<?16) ?
  65. ????????????{ ?
  66. ????????????????max?=?16; ?
  67. ????????????} ?
  68. ????????????//大于16的情况 ?
  69. ????????????else ?
  70. ????????????{ ?
  71. ????????????????intintv?=?int.Parse(Math.Ceiling(max/8).ToString()); ?
  72. ????????????????max?+=?intv; ?
  73. ????????????} ?
  74. ?
  75. ????????????//设置Y轴终点值 ?
  76. ????????????chart.YCustomEnd?=?int.Parse(max.ToString()); ?
  77. ????????????//y递增值 ?
  78. ????????????chart.YValuesInterval?=?intv; ?
  79. ?
  80. ????????????//生成 ?
  81. ????????????this.chart.RedrawChart(); ?
  82. ????????} ?
  83. ????} ?
  84. }?

Web Chart为免费ASP.NET报表控件,需要的朋友可去官方地址下载:http://www.carlosag.net/Tools/WebChart/Default.aspx,上面还有很多效果和代码示例。

  相关解决方案