using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using BLL; using System.Data; using System.Data.SqlClient; namespace FineUIPro.Web.JDGL.SGManPower { public partial class ManPowerWorkChart : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 初始化图表数据 InitializeChartData(); } } private void InitializeChartData() { try { // 获取查询参数 string unitId = Request.Params["UnitId"]; string startTime = Request.Params["StartTime"]; string endTime = Request.Params["EndTime"]; string workPostId = Request.Params["WorkPostId"]; string workAreaId = Request.Params["WorkAreaId"]; // 检查必要参数 if (string.IsNullOrEmpty(startTime) || string.IsNullOrEmpty(endTime)) { ShowNotify("缺少必要的参数,无法生成图表", MessageBoxIcon.Warning); return; } DateTime startDate = Convert.ToDateTime(startTime); DateTime endDate = Convert.ToDateTime(endTime); var getData = Funs.DB.SitePerson_Checking_Statistics.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.IntoOutTime >= startDate && x.IntoOutTime <= endDate); // 添加单位筛选条件 if (!string.IsNullOrEmpty(unitId) && unitId != Const._Null) { getData = getData.Where(x => x.UnitId == unitId); } // 添加岗位筛选条件 if (!string.IsNullOrEmpty(workPostId) && workPostId != Const._Null) { getData = getData.Where(x => x.WorkPostId == workPostId); } DataTable dt = this.LINQToDataTable(getData.ToList()); // 创建一个新的DataTable来存储处理后的数据 DataTable processedDt = dt.Clone(); foreach (System.Data.DataRow row in dt.Rows) { // string ids = row["WorkAreaId"] != DBNull.Value ? row["WorkAreaId"].ToString() : string.Empty; // string names = row["WorkAreaName"] != DBNull.Value ? row["WorkAreaName"].ToString() : string.Empty; // if (!string.IsNullOrEmpty(names)) // { // string[] unitWorkNameArray = // names.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // string[] unitWorkIdArray = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // for (var i = 0; i < unitWorkNameArray.Length; i++) // { // // 往processedDt里面添加数据,每个UnitWorkId一行 // DataRow newRow = processedDt.NewRow(); // newRow["UnitId"] = row["UnitId"]; // newRow["UnitName"] = row["UnitName"]; // newRow["WorkPostId"] = row["WorkPostId"]; // newRow["WorkPostName"] = row["WorkPostName"]; // newRow["IntoOutTime"] = row["IntoOutTime"] ?? DBNull.Value; // newRow["WorkAreaId"] = unitWorkIdArray[i].Trim(); // newRow["WorkAreaName"] = unitWorkNameArray[i].Trim(); // newRow["num"] = row["num"] != DBNull.Value ? Convert.ToInt32(row["num"]) : 0; // a++; // processedDt.Rows.Add(newRow); // } // } // else // { DataRow newRow = processedDt.NewRow(); newRow["UnitId"] = row["UnitId"]; newRow["UnitName"] = row["UnitName"]; newRow["WorkPostId"] = row["WorkPostId"]; newRow["WorkPostName"] = row["WorkPostName"]; newRow["IntoOutTime"] = row["IntoOutTime"] ?? DBNull.Value; newRow["WorkAreaId"] = row["WorkAreaId"]; newRow["WorkAreaName"] = row["WorkAreaName"]; newRow["num"] = row["num"] != DBNull.Value ? Convert.ToInt32(row["num"]) : 0; processedDt.Rows.Add(newRow); // } } // 确定要用于后续处理的数据源 IEnumerable dataSource = processedDt.AsEnumerable(); // 如果选择了特定的装置进行筛选 if (!string.IsNullOrEmpty(workAreaId) && workAreaId != Const._Null) { dataSource = dataSource.Where(x => x["WorkAreaId"] != DBNull.Value && x["WorkAreaId"].ToString() == workAreaId); } var da = dataSource.ToList(); //针对dt中的数据进行分组 var data = dataSource .GroupBy(row => new { IntoOutTime = row["IntoOutTime"] }).Select(group => new { IntoOutTime = group.Key.IntoOutTime, RecordCount = group.Sum(row => row["num"] != DBNull.Value ? Convert.ToInt32(row["num"]) : 0) }).OrderBy(x => x.IntoOutTime).ToList(); // 如果没有数据,显示提示信息 if (data.Count == 0) { ShowNotify("在指定时间范围内没有找到人力统计数据", MessageBoxIcon.Warning); return; } // 根据数据点数量动态调整图表宽度,确保每个数据点有足够的显示空间 int chartWidth = Math.Max(1000, data.Count * 50); // 每个数据点至少50像素宽,最小1000像素 // 创建图表数据对象 Model.DataSourceChart dataSourceChart = new Model.DataSourceChart { Title = "现场考勤人力统计图表", ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column, Width = chartWidth, Height = 500, IsNotEnable3D = false }; // 创建数据系列 Model.DataSourceTeam dataSourceTeam = new Model.DataSourceTeam { DataPointName = "人员数量", DataSourcePoints = new List() }; // 添加数据点 foreach (var item in data) { if (item.IntoOutTime != null) { DateTime IntoOutTime = Convert.ToDateTime(item.IntoOutTime); Model.DataSourcePoint point = new Model.DataSourcePoint { PointText = IntoOutTime.ToString("MM-dd"), PointValue = item.RecordCount.ToString() }; dataSourceTeam.DataSourcePoints.Add(point); } } dataSourceChart.DataSourceTeams = new List { dataSourceTeam }; // 生成图表 Chart1.CreateChart(dataSourceChart); } catch (Exception ex) { ShowNotify($"生成图表时发生错误: {ex.Message}", MessageBoxIcon.Error); } } } }