ChartReportUtils.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections.Generic;
  3. using SysBaseLibs;
  4. namespace SysDataLibs
  5. {
  6. public class ChartReportUtils
  7. {
  8. /********************************************************
  9. * Category1 Category2 Category3 Category4
  10. * Series1 25 28 32 31
  11. * Series2 35 44 28 36
  12. *********************************************************/
  13. // 将数据库返回结果转换成按列存放的格式
  14. // 查询语句返回列的第一个是Category,其余的每个列都视作一个Series
  15. // 如果Category是日期类型,查询语句最好按日期排序
  16. static public void PrepareData(rsQuery loQuery, ref List<Array> ListData)
  17. {
  18. int i = 0;
  19. try
  20. {
  21. if (loQuery != null && loQuery.IsOpened)
  22. {
  23. if (loQuery.RecCount > 0)
  24. {
  25. ListData = new List<Array>();
  26. // 和查询列数一致
  27. for (i = 0; i < loQuery.Columns.Count; i++)
  28. {
  29. // 一共有loQuery.RecCount行记录
  30. Array arrayData = Array.CreateInstance(typeof(string), loQuery.RecCount);
  31. ListData.Add(arrayData);
  32. }
  33. // 查询列名称
  34. Array arrayFields = UtilStr.StrToArrayEx(loQuery.AllFields, ",");
  35. // 轮询各行
  36. for (i = 0; i < loQuery.RecCount; i++)
  37. {
  38. // 对每个Field进行查询
  39. for (int j = 0; j < arrayFields.Length; j++)
  40. {
  41. string strField = arrayFields.GetValue(j).ToString();
  42. string strVal = loQuery.GetString(strField);
  43. // 数据存到对应的列缓存中
  44. ListData[j].SetValue(strVal, i);
  45. }
  46. // 下一条记录
  47. loQuery.MoveNext();
  48. }//for
  49. }
  50. }
  51. }
  52. catch (Exception err)
  53. {
  54. ThreadLog.LogException(err);
  55. ListData = null;
  56. }
  57. finally
  58. {
  59. }
  60. }
  61. /********************************************************
  62. * Category1 Category2 Category3
  63. * Series1 25 null 30
  64. * Series2 35 44 66
  65. * Series3 null 88 99
  66. * Series4 15 17 13
  67. *********************************************************/
  68. // 要求各Series有相同的x轴Category,在某个Category上没有数据时,用null填充
  69. // 查询语句返回列的第一个是Category,第二个列是Series,其余列为数据
  70. // 如果要求CategoryList排序返回,则必须把Category放在查询语句排序的第一个
  71. static public void PrepareGridData(rsQuery loQuery,
  72. ref List<string> CategoryList,
  73. ref List<string> SeriesList,
  74. ref List<Array> ListData)
  75. {
  76. int i = 0, j = 0;
  77. string strTemp = string.Empty;
  78. try
  79. {
  80. if (loQuery != null && loQuery.IsOpened)
  81. {
  82. // 该查询返回的列数>=3, Category / Series / 数值列1 / 数值列2 ....
  83. if ((loQuery.RecCount > 0) && (loQuery.Columns.Count >= 3))
  84. {
  85. // Category列表
  86. CategoryList = new List<string>();
  87. // Series列表
  88. SeriesList = new List<string>();
  89. // 列个数
  90. int nColumnCount = loQuery.Columns.Count;
  91. // 各列的名称
  92. Array arrayFields = UtilStr.StrToArrayEx(loQuery.AllFields, ",");
  93. string strField = string.Empty;
  94. string strCategory = string.Empty;
  95. string strSeries = string.Empty;
  96. // 缓存数据库查询到的各行(每行以Array方式)
  97. List<Array> listRows = new List<Array>();
  98. for (i = 0; i < loQuery.RecCount; i++)
  99. {
  100. // SQL语句返回列的第一个必须是Category
  101. strField = arrayFields.GetValue(0).ToString();
  102. strCategory = loQuery.GetString(strField);
  103. if (strCategory != "")
  104. {
  105. if (CategoryList.IndexOf(strCategory) < 0)
  106. {
  107. CategoryList.Add(strCategory);
  108. }
  109. }
  110. // SQL语句返回列的第二个必须是Series
  111. strField = arrayFields.GetValue(1).ToString();
  112. strSeries = loQuery.GetString(strField);
  113. if (SeriesList.IndexOf(strSeries) < 0)
  114. {
  115. SeriesList.Add(strSeries);
  116. }
  117. Array arrayOneRow = Array.CreateInstance(typeof(string), nColumnCount);
  118. arrayOneRow.SetValue(strCategory, 0);
  119. arrayOneRow.SetValue(strSeries, 1);
  120. // 查询数值列的数据
  121. for (j = 2; j < nColumnCount; j++)
  122. {
  123. strField = arrayFields.GetValue(j).ToString();
  124. strTemp = loQuery.GetString(strField);
  125. arrayOneRow.SetValue(strTemp, j);
  126. }
  127. // 加入缓存
  128. listRows.Add(arrayOneRow);
  129. // 下一条
  130. loQuery.MoveNext();
  131. }
  132. // 根据Series数创建对应数量的Array
  133. ListData = new List<Array>();
  134. for (i = 0; i < SeriesList.Count; i++)
  135. {
  136. // 每个Series都有CategoryList.Count个
  137. Array arraySeries = Array.CreateInstance(typeof(Array), CategoryList.Count);
  138. for (j = 0; j < CategoryList.Count; j++)
  139. {
  140. // 当前Series里面的每个节点是数值列的Array
  141. Array arrayVal = Array.CreateInstance(typeof(string), nColumnCount - 2);
  142. for (int k = 0; k < nColumnCount - 2; k++)
  143. {
  144. // 初始化为"null"
  145. arrayVal.SetValue("null", k);
  146. }
  147. arraySeries.SetValue(arrayVal, j);
  148. }
  149. // 添加当前Series
  150. ListData.Add(arraySeries);
  151. }//for
  152. // 轮询listRows缓存,把数据设置到返回结果集ListData
  153. foreach (Array rec in listRows)
  154. {
  155. int nIdxCategory = CategoryList.IndexOf(rec.GetValue(0).ToString());
  156. int nIdxSeries = SeriesList.IndexOf(rec.GetValue(1).ToString());
  157. if ((nIdxCategory >= 0) && (nIdxSeries >= 0))
  158. {
  159. Array arrayVal = Array.CreateInstance(typeof(string), nColumnCount - 2);
  160. for (i = 0; i < nColumnCount - 2; i++)
  161. {
  162. strTemp = rec.GetValue(i + 2).ToString();
  163. arrayVal.SetValue(strTemp, i);
  164. }
  165. // 设置到指定Series的指定Category位置
  166. ListData[nIdxSeries].SetValue(arrayVal, nIdxCategory);
  167. }
  168. }
  169. /*// 转换List到Array格式
  170. _arrayGoods = Array.CreateInstance(typeof(string), GoodsList.Count);
  171. for (i = 0; i < GoodsList.Count; i++)
  172. {
  173. _arrayGoods.SetValue(GoodsList[i], i);
  174. }*/
  175. }//if loQuery.RecCount
  176. }
  177. }
  178. catch (Exception err)
  179. {
  180. ThreadLog.LogException(err);
  181. CategoryList = null;
  182. SeriesList = null;
  183. ListData = null;
  184. }
  185. finally
  186. {
  187. }
  188. }
  189. /********************************************************
  190. * Category1 Category2 Category3 Category4
  191. * Series1 25 30
  192. * Series2 35 44 66 48
  193. * Series3 88 69
  194. * Series4 15 17 13
  195. *********************************************************/
  196. // 各Series的数据个数可能不一样,无统一的x轴Category
  197. static public void PrepareDataNonfixedCat(rsQuery loQuery,
  198. ref List<string> SeriesList,
  199. ref List<List<Array>> ListData)
  200. {
  201. int i = 0, j = 0;
  202. string strTemp = string.Empty;
  203. try
  204. {
  205. if (loQuery != null && loQuery.IsOpened)
  206. {
  207. // 该查询返回的列数>=3, Category / Series / 数值列1 / 数值列2 ....
  208. if ((loQuery.RecCount > 0) && (loQuery.Columns.Count >= 3))
  209. {
  210. // 返回数据.每个Series对应一个List
  211. ListData = new List<List<Array>>();
  212. // Series列表
  213. SeriesList = new List<string>();
  214. // 各列的名称
  215. Array arrayFields = UtilStr.StrToArrayEx(loQuery.AllFields, ",");
  216. string strField = string.Empty;
  217. string strCategory = string.Empty;
  218. string strSeries = string.Empty;
  219. // 列个数
  220. int nColumnCount = loQuery.Columns.Count;
  221. for (i = 0; i < loQuery.RecCount; i++)
  222. {
  223. // SQL语句返回列的第一个必须是Category
  224. strField = arrayFields.GetValue(0).ToString();
  225. strCategory = loQuery.GetString(strField);
  226. // SQL语句返回列的第二个必须是Series
  227. strField = arrayFields.GetValue(1).ToString();
  228. strSeries = loQuery.GetString(strField);
  229. Array arrayOneRow = Array.CreateInstance(typeof(string), nColumnCount);
  230. arrayOneRow.SetValue(strCategory, 0);
  231. arrayOneRow.SetValue(strSeries, 1);
  232. // 查询数值列的数据
  233. for (j = 2; j < nColumnCount; j++)
  234. {
  235. strField = arrayFields.GetValue(j).ToString();
  236. strTemp = loQuery.GetString(strField);
  237. arrayOneRow.SetValue(strTemp, j);
  238. }
  239. int nIdx = SeriesList.IndexOf(strSeries);
  240. if (nIdx < 0)
  241. {
  242. // 为新的Series添加
  243. SeriesList.Add(strSeries);
  244. List<Array> ListForCurSeries = new List<Array>();
  245. ListForCurSeries.Add(arrayOneRow);
  246. ListData.Add(ListForCurSeries);
  247. }
  248. else
  249. {
  250. // 添加到已有的Series
  251. ListData[nIdx].Add(arrayOneRow);
  252. }
  253. // 下一条
  254. loQuery.MoveNext();
  255. }
  256. }
  257. }
  258. }
  259. catch (Exception err)
  260. {
  261. ThreadLog.LogException(err);
  262. SeriesList = null;
  263. ListData = null;
  264. }
  265. finally
  266. {
  267. }
  268. }
  269. }
  270. }