CNCEC_SUBQHSE_WUHUAN/SGGL/FineUIPro.Web/JDGL/SGManPower/ManPowerWorkChart.aspx.cs

180 lines
8.0 KiB
C#
Raw Normal View History

2025-08-11 14:21:24 +08:00
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"];
2025-12-01 10:24:34 +08:00
string workAreaId = Request.Params["WorkAreaId"];
2025-08-11 14:21:24 +08:00
// 检查必要参数
if (string.IsNullOrEmpty(startTime) || string.IsNullOrEmpty(endTime))
{
ShowNotify("缺少必要的参数,无法生成图表", MessageBoxIcon.Warning);
return;
}
DateTime startDate = Convert.ToDateTime(startTime);
DateTime endDate = Convert.ToDateTime(endTime);
2025-12-01 10:24:34 +08:00
var getData = Funs.DB.SitePerson_Checking_Statistics.Where(x =>
x.ProjectId == this.CurrUser.LoginProjectId && x.IntoOutTime >= startDate && x.IntoOutTime <= endDate);
2025-08-11 14:21:24 +08:00
// 添加单位筛选条件
if (!string.IsNullOrEmpty(unitId) && unitId != Const._Null)
{
2025-12-01 10:24:34 +08:00
getData = getData.Where(x => x.UnitId == unitId);
2025-08-11 14:21:24 +08:00
}
2025-12-01 10:24:34 +08:00
2025-08-11 14:21:24 +08:00
// 添加岗位筛选条件
if (!string.IsNullOrEmpty(workPostId) && workPostId != Const._Null)
{
2025-12-01 10:24:34 +08:00
getData = getData.Where(x => x.WorkPostId == workPostId);
2025-08-11 14:21:24 +08:00
}
2025-12-01 10:24:34 +08:00
DataTable dt = this.LINQToDataTable(getData.ToList());
2025-08-21 11:37:06 +08:00
// 创建一个新的DataTable来存储处理后的数据
DataTable processedDt = dt.Clone();
foreach (System.Data.DataRow row in dt.Rows)
{
2025-12-01 10:24:34 +08:00
// 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
// {
2025-08-21 11:37:06 +08:00
DataRow newRow = processedDt.NewRow();
newRow["UnitId"] = row["UnitId"];
2025-12-01 10:24:34 +08:00
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;
2025-08-21 11:37:06 +08:00
processedDt.Rows.Add(newRow);
2025-12-01 10:24:34 +08:00
// }
2025-08-21 11:37:06 +08:00
}
// 确定要用于后续处理的数据源
IEnumerable<DataRow> dataSource = processedDt.AsEnumerable();
// 如果选择了特定的装置进行筛选
2025-12-01 10:24:34 +08:00
if (!string.IsNullOrEmpty(workAreaId) && workAreaId != Const._Null)
2025-08-21 11:37:06 +08:00
{
2025-12-01 10:24:34 +08:00
dataSource = dataSource.Where(x => x["WorkAreaId"] != DBNull.Value && x["WorkAreaId"].ToString() == workAreaId);
2025-08-21 11:37:06 +08:00
}
2025-12-01 10:24:34 +08:00
var da = dataSource.ToList();
2025-08-21 11:37:06 +08:00
//针对dt中的数据进行分组
var data = dataSource
.GroupBy(row => new {
2025-12-01 10:24:34 +08:00
IntoOutTime = row["IntoOutTime"]
2025-08-21 11:37:06 +08:00
}).Select(group => new
{
2025-12-01 10:24:34 +08:00
IntoOutTime = group.Key.IntoOutTime,
RecordCount = group.Sum(row => row["num"] != DBNull.Value ? Convert.ToInt32(row["num"]) : 0)
}).OrderBy(x => x.IntoOutTime).ToList();
2025-08-21 11:37:06 +08:00
2025-08-11 14:21:24 +08:00
// 如果没有数据,显示提示信息
2025-08-21 11:37:06 +08:00
if (data.Count == 0)
2025-08-11 14:21:24 +08:00
{
ShowNotify("在指定时间范围内没有找到人力统计数据", MessageBoxIcon.Warning);
return;
}
// 根据数据点数量动态调整图表宽度,确保每个数据点有足够的显示空间
2025-08-21 11:37:06 +08:00
int chartWidth = Math.Max(1000, data.Count * 50); // 每个数据点至少50像素宽最小1000像素
2025-08-11 14:21:24 +08:00
// 创建图表数据对象
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<Model.DataSourcePoint>()
};
// 添加数据点
2025-08-21 11:37:06 +08:00
foreach (var item in data)
2025-08-11 14:21:24 +08:00
{
2025-12-01 10:24:34 +08:00
if (item.IntoOutTime != null)
2025-08-11 14:21:24 +08:00
{
2025-12-01 10:24:34 +08:00
DateTime IntoOutTime = Convert.ToDateTime(item.IntoOutTime);
2025-08-11 14:21:24 +08:00
Model.DataSourcePoint point = new Model.DataSourcePoint
{
2025-12-01 10:24:34 +08:00
PointText = IntoOutTime.ToString("MM-dd"),
2025-08-21 11:37:06 +08:00
PointValue = item.RecordCount.ToString()
2025-08-11 14:21:24 +08:00
};
dataSourceTeam.DataSourcePoints.Add(point);
}
}
dataSourceChart.DataSourceTeams = new List<Model.DataSourceTeam> { dataSourceTeam };
// 生成图表
Chart1.CreateChart(dataSourceChart);
}
catch (Exception ex)
{
ShowNotify($"生成图表时发生错误: {ex.Message}", MessageBoxIcon.Error);
}
}
}
}