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"]; // 检查必要参数 if (string.IsNullOrEmpty(startTime) || string.IsNullOrEmpty(endTime)) { ShowNotify("缺少必要的参数,无法生成图表", MessageBoxIcon.Warning); return; } DateTime startDate = Convert.ToDateTime(startTime); DateTime endDate = Convert.ToDateTime(endTime); // 使用原生SQL查询直接在数据库中进行聚合计算,提高性能 string strSql = @" SELECT RecordDate, COUNT(1) as TotalCount FROM T_d_EmployInOutRecord WHERE ProjectId = @ProjectId AND RecordDate >= @StartDate AND RecordDate <= @EndDate"; var parameters = new List { new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId), new SqlParameter("@StartDate", startDate), new SqlParameter("@EndDate", endDate) }; // 添加单位筛选条件 if (!string.IsNullOrEmpty(unitId) && unitId != Const._Null) { strSql += " AND UnitId = @UnitId"; parameters.Add(new SqlParameter("@UnitId", unitId)); } // 添加岗位筛选条件 if (!string.IsNullOrEmpty(workPostId) && workPostId != Const._Null) { strSql += " AND PostId = @WorkPostId"; parameters.Add(new SqlParameter("@WorkPostId", workPostId)); } // 按日期分组并排序 strSql += " GROUP BY RecordDate ORDER BY RecordDate"; // 执行查询 var dt = SQLHelper.GetDataTableRunText(strSql, parameters.ToArray()); // 如果没有数据,显示提示信息 if (dt.Rows.Count == 0) { ShowNotify("在指定时间范围内没有找到人力统计数据", MessageBoxIcon.Warning); return; } // 根据数据点数量动态调整图表宽度,确保每个数据点有足够的显示空间 int chartWidth = Math.Max(1000, dt.Rows.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 (DataRow row in dt.Rows) { if (row["RecordDate"] != DBNull.Value) { DateTime recordDate = Convert.ToDateTime(row["RecordDate"]); int totalCount = Convert.ToInt32(row["TotalCount"]); Model.DataSourcePoint point = new Model.DataSourcePoint { PointText = recordDate.ToString("MM-dd"), PointValue = totalCount.ToString() }; dataSourceTeam.DataSourcePoints.Add(point); } } dataSourceChart.DataSourceTeams = new List { dataSourceTeam }; // 生成图表 Chart1.CreateChart(dataSourceChart); } catch (Exception ex) { ShowNotify($"生成图表时发生错误: {ex.Message}", MessageBoxIcon.Error); } } } }