前端模板页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4.8.0/dist/echarts.min.js"></script>
<div style="margin-top: 500px;">
传感器:
<textarea cols="10" rows="10" id="txt4"><%=sensorNames%></textarea>
开始时间:
<input type="text" id="txt1" value="<%=strStart%>" />
结束时间:
<input type="text" id="txt2" value="<%=strEnd%>" />
<input type="button" id="btn" value="click" />
</div>
<div id="main" style=" 1200px; height: 600px; margin-bottom: 500px;"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
var option = {
title: {
text: '折线图'
},
tooltip: {
trigger: 'axis',
triggerOn: 'click',
alwaysShowContent: true
//showDelay: 5000
},
xAxis: {
show: true,
type: 'category',
boundaryGap: false,
data: <%=strX%>
},
yAxis: {
type: 'value'
},
series: <%=strY%>
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
document.getElementById('btn').onclick = function () {
var start = document.getElementById('txt1').value;
var end = document.getElementById('txt2').value;
var sensorNames = document.getElementById('txt4').value.replace(/
/g, ",");
location.href = '/WebForm1.aspx?start=' + start + '&end=' + end + '&sensorNames=' + sensorNames;
};
</script>
</body>
</html>
后端页面
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public static string strX = "";
public static string strY = "";
public static List<string> list_SensorName = new List<string>();
public static DateTime start = DateTime.Now;
public static DateTime end = DateTime.Now;
public static string strStart = "";
public static string strEnd = "";
public static string sensorNames = "";
protected void Page_Load(object sender, EventArgs e)
{
List<string> axis_x = new List<string>();
List<Sensor_Record> axis_y = new List<Sensor_Record>();
//初值
strStart = "2019-01-01";
strEnd = "2019-01-09";
start = DateTime.Parse(strStart);
end = DateTime.Parse(strEnd);
//请求值
if ((Request["start"] != null) && !string.IsNullOrWhiteSpace(Request["start"].ToString()))
{
strStart = Request["start"].ToString();
start = DateTime.Parse(strStart);
}
if ((Request["end"] != null) && !string.IsNullOrWhiteSpace(Request["end"].ToString()))
{
strEnd = Request["end"].ToString();
end = DateTime.Parse(strEnd);
}
if ((Request["sensorNames"] != null) && !string.IsNullOrWhiteSpace(Request["sensorNames"].ToString()))
{
sensorNames = Request["sensorNames"].ToString();
}
//获取所有传感器
list_SensorName = GetCodes();
SetAxisXAndY();
}
public List<string> GetCodes()
{
if (!string.IsNullOrWhiteSpace(sensorNames))
{
return sensorNames.TrimEnd(',').Split(',').ToList();
}
return new List<string> { "080906" };
}
public void SetAxisXAndY()
{
string strWhere_Codes = "";
list_SensorName.ForEach(c =>
{
strWhere_Codes += $"'{c}',";
});
strWhere_Codes = strWhere_Codes.TrimEnd(',');
string sql = $@"SELECT Code,NewValue1,GetTime FROM [dbo].[tb_SensorRecord]
WHERE 1=1
AND GetTime>='{start.ToString("yyyy-MM-dd HH:mm")}'
AND GetTime<='{end.ToString("yyyy-MM-dd HH:mm")}'
AND Code IN ({strWhere_Codes})
ORDER BY ID ASC ";
DataTable dt = SqlHelper.ExecuteDataTable(sql, null);
strX = JsonConvert.SerializeObject(GetAxisX(ref dt));
strY = JsonConvert.SerializeObject(GetAxisY(ref dt));
}
public List<string> GetAxisX(ref DataTable dt)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
var key = dt.Rows[i]["GetTime"].ToString();
if (!dic.ContainsKey(key))
{
dic.Add(key, null);
}
}
return dic.Select(c => c.Key).ToList();
}
public List<Sensor_Record> GetAxisY(ref DataTable dt)
{
List<Sensor_Record> all_Sensor_Record = new List<Sensor_Record>();
list_SensorName.ForEach(item =>
{
all_Sensor_Record.Add(new Sensor_Record
{
name = item,
type = "line",
stack = "总量",
data = new List<decimal> { }
});
});
for (int i = 0; i < dt.Rows.Count; i++)
{
var code = dt.Rows[i]["Code"].ToString();
var strNewValue1 = dt.Rows[i]["NewValue1"].ToString();
var NewValue1 = string.IsNullOrWhiteSpace(strNewValue1) ? 0 : decimal.Parse(strNewValue1);
var model = all_Sensor_Record.Where(c => c.name == code).FirstOrDefault();
if (model != null)
{
model.data.Add(NewValue1);
}
}
return all_Sensor_Record;
}
}
public class Sensor_Record
{
public string name { get; set; }
public string type { get; set; }
public string stack { get; set; }
public List<decimal> data { get; set; }
}
}
类
namespace WebApplication1
{
public static class DataTableEntityInteroperate
{
/// <summary>
/// DataTable to Entities
/// </summary>
/// <typeparam name="T">Entity</typeparam>
/// <param name="dt">DataTable</param>
/// <returns>List<T</returns>
public static List<T> ToEntities<T>(this DataTable dt)/*必须来在于数据库来自于文件可能存在问题*/ where T : class, new()
{
//IsNullable
if (null == dt || dt.Rows.Count == 0)
return null;
//Initial Entities
List<T> entities = new List<T>();
try
{
foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = typeof(T).GetProperties();
T entity = new T();
Array.ForEach<PropertyInfo>(pArray, p =>
{
if (row[p.Name] != DBNull.Value)
p.SetValue(entity, row[p.Name], null);
});
entities.Add(entity);
}
return entities;
}
catch (Exception)
{
throw;
}
}
public static List<T> ToEntities1<T>(this DataTable dt)/*必须来在于数据库来自于文件可能存在问题*/ where T : class, new()
{
if (null == dt || dt.Rows.Count == 0) { return null; }
List<T> entities = new List<T>();
foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = typeof(T).GetProperties();
T entity = new T();
string str = "";
Array.ForEach<PropertyInfo>(pArray, p =>
{
object value = row[p.Name];
if (value != DBNull.Value)
{
//设置 值
if (p.PropertyType.Name.Equals("Int32"))
{
p.SetValue(entity, Convert.ToInt32(value), null);
}
else if (p.PropertyType.Name.Equals("String"))
{
p.SetValue(entity, Convert.ToString(value), null);
}
else if (p.PropertyType.Name.Equals("Nullable`1"))
{
p.SetValue(entity, Convert.ToInt32(value), null);
}
//p.SetValue(entity, row[p.Name], null);
}
//得到 属性 和 属性的类型
str += $"{p.Name} => {p.PropertyType.Name}
";
});
//throw new Exception(str);
entities.Add(entity);
}
return entities;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class SqlHelper
{
//连接字符串
//1、添加引用 2、导入命名空间 为了使用ConfigurationManager
//private static string conStr = "data source=.\sqlexpress;initial catalog=AEHMS-2019-08-28;user id=sa;password=sa;";
//private static string conStr = "data source=192.168.2.98;initial catalog=QMS;user id=sa;password=Sql123456;";
//private static string conStr = "data source=192.168.2.51;initial catalog=AEHMSTest;user id=sa;password=Sql123456;";
//private static string conStr = "data source=192.168.2.51;initial catalog=AEHMSTest;user id=sa;password=Sql123456;";
//private static string conStr = "data source=192.168.2.51;initial catalog=AEHMS20201216;user id=sa;password=Sql123456;";
private static string conStr = "data source=.;initial catalog=AEHMS20210629;user id=sa;password=sa;";
//增删改查
//查找数据 ExecuteScalar()返回首行首列 ExecuteReader() DataTable
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="sql">所用的sql语句</param>
/// <param name="param">可变,可以传参也可以不传参数</param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] param)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, con))
{
//添加参数
if (param != null)
{
adapter.SelectCommand.Parameters.AddRange(param);
}
//1.打开链接,如果连接没有打开,则它给你打开;如果打开,就算了
//2.去执行sql语句,读取数据库
//3.sqlDataReader,把读取到的数据填充到内存表中
adapter.Fill(dt);
}
}
return dt;
}
/// <summary>
/// 执行查询,返回首行首列
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static object ExecuteScalar(string sql, params SqlParameter[] param)
{
object o = null;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddRange(param);
con.Open();
o = cmd.ExecuteScalar();
}
}
return o;
}
/// <summary>
/// 执行查询,返回SqlDataReader对象
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] param)
{
SqlDataReader reader = null;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddRange(param);
con.Open();
reader = cmd.ExecuteReader();
}
}
return reader;
}
/// <summary>
/// 执行增删改,返回受影响的行数
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql, params SqlParameter[] param)
{
int n = -1;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddRange(param);
con.Open();
n = cmd.ExecuteNonQuery();
}
}
return n;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public partial class tb_SensorRecordModel
{
public tb_SensorRecordModel()
{ }
//增加的属性 2019-7-12 14:10:29
/// <summary>
/// 对内 数据层面的报警 值可能是修正后的VP值(均值)
/// </summary>
public bool? InAlarm { get; set; }
/// <summary>
/// 对外 功能层面的报警 值也会超过报警阈值 可能会发送预警短信 图表上能看到这个非正常值
/// </summary>
public bool? OutAlram { get; set; }
/// <summary>
/// 是否已用VP值替换了原来的报警值
/// </summary>
public bool? IsChangeValue { get; set; }
/// <summary>
/// 一段报警数据,用一个标识进行划分。一段报警数据的完结 以找到 恢复正常的数据 为准
/// </summary>
public string BlockCode { get; set; }
/// <summary>
/// 当前这条数据 在这段报警数据中的(区块) 索引
/// 区块内元素的排序
/// </summary>
public int? BlockEleSort { get; set; }
/// <summary>
/// 是否发送了 预警短信
/// </summary>
public bool? IsSendSMS { get; set; }
/// <summary>
/// 预警时的 阈值范围
/// </summary>
public string ScopeSign { get; set; }
/// <summary>
/// 真实的 计算后的 值1
/// </summary>
public string RealNewValue1 { get; set; }
/// <summary>
/// 真实的 计算后的 值2
/// </summary>
public string RealNewValue2 { get; set; }
/// <summary>
/// 这区块的数据 是否完结
/// </summary>
public bool? IsEnd { get; set; }
/// <summary>
/// 是否已处理
/// </summary>
public bool? IsDealtWith { get; set; }
/// <summary>
/// 是否显示
/// </summary>
public bool? IsShow { get; set; }
/// <summary>
/// 报警等级
/// </summary>
public int? AlarmLevel { get; set; }
#region Model
private int? _id;
private int? _sensorid;
private int? _sensortypeid;
private int? _bridgeid;
private string _bridgename;
private string _sensorname;
private string _sensortype;
private string _sensornumber;
private string _sensorposition;
private DateTime? _gettime;
private decimal? _value1;
private decimal? _value2;
private string _unit;
private int? _sensorpositionid;
private bool _isread;
private string _code;
private DateTime? _createtime;
private string _state;
private decimal? _temperature;
private int? _resistance;
private string _newvalue1;
private string _newvalue2;
/// <summary>
/// 编号
/// </summary>
public int? ID
{
set { _id = value; }
get { return _id; }
}
/// <summary>
/// 传感器ID
/// </summary>
public int? SensorID
{
set { _sensorid = value; }
get { return _sensorid; }
}
/// <summary>
/// 传感器类型ID
/// </summary>
public int? SensorTypeID
{
set { _sensortypeid = value; }
get { return _sensortypeid; }
}
/// <summary>
/// 桥梁ID
/// </summary>
public int? BridgeID
{
set { _bridgeid = value; }
get { return _bridgeid; }
}
/// <summary>
/// 桥梁名称
/// </summary>
public string BridgeName
{
set { _bridgename = value; }
get { return _bridgename; }
}
/// <summary>
/// 传感器名字
/// </summary>
public string SensorName
{
set { _sensorname = value; }
get { return _sensorname; }
}
/// <summary>
/// 传感器类型名
/// </summary>
public string SensorType
{
set { _sensortype = value; }
get { return _sensortype; }
}
/// <summary>
/// 传感器编号
/// </summary>
public string SensorNumber
{
set { _sensornumber = value; }
get { return _sensornumber; }
}
/// <summary>
/// 传感器位置
/// </summary>
public string SensorPosition
{
set { _sensorposition = value; }
get { return _sensorposition; }
}
/// <summary>
/// 获取时间
/// </summary>
public DateTime? GetTime
{
set { _gettime = value; }
get { return _gettime; }
}
/// <summary>
/// 值1
/// </summary>
public decimal? Value1
{
set { _value1 = value; }
get { return _value1; }
}
/// <summary>
/// 值2 可为空
/// </summary>
public decimal? Value2
{
set { _value2 = value; }
get { return _value2; }
}
/// <summary>
/// 单位
/// </summary>
public string Unit
{
set { _unit = value; }
get { return _unit; }
}
/// <summary>
/// 传感器位置ID
/// </summary>
public int? SensorPositionID
{
set { _sensorpositionid = value; }
get { return _sensorpositionid; }
}
/// <summary>
///
/// </summary>
public bool IsRead
{
set { _isread = value; }
get { return _isread; }
}
/// <summary>
///
/// </summary>
public string Code
{
set { _code = value; }
get { return _code; }
}
/// <summary>
///
/// </summary>
public DateTime? CreateTime
{
set { _createtime = value; }
get { return _createtime; }
}
/// <summary>
///
/// </summary>
public string State
{
set { _state = value; }
get { return _state; }
}
/// <summary>
/// 温度值
/// </summary>
public decimal? Temperature
{
set { _temperature = value; }
get { return _temperature; }
}
/// <summary>
/// 电阻值
/// </summary>
public int? Resistance
{
set { _resistance = value; }
get { return _resistance; }
}
/// <summary>
/// 处理后的值1
/// </summary>
public string NewValue1
{
set { _newvalue1 = value; }
get { return _newvalue1; }
}
/// <summary>
/// 处理后的值2
/// </summary>
public string NewValue2
{
set { _newvalue2 = value; }
get { return _newvalue2; }
}
#endregion Model
}
}