Home >Backend Development >C#.Net Tutorial >Using Asp.net to implement data statistics function of information management system

Using Asp.net to implement data statistics function of information management system

巴扎黑
巴扎黑Original
2017-08-15 13:48:582504browse

This article mainly introduces the implementation method of data statistics function in Asp.net management information system. Friends who need it can refer to it

Data statistics is an essential function in every system. It is for leaders Very useful when reporting statistics and progress data needed for work.

In my opinion, a statistical module should implement the following functions:

  • Be able to display the statistical results of commonly used queries;

  • The displayed results can be in tabular form or graphic form. If it is graphic, it can be displayed in multiple forms (bar chart, line chart, pie chart, radar chart, stacked bar chart, etc.):

  • Statistical query results, click on the number or percentage to display detailed data;

  • You can freely combine query conditions, filter conditions, grouping conditions, and sorting etc.;

  • It is best to have a real-time preview of statistical results;

  • The query statistics can be saved so that the statistical query can be directly called and displayed next time The results;

  • For the saved query statistics, the query results can also be filtered according to the flexible filtering method the next time it is called;

  • The interface needs to be simple and intuitive, so that even operators who don’t know much about computers can use it easily;

  • For some complex queries, you can directly write Sql in the background or call Sp Out of data

......

Okay, the following is the implementation and application in the actual environment:

This is a student In the employment system, students will register their graduation destination at different periods, so the statistics based on different time cutoffs are different. The data table has more than 100 fields (not all fields require statistics).

First, we build a table-valued function in the database that can return data according to different time cutoffs. The table also serves as a view, and the value of the parameter table is directly included in the return result.


