-
Notifications
You must be signed in to change notification settings - Fork 0
/
japangraph.html
414 lines (367 loc) · 13 KB
/
japangraph.html
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
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Japan Alert Graph</title>
<!--Script-->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-WK0WDPMXF1"></script>
<script src="./ga.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.min.js"></script>
<script>
//Main
var KEY_NAME = "東京都";
var newdata = "";
var cities = new Array();
var citiesIsLoad = false;
var isWidget = false;
var isDark = false;
var days = new Array();
var newdays = new Array();
var minnewday = 999999;
var maxnewday = 0;
function init() {
var params = (new URL(document.location)).searchParams;
try {
var changetime = removehtml(params.get("changetime"));
if ( changetime != "" && Number(changetime) > 0 ) { autochangetime = Number(changetime); }
} catch(e) {
}
try {
var darkmode = removehtml(params.get("darkmode"));
if ( darkmode == "dark" ) {
isDark = true;
document.body.style.color = "white";
document.body.style.backgroundColor = "black";
}
} catch(e) {
}
try {
var widgetmode = removehtml(params.get("widgetmode"));
if ( widgetmode == "widget" ) {
isWidget = true;
var form = document.getElementById("form");
form.style.visibility = "hidden";
form.style.height = "0px";
var title = document.getElementById("title");
title.style.visibility = "hidden";
title.style.height = "0px";
}
} catch(e) {
}
var startdate = "2020-01-01";
var enddate = dateToFormatString(new Date(), "%YYYY%-%MM%-%DD%");
showdateform(startdate, enddate);
loaddata();
}
function nowmonth() {
var date = new Date();
date.setDate(1);
return date;
}
function showdateform(startvalue, endvalue) {
var start = document.getElementById("start");
var end = document.getElementById("end");
start.value = startvalue;
start.min = "2020-01-01";
start.max = endvalue;
end.value = endvalue;
end.min = "2020-01-01";
end.max = endvalue;
}
function checkdaterange(date) {
try {
var start = document.getElementById("start");
var end = document.getElementById("end");
var startdate = Date.parse(start.value);
var enddate = Date.parse(end.value);
var checkday = Date.parse(date);
var flag = false;
if ( Number(startdate) <= Number(checkday) && Number(checkday) <= Number(enddate) ) {
flag = true;
} else {
flag = false;
}
} catch(e) {
}
return flag;
}
function setchangekey(text) {
KEY_NAME = text;
}
function changecity() {
var cty = document.getElementById("cities");
var index = cty.selectedIndex;
var value = cty.options[index].value;
var text = cty.options[index].text;
KEY_NAME = text;
generatecsv();
}
function setchangecity() {
var option = document.getElementById("cities").getElementsByTagName("option");
for( var i=0; i<option.length; i++ ) {
try {
if( option[i].text == KEY_NAME ){
option[i].selected = true;
return;
}
} catch(e) {
}
}
}
function drawcities() {
var cty = document.getElementById("cities");
if ( cty.length < cities.length ) {
for ( var i=0; i<cities.length; i++ ) {
var option = document.createElement("option");
option.text = cities[i];
option.value = i;
cty.appendChild(option);
}
}
cty.disabled = false;
setchangecity();
}
function loaddata() {
csv_data('https://www3.nhk.or.jp/n-data/opendata/coronavirus/nhk_news_covid19_prefectures_daily_data.csv');
function csv_data(dataPath) {
const request = new XMLHttpRequest();
request.addEventListener('load', (event) => {
const response = event.target.responseText;
var resArray = response.split("\r");
//Keyの生成
for ( var i=1; i<resArray.length; i++ ) {
var data = resArray[i].split(",");
var day = data[0];
var city = data[2];
var totalday = data[4];
var newday = data[3];
if ( cities.indexOf(city) == -1 && city != "" && city != null ) {
cities.push(city);
}
}
cities = Array.from(new Set(cities));
drawcities();
for ( var i=1; i<resArray.length; i++ ) {
try {
var data = resArray[i].split(",");
var day = data[0];
var city = data[2];
var totalday = data[4];
var newday = data[3];
if ( city.indexOf(KEY_NAME) !== -1 ) {
if ( checkdaterange(day) ) {
days.push(day);
newdays.push(newday);
if ( maxnewday < Number(newday) ) {
maxnewday = Number(newday);
}
if ( minnewday > Number(newday) ) {
minnewday = Number(newday);
}
}
}
} catch(e) {
}
}
drawchart(days, newdays);
});
request.open('GET', dataPath, true);
request.send();
}
}
function reloaddata() {
var graph = document.getElementById("graph");
graph.innerHTML = "<canvas id='chart'></canvas>";
days = new Array();
newdays = new Array();
newweeks = new Array();
newweeksdays = new Array();
minnewday = 999999;
maxnewday = 0;
loaddata();
}
function drawchart(lbl, data) {
var borderColor = "rgba(255,0,0,1)";
var backgroundColor = "rgba(0,0,0,0)";
var fontColor = "rgba(255,0,0,1)";
var ctx = document.getElementById("chart");
var myLineChart = new Chart(ctx, {
type: 'line',
data: {
labels: lbl,
datasets: [
{
id: 'y1',
label: '新規感染者数',
data: data,
borderColor: borderColor,
backgroundColor: backgroundColor
},
],
},
options: {
title: {
display: true,
text: KEY_NAME + "(" + lbl[0] + "~" + lbl[lbl.length-1] + ")"
},
scales: {
yAxes: [{
id: 'y1',
position: 'left',
scaleLabel: {
display: true,
labelString: '新規感染者数',
fontColor: fontColor
},
ticks: {
beginAtZero: true,
max: maxnewday
}
}]
},
}
});
}
function dateToFormatString(date, fmt, locale, pad) {
var padding = function(n, d, p) {
p = p || '0';
return (p.repeat(d) + n).slice(-d);
};
var DEFAULT_LOCALE = 'ja-JP';
var getDataByLocale = function(locale, obj, param) {
var array = obj[locale] || obj[DEFAULT_LOCALE];
return array[param];
};
var format = {
'YYYY': function() { return padding(date.getFullYear(), 4, pad); },
'YY': function() { return padding(date.getFullYear() % 100, 2, pad); },
'MMMM': function(locale) {
var month = {
'ja-JP': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
'en-US': ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'],
};
return getDataByLocale(locale, month, date.getMonth());
},
'MMM': function(locale) {
var month = {
'ja-JP': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
'en-US': ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June',
'July', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'],
};
return getDataByLocale(locale, month, date.getMonth());
},
'MM': function() { return padding(date.getMonth()+1, 2, pad); },
'M': function() { return date.getMonth()+1; },
'DD': function() { return padding(date.getDate(), 2, pad); },
'D': function() { return date.getDate(); },
'HH': function() { return padding(date.getHours(), 2, pad); },
'H': function() { return date.getHours(); },
'hh': function() { return padding(date.getHours() % 12, 2, pad); },
'h': function() { return date.getHours() % 12; },
'mm': function() { return padding(date.getMinutes(), 2, pad); },
'm': function() { return date.getMinutes(); },
'ss': function() { return padding(date.getSeconds(), 2, pad); },
's': function() { return date.getSeconds(); },
'A': function() {
return date.getHours() < 12 ? 'AM' : 'PM';
},
'a': function(locale) {
var ampm = {
'ja-JP': ['午前', '午後'],
'en-US': ['am', 'pm'],
};
return getDataByLocale(locale, ampm, date.getHours() < 12 ? 0 : 1);
},
'W': function(locale) {
var weekday = {
'ja-JP': ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日'],
'en-US': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
};
return getDataByLocale(locale, weekday, date.getDay());
},
'w': function(locale) {
var weekday = {
'ja-JP': ['日', '月', '火', '水', '木', '金', '土'],
'en-US': ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'],
};
return getDataByLocale(locale, weekday, date.getDay());
},
};
var fmtstr = [''];
Object.keys(format).forEach(function(key) {
fmtstr.push(key);
})
var re = new RegExp('%(' + fmtstr.join('|') + ')%', 'g');
var replaceFn = function(match, fmt) {
if(fmt === '') {
return '%';
}
var func = format[fmt];
if(func === undefined) {
return match;
}
return func(locale);
};
return fmt.replace(re, replaceFn);
}
function removehtml(str) {
return str.replace(/<("[^"]*"|'[^']*'|[^'">])*>/g,'');
}
function delayedCall(second, callBack){
setTimeout(callBack, second * 1000);
}
</script>
<!--Script-->
<style>
body {
margin: 0;
padding-top: 2em;
display:block;
justify-content: center;
text-align: center;
background-color: white;
}
.form {
background-color: red;
padding: 10px 20px;
}
#graph {
margin: 0;
position: relative;
margin: 0 auto;
width: 100%;
height: auto;
overflow: hidden;
}
#footer {
position: fixed;
display: inline-block;
bottom:0px;
left: 0px;
width:100%;
color: #aaa;
font-size: 16px;
background: #555;
}
</style>
</head>
<body onload="init();">
<h1 id="title">Japan Alert Graph</h1>
<div class="form" id="form">
<select name="cities" id="cities" onchange="changecity();"></select><br/>
<input type="date" id="start" value="2020/01/01"><br/>
<input type="date" id="end" value=""><br/>
<button type="submit" onclick="reloaddata();"><big>読み込み</big></button>
</div>
<hr/>
<div id="graph">
<canvas id="chart"></canvas>
</div>
<div id="footer">
<script>
document.write("2021©️" + document.domain);
</script>
</div>
</body>
</html>