-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreemap.html
More file actions
376 lines (346 loc) · 8.76 KB
/
Copy pathtreemap.html
File metadata and controls
376 lines (346 loc) · 8.76 KB
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
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>上場企業ダッシュボード ツリーマップ</title>
</head>
<body>
<h1><a href=./treemap.html>上場企業ダッシュボード ツリーマップ</a></h1>
<select id="selpref"></select>
<select id="selindustry"></select>
<select id="selmarket"></select>
<label><input id="chkdiff" type="checkbox">差分</label>
<br>
<div class=chart id=divtreemap></div>
<div class=chart id=divtreemap2></div>
<div id=lastUpdate></div>
<link rel="stylesheet" href="banner.css">
<div id="banners"></div>
<div id="credit">
DATA: <a href="https://disclosure2.edinet-fsa.go.jp/WEEK0010.aspx">EDINET</a>, <a href="https://www.houjin-bangou.nta.go.jp/">国税庁法人番号公表サイト</a><br>
Lib: <a href=https://github.com/code4fukui/apexcharts.js/>ApexCharts</a><br>
<a href=https://github.com/code4fukui/EDINET/>data and src on GitHub</a><br>
</div>
<script type="module">
import { CSV } from "https://js.sabae.cc/CSV.js";
import ApexCharts from "https://code4fukui.github.io/apexcharts.js/ApexCharts.js";
import { setSelectMarket, markets, setSelectIndustry, industry17, setSelectPref } from "./setSelect.js";
import { removeHash } from "https://js.sabae.cc/removeHash.js";
import { Num } from "https://js.sabae.cc/Num.js";
import "./banner.js";
import { hsl2rgb } from "https://js.sabae.cc/hsl2rgb.js";
setSelectPref(selpref);
setSelectMarket(selmarket);
setSelectIndustry(selindustry);
const calcAll = (list, pref = true) => {
const res = {};
for (const i of list) {
const name = i.name.replace(/株式会社/, "") + " (" + (pref ? i.pref : i.city) + ")";
res[name] = i.marketcap_m;
}
return res;
};
const calcPref = (list) => {
const res = {};
for (const i of list) {
if (!res[i.pref]) {
res[i.pref] = 0;
}
res[i.pref] += i.marketcap_m;
}
return res;
};
const calcCity = (list, pref) => {
const res = {};
for (const i of list) {
if (!res[i.city]) {
res[i.city] = 0;
}
res[i.city] += i.marketcap_m;
}
return res;
};
const url = "./data/marketcap/index.csv";
const list = await CSV.fetchJSON(url);
const fetchData = async (name) => {
const data = await CSV.fetchJSON("./data/marketcap/" + name);
data.forEach(i => {
i.marketcap_m = parseInt(i.marketcap_m);
i.name = i.name.replace(/株式会社/, "");// + " (" + (pref ? i.pref : i.city) + ")";
});
return data;
};
const dlatest = list[list.length - 1];
const dpast = list[list.length - 2];
const latest = await fetchData(dlatest.file);
const past = await fetchData(dpast.file);
lastUpdate.textContent = `更新日: ${dlatest.date} (差分は${dpast.date}から)`;
const getData = (list) => {
return list.filter(i => {
if (selpref.value != "all" && i.pref != selpref.value) {
return false;
} else if (selmarket.value != "all" && i.market != selmarket.value) {
return false;
} else if (selindustry.value != "all" && i.type != selindustry.value) {
return false;
}
return true;
});
};
const sels = [selpref, selindustry, selmarket];
document.location.hash.substring(1).split(",").forEach((n, idx) => {
if (idx < sels.length) {
sels[idx].selectedIndex = n;
} else if (idx == sels.length) {
chkdiff.checked = n == "1";
}
});
const omitSmall = (res) => {
if (res.length < 1000) {
return res;
}
// omit if over 1000
let max = 0;
for (const d of res) {
const am = Math.abs(d.y);
if (am > max) {
max = am;
}
}
const min = max / 1000; // omit under 0.1%
return res.filter(i => Math.abs(i.y) > min);
};
const makeDiff = (a, b) => {
const res = [];
for (const d of a) {
const d2 = b.find(i => i.seccode == d.seccode);
if (!d2) continue;
const dmarketcap_m = d.marketcap_m - d2.marketcap_m;
res.push({ x: d.name, y: dmarketcap_m });
}
return omitSmall(res).sort((a, b) => b.y - a.y);
};
const makeOne = (a) => {
const res = a.map(i => ({ x: i.name, y: i.marketcap_m }));
return omitSmall(res).sort((a, b) => b.y - a.y);
};
const getIndustry = (name) => {
const d = latest.find(i => i.name == name);
if (!d) throw new Error("illegal name");
return industry17.find(i => i.type == d.type).name;
};
const joinByIndustry = (data) => {
const list = {};
for (const d of data) {
const id = getIndustry(d.x);
const p = list[id];
if (p) {
p.y += d.y;
} else {
list[id] = { x: id, y: d.y };
}
}
const res = Object.values(list);
res.sort((a, b) => b.y - a.y);
return res;
};
const hex = (n, digits) => {
n = Math.floor(n);
const s = "000000000000000000" + n.toString(16);
const res = s.substring(s.length - digits);
return res;
};
const fromHSL = (h, s, l) => {
const rgb = hsl2rgb(h, s, l);
const c = "#" + rgb.map(i => hex(i, 2)).join("");
return c;
};
const makeColorsByIndustry = (data) => {
const colors = data.map(i => {
const n = industry17.findIndex(j => j.name == i.x);
return fromHSL(n / 18 * 360, 0.6, 0.55 - (n % 2) * 0.3)
});
return colors;
};
const showTreemap = (divtreemap, data, colors, showtitle) => {
const options = {
series: [{ data }],
legend: {
show: false
},
chart: {
height: 650,
type: 'treemap'
},
dataLabels: {
enabled: true,
style: {
fontSize: '12px',
},
formatter: (text, op) => {
return [text, Num.fixbig(op.value, true) + "円"]
},
offsetY: -4
},
plotOptions: {
treemap: {
enableShades: true,
shadeIntensity: 0.5,
reverseNegativeShade: true,
colorScale: {
ranges: [
{
from: -1000000000,
to: 0,
color: '#CD363A'
},
{
from: 0.001,
to: 1000000000,
color: '#32219C'
}
]
}
}
},
tooltip: {
/*
custom: ({series, seriesIndex, dataPointIndex, w}) => {
const d = w.config.series[seriesIndex].data[dataPointIndex];
return `${d.x}<br>${Num.fixbig(d.y, true)}円`
},
*/
y: {
formatter: (v) => {
return Num.fixbig(v, true) + "円";
},
}
},
};
if (colors) {
options.colors = colors;
options.plotOptions.treemap.distributed = true;
options.plotOptions.treemap.enableShades = false;
delete options.plotOptions.treemap.colorScale;
}
if (showtitle) {
options.title = {
text: Num.fixbig(data.reduce((pre, i) => pre + i.y, 0), true) + "円",
};
}
divtreemap.innerHTML = "";
const chart = new ApexCharts(divtreemap, options);
chart.render();
};
const show = () => {
const prefmode = selpref.value == "all";
document.location.hash = sels.map(i => i.selectedIndex).join(",") + "," + (chkdiff.checked ? 1 : 0);
if (document.location.hash == "#0,0,0,0") {
removeHash();
}
if (chkdiff.checked) {
const data = makeDiff(getData(latest), getData(past));
showTreemap(divtreemap, data, null, true);
const data2 = joinByIndustry(data);
const colors = makeColorsByIndustry(data2);
showTreemap(divtreemap2, data2);
} else {
const data = makeOne(getData(latest));
showTreemap(divtreemap, data, null, true);
const data2 = joinByIndustry(data);
const colors = makeColorsByIndustry(data2);
showTreemap(divtreemap2, data2, colors);
}
};
show();
sels.forEach(i => i.oninput = () => show());
chkdiff.oninput = () => show();
</script>
<style>
body {
font-family: sans-serif;
text-align: center;
margin: 0;
padding: 0;
}
h1 {
margin: 0;
font-size: 4vw;
padding: 2vw 0;
margin-bottom: 1vw;
background-color: #ff5959;
color: white;
}
h1 a {
text-decoration: none;
color: white !important;
}
.piechart {
display: inline-block;
margin: 1em;
}
.chart {
display: block;
height: 650px;
}
.tbl {
margin-top: 1em;
display: inline-block;
vertical-align: top;
}
.tbl table td:first-child {
text-align: left;
}
table {
border-collapse: collapse;
display: inline-block;
text-align: right;
}
td {
font-size: 70%;
border: 1px solid #444;
padding: 2px;
x-white-space: nowrap;
word-break: break-all;
}
select {
font-size: 16px;
}
#credit a {
color: gray !important;
text-decoration: none;
}
#credit {
margin-top: 1em;
font-size: 80%;
}
#head {
font-size: 80%;
margin-bottom: .5em;
}
#head a {
color: gray !important;
text-decoration: none;
}
#title {
margin-top: 1em;
}
button {
margin: 3px;
background-color: white;
}
@media screen and (max-width: 440px) {
.piechart {
margin: 0;
}
canvas {
display: inline-block;
width: 360px;
height: 360px;
}
}
#credit {
margin-top: 1em;
}
</style>
<script type="module" src="https://code4fukui.github.io/qr-code/qr-code.js"></script>
<qr-code></qr-code><br>
</body>
</html>