1、首先配置web.config
<add key="SUANLI_BASE" value="E:\算例"/> //本地E盘下创建一个文件夹
2、创建一个实体类,用作对象吧
public class T_ZAIHEYUCE{
private string _x1;
public string X1
{
get { return _x1; }
set { _x1 = value; }
}
private string _x2;
public string X2
{
get { return _x2; }
set { _x2 = value; }
}
private string _x3;
public string X3
{
get { return _x3; }
set { _x3 = value; }
}
private string _x4;
public string X4
{
get { return _x4; }
set { _x4 = value; }
}
private string _x5;
public string X5
{
get { return _x5; }
set { _x5 = value; }
}
}
3、创建一般处理程序ashx,继承自AjaxHandleBase:
public class ZaiHeYuCe : AjaxHandleBase{
public override void BeforeProcess(HttpContext context)
{
AddMap("getzhtxt", GetZhtxt);
}
private void GetZhtxt(HttpContext context){
decimal? id = QueryDecimal(context, "id");
Work.Logic.T_SUANLI bll = new Work.Logic.T_SUANLI();
Work.Model.T_SUANLI model = bll.GetEntity(id); //得到id为XX的算例
//此处先建立两个第二步所创建的实例类,方便后边用
List<Work.Model.T_ZAIHEYUCE> zhlist = new List<Work.Model.T_ZAIHEYUCE>();
List<Work.Model.T_ZAIHEYUCE> zhyclist = new List<Work.Model.T_ZAIHEYUCE>();
//获取web配置里的地址
string suanliBasePath = ConfigurationManager.AppSettings["SUANLI_BASE"];
//获取文件夹下根据年份和case_加上此算例ID所生成的地址
string slpath = Path.Combine(suanliBasePath, DateTime.Now.Year.ToString(), "case_" + model.L_ID);
//获取此算例下的Solver文件夹
string solverpath = Path.Combine(slpath, "Solver");
//判断前台往数据库里添加的文件是否存在,此处我有四个文件需同时上传
if (File.Exists(model.F_ZHTXT_KEY) && File.Exists(model.F_ZHTRA_KEY) && File.Exists(model.F_WLTXT_KEY) && File.Exists(model.F_WLTRA_KEY)){
//获取Solver文件下载荷预测文件里综合类和物理类文件下的data_lo.txt。
//注意:综合类包括F_ZHTXT_KEY和F_ZHTRA_KEY,物理类包括model.F_WLTXT_KEY和model.F_WLTRA_KEY且这两类文件的data_log.txt都是149行3列分别是节点、textloss、trainloss
string zhpath = Path.Combine(solverpath, "载荷预测", "zonghe", "data_log.txt");
//读取综合文件下的data_log.txt
string[] zhstrs = System.IO.File.ReadAllLines(zhpath);
//同理物理文件
string wlpath = Path.Combine(solverpath, "载荷预测", "wuli", "data_log.txt");
string[] wlstrs = System.IO.File.ReadAllLines(wlpath);
//上文提到两个data_log的行列相等,
if (zhstrs.Length == wlstrs.Length)
{
//遍历综合类的txt,并添加到zhlist集合中,文本是用\t分隔的,所以
for (int i = 1; i < zhstrs.Length; i++)
{
var strs = zhstrs[i];
var s = strs.Substring(0, strs.Length - 1).Split("\t".ToCharArray());
zhlist.Add(new Work.Model.T_ZAIHEYUCE
{
X1 = s[0],
X2 = s[1],
X3 = s[s.Length - 1]
});
}
//遍历物理下的txt若zhlist集合中的X1节点等于i则赋值
注意:此处没有赋值s[0],是因为它俩在表格里共用同一节点
for (int i = 1; i < wlstrs.Length; i++)
{
var strs = wlstrs[i];
var s = strs.Substring(0, strs.Length - 1).Split("\t".ToCharArray());
var x1 = zhlist.Where(a => a.X1 == i.ToString()).FirstOrDefault().X1;
var x2 = zhlist.Where(a => a.X1 == i.ToString() && a.X2 != null).FirstOrDefault().X2;
var x3 = zhlist.Where(a => a.X1 == i.ToString() && a.X3 != null).FirstOrDefault().X3;
zhyclist.Add(new Work.Model.T_ZAIHEYUCE
{
X1 = x1,
X2 = x2,
X3 = x3,
X4 = s[1],
X5 = s[2]
});
}
}
}
//返回此集合的json格式,共有149条数据
string json = JsonHelper.ToJson(zhyclist);
context.Response.Write(json);
}
}
4、前台aspx:
<script type="text/javascript">
var url = "/Pages/Youhua/Data/ZaiHeYuCe.ashx?req_fun=";
$(function () {
loadLine();
//随浏览器窗口调整
$(window).resize(function () {
loadLine();
});
});
//或者在此处用colums写多行表头也可以
function loadLine()
{
$('#tabList').datagrid({
url:url + "getzhtxt&id="+<%=SLID%>,
//columns: [
// [{ "title": " " },
// { "title": "数字孪生", "colspan": 2 },
// { "title": "物理", "colspan": 2 }],
// [
// { "field": "X1", "title": "节点" },
// { "field": "X2", "title": "test-loss" },
// { "field": "X3", "title": "train-loss" },
// { "field": "X4", "title": "test-loss" },
// { "field": "X5", "title": "train-loss" }
// ]
//]
});
}
</script>
<table id="tabList" border="false" toolbar="" singleselect="true" rownumbers="true" striped="true" remotesort="false" class="easyui-datagrid" fit="true">
<thead>
<tr>
<th align="center"></th>
<th align="center" colspan="2">数字孪生</th>
<th align="center" colspan="2">物理</th>
</tr>
<tr>
<th align="center" data-options="sortable:true,field:'X1'">节点</th>
<th align="center" data-options="sortable:true,field:'X2'">test-loss</th>
<th align="center" data-options="sortable:true,field:'X3'">train-loss</th>
<th align="center" data-options="sortable:true,field:'X4'">test-loss</th>
<th align="center" data-options="sortable:true,field:'X5'">train-loss</th>
</tr>
</thead>
</table>
5、部分图像