最近作一个项目其中的财务统计部分需要作相关的统计分析,以前见过同事用Siverlight做过一个统计图很绚,可是用户在是用的时候需要首先安装siverlight插件,感觉不爽,另外自己也想尝试一下独立完成该功能,有了这个想法于是想着去自己写统计图控件 ,可没几天老大就阻止了,他说这样做不值得.有专门的公司做这样的控件 ,我们如果把精力花在做控件上会影响项目的进度,想想也对,毕竟术业有专攻。于是在网上找了很多资料看看,发现dotnetCharting是目前公认比较好的统计图控件。使用方法:

  在网上找到最新的dotNetcharting,现在我发现的最新的有5.0,正式版的用的时候灵活性不高很多东西改不了,比如生成后的图片会带控件的原创地址链接等信息,不过没关系网上高手很多,找个破解版的dll。

  下载完成后就可以使用了,两种方法:1 将下载的dll在工具箱中添加,以后就可以拖着用了。2。在你需要的项目中添加dll使用的时候用代码去添加,我采取的方式是二者结合,将控件添加到工具箱用的时候拖进来 ,用代码动态改变它的属性和数据源。

    网上有一个彭建军写的一个饼图,曲线图,柱状图的类,个人感觉这是一个基本操作类,就实际应用而言需要根据实际情况修改,加入相应的实体类,对结构也需要做进一步的修改,这样在开发的时候会大大改善代码冗余,同时符合面向对象的要求。另外曲线图部分此类只能实现一个数据源的情况,如果需要画多条线需要添加Series。总的来说建军的这个类对于初学者来说是一个很好的帮助材料可以学会如何使用dotNetcharting 控件。

  他的代码如下,我的代码由于涉及公司机密不能写出来。
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using dotnetCHARTING;
  13. using System.Drawing;

  14. /**//// <summary>
  15. ///ShowData 的摘要说明
  16. /// </summary>
  17. public class ShowData
  18. {

  19. private string _phaysicalimagepath;//图片存放路径
  20. private string _title; //图片标题
  21. private string _xtitle;//图片x座标名称
  22. private string _ytitle;//图片y座标名称
  23. private string _seriesname;//图例名称
  24. private int _picwidth;//图片宽度
  25. private int _pichight;//图片高度
  26. private DataTable _dt;//图片数据源

  27. /**//**//**//// <summary>
  28. /// 图片存放路径
  29. /// </summary>
  30. public string PhaysicalImagePath
  31. {
  32. set{_phaysicalimagepath=value;}
  33. get{return _phaysicalimagepath;}
  34. }
  35. /**//**//**//// <summary>
  36. /// 图片标题
  37. /// </summary>
  38. public string Title
  39. {
  40. set{_title=value;}
  41. get{return _title;}
  42. }
  43. /**//**//**//// <summary>
  44. /// 图片标题
  45. /// </summary>
  46. public string XTitle
  47. {
  48. set{_xtitle=value;}
  49. get{return _xtitle;}
  50. }
  51. /**//**//**//// <summary>
  52. /// 图片标题
  53. /// </summary>
  54. public string YTitle
  55. {
  56. set{_ytitle=value;}
  57. get{return _ytitle;}
  58. }

  59. /**//**//**//// <summary>
  60. /// 图例名称
  61. /// </summary>
  62. public string SeriesName
  63. {
  64. set{_seriesname=value;}
  65. get{return _seriesname;}
  66. }
  67. /**//**//**//// <summary>
  68. /// 图片宽度
  69. /// </summary>
  70. public int PicWidth
  71. {
  72. set{_picwidth=value;}
  73. get{return _picwidth;}
  74. }
  75. /**//**//**//// <summary>
  76. /// 图片高度
  77. /// </summary>
  78. public int PicHight
  79. {
  80. set{_pichight=value;}
  81. get{return _pichight;}
  82. }
  83. /**//**//**//// <summary>
  84. /// 图片数据源
  85. /// </summary>
  86. public DataTable DataSource
  87. {
  88. set{_dt=value; }
  89. get{return _dt;}
  90. }

  91. public ShowData()
  92. {
  93. //
  94. // TOD 在此处添加构造函数逻辑
  95. //
  96. }

  97. public ShowData(string PhaysicalImagePath,string Title,string XTitle,string YTitle,string SeriesName)
  98. {
  99. _phaysicalimagepath=PhaysicalImagePath;
  100. _title=Title;
  101. _xtitle=XTitle;
  102. _ytitle=YTitle;
  103. _seriesname=SeriesName;
  104. }


  105. /**//**//**//// <summary>
  106. /// 柱形图
  107. /// </summary>
  108. /// <returns></returns>
  109. public void CreateColumn(dotnetCHARTING.Chart chart)
  110. {
  111. chart.Title=this._title;
  112. chart.XAxis.Label.Text=this._xtitle;
  113. chart.YAxis.Label.Text=this._ytitle;
  114. chart.TempDirectory =this._phaysicalimagepath;
  115. chart.Width = this._picwidth;
  116. chart.Height = this._pichight;
  117. chart.Type = ChartType.Combo ;
  118. chart.Series.Type =SeriesType.Cylinder;
  119. chart.Series.Name = this._seriesname;
  120. chart.Series.Data = this._dt;
  121. chart.SeriesCollection.Add();
  122. chart.DefaultSeries.DefaultElement.ShowValue = true;
  123. chart.ShadingEffect = true;
  124. chart.Use3D = false;
  125. chart.Series.DefaultElement.ShowValue =true;


  126. }

  127. /**//**//**//// <summary>
  128. /// 饼图
  129. /// </summary>
  130. /// <returns></returns>
  131. public void CreatePie(dotnetCHARTING.Chart chart)
  132. {
  133. chart.Title=this._title;
  134. chart.TempDirectory =this._phaysicalimagepath;
  135. chart.Width = this._picwidth;
  136. chart.Height = this._pichight;
  137. chart.Type = ChartType.Pie;
  138. chart.Series.Type =SeriesType.Cylinder;
  139. chart.Series.Name = this._seriesname;

  140. chart.ShadingEffect = true;
  141. chart.Use3D = true;
  142. chart.DefaultSeries.DefaultElement.Transparency = 20;
  143. chart.DefaultSeries.DefaultElement.ShowValue = true;
  144. chart.PieLabelMode = PieLabelMode.Outside;
  145. chart.SeriesCollection.Add(getArrayData());
  146. chart.Series.DefaultElement.ShowValue = true; 
  147. }

  148. private SeriesCollection getArrayData()        
  149. {
  150. SeriesCollection SC = new SeriesCollection();
  151. DataTable dt = this._dt;

  152. for(int i=0; i < dt.Rows.Count; i++)
  153. {
  154. Series s = new Series();
  155. s.Name = dt.Rows[0].ToString();

  156. Element e = new Element();

  157. // 每元素的名称
  158. e.Name = dt.Rows[0].ToString();

  159. // 每元素的大小数值
  160. e.YValue = Convert.ToDouble(dt.Rows[1].ToString());
  161.           
  162. s.Elements.Add(e);
  163. SC.Add(s);
  164. }
  165. return SC;
  166. }

  167. /**//**//**//// <summary>
  168. /// 曲线图
  169. /// </summary>
  170. /// <returns></returns>
  171. public void CreateLine(dotnetCHARTING.Chart chart,DataTable dt2)
  172. {
  173. chart.Title=this._title;
  174. chart.XAxis.Label.Text=this._xtitle;
  175. chart.YAxis.Label.Text=this._ytitle;
  176. chart.TempDirectory =this._phaysicalimagepath;
  177. chart.Width = this._picwidth;
  178. chart.Height = this._pichight;
  179. chart.Type = ChartType.Combo ;
  180. chart.Series.Type =SeriesType.Line;
  181. chart.Series.Name = this._seriesname;
  182. chart.Series.Data = this._dt;
  183. chart.SeriesCollection.Add();
  184.     Series series2 = new Series();
  185. series2.Type = SeriesType.Line;
  186. series2.Name = "08年费用(千元x)";
  187. series2.Data = dt2;
  188. chart.SeriesCollection.Add(series2);
  189. chart.DefaultSeries.DefaultElement.ShowValue = true;
  190. chart.ShadingEffect = true;
  191. chart.Use3D = false;
  192. chart.Series.DefaultElement.ShowValue =true;

  193. }
  194. }
复制代码
使用的时候代码如下:
  1.         ShowData show = new ShowData();
  2.         show.Title = Title;
  3.         show.XTitle = xTitle;
  4.         show.YTitle = yTitle;
  5.         show.SeriesName = SeriesName;
  6.         show.DataSource = dataView.ToTable(false, colums);
  7.         show.PicHight = 500;
  8.         show.PicWidth = 700;
  9.         show.PhaysicalImagePath = "testImag";
  10.         show.CreatePie(this.Chart1);
复制代码
dotNetCharting的展示效果由于是将生成的一张临时图片展现在页面所以比较绚,说到这dotNetCharting在使用的时候需要建一个临时文件夹用于存放临时生成的图片文件,不要担心临时文件会越来越多,页面在每次加载的时候会删除掉以前的图片文件,这样的方式也为我将图片插入到Excel中提供了帮助,页面和统计图生成以后在导出Excel的时候可以利用脚本在页面获取图片在服务器上的地址,得到这一地址就可以在生成Excel的时候,将数据和图片一起插入。

总之DotNetCharting我觉得是一个很不错的统计图制作控件

效果图如下:
附件: dnc-4it7e53y.png

附件: dnc-f18hu7u3.png



文/lab  出处/博客园
TOP