ALTER FUNCTION [dbo].[Get.............]
( 
 @gxsj datetime
)
RETURNS TABLE 
AS
RETURN 
(
 select t1.*, 
 dbo.depacode.xymc, 
 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, 
 CASE WHEN LEFT(t1.sydqdm, 2) IN ('11', '12', '13', '21', '31', '32', '33', '35', '37', '44', '46', '71', '81', '82') THEN '东部' 
 WHEN LEFT(t1.sydqdm, 2) IN ('14', '22', '23', '34', '36', '41', '42', '43') THEN '中部' 
 WHEN LEFT(t1.sydqdm, 2) IN ('15', '45', '51', '50', '52', '53', '54', '61', '62', '65', '63', '64') THEN '西部' ELSE '' END AS sydq, 
 sydq.dwdqmc AS sysf,
 CASE WHEN LEFT(t1.dwdqdm, 2) IN ('11', '12', '13', '21', '31', '32', '33', '35', '37', '44', '46', '71', '81', '82') THEN '东部' 
 WHEN LEFT(t1.dwdqdm, 2) IN ('14', '22', '23', '34', '36', '41', '42', '43') THEN '中部' 
 WHEN LEFT(t1.dwdqdm, 2) IN ('15', '45', '51', '50', '52', '53', '54', '61', '62', '65', '63', '64') THEN '西部' ELSE '' END AS dwdq, 
  dwdq.dwdqmc AS dwsf, dbo.Entcode.hyname, 
 dbo.hydygx.hymldm, dbo.hydygx.hyml, 
 CASE t1.xbdm WHEN 1 THEN '男' WHEN 2 THEN '女' ELSE '男' END AS xbmc,
 [mzdmb].[nation] AS mzmc,
 [EjByqxdmb].[Ejbyqxmc], dbo.byqxdygx.jybbyqx, t1.gn500 AS jybdwxzdm,
 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
 from [table] as t1 
 LEFT OUTER JOIN
 dbo.depacode ON t1.xydm = dbo.depacode.xydm LEFT OUTER JOIN
 dbo.dwdq AS sydq ON LEFT(t1.sydqdm, 2) + '0000' = sydq.dwdqdm LEFT OUTER JOIN
 dbo.dwdq AS dwdq ON LEFT(t1.dwdqdm, 2) + '0000' = dwdq.dwdqdm LEFT OUTER JOIN
 dbo.Entcode ON t1.hylb = dbo.Entcode.hycode LEFT OUTER JOIN
 dbo.hydygx ON t1.hylb = dbo.hydygx.hydldm LEFT OUTER JOIN
 [mzdmb] ON t1.mzdm = [mzdmb].[mzdm] LEFT OUTER JOIN
 [EjByqxdmb] ON t1.byqx2 = [EjByqxdmb].[Ejbyqxdm] LEFT OUTER JOIN
 dbo.byqxdygx ON t1.byqx = dbo.byqxdygx.shbyqx AND 
 t1.dwxzdm = dbo.byqxdygx.shdwxzdm
 where [gxsj] <= dateadd(day,1,@gxsj) and HisId in 
 (SELECT TOP 1 HisId FROM [table]
 WHERE [gxsj] <= dateadd(day,1,@gxsj) and xsxh = t1.xsxh
 and bynf = t1.bynf and t1.byqx not in (&#39;08&#39;,&#39;05&#39;,&#39;11&#39;)
 ORDER BY [gxsj] DESC)
)

In this way, we can use select * from [get...]('2016-8-25') to query August 25 Data as of date.

The next step is the interface design. We use the dropable\dragable controls in jquery-ui. The fields are arranged on the interface and can be directly dragged into the corresponding fields to perform statistics.

In addition to the grouping fields, the display fields can also perform statistical filtering based on specific values, serving the function of multiple grouping statistics.

As you can see, the top column is data filtering, and then there are queries that have been saved by the system (divided into table queries and Graphical query), click the saved query to directly display the query results, or delete the saved query. Below is a custom query, above is a row of conditions, and then fields that can be dragged and dropped. When a field is dragged to the grouping column, the field name is displayed; by dragging to the display column, the specific values ​​of the displayed data can also be grouped and filtered. statistics. Below are some options, whether to display subtotals and totals, and how to display the chart.

Display statistics in tabular form. You can see that each value can be clicked on the pop-up box to display details. At the bottom, query conditions can be saved and displayed graphically:

Graphic display:

##The following is the core class InquireHelper.cs:

Field entity class (part)


[Serializable]
 [XmlInclude(typeof(BYNF_InquireField))]
 [XmlInclude(typeof(Count_InquireField))]
 [XmlInclude(typeof(XYMC_InquireField))]
 [XmlInclude(typeof(ZYMC_InquireField))]
 [XmlInclude(typeof(SZBJ_InquireField))]
 [XmlInclude(typeof(FDY_InquireField))]
 [XmlInclude(typeof(XL_InquireField))]
 [XmlInclude(typeof(SYDQ_InquireField))]
 [XmlInclude(typeof(SYSF_InquireField))]
 [XmlInclude(typeof(DWDQ_InquireField))]
 [XmlInclude(typeof(DWSF_InquireField))]
 [XmlInclude(typeof(HYML_InquireField))]
 [XmlInclude(typeof(HYDL_InquireField))]
 [XmlInclude(typeof(XBMC_InquireField))]
 [XmlInclude(typeof(MZMC_InquireField))]
 [XmlInclude(typeof(BYQX_InquireField))]
 [XmlInclude(typeof(KNSLB_InquireField))]
 [XmlInclude(typeof(ZYDKL_InquireField))]
 [XmlInclude(typeof(DWXZ_InquireField))]
 [XmlInclude(typeof(EJBYQXMC_InquireField))]
 [XmlInclude(typeof(GZ_InquireField))]
 [XmlInclude(typeof(WYJE_InquireField))]
 public abstract class InquireFieldBase
 {
  public InquireFieldBase()
  {
   FieldItems = this.GetInquireItemsByInquireType();
  }
  [XmlAttribute]
  public int FieldDisplayOrder { get; set; }
  [XmlAttribute]
  public string FieldName { get; set; }
  [XmlAttribute]
  public string DbName { get; set; }
  [XmlAttribute]
  public bool IsAggregate { get; set; }
  [XmlAttribute]
  public InquireHelper.FieldType FieldType { get; set; }
  //用于highchart统计
  [XmlAttribute]
  public bool IsNameField { get; set; }
  //用于统计输出数据
  [XmlAttribute]
  public bool IsPercent { get; set; }
  [XmlIgnore]
  public List<string> FieldItems { get; set; }
  public List<string> FieldValue { get; set; }
  public bool? OrderByAsc { get; set; }
 }
 [Serializable]
 public class BYNF_InquireField : InquireFieldBase
 {
  public BYNF_InquireField()
  {
   FieldDisplayOrder = 1;
   FieldName = "毕业年份";
   DbName = "BYNF";
  }
 }
 [Serializable]
 public class XYMC_InquireField : InquireFieldBase
 {
  public XYMC_InquireField()
  {
   FieldDisplayOrder = 5;
   FieldName = "学院名称";
   DbName = "XYMC";
  }
 }
 [Serializable]
 public class ZYMC_InquireField : InquireFieldBase
 {
  public ZYMC_InquireField()
  {
   FieldDisplayOrder = 6;
   FieldName = "专业名称";
   DbName = "ZYMC";
  }
 }
 [Serializable]
 public class SZBJ_InquireField : InquireFieldBase
 {
  public SZBJ_InquireField()
  {
   FieldDisplayOrder = 7;
   FieldName = "所在班级";
   DbName = "SZBJ";
  }
 }
 [Serializable]
 public class FDY_InquireField : InquireFieldBase
 {
  public FDY_InquireField()
  {
   FieldDisplayOrder = 8;
   FieldName = "辅导员";
   DbName = "FDY";
  }
 }
 [Serializable]
 public class XL_InquireField : InquireFieldBase
 {
  public XL_InquireField()
  {
   FieldDisplayOrder = 9;
   FieldName = "学历";
   DbName = "XLMC";
  }
 }
 [Serializable]
 public class SYDQ_InquireField : InquireFieldBase
 {
  public SYDQ_InquireField()
  {
   FieldDisplayOrder = 10;
   FieldName = "生源地区";
   DbName = "SYDQ";
  }
 }
 [Serializable]
 public class SYSF_InquireField : InquireFieldBase
 {
  public SYSF_InquireField()
  {
   FieldDisplayOrder = 11;
   FieldName = "生源省份";
   DbName = "SYSF";
  }
 }
 [Serializable]
 public class DWDQ_InquireField : InquireFieldBase
 {
  public DWDQ_InquireField()
  {
   FieldDisplayOrder = 12;
   FieldName = "单位地区";
   DbName = "DWDQ";
  }
 }
 [Serializable]
 public class DWSF_InquireField : InquireFieldBase
 {
  public DWSF_InquireField()
  {
   FieldDisplayOrder = 13;
   FieldName = "单位省份";
   DbName = "DWSF";
  }
 }

Control class



public static class InquireHelper
 {
  public static List<InquireFieldBase> GetSubInquireList()
  {
   var inquires = new List<InquireFieldBase>();
   var subTypeQuery = from t in Assembly.GetExecutingAssembly().GetTypes()
        where IsSubClassOf(t, typeof(InquireFieldBase))
        select t;
   foreach (var type in subTypeQuery)
   {
    InquireFieldBase obj = CreateObject(type.FullName) as InquireFieldBase;
    if (obj != null)
    {
     inquires.Add(obj);
    }
   }
   return inquires;
  }
  static bool IsSubClassOf(Type type, Type baseType)
  {
   var b = type.BaseType;
   while (b != null)
   {
    if (b.Equals(baseType))
    {
     return true;
    }
    b = b.BaseType;
   }
   return false;
  }
  /// <summary>
  /// 创建对象(当前程序集)
  /// </summary>
  /// <param name="typeName">类型名</param>
  /// <returns>创建的对象,失败返回 null</returns>
  public static object CreateObject(string typeName)
  {
   object obj = null;
   try
   {
    Type objType = Type.GetType(typeName, true);
    obj = Activator.CreateInstance(objType);
   }
   catch (Exception ex)
   {
   }
   return obj;
  }
  public static List<InquireFieldBase> BindCondition(this List<InquireFieldBase> conditions, string conditionName, List<string> values)
  {
   var condition = conditions.FirstOrDefault(c => c.GetType().Name == conditionName && c.FieldType == FieldType.ConditionField);
   if (condition == null)
   {
    condition = CreateObject("BLL." + conditionName) as InquireFieldBase;
    condition.FieldType = FieldType.ConditionField;
    conditions.Add(condition);
   }
   condition.FieldValue = values;
   return conditions;
  }
  //public static List<InquireFieldBase> BindCondition(this List<InquireFieldBase> conditions, string conditionName, string range1, string range2)
  //{
  // var condition = conditions.FirstOrDefault(c => c.GetType().Name == conditionName && c.FieldType == FieldType.ConditionField);
  // if (!string.IsNullOrEmpty(range2)&&!string.IsNullOrEmpty(range1))
  // {
  //  if (condition == null)
  //  {
  //   condition = CreateObject("BLL." + conditionName) as InquireFieldBase;
  //   condition.FieldType = FieldType.ConditionField;
  //   conditions.Add(condition);
  //  }
  //  condition.FieldValue = string.Concat(condition.DbName,
  //   " between to_date(&#39;", range1, "&#39;, &#39;yyyy-mm-dd hh24:mi:ss&#39;) and to_date(&#39;", range2,
  //   "&#39;, &#39;yyyy-mm-dd hh24:mi:ss&#39;)");
  // }
  // return conditions;
  //}
  public static DataTable GetDataTable(StatisticsInquire inquire)
  {
   var inquireCond = new List<string>();
   inquire.InquireFields.Where(f => f.FieldType == InquireHelper.FieldType.GroupField).ToList()
    .ForEach(f =>
    {
     if (!f.IsAggregate)
     {
      inquireCond.Add(string.Concat(f.DbName, " AS ", f.FieldName));
     }
    });
   inquire.InquireFields.Where(f => f.FieldType == FieldType.DisplayField).ToList().ToList()
    .ForEach(f => {
     if (f.IsAggregate)
     {
      inquireCond.Add(string.Concat(f.DbName, " AS ", f.FieldName));
     }
     else
     {
      if (f.IsPercent)
      {
       inquireCond.Add(string.Concat("ltrim(Convert(numeric(9,2), SUM(CASE WHEN ", f.DbName, " IN (&#39;", string.Join("&#39;, &#39;", f.FieldValue), "&#39;) THEN 1 ELSE 0 END)*100.0/Count(*))) + &#39;%&#39; AS &#39;", f.FieldName, ":", string.Join(",", f.FieldValue).SubStr(60), "(%)&#39;"));
      }
      else
      {
       inquireCond.Add(string.Concat("SUM(CASE WHEN ", f.DbName, " IN (&#39;", string.Join("&#39;, &#39;", f.FieldValue) , "&#39;) THEN 1 ELSE 0 END) AS &#39;", f.FieldName, ":", string.Join(",", f.FieldValue).SubStr(60), "&#39;"));
      }
     }
    });
   var whereCond = new List<string>();
   inquire.InquireFields.Where(f => f.FieldType == InquireHelper.FieldType.ConditionField).ToList()
    .ForEach(f =>
    {
     whereCond.Add(string.Concat(f.DbName, " IN (&#39;", string.Join("&#39;,&#39;", f.FieldValue), "&#39;)"));
    });
   var groupCond = new List<string>();
   inquire.InquireFields.Where(f => f.FieldType == InquireHelper.FieldType.GroupField).ToList()
    .ForEach(f =>
    {
     groupCond.Add(f.DbName);
    });
   var orderbyCond = new List<string>();
   inquire.InquireFields.Where(f => f.FieldType == InquireHelper.FieldType.OrderByField).ToList()
    .ForEach(f =>
    {
     orderbyCond.Add(string.Concat(f.DbName, " ", f.OrderByAsc.GetValueOrDefault() ? "ASC" : "DESC"));
    });
   var sqlStr = string.Concat("SELECT ",
    string.Join(", ", inquireCond),
    " FROM GetStudentStatusByGxsj(&#39;", inquire.StatisticsDate , "&#39;)",
    whereCond.Any() ? " WHERE " : string.Empty,
    string.Join(" AND ", whereCond),
    groupCond.Any() ? " GROUP BY " : string.Empty,
    (inquire.ShowSubSummary || inquire.ShowSummary)
     ? string.Concat("rollup(", string.Join(", ", groupCond), ")")
     : string.Join(", ", groupCond),
    orderbyCond.Any() ? " ORDER BY " : string.Empty,
    string.Join(", ", orderbyCond));
   var dt = DBUtility.DbHelperSql.Query(sqlStr).Tables[0];
   if (!inquire.ShowSubSummary)
   {
    if (inquire.ShowSummary)
    {
     var col = inquire.InquireFields.Where(f => f.FieldType == InquireHelper.FieldType.GroupField).Count();
     for(int i = dt.Rows.Count - 2; i >=0 ; i -- ){
      if (dt.Rows[i][col - 1].ToString() == "")
      {
       dt.Rows.RemoveAt(i);
       //dt.Rows.Remove[dt.Rows[i]);
      }
     }
    }
   }
   else
   {
    var col = inquire.InquireFields.Where(f => f.FieldType == InquireHelper.FieldType.GroupField).Count();
    for (int i = 0; i < dt.Rows.Count - 1; i++)
    {
     for (int j = 1; j < col; j++)
     {
      if (dt.Rows[i][j].ToString() == "")
      {
       dt.Rows[i][j] = "小计";
       break;
      }
     }
    }
   }
   if (inquire.ShowSubSummary || inquire.ShowSummary)
   {
    dt.Rows[dt.Rows.Count - 1][0] = "合计";
   }
   return dt;
  }
  public static string SubStr(this string str, int maxLength)
  {
   if (str.Length > maxLength)
   {
    return str.Substring(0, maxLength - 1);
   }
   else
   {
    return str;
   }
  }
  public static string ToSerializableXML<T>(this T t)
  {
   XmlSerializer mySerializer = new XmlSerializer(typeof(T));
   StringWriter sw = new StringWriter();
   mySerializer.Serialize(sw, t);
   return sw.ToString();
  }
  public static T ToEntity<T>(this string xmlString)
  {
   var xs = new XmlSerializer(typeof(T));
   var srReader = new StringReader(xmlString);
   var steplist = (T)xs.Deserialize(srReader);
   return steplist;
  }
  public enum FieldType
  {
   DisplayField, GroupField, ConditionField, OrderByField
  }
  private static ConcurrentDictionary<InquireFieldBase, List<string>> _inquireItems = new ConcurrentDictionary<InquireFieldBase,List<string>>();
  public static List<string> GetInquireItemsByInquireType(this InquireFieldBase inquireField)
  {
   List<string> inquireItems;
   if (_inquireItems.TryGetValue(inquireField, out inquireItems))
   {
    return inquireItems;
   }
   switch (inquireField.GetType().Name)
   {
    case "XYMC_InquireField":
     inquireItems = new BLL.depacode().GetModelList("").OrderBy(d => d.xydm).Select(d => d.xymc).ToList();
     break;
    case "ZYMC_InquireField":
     inquireItems = new BLL.profcode().GetModelList("").OrderBy(d => d.xydm).ThenBy(d => d.zydm).Select(d => d.zymc).ToList();
     break;
    case "SZBJ_InquireField":
     inquireItems = DbHelperSql.Query("select distinct szbj from jbdate order by szbj").Tables[0].AsEnumerable().Select(b => b["szbj"].ToString()).ToList();
     break;
    case "FDY_InquireField":
     inquireItems = new BLL.DepaUser().GetModelList("").OrderBy(d => d.XYDM).ThenBy(y => y.YHXM).Select(d => d.YHXM).ToList();
     break;
    case "XL_InquireField":
     inquireItems = new[] { "博士", "硕士", "双学位", "本科", "专科", "高职" }.ToList();
     break;
    case "SYDQ_InquireField":
     inquireItems = new[] { "东部", "中部", "西部" }.ToList();
     break;
    case "SYSF_InquireField":
     inquireItems = DbHelperSql.Query("select [Name] from [Sydqdm] where RIGHT([code], 4) = &#39;0000&#39; order by code").Tables[0].AsEnumerable().Select(b => b["Name"].ToString()).ToList();
     break;
    case "DWDQ_InquireField":
     inquireItems = new[] { "东部", "中部", "西部" }.ToList();
     break; 
    case "DWSF_InquireField":
     inquireItems = DbHelperSql.Query("select [Name] from [Sydqdm] where RIGHT([code], 4) = &#39;0000&#39; order by code").Tables[0].AsEnumerable().Select(b => b["Name"].ToString()).ToList();
     break;
    case "HYML_InquireField":
     inquireItems = DbHelperSql.Query("select distinct hyml from [hydygx]").Tables[0].AsEnumerable().Select(b => b["hyml"].ToString()).ToList();
     break;
    case "HYDL_InquireField":
     inquireItems = DbHelperSql.Query("select hydl from [hydygx] order by hydldm").Tables[0].AsEnumerable().Select(b => b["hydl"].ToString()).ToList();
     break;
    case "XBMC_InquireField":
     inquireItems = new[] { "男", "女" }.ToList();
     break;
    case "MZMC_InquireField":
     inquireItems = DbHelperSql.Query("select nation from [mzdmb] where nation in (select nation from jbdate) order by mzdm").Tables[0].AsEnumerable().Select(b => b["nation"].ToString()).ToList();
     break;
    case "BYQX_InquireField":
     inquireItems = new BLL.Byqxdmb().GetModelList("").OrderBy(d => d.Byqxdm).Select(d => d.Byqxmc).ToList();
     break;
    case "KNSLB_InquireField":
     inquireItems = new[] { "就业困难、家庭困难和残疾", "家庭困难和残疾", "就业困难和残疾", "残疾", "就业和家庭困难", "家庭困难", "就业困难", "非困难生" }.ToList();
     break;
    case "ZYDKL_InquireField":
     inquireItems = new[] { "专业对口", "专业相关", "不对口", "未填写" }.ToList();
     break;
    case "DWXZ_InquireField":
     inquireItems = new BLL.Dwxz().GetModelList("").OrderBy(d => d.dwxzdm).Select(d => d.dwxzmc).ToList();
     break;
    case "EJBYQXMC_InquireField":
     inquireItems = new BLL.EjByqxdmb().GetModelList("").OrderBy(d => d.Ejbyqxdm).Select(d => d.Ejbyqxmc).ToList();
     break;
   }
   if (inquireItems != null)
   {
    _inquireItems[inquireField] = inquireItems;
    return inquireItems;
   }
   return new List<string>();
  }
 }
 [Serializable]
 public class StatisticsInquire
 {
  public List<InquireFieldBase> InquireFields { get; set; } 
  [XmlAttribute]
  public bool ShowSummary { get; set; }
  [XmlAttribute]
  public bool ShowSubSummary { get; set; }
  [XmlAttribute]
  public string StatisticsDate { get; set; }
  [XmlAttribute]
  public HighChart.ChartType ChartType { get; set; }
 }

In actual use, it is still very convenient

It is expected that future versions will need to produce functions:

Further optimization of statistical fields and the ability to use multiple conditions Combining and filtering the same field is relatively simple, just extend the next class and adjust the UI.


The above is the detailed content of Using Asp.net to implement data statistics function of information management system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn