-
Notifications
You must be signed in to change notification settings - Fork 24
/
mhlw-graph_all.mjs
78 lines (68 loc) · 2.23 KB
/
mhlw-graph_all.mjs
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
import Chart from "https://code4sabae.github.io/kafumon/lib/Chart.mjs";
import { CSV } from "https://js.sabae.cc/CSV.js";
const main = async (parent) => {
//const url = "https://www.stopcovid19.jp/data/mhlw_go_jp/opendata/covid19.csv";
const label = "日付";
const title = "厚生労働省オープンデータ - 新型コロナウイルス感染症について";
const url = "./data/mhlw_go_jp/opendata/covid19_all.csv";
const csv = await CSV.fetch(url);
const datas = {};
for (let i = 0; i < csv[0].length; i++) {
const data = [];
datas[csv[0][i]] = data;
for (let j = 1; j < csv.length; j++) {
data.push(csv[j][i]);
}
}
const datasets = [];
for (const name in datas) {
if (name == label) {
continue;
}
const borderColor = `hsl(${datasets.length * 15}, 80%, 80%)`;
console.log(borderColor);
datasets.push({ type: "line", fill: false, label: name, data: datas[name], borderColor, lineTension: 0, yAxisID: "yr" });
}
console.log(datasets);
const config = {
data: {
labels: datas[label],
datasets,
},
options: {
title: { display: true, text: title },
scales: {
xAxes: [{ scaleLabel: { display: false, labelString: label } }],
yAxes: [
{ id: "yr", position: "right", scaleLabel: { display: true, labelString: "数" }, ticks: { beginAtZero: true } },
],
},
legend: { display: true }
}
};
parent.style.display = "block";
parent.style.marginBottom = ".5em";
const chart = document.createElement("canvas");
chart.width = 600;
chart.height = 350;
new Chart.Chart(chart, config);
parent.appendChild(chart);
const atts = {};
for (const a of parent.attributes) {
atts[a.nodeName] = a.value;
}
if (atts["view-src"] && atts["view-src"]) {
const div = document.createElement("div");
div.style.textAlign = "center";
div.style.fontSize = "80%";
div.innerHTML = `データ出典:<a href=https://www.mhlw.go.jp/stf/covid-19/open-data.html>オープンデータ|厚生労働省</a>→<a href=${url}>CSV</a>`;
parent.appendChild(div);
}
};
class MHLWGraph extends HTMLElement {
constructor () {
super();
main(this);
}
}
customElements.define('mhlw-graph', MHLWGraph);