數據統計是每個系統中必備的功能,在給領導彙報統計數據,工作中需要的進展數據時非常有用。 在我看來,一個統計的模塊應該實現以下功能: 能夠將常用的查詢的統計結果顯示出來; 顯示的結果可以是表格形式,也可以是圖形形式,如果是圖形的話能夠以多種形式顯示(柱狀圖、折線圖、餅圖、雷達圖、堆疊柱狀圖等): 統計 ...
數據統計是每個系統中必備的功能,在給領導彙報統計數據,工作中需要的進展數據時非常有用。
在我看來,一個統計的模塊應該實現以下功能:
- 能夠將常用的查詢的統計結果顯示出來;
- 顯示的結果可以是表格形式,也可以是圖形形式,如果是圖形的話能夠以多種形式顯示(柱狀圖、折線圖、餅圖、雷達圖、堆疊柱狀圖等):
- 統計查詢的結果,點擊數字或者百分比能夠顯示詳細的數據;
- 能夠自由組合查詢條件、篩選條件、分組條件、排序等;
- 統計結果最好有個實時預覽;
- 查詢統計能夠保存,以便下次能直接調用並顯示統計查詢的結果;
- 對於保存後的查詢統計,下次調用時也可以按照靈活的篩選手段對查詢結果進行篩選;
- 界面需要做的簡潔、直觀,就算是不太懂電腦的操作員也能夠方便使用;
- 對於一些複雜的查詢,能夠直接在後臺寫Sql或者調用Sp出數據
- ......
好了,以下是在實際環境中的實現和應用:
這是一個學生的就業系統,學生在不同的時期會對自己畢業去向進行登記,因此按照不同時間截點統計出來的數據是不一樣的。數據表有100多個欄位(並不是所有欄位都需要統計)。
首先,我們在資料庫中構建一個表值函數,能夠按照不同的時間截點返回出數據,表也起到視圖的作用,將參數表的值直接包含到返回結果中去。
1 ALTER FUNCTION [dbo].[Get.............] 2 ( 3 @gxsj datetime 4 ) 5 RETURNS TABLE 6 AS 7 RETURN 8 ( 9 select t1.*, 10 dbo.depacode.xymc, 11 CASE t1.xldm WHEN '01' THEN '博士' WHEN '11' THEN '碩士' WHEN '25' THEN '雙學位' WHEN '31' THEN '本科' WHEN '41' THEN '專科' WHEN '61' THEN '高職' ELSE '' END AS xlmc, 12 CASE WHEN LEFT(t1.sydqdm, 2) IN ('11', '12', '13', '21', '31', '32', '33', '35', '37', '44', '46', '71', '81', '82') THEN '東部' 13 WHEN LEFT(t1.sydqdm, 2) IN ('14', '22', '23', '34', '36', '41', '42', '43') THEN '中部' 14 WHEN LEFT(t1.sydqdm, 2) IN ('15', '45', '51', '50', '52', '53', '54', '61', '62', '65', '63', '64') THEN '西部' ELSE '' END AS sydq, 15 sydq.dwdqmc AS sysf, 16 CASE WHEN LEFT(t1.dwdqdm, 2) IN ('11', '12', '13', '21', '31', '32', '33', '35', '37', '44', '46', '71', '81', '82') THEN '東部' 17 WHEN LEFT(t1.dwdqdm, 2) IN ('14', '22', '23', '34', '36', '41', '42', '43') THEN '中部' 18 WHEN LEFT(t1.dwdqdm, 2) IN ('15', '45', '51', '50', '52', '53', '54', '61', '62', '65', '63', '64') THEN '西部' ELSE '' END AS dwdq, 19 dwdq.dwdqmc AS dwsf, dbo.Entcode.hyname, 20 dbo.hydygx.hymldm, dbo.hydygx.hyml, 21 CASE t1.xbdm WHEN 1 THEN '男' WHEN 2 THEN '女' ELSE '男' END AS xbmc, 22 [mzdmb].[nation] AS mzmc, 23 [EjByqxdmb].[Ejbyqxmc], dbo.byqxdygx.jybbyqx, t1.gn500 AS jybdwxzdm, 24 CASE t1.knslbdm WHEN '7' THEN '就業困難、家庭困難和殘疾' WHEN '6' THEN '家庭困難和殘疾' WHEN '5' THEN '就業困難和殘疾' WHEN '4' THEN '殘疾' WHEN '3' THEN '就業和家庭困難' WHEN '2' THEN '家庭困難' WHEN '1' THEN '就業困難' ELSE '非困難生' END AS Knslb 25 from [table] as t1 26 LEFT OUTER JOIN 27 dbo.depacode ON t1.xydm = dbo.depacode.xydm LEFT OUTER JOIN 28 dbo.dwdq AS sydq ON LEFT(t1.sydqdm, 2) + '0000' = sydq.dwdqdm LEFT OUTER JOIN 29 dbo.dwdq AS dwdq ON LEFT(t1.dwdqdm, 2) + '0000' = dwdq.dwdqdm LEFT OUTER JOIN 30 dbo.Entcode ON t1.hylb = dbo.Entcode.hycode LEFT OUTER JOIN 31 dbo.hydygx ON t1.hylb = dbo.hydygx.hydldm LEFT OUTER JOIN 32 [mzdmb] ON t1.mzdm = [mzdmb].[mzdm] LEFT OUTER JOIN 33 [EjByqxdmb] ON t1.byqx2 = [EjByqxdmb].[Ejbyqxdm] LEFT OUTER JOIN 34 dbo.byqxdygx ON t1.byqx = dbo.byqxdygx.shbyqx AND 35 t1.dwxzdm = dbo.byqxdygx.shdwxzdm 36 where [gxsj] <= dateadd(day,1,@gxsj) and HisId in 37 (SELECT TOP 1 HisId FROM [table] 38 WHERE [gxsj] <= dateadd(day,1,@gxsj) and xsxh = t1.xsxh 39 and bynf = t1.bynf and t1.byqx not in ('08','05','11') 40 ORDER BY [gxsj] DESC) 41 )View Code
這樣我們使用 select * from [get...]('2016-8-25') 就可以查詢出8月25日截止日期的數據。
接下來是界面設計,我們使用jequery-ui中dropable\dragable的控制項,欄位排列在界面上,直接拖拽到相應域里,就能夠進行統計。
除了分組欄位外,顯示欄位還能夠按照具體的值進行統計過濾,起到多重分組統計的功能。
大家可以看到,最上面一欄是數據篩選,然後是系統已經保存的查詢(分為表格查詢和圖形查詢),點擊保存好的查詢直接出查詢結果,也可以刪除保存的查詢。在下麵是自定義查詢,上面是一排條件,然後是可以拖拽的欄位,當欄位拖至分組列,則顯示欄位名稱;拖至顯示列,還可以對顯示的數據的具體值進行分組篩選統計。下方則是一些選項,是否顯示小計、總計,以何種方式顯示圖表。
以表格形式的顯示統計,可以看到,每個數值都可以點擊彈出框顯示詳情,最下方能夠保存查詢條件,以圖形方式顯示等:
圖形的展示:
以下是核心類InquireHelper.cs:
欄位實體類(部分)
1 [Serializable] 2 [XmlInclude(typeof(BYNF_InquireField))] 3 [XmlInclude(typeof(Count_InquireField))] 4 [XmlInclude(typeof(XYMC_InquireField))] 5 [XmlInclude(typeof(ZYMC_InquireField))] 6 [XmlInclude(typeof(SZBJ_InquireField))] 7 [XmlInclude(typeof(FDY_InquireField))] 8 [XmlInclude(typeof(XL_InquireField))] 9 [XmlInclude(typeof(SYDQ_InquireField))] 10 [XmlInclude(typeof(SYSF_InquireField))] 11 [XmlInclude(typeof(DWDQ_InquireField))] 12 [XmlInclude(typeof(DWSF_InquireField))] 13 [XmlInclude(typeof(HYML_InquireField))] 14 [XmlInclude(typeof(HYDL_InquireField))] 15 [XmlInclude(typeof(XBMC_InquireField))] 16 [XmlInclude(typeof(MZMC_InquireField))] 17 [XmlInclude(typeof(BYQX_InquireField))] 18 [XmlInclude(typeof(KNSLB_InquireField))] 19 [XmlInclude(typeof(ZYDKL_InquireField))] 20 [XmlInclude(typeof(DWXZ_InquireField))] 21 [XmlInclude(typeof(EJBYQXMC_InquireField))] 22 [XmlInclude(typeof(GZ_InquireField))] 23 [XmlInclude(typeof(WYJE_InquireField))] 24 public abstract class InquireFieldBase 25 { 26 public InquireFieldBase() 27 { 28 FieldItems = this.GetInquireItemsByInquireType(); 29 } 30 31 [XmlAttribute] 32 public int FieldDisplayOrder { get; set; } 33 [XmlAttribute] 34 public string FieldName { get; set; } 35 [XmlAttribute] 36 public string DbName { get; set; } 37 [XmlAttribute] 38 public bool IsAggregate { get; set; } 39 [XmlAttribute] 40 public InquireHelper.FieldType FieldType { get; set; } 41 42 //用於highchart統計 43 [XmlAttribute] 44 public bool IsNameField { get; set; } 45 46 //用於統計輸出數據 47 [XmlAttribute] 48 public bool IsPercent { get; set; } 49 50 [XmlIgnore] 51 public List<string> FieldItems { get; set; } 52 public List<string> FieldValue { get; set; } 53 public bool? OrderByAsc { get; set; } 54 } 55 [Serializable] 56 public class BYNF_InquireField : InquireFieldBase 57 { 58 public BYNF_InquireField() 59 { 60 FieldDisplayOrder = 1; 61 FieldName = "畢業年份"; 62 DbName = "BYNF"; 63 } 64 } 65 [Serializable] 66 public class XYMC_InquireField : InquireFieldBase 67 { 68 public XYMC_InquireField() 69 { 70 FieldDisplayOrder = 5; 71 FieldName = "學院名稱"; 72 DbName = "XYMC"; 73 } 74 } 75 [Serializable] 76 public class ZYMC_InquireField : InquireFieldBase 77 { 78 public ZYMC_InquireField() 79 { 80 FieldDisplayOrder = 6; 81 FieldName = "專業名稱"; 82 DbName = "ZYMC"; 83 } 84 } 85 [Serializable] 86 public class SZBJ_InquireField : InquireFieldBase 87 { 88 public SZBJ_InquireField() 89 { 90 FieldDisplayOrder = 7; 91 FieldName = "所在班級"; 92 DbName = "SZBJ"; 93 } 94 } 95 [Serializable] 96 public class FDY_InquireField : InquireFieldBase 97 { 98 public FDY_InquireField() 99 { 100 FieldDisplayOrder = 8; 101 FieldName = "輔導員"; 102 DbName = "FDY"; 103 } 104 } 105 [Serializable] 106 public class XL_InquireField : InquireFieldBase 107 { 108 public XL_InquireField() 109 { 110 FieldDisplayOrder = 9; 111 FieldName = "學歷"; 112 DbName = "XLMC"; 113 } 114 } 115 [Serializable] 116 public class SYDQ_InquireField : InquireFieldBase 117 { 118 public SYDQ_InquireField() 119 { 120 FieldDisplayOrder = 10; 121 FieldName = "生源地區"; 122 DbName = "SYDQ"; 123 } 124 } 125 [Serializable] 126 public class SYSF_InquireField : InquireFieldBase 127 { 128 public SYSF_InquireField() 129 { 130 FieldDisplayOrder = 11; 131 FieldName = "生源省份"; 132 DbName = "SYSF"; 133 } 134 } 135 [Serializable] 136 public class DWDQ_InquireField : InquireFieldBase 137 { 138 public DWDQ_InquireField() 139 { 140 FieldDisplayOrder = 12; 141 FieldName = "單位地區"; 142 DbName = "DWDQ"; 143 } 144 } 145 [Serializable] 146 public class DWSF_InquireField : InquireFieldBase 147 { 148 public DWSF_InquireField() 149 { 150 FieldDisplayOrder = 13; 151 FieldName = "單位省份"; 152 DbName = "DWSF"; 153 } 154 }View Code
控制類
1 public static class InquireHelper 2 { 3 public static List<InquireFieldBase> GetSubInquireList() 4 { 5 var inquires = new List<InquireFieldBase>(); 6 var subTypeQuery = from t in Assembly.GetExecutingAssembly().GetTypes() 7 where IsSubClassOf(t, typeof(InquireFieldBase)) 8 select t; 9 10 foreach (var type in subTypeQuery) 11 { 12 InquireFieldBase obj = CreateObject(type.FullName) as InquireFieldBase; 13 if (obj != null) 14 { 15 inquires.Add(obj); 16 } 17 } 18 return inquires; 19 20 } 21 22 static bool IsSubClassOf(Type type, Type baseType) 23 { 24 var b = type.BaseType; 25 while (b != null) 26 { 27 if (b.Equals(baseType)) 28 { 29 return true; 30 } 31 b = b.BaseType; 32 } 33 return false; 34 } 35 /// <summary> 36 /// 創建對象(當前程式集) 37 /// </summary> 38 /// <param name="typeName">類型名</param> 39 /// <returns>創建的對象,失敗返回 null</returns> 40 public static object CreateObject(string typeName) 41 { 42 object obj = null; 43 try 44 { 45 Type objType = Type.GetType(typeName, true); 46 obj = Activator.CreateInstance(objType); 47 } 48 catch (Exception ex) 49 { 50 51 } 52 return obj; 53 } 54 55 public static List<InquireFieldBase> BindCondition(this List<InquireFieldBase> conditions, string conditionName, List<string> values) 56 { 57 var condition = conditions.FirstOrDefault(c => c.GetType().Name == conditionName && c.FieldType == FieldType.ConditionField); 58 59 if (condition == null) 60 { 61 condition = CreateObject("BLL." + conditionName) as InquireFieldBase; 62 condition.FieldType = FieldType.ConditionField; 63 conditions.Add(condition); 64 } 65 66 condition.FieldValue = values; 67 68 return conditions; 69 } 70 //public static List<InquireFieldBase> BindCondition(this List<InquireFieldBase> conditions, string conditionName, string range1, string range2) 71 //{ 72 // var condition = conditions.FirstOrDefault(c => c.GetType().Name == conditionName && c.FieldType == FieldType.ConditionField); 73 74 75 // if (!string.IsNullOrEmpty(range2)&&!string.IsNullOrEmpty(range1)) 76 // { 77 // if (condition == null) 78 // { 79 // condition = CreateObject("BLL." + conditionName) as InquireFieldBase; 80 //