echart睡眠后台代码,
//睡眠时间-add/2021/3/2
case "sleep":
//1判断是天睡眠还是时间区间睡眠
//2:睡眠开始>睡眠结束{开始=时间-1天,结束=今天},睡眠开始<睡眠结束{开始=时间,结束=今天}
//3:0~5翻滚次数=深度睡眠,5~10次翻滚次数=浅度睡眠,>10=清醒;
//4:合计深度睡眠时间,合计浅度睡眠时间,合计清醒时间
//5:根据翻滚次数来显示不同的颜色(标记睡眠质量)
//6:统计时间和睡眠时间如何实现同步???
//7:barCategoryGap属性使用,option属性加载
//1周睡眠,把开始时间,结束时间,转换为星期,存list时间保存
//2周睡眠,把周睡眠数据的时间==list时间,对比,存在,则取出,存入深睡字典,浅水字典,清醒字典
//3周睡眠数据时间!=list时间,list.add(list时间,0);
string D_SleepBegin = "";
string D_SleepEnd = "";
mdl.wd_device device2 = new bll.wd_device().GetModel(deviceID);
if (device2 != null)
{
D_SleepBegin = device2.D_SleepBegin;
D_SleepEnd = device2.D_SleepEnd;
}
int deepRate = 0;
int lightRate = 0;
int wakeRate = 0;
int.TryParse(ProUtil.deepRate, out deepRate);//深睡翻滚频率
int.TryParse(ProUtil.lightRate, out lightRate);//浅睡翻滚频率
int.TryParse(ProUtil.wakeRate, out wakeRate);//清醒翻滚频率
string beginTime1 = "";
string EndTime1 = "";
int checkBegin = D_SleepBegin != "" ? Convert.ToInt32(D_SleepBegin.Substring(0, 2)) : 0;
int checkEnd = D_SleepEnd != "" ? Convert.ToInt32(D_SleepEnd.Substring(0, 2)) : 0;
string sleep1 = D_SleepBegin != "" ? D_SleepBegin.Substring(0, 2) + ":" + D_SleepBegin.Substring(2, 2) : "23:00";
string sleep2 = D_SleepEnd != "" ? D_SleepEnd.Substring(0, 2) + ":" + D_SleepEnd.Substring(2, 2) : "7:00";
if (context.Request["sleepType"].ToString() == "day")
{
#region 日睡眠统计
if (checkBegin > checkEnd)
{
beginTime1 = Convert.ToDateTime(endTime).AddDays(-1).ToString("yyyy-MM-dd") + " " + sleep1;
EndTime1 = Convert.ToDateTime(endTime).ToString("yyyy-MM-dd") + " " + sleep2;
}
else
{
beginTime1 = Convert.ToDateTime(endTime).ToString("yyyy-MM-dd") + " " + sleep1;
EndTime1 = Convert.ToDateTime(endTime).ToString("yyyy-MM-dd") + " " + sleep2;
}
string sleepStr = string.Format(@"select DATE_FORMAT(S_MeasureEndTime,'%H:00') hours,sum(S_RollCount) count from wd_sport where
S_MeasureEndTime>='{0}' and S_MeasureEndTime<'{1}' and L_DID={2} and S_state=0 group by hours order by hours", beginTime1, EndTime1, deviceID);
DataSet ds2 = T9.DBUtility.DbHelperMySQL.Query(sleepStr);
DataTable dt2 = ds2.Tables[0];
int rowCount2 = dt2.Rows.Count;
List<string> listTime = new List<string>();
if (rowCount2 > 0)
{
#region 记录一天的小时时间
for (int i = 0; i < rowCount2; i++)
{
listTime.Add(dt2.Rows[i]["hours"].ToString());
}
#endregion
#region 计算小时睡眠时长
int DeepHour = 0;//深度睡眠时间
int lightHour = 0;//浅度睡眠时间
int wakeHour = 0;//清醒时间
for (int i = 0; i < rowCount2; i++)
{
int S_RollCount = Convert.ToInt32(dt2.Rows[i]["count"].ToString());
if (S_RollCount >= 0 && S_RollCount <= deepRate)
{
DeepHour = DeepHour + 1;
}
else if (S_RollCount >= lightRate && S_RollCount < wakeRate)
{
lightHour = lightHour + 1;
}
else if (S_RollCount >= wakeRate)
{
wakeHour = wakeHour + 1;
}
}
#endregion
#region 组合list对象
sleepdayDetail[] Data = new sleepdayDetail[rowCount2];
for (int i = 0; i < rowCount2; i++)
{
int S_RollCount = Convert.ToInt32(dt2.Rows[i]["count"].ToString());
if (S_RollCount >= 0 && S_RollCount <= deepRate)
{
//深度睡眠
itemStyle itemStyle1 = new itemStyle();
itemStyle1.color = "#0c5290";
Data[i] = new sleepdayDetail() { value = new string[2] { dt2.Rows[i]["hours"].ToString(), DeepHour.ToString() } };
Data[i].itemStyle = itemStyle1;
}
else if (S_RollCount >= lightRate && S_RollCount < wakeRate)
{
//浅度睡眠
itemStyle itemStyle2 = new itemStyle();
itemStyle2.color = "#4da9f2";
Data[i] = new sleepdayDetail() { value = new string[2] { dt2.Rows[i]["hours"].ToString(), lightHour.ToString() } };
Data[i].itemStyle = itemStyle2;
}
else if (S_RollCount >= wakeRate)
{
//清醒
itemStyle itemStyle3 = new itemStyle();
itemStyle3.color = "#fe027b";
Data[i] = new sleepdayDetail() { value = new string[2] { dt2.Rows[i]["hours"].ToString(), wakeHour.ToString() } };
Data[i].itemStyle = itemStyle3;
}
}
#endregion
datas.Add(new ChartDetail()
{
sleeptimes = listTime.ToArray(),
sleepdata = Data,
type = "bar",
});
result.sleepDeep = DeepHour;
result.sleepLight = lightHour;
result.sleepWake = wakeHour;
result.Return = true;
result.Data = datas;
}
else
{
result.Return = false;
}
#endregion
}
else if (context.Request["sleepType"].ToString() == "week")
{
//a:前台传开始时间和结束时间
//b:从设备中查询出开始睡眠和结束时间时间
//c:开始睡眠>结束睡眠;时间>23点,时间+1天
#region 变量定义
List<string> listTime = new List<string>();
List<int> listdata1 = new List<int>();
List<int> listdata2 = new List<int>();
List<int> listdata3 = new List<int>();
Dictionary<string, int> dicDeep = new Dictionary<string, int>();//深睡字典
Dictionary<string, int> dicLigth = new Dictionary<string, int>();//浅水字典
Dictionary<string, int> dicWake = new Dictionary<string, int>();//清醒字典
#endregion
#region 返回周数据
DataTable dtweek = null;
int rowCountweek = 0;
LoadSql(deviceID, ref dtweek, ref rowCountweek, checkBegin, checkEnd, beginTime, endTime, sleep1, sleep2);
#endregion
#region 日期转换为星期几
var days = (endTime - beginTime).Days + 1;
for (int j = 0; j < days; j++)
{
string year = beginTime.AddDays(j).ToString("yyyy");
string month = beginTime.AddDays(j).ToString("MM");
string day = beginTime.AddDays(j).ToString("dd");
string time1 = ProUtil.CaculateWeekDay(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));
listTime.Add(time1);
}
#endregion
if (rowCountweek > 0)
{
result = LoadWeekJson(true, listTime, dtweek, dicDeep, dicLigth, dicWake, listdata1, listdata2, listdata3, deepRate, lightRate, wakeRate);
}
else
{
result = LoadWeekJson(false, listTime, dtweek, dicDeep, dicLigth, dicWake, listdata1, listdata2, listdata3, deepRate, lightRate, wakeRate);
}
}
break;
#region 执行周睡眠语句返回dataTable
/// <summary>
/// 执行周睡眠语句返回dataTable
/// </summary>
/// <param name="deviceID"></param>
/// <param name="dtweek"></param>
/// <param name="rowCountweek"></param>
/// <param name="checkBegin"></param>
/// <param name="checkEnd"></param>
/// <param name="beginTime"></param>
/// <param name="endTime"></param>
/// <param name="sleep1"></param>
/// <param name="sleep2"></param>
public void LoadSql(int deviceID, ref DataTable dtweek, ref int rowCountweek, int checkBegin, int checkEnd, DateTime beginTime, DateTime endTime, string sleep1, string sleep2)
{
#region 周睡眠统计语句
string beginTime1 = "";
string EndTime1 = "";
string sqlstr = "";
if (checkBegin > checkEnd)
{
beginTime1 = Convert.ToDateTime(beginTime).AddDays(-1).ToString("yyyy-MM-dd") + " " + sleep1;
EndTime1 = Convert.ToDateTime(endTime).ToString("yyyy-MM-dd") + " " + sleep2;
sqlstr = string.Format(@"SELECT strhour,SUM(S_RollCount) S_RollCount,SUBSTRING(strhour,1,8) strday,S_MeasureEndTime AS ttt,
(CASE WHEN CONVERT(DATE_FORMAT(S_MeasureEndTime,'%H'), UNSIGNED INTEGER)>=23 THEN
DATE_FORMAT(DATE_ADD(S_MeasureEndTime,INTERVAL 1 DAY),'%Y-%m-%d')
ELSE S_MeasureEndTime
END) S_MeasureEndTime
FROM
(
SELECT *,DATE_FORMAT(S_MeasureEndTime,'%Y%m%d%H') AS strhour FROM wd_sport
WHERE L_DID={0} AND S_state=0 AND S_MeasureEndTime>='{1}' AND S_MeasureEndTime<'{2}'
AND
(
({3}<=CONVERT(DATE_FORMAT(S_MeasureEndTime,'%H'), UNSIGNED INTEGER))
OR
(CONVERT(DATE_FORMAT(S_MeasureEndTime,'%H'), UNSIGNED INTEGER)>=0 AND CONVERT(DATE_FORMAT(S_MeasureEndTime,'%H'), UNSIGNED INTEGER)<{4})
)
) tblhours
GROUP BY strhour; ", deviceID, beginTime1, EndTime1, checkBegin, checkEnd);
}
else
{
beginTime1 = Convert.ToDateTime(beginTime).ToString("yyyy-MM-dd") + " " + sleep1;
EndTime1 = Convert.ToDateTime(endTime).ToString("yyyy-MM-dd") + " " + sleep2;
sqlstr = string.Format(@"SELECT strhour,SUM(S_RollCount) S_RollCount,SUBSTRING(strhour,1,8) strday,S_MeasureEndTime
FROM
(
SELECT *,DATE_FORMAT(S_MeasureEndTime,'%Y%m%d%H') AS strhour FROM wd_sport
WHERE L_DID={0} AND S_state=0 AND S_MeasureEndTime>='{1}' AND S_MeasureEndTime<'{2}'
AND
(
(CONVERT(DATE_FORMAT(S_MeasureEndTime,'%H'), UNSIGNED INTEGER)>={3} AND CONVERT(DATE_FORMAT(S_MeasureEndTime,'%H'), UNSIGNED INTEGER)<={4})
)
) tblhours
GROUP BY strhour; ", deviceID, beginTime1, EndTime1, checkBegin, checkEnd);
}
DataSet dsweeksleep = T9.DBUtility.DbHelperMySQL.Query(sqlstr);
dtweek = dsweeksleep.Tables[0];
rowCountweek = dtweek.Rows.Count;
#endregion
}
#endregion
#region 加载周睡眠json
/// <summary>
/// 加载周睡眠json
/// </summary>
/// <param name="listTime"></param>
/// <param name="dtweek"></param>
/// <param name="dicDeep"></param>
/// <param name="dicLigth"></param>
/// <param name="dicWake"></param>
/// <param name="listdata1"></param>
/// <param name="listdata2"></param>
/// <param name="listdata3"></param>
/// <param name="deepRate"></param>
/// <param name="lightRate"></param>
/// <param name="wakeRate"></param>
/// <param name="deepdata"></param>
/// <param name="ligthdata"></param>
/// <param name="wakedata"></param>
public ChartBack LoadWeekJson(
bool IsData,
List<string> listTime,
DataTable dtweek,
Dictionary<string, int> dicDeep,
Dictionary<string, int> dicLigth,
Dictionary<string, int> dicWake,
List<int> listdata1,
List<int> listdata2,
List<int> listdata3,
int deepRate = 0,
int lightRate = 0,
int wakeRate = 0)
{
ChartBack result = new ChartBack();
sleepweekDetail deepdata = new sleepweekDetail();
sleepweekDetail ligthdata = new sleepweekDetail();
sleepweekDetail wakedata = new sleepweekDetail();
List<sleepweekDetail> listWeek = new List<sleepweekDetail>();
#region 返回的list数据对象
InitSleepData(listTime, dtweek, dicDeep, dicLigth, dicWake, ref listdata1, ref listdata2, ref listdata3, deepRate, lightRate, wakeRate);
deepdata = new sleepweekDetail();
deepdata.name = "深睡";
deepdata.stack = "状态";
deepdata.type = "bar";
deepdata.barWidth = "40";
itemStyle itemstyle = new itemStyle();
itemstyle.color = "#0c5290";
deepdata.itemStyle = itemstyle;
string strArray = string.Join(",", listdata1);
int[] str1 = Array.ConvertAll<string, int>(strArray.Split(','), s => int.Parse(s));
deepdata.data = str1;
listWeek.Add(deepdata);
ligthdata = new sleepweekDetail();
ligthdata.name = "浅睡";
ligthdata.stack = "状态";
ligthdata.type = "bar";
itemStyle itemstyle2 = new itemStyle();
itemstyle2.color = "#4da9f2";
ligthdata.itemStyle = itemstyle2;
string strArray2 = string.Join(",", listdata2);
int[] str2 = Array.ConvertAll<string, int>(strArray2.Split(','), s => int.Parse(s));
ligthdata.data = str2;
listWeek.Add(ligthdata);
wakedata = new sleepweekDetail();
itemStyle itemstyle3 = new itemStyle();
itemstyle3.color = "#fe027b";
wakedata.itemStyle = itemstyle3;
wakedata.name = "清醒";
wakedata.stack = "状态";
wakedata.type = "bar";
string strArray3 = string.Join(",", listdata3);
int[] str3 = Array.ConvertAll<string, int>(strArray3.Split(','), s => int.Parse(s));
wakedata.data = str3;
listWeek.Add(wakedata);
result.sleepweekDetail = listWeek;
result.listtime = listTime;
if (IsData)
{
result.Return = true;
}
else
{
result.Return = false;
}
#endregion
return result;
}
#endregion
#region 计算睡眠数据add/2021/3/4
/// <summary>
/// 计算睡眠数据add/2021/3/4
/// </summary>
/// <param name="listTime">时间集合</param>
/// <param name="dtweek">周数据</param>
/// <param name="dicDeep">深睡字典数据</param>
/// <param name="dicLigth">浅睡字典数据</param>
/// <param name="dicWake">清醒字典数据</param>
/// <param name="listdata1">深睡数据</param>
/// <param name="listdata2">浅睡数据</param>
/// <param name="listdata3">清醒数据</param>
/// <param name="deepRate">深睡频率</param>
/// <param name="lightRate">浅睡频率</param>
/// <param name="wakeRate">清醒频率</param>
public void InitSleepData(
List<string> listTime,
DataTable dtweek,
Dictionary<string, int> dicDeep,
Dictionary<string, int> dicLigth,
Dictionary<string, int> dicWake, ref List<int> listdata1, ref List<int> listdata2, ref List<int> listdata3, int deepRate, int lightRate, int wakeRate
)
{
int deepHour = 0;
int lightHour = 0;
int wakeHour = 0;
foreach (var strtime in listTime)
{
if (dtweek.Rows.Count > 0)
{
for (int i = 0; i < dtweek.Rows.Count; i++)
{
int S_RollCount = Convert.ToInt32(dtweek.Rows[i]["S_RollCount"].ToString());
string checktime = Convert.ToDateTime(dtweek.Rows[i]["S_MeasureEndTime"]).ToString("yyyy-MM-dd");
#region 睡眠类型
string year = Convert.ToDateTime(dtweek.Rows[i]["S_MeasureEndTime"]).ToString("yyyy");
string month = Convert.ToDateTime(dtweek.Rows[i]["S_MeasureEndTime"]).ToString("MM");
string day = Convert.ToDateTime(dtweek.Rows[i]["S_MeasureEndTime"]).ToString("dd");
string time1 = ProUtil.CaculateWeekDay(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));
if (strtime == time1)
{
#region 睡眠翻滚次数统计
if (S_RollCount >= 0 && S_RollCount <= deepRate)
{
if (dtweek.Rows[i]["S_RollCount"] != null && dtweek.Rows[i]["S_RollCount"].ToString() != "")
{
if (!dicDeep.ContainsKey(time1))
{
dicDeep.Add(time1, deepHour + 1);
}
else
{
int deepOld = 0;
dicDeep.TryGetValue(time1, out deepOld);
dicDeep[time1] = deepOld + 1;
}
}
else
{
if (!dicDeep.ContainsKey(time1))
{
dicDeep.Add(time1, 0);
}
}
}
else if (S_RollCount >= lightRate && S_RollCount < wakeRate)
{
if (dtweek.Rows[i]["S_RollCount"] != null && dtweek.Rows[i]["S_RollCount"].ToString() != "")
{
if (!dicLigth.ContainsKey(time1))
{
dicLigth.Add(time1, lightHour + 1);
}
else
{
int deepOld = 0;
dicLigth.TryGetValue(time1, out deepOld);
dicLigth[time1] = deepOld + 1;
}
}
else
{
if (!dicLigth.ContainsKey(time1))
{
dicLigth.Add(time1, 0);
}
}
}
else if (S_RollCount >= wakeRate)
{
if (dtweek.Rows[i]["S_RollCount"] != null && dtweek.Rows[i]["S_RollCount"].ToString() != "")
{
if (!dicWake.ContainsKey(time1))
{
dicWake.Add(time1, wakeHour + 1);
}
else
{
int deepOld = 0;
dicWake.TryGetValue(time1, out deepOld);
dicWake[time1] = deepOld + 1;
}
}
else
{
if (!dicWake.ContainsKey(time1))
{
dicWake.Add(time1, 0);
}
}
}
#endregion
}
else
{
#region 没有数据加0
if (!dicDeep.ContainsKey(strtime))
{
dicDeep.Add(strtime, 0);
}
if (!dicLigth.ContainsKey(strtime))
{
dicLigth.Add(strtime, 0);
}
if (!dicWake.ContainsKey(strtime))
{
dicWake.Add(strtime, 0);
}
#endregion
}
#endregion
}
}
else
{
#region 无数据也加默认为0的值
if (!dicDeep.ContainsKey(strtime))
{
dicDeep.Add(strtime, 0);
}
if (!dicLigth.ContainsKey(strtime))
{
dicLigth.Add(strtime, 0);
}
if (!dicWake.ContainsKey(strtime))
{
dicWake.Add(strtime, 0);
}
#endregion
}
}
foreach (var item1 in dicDeep)
{
listdata1.Add(item1.Value);
}
foreach (var item2 in dicLigth)
{
listdata2.Add(item2.Value);
}
foreach (var item3 in dicWake)
{
listdata3.Add(item3.Value);
}
}
#endregion
[Serializable]
public class ChartBack
{
public bool Return { get; set; }
public List<ChartDetail> Data { get; set; }
public List<ChartYaxis> Yaxis { get; set; }
public markLine[] markLine { get; set; }
public markLineTiwen[] markLineTiwen { get; set; }//add/2021/1/5
public int min { get; set; }
public int max { get; set; }
public string maxTime { get; set; }
public string minTime { get; set; }
public int avg { get; set; }
//add/2021/1/5---体温相关参数
public decimal tiwenMin { get; set; }
public decimal tiwenMax { get; set; }
public decimal tiwenAvg { get; set; }
//add/2021/3/2---睡眠相关参数,sleepDeep深度,sleepLight浅睡,sleepWake清醒
public List<sleepweekDetail> sleepweekDetail { get; set; }
public List<string> listtime { get; set; }
public decimal sleepDeep { get; set; }
public decimal sleepLight { get; set; }
public decimal sleepWake { get; set; }
}
// 画线
public class markLine
{
public bool silent { get; set; }
public LineDetail[] data { get; set; }
}
//add/2021/1/5
public class markLineTiwen
{
public bool silent { get; set; }
public LineDetailTiwen[] data { get; set; }
}
//画线 每一条的值
public class LineDetail
{
public int yAxis { get; set; }
public string name { get; set; }
}
//画线 每一条的值//add/2021/1/5
public class LineDetailTiwen
{
public decimal yAxis { get; set; }
public string name { get; set; }
}
//每个点数据
public class dataDetail
{
public string[] value;
}
//每个点天睡眠数据
public class sleepdayDetail
{
public string[] value;
public itemStyle itemStyle { get; set; }//测试测试
}
//测试/2021/3/2
public class sleepDetail
{
public string value { get; set; }
public string Hours { get; set; }
public itemStyle itemStyle { get; set; }
}
//测试/2021/3/2
public class sleepweekDetail
{
public string name { get; set; }
public string type { get; set; }
public string stack { get; set; }
public string barWidth { get; set; }
public int[] data { get; set; }
public itemStyle itemStyle { get; set; }
}
//测试/2021/3/2
public class itemStyle
{
public string color { get; set; }
}
//ecahrt 数据
public class ChartDetail
{
public string name { get; set; }
public string type { get; set; }
public bool smooth { get; set; }
public dataDetail[] data { get; set; }
public int yAxisIndex { get; set; }
public string[] color { get; set; }
//测试/2021/3/2
public sleepdayDetail[] sleepdata { get; set; }
public sleepweekDetail[] sleepweekdata { get; set; }
public string barCategoryGap { get; set; }
public string[] sleeptimes { get; set; }
}
//echart y轴线 值样式
public class axisLabel
{
public string formatter { get; set; }
}
//echart y轴线
public class ChartYaxis
{
public string type { get; set; }
public string name { get; set; }
public int min { get; set; }
public int max { get; set; }
public string position { get; set; }
public int offset { get; set; }
public axisLabel axisLabel { get; set; }
}
#endregion
本文来自博客园,作者:.net&new,转载请注明原文链接:https://www.cnblogs.com/wugh8726254/p/14491548.html