-
Notifications
You must be signed in to change notification settings - Fork 13
/
estat-chart.js
101 lines (87 loc) · 2.67 KB
/
estat-chart.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
const html = `
<canvas id="chart" width="400" height="400"></canvas>
<script id="chartjs" src="https://cdn.jsdelivr.net/npm/chart.js@3.6.1/dist/chart.min.js"></script>
<script>
let property, chart, estatdata;
function fetchData() {
if (estatdata) return Promise.resolve(estatdata);
if (!property) return Promise.resolve();
return fetch(
"https://api.e-stat.go.jp/rest/3.0/app/json/getStatsData?lang=J&statsDataId=0003410379&metaGetFlg=Y&cntGetFlg=N&explanationGetFlg=Y&annotationGetFlg=Y§ionHeaderFlg=1&replaceSpChars=0&appId=" + property.appId
).then(r => {
if (r.ok) return r.json();
}).then(r => {
if (!r) return;
estatdata = r;
return r;
});
}
function calcData(data) {
if (!property || !data) return;
const data2 = data.GET_STATS_DATA.STATISTICAL_DATA.DATA_INF.VALUE
.filter(d => d["@area"] === property.area && d["@tab"] === "020");
if (data2.length === 0) return;
return {
datasets: [
{ cat: "100", label: "人口", color: "rgb(255, 159, 64)" },
{ cat: "110", label: "人口(男)", color: "rgb(54, 162, 235)" },
{ cat: "120", label: "人口(女)", color: "rgb(255, 99, 132)" }
].map(cat => ({
label: cat.label,
borderColor: cat.color,
data: data2.filter(d => d["@cat01"] === cat.cat).map(d => ({
x: Math.floor(d["@time"] / 1000000) + "年",
y: d.$
})),
}))
};
}
function updateChart() {
return fetchData().then(d => {
if (!d || !chart) return;
chart.data = calcData(d);
chart.update();
});
}
document.getElementById("chartjs").addEventListener("load", () => {
const ctx = document.getElementById('chart').getContext('2d');
chart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: []
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
updateChart();
});
window.addEventListener("message", e => {
if (e.source !== parent || !e.data.reearth) return;
property = e.data;
if (property.area) {
property.area = ("0" + property.area).slice(-2) + "000";
}
updateChart();
});
</script>
`;
reearth.ui.show(html);
reearth.on("update", send);
send();
function send() {
if (reearth.block?.property?.default) {
reearth.ui.postMessage({
// This is necessary to determine if the message is caused by a plugin,
// since the browser may send a message to all iframes
// depending on the browser and operating environment, such as Chrome on Android.
reearth: true,
...reearth.block.property.default
});
}
}