-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverview.js
404 lines (327 loc) · 10.6 KB
/
overview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
var Columns_Parent =[
{ "title":"天差", "data": "TimeOffset" },
{ "title":"更新时间", "data": "UpdateTime" },
{ "title":"地区", "data": "ParentName" },
{ "title":"确诊数", "data": "ParentConfirmedCount" },
{ "title":"康复数", "data": "ParentCuredCount" },
{ "title":"死亡数", "data": "ParentDeadCount" },
{ "title":"在治数", "data": "ParentTreatingCount" },
{ "title":"康复率", "data": "ParentCuredRate" },
{ "title":"死亡率", "data": "ParentDeadRate" },
{ "title":"在治率", "data": "ParentTreatingRate" },
{ "title":"推算康复率", "data": "ParentCuredRateEx" },
{ "title":"推算死亡率", "data": "ParentDeadRateEx" },
];
var Columns_Child =[
{ "title":"天差", "data": "TimeOffset" },
{ "title":"更新时间", "data": "UpdateTime" },
{ "title":"地区", "data": "ParentName" },
{ "title":"子地区", "data": "ChildName" },
{ "title":"确诊数", "data": "ChildConfirmedCount" },
{ "title":"康复数", "data": "ChildCuredCount" },
{ "title":"死亡数", "data": "ChildDeadCount" },
{ "title":"在治数", "data": "ChildTreatingCount" },
{ "title":"康复率", "data": "ChildCuredRate" },
{ "title":"死亡率", "data": "ChildDeadRate" },
{ "title":"在治率", "data": "ChildTreatingRate" },
{ "title":"推算康复率", "data": "ChildCuredRateEx" },
{ "title":"推算死亡率", "data": "ChildDeadRateEx" },
];
var SelectMsg = "请点击选择一些数据,再点击相应操作!";
var LoadDataErrorMsg = "数据加载失败,请检查数据地址是否正确!\n";
var SimulateErrorMsg = "仿真数据加载失败,仿真功能将被禁用!\n";
var DataUrls = [
"Total.json",
"LastParents.json",
"LastChildren.json",
];
var SimulateUrls = [
"ParentsBoltzmann.json",
"ChildrenBoltzmann.json",
];
var Params = {
//Url:"http://127.0.0.1:8081/data",
"DataUrl":"https://zyq5945.github.io/DXY-COVID-19-Data-Arrange-DJSON/data",
"MinDay": "",
"MaxDay": "",
"DaysOfTreatment": 18,
"Width": "100%",
"Height": 600,
"StartTime": "2020-01-24",
}
var SimulateData = { Parents:[], Children:[]};
var IsFrame = false;
var jdxLoad = jdx("load");
var Tables = [];
function fetch_data(data) {
window.Tables.forEach(x=> x.destroy());
window.Tables.length = 0;
function fmtRate(tds, index, cols, data) {
var len = index+5;
for (var i=index; i<len; i++) {
var v = data[cols[i].data];
if (isValidNum(v)) {
v = Math.round(v * 10000) / 100;
tds.eq(i).text(`${v}%`);
}
}
}
function fmtNum(tds, indexs, cols, data) {
indexs.forEach(i=> {
var v = data[cols[i].data];
if (isValidNum(v)) {
if (v<1) {
v = Math.round(v * 1000) / 1000;
}
else {
v = Math.round(v * 100) / 100;
}
tds.eq(i).text(`${v}`);
}
})
}
function createdRow(columns, tbl_order , tbl_offset) {
var idx = tbl_order + tbl_offset;
var idxs = [0];
return function(row, data, index) {
var tds = $('td', row);
fmtRate(tds, idx, columns, data);
fmtNum(tds, idxs, columns, data);
}
}
var columns_p = Columns_Parent;
var columns_c = Columns_Child;
var dataTotal = data[0];
var dataLastParents = data[1];
var dataLastChildren = data[2];
function createTableDrawCb(n) {
return function () {
var table = window.Tables[n];
if (table) {
table.draw();
}
}
};
var names = dataLastParents.map(x=> x.ParentName).sort(nameCompare);
divSelectClick("LastParentsName", names, createTableDrawCb(1), "LastParentsName");
names = arrayUnique(dataLastChildren.map(x=> x.ParentName)).sort(nameCompare);
divSelectClick("LastChildrenName", names, createTableDrawCb(2), "LastChildrenName");
divFilterClick("LastParentsFilter", createTableDrawCb(1), "LastParentsFilter");
divFilterClick("LastChildrenFilter", createTableDrawCb(2), "LastChildrenFilter");
var tbl_order = 3;
var tbl_offset = 4;
var columns = columns_p;
var table_tps = $('#Total').DataTable({
"paging": false,
"ordering": false,
"info": false,
"searching": false,
//"order": [[ tbl_order, "desc" ]],
columns : columns,
data : dataTotal,
createdRow : createdRow(columns, tbl_order, tbl_offset)});
var table_lps = $('#LastParents').DataTable({
"paging": true,
"ordering": true,
"info": true,
"searching": true,
//"stateSave" : true,
"order": [[ tbl_order, "desc" ]],
columns : columns,
data : dataLastParents,
createdRow : createdRow(columns, tbl_order, tbl_offset)});
tbl_order = 4;
columns = columns_c;
var table_lcs =$('#LastChildren').DataTable({
"paging": true,
"ordering": true,
"info": true,
"searching": true,
//"stateSave" : true,
"order": [[ tbl_order, "desc" ]],
columns : columns,
data : dataLastChildren,
createdRow : createdRow(columns, tbl_order, tbl_offset)});
window.Tables = [table_tps, table_lps, table_lcs];
function highlightDivName(table, divName) {
table.on("draw", function() {
var names = arrayUnique(table.rows({search:'applied'}).data(), x=> x.ParentName);
$(`${divName} input[type='checkbox']`).each(function() {
var isHighlight = names.includes(this.value);
var node = $(this.nextSibling);
if (isHighlight) {
node.removeClass("textGray");
}
else {
node.addClass("textGray");
}
})
})
table.draw();
}
highlightDivName(table_lps, "#LastParentsName");
highlightDivName(table_lcs, "#LastChildrenName");
$("tbody").off("click",'tr');
$('tbody').on("click", 'tr', function () {
$(this).toggleClass('selected');
});
window.getSelectRows = function() {
var names = arrayMap(table_lps.rows('.selected').data().map(x => x.ParentName))
.concat(arrayMap(table_lcs.rows('.selected').data().map(x => x.ParentName + "-" + x.ChildName)))
return names;
};
var urls = SimulateUrls.map(x=> Params.DataUrl +"/" + x);
jqueryBatchAjax(urls,
data=> {
SimulateData.Parents = data[0];
SimulateData.Children = data[1];
$("button[name='Simulate']").attr("disabled",false);
},
error => {
$("button[name='Simulate']").attr("disabled",true);
//alert(SimulateErrorMsg + error);
//alert(SimulateErrorMsg);
});
}
function load_data() {
jdxLoad.get(Params);
var urls = DataUrls.map(x=> Params.DataUrl +"/" + x);
jqueryBatchAjax(urls,
fetch_data,
error => {
alert(LoadDataErrorMsg + error);
//alert(LoadDataErrorMsg);
});
}
function createUrl(obj) {
var s = Url.stringify(obj);
var url = "detail.html?" + s;
console.log("---url:", url);
return url;
}
function detailClick(type) {
var names = getSelectRows();
if (names.length <=0) {
alert(SelectMsg);
return;
}
jdxLoad.get(Params);
var ret = Object.assign({Type:type}, Params);
var urls = [];
if (type == 'Compare') {
Object.assign(ret, {Name :names});
urls.push(createUrl(ret));
}
else {
var isSimulate = type == 'Simulate';
var listParent = SimulateData.Parents;
var listChild = SimulateData.Children;
names.forEach(x => {
ret.Name = x;
if (isSimulate) {
var data = x.includes("-")? listChild : listParent;
var p = data.find(y=> y.Name == x) || {};
Object.assign(ret, p)
}
urls.push(createUrl(ret));
});
}
if (IsFrame) {
d3.select("#divFrame")
.selectAll("iframe")
.data(urls)
.join("iframe")
.attr("src", d=>d);
}
else {
d3.select("#divFrame")
.selectAll("iframe")
.remove();
urls.forEach(x => window.open(x, "_blank"));
}
}
function loadClick() {
load_data();
Storage.set("cfg", Params);
}
function tableValueFilter( settings, data, dataIndex ) {
var table = $(settings.nTable);
var id = table.data("myfilter");
var columns = window[table.data("mycolumns")];
if (!id || !columns ) {
return true;
}
var obj = window[id] || {};
for (var key in obj) {
var val = obj[key];
if (!isValidNum(val)) {
continue;
}
//val = val * 1.0;
if (key.lastIndexOf("Rate") !=-1) {
val = val / 100.0;
}
var attr = key.substr(3);
var idx = columns.findIndex(x=> x.data == attr);
var dataVal = data[idx];
if (key.startsWith("Min")) {
if (dataVal < val) {
return false;
}
}
else {
if (dataVal > val) {
return false;
}
}
}
return true;
}
function tableNameFilter( settings, data, dataIndex ) {
var id = $(settings.nTable).attr("name");
var names = window[id];
if (!names) {
return true;
}
var name = data[2];
if (names.includes(name))
{
return true;
}
return false;
}
$.fn.dataTable.ext.search.push(
tableNameFilter,
);
$.fn.dataTable.ext.search.push(
tableValueFilter,
);
$.fn.dataTable.ext.errMode = 'none';
function init() {
$.ajaxSetup({cache:false});
Object.assign(Params, Storage.get("cfg", {}), Url.parseQuery());
IsFrame = Storage.get("IsFrame", false);
var chkFrame = $("input.Frame");
chkFrame.prop('checked', IsFrame);
jdxLoad.set(Params);
$("#btnLoad").click(loadClick);
$(".btnOption").click(function() {
var type = this.name;
detailClick(type);
});
$(".btnClsSel").click(function() {
Tables.forEach(x=> {
$(x.rows('.selected').nodes()).removeClass('selected')
});
});
chkFrame.click(function() {
var status = $(this).is(':checked');
chkFrame.prop('checked', status);
IsFrame = status;
Storage.set("IsFrame", IsFrame);
});
load_data();
}
$(document).ready(function() {
init();
});