| <!DOCTYPE html> |
| <html lang="ja"> |
| <head> |
| <meta charset="utf-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <title>レアアース サプライチェーン フロー / アジア</title> |
| <style> |
| :root{ |
| --sea:#08131C; --sea2:#0C1B26; --land:#17262F; --coast:#2E4756; --grid:#132330; |
| --ink:#E7EFF4; --muted:#7B93A2; --dim:#4C6373; --panel:#0B1721; --line:#1E323F; |
| --s1:#6A7C8C; --s2:#35A7C4; --s3:#43CFA2; --s4:#E2C24A; --s5:#F4705A; --s6:#B57BE8; |
| --mono:ui-monospace,"SFMono-Regular",Menlo,Consolas,"Liberation Mono",monospace; |
| --sans:-apple-system,BlinkMacSystemFont,"Hiragino Sans","Hiragino Kaku Gothic ProN", |
| "Yu Gothic","Noto Sans JP","Segoe UI",Roboto,sans-serif; |
| } |
| *{box-sizing:border-box} |
| html,body{height:100%;margin:0} |
| body{ |
| background:var(--sea); color:var(--ink); font-family:var(--sans); |
| font-size:13px; line-height:1.5; overflow:hidden; |
| display:grid; grid-template-rows:auto 1fr auto; grid-template-columns:320px 1fr; |
| grid-template-areas:"head head" "side map" "foot foot"; |
| } |
| header{ |
| grid-area:head; display:flex; align-items:baseline; gap:14px; flex-wrap:wrap; |
| padding:12px 18px; border-bottom:1px solid var(--line); background:var(--panel); |
| } |
| h1{margin:0; font-size:14px; font-weight:600; letter-spacing:.14em; |
| text-transform:uppercase; font-family:var(--mono)} |
| h1 span{color:var(--s5)} |
| .sub{color:var(--muted); font-size:11px} |
| .tag{margin-left:auto; font-family:var(--mono); font-size:10px; letter-spacing:.1em; |
| color:var(--s4); border:1px solid var(--s4); border-radius:2px; padding:2px 7px; opacity:.85} |
| aside{grid-area:side; border-right:1px solid var(--line); background:var(--panel); |
| overflow-y:auto; padding:14px 14px 30px} |
| .block{margin-bottom:18px} |
| .lbl{font-family:var(--mono); font-size:10px; letter-spacing:.16em; text-transform:uppercase; |
| color:var(--dim); margin-bottom:8px; display:flex; justify-content:space-between; gap:8px} |
| .lbl b{color:var(--muted); font-weight:400} |
| |
| |
| .ladder{display:flex; flex-direction:column-reverse; gap:1px} |
| .rung{display:grid; grid-template-columns:22px 1fr auto; align-items:center; gap:9px; |
| padding:6px 8px; border:0; width:100%; text-align:left; cursor:pointer; |
| background:transparent; color:var(--ink); font:inherit; border-radius:3px; position:relative} |
| .rung:hover{background:#12222D} |
| .rung .no{font-family:var(--mono); font-size:10px; color:var(--dim)} |
| .rung .bar{position:absolute; left:0; top:0; bottom:0; width:2px; background:var(--c)} |
| .rung .nm{font-size:12px} |
| .rung .amt{font-family:var(--mono); font-size:10px; color:var(--muted); font-variant-numeric:tabular-nums} |
| .rung[aria-pressed="false"]{opacity:.32} |
| .rung[aria-pressed="false"] .bar{background:var(--dim)} |
| |
| |
| .routes{display:flex; flex-direction:column; gap:2px} |
| .route{border:0; background:transparent; color:var(--ink); font:inherit; text-align:left; |
| padding:7px 8px; border-radius:3px; cursor:pointer; border-left:2px solid transparent; width:100%} |
| .route:hover,.route.on{background:#12222D; border-left-color:var(--s5)} |
| .route .p{font-size:12px} |
| .route .m{font-family:var(--mono); font-size:10px; color:var(--muted); |
| display:flex; gap:8px; margin-top:2px; flex-wrap:wrap} |
| .route .d{color:var(--s5)} |
| .route .up{color:var(--s3)} |
| |
| |
| details{border-top:1px solid var(--line); padding:9px 0} |
| details summary{cursor:pointer; list-style:none; font-family:var(--mono); font-size:10px; |
| letter-spacing:.16em; text-transform:uppercase; color:var(--dim); |
| display:flex; justify-content:space-between; align-items:center} |
| details summary::-webkit-details-marker{display:none} |
| details summary .cnt{color:var(--s5); font-size:10px} |
| details[open] summary{margin-bottom:8px} |
| input[type=text]{width:100%; background:#0E1D27; border:1px solid var(--line); color:var(--ink); |
| padding:6px 8px; border-radius:3px; font-family:var(--mono); font-size:12px; margin-bottom:6px} |
| input[type=text]:focus{outline:2px solid var(--s2); outline-offset:1px} |
| .opts{max-height:210px; overflow-y:auto; display:flex; flex-direction:column; gap:1px} |
| .opt{display:grid; grid-template-columns:14px 1fr auto; gap:7px; align-items:center; |
| padding:3px 5px; border-radius:3px; cursor:pointer; font-size:12px} |
| .opt:hover{background:#12222D} |
| .opt .bx{width:11px; height:11px; border:1px solid var(--dim); border-radius:2px} |
| .opt.on .bx{background:var(--s2); border-color:var(--s2)} |
| .opt .t{overflow:hidden; text-overflow:ellipsis; white-space:nowrap} |
| .opt .v{font-family:var(--mono); font-size:10px; color:var(--muted); font-variant-numeric:tabular-nums} |
| .clear{background:none; border:0; color:var(--dim); font:inherit; font-size:10px; |
| cursor:pointer; padding:0} |
| .clear:hover{color:var(--s5)} |
| |
| .seg{display:flex; gap:4px; flex-wrap:wrap} |
| .seg button, .presets button{background:#0E1D27; border:1px solid var(--line); color:var(--muted); |
| font:inherit; font-size:11px; padding:4px 9px; border-radius:3px; cursor:pointer} |
| .seg button.on{color:var(--ink); border-color:var(--s2); background:#12222D} |
| .seg button:hover,.presets button:hover{color:var(--ink); border-color:var(--dim)} |
| .presets{display:flex; gap:5px; flex-wrap:wrap} |
| |
| .readout{font-family:var(--mono); font-variant-numeric:tabular-nums} |
| .readout .big{font-size:26px; color:var(--s5); letter-spacing:-.02em} |
| .readout .u{font-size:11px; color:var(--muted); margin-left:5px} |
| .readout .delta{font-size:11px; color:var(--muted); margin-top:3px} |
| |
| main{grid-area:map; position:relative; overflow:hidden; background:var(--sea)} |
| canvas{display:block; width:100%; height:100%; cursor:grab} |
| canvas.drag{cursor:grabbing} |
| #tip{position:absolute; pointer-events:none; opacity:0; transition:opacity .1s; |
| background:rgba(8,19,28,.94); border:1px solid var(--line); border-left-width:3px; |
| padding:8px 11px; border-radius:3px; max-width:260px; font-size:12px; |
| box-shadow:0 6px 24px rgba(0,0,0,.5)} |
| #tip .t{font-weight:600; margin-bottom:3px} |
| #tip .r{font-family:var(--mono); font-size:11px; color:var(--muted); |
| display:flex; justify-content:space-between; gap:12px} |
| #tip .r b{color:var(--ink); font-weight:500} |
| #clip{position:absolute; right:12px; bottom:12px; font-size:11px; color:var(--dim); |
| background:rgba(8,19,28,.85); border:1px solid var(--line); padding:5px 9px; border-radius:3px} |
| #loader{position:fixed; inset:0; z-index:50; background:rgba(8,19,28,.97); |
| display:flex; align-items:center; justify-content:center; padding:24px} |
| #loader.over .lbox{border-color:var(--s2)} |
| #loader .lbox{border:1px dashed var(--line); border-radius:6px; padding:26px 30px; |
| max-width:460px; background:var(--panel)} |
| #loader h2{margin:0 0 10px; font-size:14px; font-family:var(--mono); letter-spacing:.1em} |
| #loader p{color:var(--muted); font-size:12px; margin:0 0 14px} |
| #loader code{color:var(--s4); font-family:var(--mono)} |
| #loader input[type=file]{font-size:12px; color:var(--muted)} |
| #loader .st{display:flex; gap:10px; margin-top:14px; font-family:var(--mono); font-size:11px} |
| #loader .st span{color:var(--dim); border:1px solid var(--line); padding:3px 8px; border-radius:3px} |
| #loader .st span.ok{color:var(--s3); border-color:var(--s3)} |
| #loader .err{color:var(--s5); margin:10px 0 0; min-height:1em} |
| #warn{position:absolute; left:12px; bottom:12px; font-size:11px; color:var(--s4); |
| background:rgba(8,19,28,.85); border:1px solid var(--line); padding:5px 9px; border-radius:3px} |
| |
| footer{grid-area:foot; border-top:1px solid var(--line); background:var(--panel); |
| padding:9px 18px; display:flex; align-items:center; gap:14px; flex-wrap:wrap} |
| #period{font-family:var(--mono); font-size:18px; min-width:9.5em; font-variant-numeric:tabular-nums} |
| input[type=range]{flex:1; min-width:160px; accent-color:var(--s5); height:20px} |
| .hint{font-size:11px; color:var(--dim)} |
| @media (max-width:900px){ |
| body{grid-template-columns:1fr; grid-template-areas:"head" "map" "side" "foot"; |
| grid-template-rows:auto 50vh 1fr auto; overflow:auto} |
| aside{border-right:0; border-top:1px solid var(--line)} |
| } |
| @media (prefers-reduced-motion:reduce){*{transition:none!important}} |
| </style> |
| </head> |
| <body> |
|
|
| <header> |
| <h1>Rare Earth <span>/</span> Flow</h1> |
| <div class="sub">鉱石から磁石まで、アジア域内の加工段階別フロー</div> |
| <div class="tag" id="srctag">—</div> |
| </header> |
|
|
| <aside> |
| <div class="block"> |
| <div class="lbl"><span>対日流入</span><span id="plabel"></span></div> |
| <div class="readout"> |
| <span class="big" id="total">—</span><span class="u" id="unit">USD</span> |
| <div class="delta" id="delta"></div> |
| </div> |
| <div class="seg" style="margin-top:8px"> |
| <button data-mode="v" class="on">金額</button> |
| <button data-mode="q">数量(日本側のみ)</button> |
| </div> |
| </div> |
|
|
| <div class="block"> |
| <div class="lbl"><span>加工段階</span><span>付加価値 →</span></div> |
| <div class="ladder" id="ladder"></div> |
| </div> |
|
|
| <div class="block"> |
| <div class="lbl"><span>経由ルート</span><span>2 hop 推定</span></div> |
| <div class="routes" id="routes"></div> |
| </div> |
|
|
| <details id="d-hs"> |
| <summary><span>HS6</span><span class="cnt" data-cnt></span></summary> |
| <input type="text" data-search placeholder="253090 …" autocomplete="off"> |
| <div class="opts" data-opts></div> |
| <button class="clear" data-clear>選択を解除</button> |
| </details> |
|
|
| <details id="d-party"> |
| <summary><span>者(輸入者・輸出者)</span><span class="cnt" data-cnt></span></summary> |
| <div class="seg" style="margin-bottom:6px"> |
| <button data-role="both" class="on">両方</button> |
| <button data-role="imp">輸入者</button> |
| <button data-role="exp">輸出者</button> |
| </div> |
| <input type="text" data-search placeholder="社名の一部 …" autocomplete="off"> |
| <div class="opts" data-opts></div> |
| <button class="clear" data-clear>選択を解除</button> |
| </details> |
|
|
| <details id="d-goods"> |
| <summary><span>品名</span><span class="cnt" data-cnt></span></summary> |
| <input type="text" data-search placeholder="磁石 / OXIDE …" autocomplete="off"> |
| <div class="opts" data-opts></div> |
| <button class="clear" data-clear>選択を解除</button> |
| </details> |
|
|
| <details id="d-oc"> |
| <summary><span>原産国</span><span class="cnt" data-cnt></span></summary> |
| <div class="opts" data-opts></div> |
| <button class="clear" data-clear>選択を解除</button> |
| </details> |
|
|
| <div class="block" style="margin-top:16px"> |
| <div class="lbl"><span>表示範囲</span></div> |
| <div class="presets" id="presets"></div> |
| </div> |
|
|
| <div class="block"> |
| <button class="clear" id="csv">現在の選択を CSV 出力</button> |
| </div> |
| </aside> |
|
|
| <main> |
| <canvas id="cv"></canvas> |
| <div id="tip"></div> |
| <div id="warn" hidden></div> |
| <div id="clip" hidden></div> |
| </main> |
|
|
| <footer> |
| <div id="period">—</div> |
| <input type="range" id="slider" min="0" max="0" step="1" value="0"> |
| <div class="seg" id="winseg"> |
| <button data-win="1">単月</button> |
| <button data-win="3" class="on">四半期</button> |
| <button data-win="12">12ヶ月</button> |
| </div> |
| <div class="hint">ドラッグで移動 / ホイールで拡大 / 弧にカーソルで詳細</div> |
| </footer> |
|
|
| |
| <script src="data/asia_land.js"></script> |
| |
| <script src="data/facts.js"></script> |
|
|
| <script> |
| window.__start = function(){ |
| 'use strict'; |
| |
| const F = window.FACTS, LAND = window.ASIA_LAND; |
| |
| const C = F.cols, N = F.rows; |
| const DICT = F.dict; |
| const cv = document.getElementById('cv'), ctx = cv.getContext('2d'); |
| const tip = document.getElementById('tip'); |
| const RM = matchMedia('(prefers-reduced-motion: reduce)').matches; |
| const cssv = n => getComputedStyle(document.documentElement).getPropertyValue(n).trim(); |
| |
| |
| const STAGES = [ |
| {id:1, n:'鉱石・精鉱', c:'--s1'}, |
| {id:2, n:'酸化物・化合物', c:'--s2'}, |
| {id:3, n:'金属・地金', c:'--s3'}, |
| {id:4, n:'合金・粉末', c:'--s4'}, |
| {id:5, n:'永久磁石', c:'--s5'}, |
| {id:6, n:'部品・モーター', c:'--s6'} |
| ]; |
| STAGES.forEach(s => s.hex = cssv(s.c)); |
| const ST = Object.fromEntries(STAGES.map(s => [s.id, s])); |
| |
| const JPN = DICT.country.indexOf('JPN'); |
| const PARTNERS = (F.partners || []).map(c => DICT.country.indexOf(c)).filter(i => i >= 0); |
| const GEO = F.geo || {}; |
| const nParty = DICT.party.length, nHs = DICT.hs.length, |
| nGoods = DICT.goods.length, nCty = DICT.country.length; |
| |
| |
| const S = { |
| m0: Math.max(0, F.n_months - 3), win: 3, |
| mode: 'v', role: 'both', |
| stages: new Set(STAGES.map(s => s.id)), |
| hs: new Set(), party: new Set(), goods: new Set(), oc: new Set(), |
| route: null, hoverRoute: null, hover: null, hoverStage: null, focus: null |
| }; |
| |
| |
| let AGG = new Map(); |
| let stageTot = {}, jpTot = 0, jpPrev = 0, grandTot = 0; |
| let rankHs, rankParty, rankGoods, rankOc; |
| let inflow = new Map(), outflow = new Map(); |
| |
| function recompute(){ |
| const lo = S.m0, hi = S.m0 + S.win - 1; |
| const plo = lo - S.win, phi = lo - 1; |
| const qMode = S.mode === 'q'; |
| |
| AGG = new Map(); stageTot = {}; jpTot = 0; jpPrev = 0; grandTot = 0; |
| rankHs = new Float64Array(nHs); rankParty = new Float64Array(nParty); |
| rankGoods = new Float64Array(nGoods); rankOc = new Float64Array(nCty); |
| inflow = new Map(); outflow = new Map(); |
| |
| const fHs = S.hs.size > 0, fPa = S.party.size > 0, |
| fGo = S.goods.size > 0, fOc = S.oc.size > 0; |
| |
| for (let i = 0; i < N; i++){ |
| const m = C.ym[i]; |
| const inCur = m >= lo && m <= hi, inPrev = m >= plo && m <= phi; |
| if (!inCur && !inPrev) continue; |
| if (qMode && C.src[i] !== 1) continue; |
| |
| const st = C.st[i]; |
| if (!S.stages.has(st)) continue; |
| |
| const hs = C.hs[i], g = C.g[i], oc = C.oc[i], im = C.imp[i], ex = C.exp[i]; |
| const pHs = !fHs || S.hs.has(hs); |
| const pGo = !fGo || S.goods.has(g); |
| const pOc = !fOc || S.oc.has(oc); |
| let pPa = !fPa; |
| if (fPa){ |
| if (S.role === 'imp') pPa = S.party.has(im); |
| else if (S.role === 'exp') pPa = S.party.has(ex); |
| else pPa = S.party.has(im) || S.party.has(ex); |
| } |
| |
| const v = qMode ? C.q[i] : C.v[i]; |
| if (v < 0) continue; |
| |
| |
| if (inCur){ |
| if (pGo && pOc && pPa) rankHs[hs] += v; |
| if (pHs && pOc && pPa) rankGoods[g] += v; |
| if (pHs && pGo && pPa) rankOc[oc] += v; |
| if (pHs && pGo && pOc){ rankParty[im] += v; if (ex !== im) rankParty[ex] += v; } |
| } |
| |
| if (!(pHs && pGo && pOc && pPa)) continue; |
| |
| const a = C.a[i], b = C.b[i]; |
| if (inPrev){ if (b === JPN) jpPrev += v; continue; } |
| |
| grandTot += v; |
| if (b === JPN){ jpTot += v; stageTot[st] = (stageTot[st] || 0) + v; } |
| |
| const k = a + ',' + b + ',' + st; |
| let e = AGG.get(k); |
| if (!e){ e = {a, b, st, v:0, rows:0}; AGG.set(k, e); } |
| e.v += v; e.rows++; |
| |
| |
| if (b === JPN && a !== JPN){ |
| let o = outflow.get(a); if (!o){ o = {tot:0, st:{}}; outflow.set(a, o); } |
| o.tot += v; o.st[st] = (o.st[st] || 0) + v; |
| } else if (PARTNERS.includes(b)){ |
| let mm = inflow.get(b); if (!mm){ mm = new Map(); inflow.set(b, mm); } |
| let o = mm.get(a); if (!o){ o = {tot:0, st:{}}; mm.set(a, o); } |
| o.tot += v; o.st[st] = (o.st[st] || 0) + v; |
| } |
| } |
| buildRoutes(); |
| } |
| |
| |
| let ROUTES = []; |
| function buildRoutes(){ |
| ROUTES = []; |
| for (const P of PARTNERS){ |
| const out = outflow.get(P); if (!out || out.tot <= 0) continue; |
| const ins = inflow.get(P); if (!ins) continue; |
| const top = [...ins.entries()].sort((x, y) => y[1].tot - x[1].tot).slice(0, 2); |
| for (const [X, o] of top){ |
| if (X === JPN) continue; |
| ROUTES.push({ |
| x:X, p:P, vIn:o.tot, vOut:out.tot, |
| stIn:+topKey(o.st), stOut:+topKey(out.st) |
| }); |
| } |
| } |
| ROUTES.sort((a, b) => b.vOut - a.vOut); |
| ROUTES = ROUTES.slice(0, 6); |
| } |
| const topKey = o => Object.keys(o).sort((a, b) => o[b] - o[a])[0] || 0; |
| const routeArcs = r => r ? new Set([r.x + ',' + r.p, r.p + ',' + JPN]) : null; |
| |
| |
| const R = Math.PI / 180; |
| const my = lat => Math.log(Math.tan(Math.PI/4 + Math.max(-85, Math.min(85, lat))*R/2)); |
| const V = {cx:0, cy:0, k:1}; |
| let W = 0, H = 0, DPR = 1; |
| const px = (lon, lat) => [(lon*R - V.cx)*V.k + W/2, H/2 - (my(lat) - V.cy)*V.k]; |
| function fit(b){ |
| const x0=b[0]*R, x1=b[2]*R, y0=my(b[1]), y1=my(b[3]); |
| V.k = Math.min(W*0.96/(x1-x0), H*0.94/(y1-y0)); |
| V.cx = (x0+x1)/2; V.cy = (y0+y1)/2; |
| geomDirty = true; |
| } |
| const PRESETS = { |
| '東アジア':[104,17,149,45], |
| '東南アジア→日本':[95,-3,146,41], |
| '広域':[70,-36,153,55] |
| }; |
| const toVec = (lon, lat) => { const a=lon*R, b=lat*R, c=Math.cos(b); |
| return [c*Math.cos(a), c*Math.sin(a), Math.sin(b)]; }; |
| const toLL = v => [Math.atan2(v[1], v[0])/R, Math.asin(Math.max(-1, Math.min(1, v[2])))/R]; |
| |
| function arcPoints(A, B, lift, n){ |
| const va = toVec(A[0], A[1]), vb = toVec(B[0], B[1]); |
| const dot = Math.max(-1, Math.min(1, va[0]*vb[0]+va[1]*vb[1]+va[2]*vb[2])); |
| const om = Math.acos(dot), sw = Math.sin(om), raw = []; |
| for (let i = 0; i <= n; i++){ |
| const t = i/n; let v; |
| if (sw < 1e-9) v = va; |
| else { const s1 = Math.sin((1-t)*om)/sw, s2 = Math.sin(t*om)/sw; |
| v = [va[0]*s1+vb[0]*s2, va[1]*s1+vb[1]*s2, va[2]*s1+vb[2]*s2]; } |
| const ll = toLL(v); raw.push(px(ll[0], ll[1])); |
| } |
| const p0 = raw[0], p1 = raw[n]; |
| const dx = p1[0]-p0[0], dy = p1[1]-p0[1], len = Math.hypot(dx, dy) || 1; |
| const nx = -dy/len, ny = dx/len, h = Math.min(lift*len, 130); |
| return raw.map((p, i) => { const w = Math.sin(Math.PI*i/n); |
| return [p[0] + nx*h*w, p[1] + ny*h*w]; }); |
| } |
| |
| |
| |
| |
| |
| let paths = [], landPaths = [], gridPath = null, geomDirty = true, maxV = 1; |
| const DRAW_LIMIT = 260; |
| const wScale = v => Math.max(1.1, Math.sqrt(v/maxV) * 13); |
| const dirty = () => { geomDirty = true; }; |
| |
| function buildGeom(){ |
| gridPath = new Path2D(); |
| for (let lon = 60; lon <= 180; lon += 10){ |
| const a = px(lon, -40), b = px(lon, 60); |
| gridPath.moveTo(a[0], a[1]); gridPath.lineTo(b[0], b[1]); |
| } |
| for (let lat = -40; lat <= 60; lat += 10){ |
| const s0 = px(60, lat); gridPath.moveTo(s0[0], s0[1]); |
| for (let lon = 70; lon <= 180; lon += 10){ const p = px(lon, lat); gridPath.lineTo(p[0], p[1]); } |
| } |
| |
| landPaths = LAND.features.map(f => { |
| const p = new Path2D(); |
| for (const ring of f.g){ |
| for (let i = 0; i < ring.length; i++){ |
| const q = px(ring[i][0], ring[i][1]); |
| i ? p.lineTo(q[0], q[1]) : p.moveTo(q[0], q[1]); |
| } |
| p.closePath(); |
| } |
| return {id:f.id, p}; |
| }); |
| |
| const fi = S.focus ? DICT.country.indexOf(S.focus) : -1; |
| let list = [...AGG.values()].filter(e => { |
| if (!GEO[DICT.country[e.a]] || !GEO[DICT.country[e.b]]) return false; |
| if (fi >= 0 && e.a !== fi && e.b !== fi) return false; |
| return true; |
| }); |
| list.sort((a, b) => b.v - a.v); |
| const clipped = list.length > DRAW_LIMIT; |
| if (clipped) list = list.slice(0, DRAW_LIMIT); |
| maxV = list.length ? list[0].v : 1; |
| list.reverse(); |
| |
| paths = list.map(e => { |
| const pts = arcPoints(GEO[DICT.country[e.a]], GEO[DICT.country[e.b]], 0.20, 36); |
| const p = new Path2D(); |
| p.moveTo(pts[0][0], pts[0][1]); |
| for (let i = 1; i < pts.length; i++) p.lineTo(pts[i][0], pts[i][1]); |
| return {e, pts, p, w:wScale(e.v), key:e.a + ',' + e.b}; |
| }); |
| document.getElementById('clip').hidden = !clipped; |
| if (clipped) document.getElementById('clip').textContent = |
| `金額上位 ${DRAW_LIMIT} 弧のみ描画(集計値は全弧)`; |
| geomDirty = false; |
| } |
| |
| function draw(t){ |
| ctx.setTransform(DPR, 0, 0, DPR, 0, 0); |
| if (geomDirty) buildGeom(); |
| ctx.clearRect(0, 0, W, H); |
| const g = ctx.createLinearGradient(0, 0, 0, H); |
| g.addColorStop(0, cssv('--sea2')); g.addColorStop(1, cssv('--sea')); |
| ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); |
| |
| ctx.strokeStyle = cssv('--grid'); ctx.lineWidth = 1; ctx.stroke(gridPath); |
| |
| const landC = cssv('--land'), coastC = cssv('--coast'); |
| ctx.lineWidth = 1; ctx.strokeStyle = coastC; |
| for (const f of landPaths){ |
| ctx.fillStyle = (S.focus === f.id) ? '#213645' : landC; |
| ctx.fill(f.p); ctx.stroke(f.p); |
| } |
| |
| const hotSet = routeArcs(S.hoverRoute || S.route); |
| const dim = !!(hotSet || S.hover || S.hoverStage); |
| |
| ctx.lineCap = 'round'; ctx.lineJoin = 'round'; |
| for (const pa of paths){ |
| const e = pa.e; |
| const hot = (hotSet && hotSet.has(pa.key)) || S.hover === pa.key + ',' + e.st |
| || S.hoverStage === e.st; |
| const alpha = dim ? (hot ? 1 : 0.10) : 0.82; |
| const col = ST[e.st] ? ST[e.st].hex : cssv('--muted'); |
| ctx.globalAlpha = alpha; |
| ctx.strokeStyle = col; ctx.lineWidth = pa.w; |
| if (hot){ ctx.shadowColor = col; ctx.shadowBlur = 14; } |
| ctx.stroke(pa.p); |
| if (hot){ ctx.shadowBlur = 0; } |
| } |
| ctx.globalAlpha = 1; |
| |
| |
| if (!RM){ |
| ctx.fillStyle = '#fff'; |
| for (const pa of paths){ |
| if (pa.w < 3) continue; |
| const e = pa.e; |
| const hot = (hotSet && hotSet.has(pa.key)) || S.hover === pa.key + ',' + e.st |
| || S.hoverStage === e.st; |
| const alpha = dim ? (hot ? 1 : 0) : 0.9; |
| if (alpha <= 0.05) continue; |
| ctx.globalAlpha = alpha; |
| const nP = Math.min(4, Math.max(1, Math.round(pa.w/3))); |
| for (let k = 0; k < nP; k++){ |
| const ph = ((t/2600) + k/nP + (e.a % 7)/9) % 1; |
| const idx = ph*(pa.pts.length-1), i0 = Math.floor(idx), fr = idx - i0; |
| const q0 = pa.pts[i0], q1 = pa.pts[Math.min(pa.pts.length-1, i0+1)]; |
| ctx.beginPath(); |
| ctx.arc(q0[0]+(q1[0]-q0[0])*fr, q0[1]+(q1[1]-q0[1])*fr, |
| Math.min(2.6, pa.w*0.30), 0, 7); |
| ctx.fill(); |
| } |
| } |
| ctx.globalAlpha = 1; |
| } |
| |
| ctx.font = '11px ' + cssv('--sans'); |
| for (const [code, ll] of Object.entries(GEO)){ |
| const p = px(ll[0], ll[1]); |
| if (p[0] < -60 || p[0] > W+60 || p[1] < -40 || p[1] > H+40) continue; |
| const isJP = code === 'JPN'; |
| ctx.beginPath(); ctx.arc(p[0], p[1], isJP ? 5 : 3.2, 0, 7); |
| ctx.fillStyle = isJP ? cssv('--s5') : cssv('--ink'); |
| ctx.globalAlpha = isJP ? 1 : .7; ctx.fill(); ctx.globalAlpha = 1; |
| if (isJP){ |
| ctx.strokeStyle = cssv('--s5'); ctx.lineWidth = 1; ctx.globalAlpha = .5; |
| ctx.beginPath(); ctx.arc(p[0], p[1], 11, 0, 7); ctx.stroke(); ctx.globalAlpha = 1; |
| } |
| ctx.fillStyle = cssv(S.focus === code ? '--ink' : '--muted'); |
| ctx.fillText(code, p[0] + 9, p[1] + 4); |
| } |
| } |
| let raf = 0; |
| |
| |
| function resize(){ |
| DPR = Math.min(2, devicePixelRatio || 1); |
| const r = cv.getBoundingClientRect(); W = r.width; H = r.height; |
| cv.width = W*DPR; cv.height = H*DPR; |
| } |
| new ResizeObserver(() => { const k = V.k, cx = V.cx, cy = V.cy; |
| resize(); V.k = k || V.k; V.cx = cx; V.cy = cy; dirty(); }).observe(cv); |
| |
| let drag = null, suppressClick = false; |
| cv.addEventListener('pointerdown', ev => { |
| drag = {x:ev.clientX, y:ev.clientY, cx:V.cx, cy:V.cy, moved:false}; |
| cv.setPointerCapture(ev.pointerId); cv.classList.add('drag'); |
| }); |
| cv.addEventListener('pointermove', ev => { |
| const r = cv.getBoundingClientRect(), mx = ev.clientX - r.left, myy = ev.clientY - r.top; |
| if (drag){ |
| const dx = ev.clientX - drag.x, dy = ev.clientY - drag.y; |
| if (Math.abs(dx)+Math.abs(dy) > 3) drag.moved = true; |
| V.cx = drag.cx - dx/V.k; V.cy = drag.cy + dy/V.k; dirty(); return; |
| } |
| let best = null, bd = 9; |
| for (const pa of paths){ |
| const tol = Math.max(6, pa.w/2 + 4); |
| for (let i = 0; i < pa.pts.length-1; i++){ |
| const d = segDist(mx, myy, pa.pts[i], pa.pts[i+1]); |
| if (d < tol && d < bd){ bd = d; best = pa; } |
| } |
| } |
| S.hover = best ? best.e.a + ',' + best.e.b + ',' + best.e.st : null; |
| if (best) showTip(best.e, mx, myy); else tip.style.opacity = 0; |
| }); |
| addEventListener('pointerup', () => { suppressClick = !!(drag && drag.moved); |
| drag = null; cv.classList.remove('drag'); }); |
| cv.addEventListener('click', ev => { |
| if (suppressClick){ suppressClick = false; return; } |
| if (S.hover) return; |
| const r = cv.getBoundingClientRect(); |
| const hit = hitCountry(ev.clientX - r.left, ev.clientY - r.top); |
| S.focus = (S.focus === hit) ? null : hit; dirty(); |
| }); |
| cv.addEventListener('pointerleave', () => { S.hover = null; tip.style.opacity = 0; }); |
| cv.addEventListener('wheel', ev => { |
| ev.preventDefault(); |
| const r = cv.getBoundingClientRect(), mx = ev.clientX - r.left, myy = ev.clientY - r.top; |
| const wx = (mx - W/2)/V.k + V.cx, wy = V.cy - (myy - H/2)/V.k; |
| V.k = Math.max(120, Math.min(9000, V.k * Math.exp(-ev.deltaY * 0.0016))); |
| V.cx = wx - (mx - W/2)/V.k; V.cy = wy + (myy - H/2)/V.k; dirty(); |
| }, {passive:false}); |
| |
| function segDist(x, y, a, b){ |
| const dx = b[0]-a[0], dy = b[1]-a[1], L = dx*dx + dy*dy; |
| const t = L ? Math.max(0, Math.min(1, ((x-a[0])*dx + (y-a[1])*dy)/L)) : 0; |
| return Math.hypot(x - (a[0]+t*dx), y - (a[1]+t*dy)); |
| } |
| function hitCountry(x, y){ |
| for (const f of LAND.features){ |
| for (const ring of f.g){ |
| let inside = false; |
| for (let i = 0, j = ring.length-1; i < ring.length; j = i++){ |
| const pi = px(ring[i][0], ring[i][1]), pj = px(ring[j][0], ring[j][1]); |
| if ((pi[1] > y) !== (pj[1] > y) && |
| x < (pj[0]-pi[0])*(y-pi[1])/(pj[1]-pi[1]) + pi[0]) inside = !inside; |
| } |
| if (inside) return f.id; |
| } |
| } |
| return null; |
| } |
| |
| const fmt = v => v >= 10000 ? Math.round(v/1000).toLocaleString() + 'k' |
| : v >= 100 ? Math.round(v).toLocaleString() : v.toFixed(1); |
| const unit = () => S.mode === 'q' ? 'kg' : 'USD'; |
| |
| function showTip(e, x, y){ |
| const st = ST[e.st] || {n:'その他', hex:cssv('--muted')}; |
| const share = grandTot ? e.v/grandTot*100 : 0; |
| tip.style.borderLeftColor = st.hex; |
| tip.innerHTML = |
| `<div class="t">${DICT.country[e.a]} → ${DICT.country[e.b]}</div>` + |
| `<div class="r"><span>${st.n}</span><b>S${e.st}</b></div>` + |
| `<div class="r"><span>${S.mode==='q'?'数量':'金額'}</span><b>${fmt(e.v)} ${unit()}</b></div>` + |
| `<div class="r"><span>構成比</span><b>${share.toFixed(1)}%</b></div>` + |
| `<div class="r"><span>明細行</span><b>${e.rows.toLocaleString()}</b></div>`; |
| tip.style.opacity = 1; |
| const w = tip.offsetWidth, h = tip.offsetHeight; |
| tip.style.left = Math.min(W-w-8, Math.max(8, x+14)) + 'px'; |
| tip.style.top = Math.min(H-h-8, Math.max(8, y-h-12)) + 'px'; |
| } |
| |
| |
| const ladder = document.getElementById('ladder'); |
| STAGES.forEach(s => { |
| const b = document.createElement('button'); |
| b.className = 'rung'; b.type = 'button'; b.style.setProperty('--c', s.hex); |
| b.setAttribute('aria-pressed', 'true'); |
| b.innerHTML = `<span class="bar"></span><span class="no">S${s.id}</span>` + |
| `<span class="nm">${s.n}</span><span class="amt" data-a></span>`; |
| b.onclick = () => { |
| S.stages.has(s.id) ? S.stages.delete(s.id) : S.stages.add(s.id); |
| b.setAttribute('aria-pressed', S.stages.has(s.id)); refresh(); |
| }; |
| b.onmouseenter = () => { S.hoverStage = s.id; }; |
| b.onmouseleave = () => { S.hoverStage = null; }; |
| ladder.append(b); |
| }); |
| |
| |
| function panel(id, labels, set, rank, opts){ |
| const el = document.getElementById(id); |
| const box = el.querySelector('[data-opts]'); |
| const search = el.querySelector('[data-search]'); |
| const cnt = el.querySelector('[data-cnt]'); |
| const limit = opts && opts.limit || 40; |
| let q = ''; |
| if (search) search.oninput = () => { q = search.value.trim().toUpperCase(); render(); }; |
| el.querySelector('[data-clear]').onclick = () => { set.clear(); refresh(); }; |
| |
| function render(){ |
| const rk = rank(); |
| cnt.textContent = set.size ? set.size + ' 選択中' : ''; |
| const idx = []; |
| for (let i = 0; i < labels.length; i++){ |
| if (q && !labels[i].toUpperCase().includes(q)) continue; |
| if (!set.has(i) && rk[i] <= 0 && !opts.keepZero) continue; |
| idx.push(i); |
| } |
| idx.sort((a, b) => (set.has(b) - set.has(a)) || (rk[b] - rk[a])); |
| const show = idx.slice(0, limit); |
| box.innerHTML = ''; |
| for (const i of show){ |
| const d = document.createElement('div'); |
| d.className = 'opt' + (set.has(i) ? ' on' : ''); |
| d.innerHTML = `<span class="bx"></span><span class="t">${esc(labels[i])}</span>` + |
| `<span class="v">${rk[i] > 0 ? fmt(rk[i]) : '—'}</span>`; |
| d.onclick = () => { set.has(i) ? set.delete(i) : set.add(i); refresh(); }; |
| box.append(d); |
| } |
| if (idx.length > show.length){ |
| const m = document.createElement('div'); |
| m.className = 'opt'; m.style.color = cssv('--dim'); |
| m.innerHTML = `<span></span><span class="t">他 ${idx.length - show.length} 件(検索で絞り込み)</span><span></span>`; |
| box.append(m); |
| } |
| } |
| return render; |
| } |
| const esc = s => s.replace(/[&<>]/g, c => ({'&':'&','<':'<','>':'>'}[c])); |
| |
| let renderHs, renderParty, renderGoods, renderOc; |
| function initPanels(){ |
| renderHs = panel('d-hs', DICT.hs, S.hs, () => rankHs, {limit:60}); |
| renderParty = panel('d-party', DICT.party, S.party, () => rankParty, {limit:40}); |
| renderGoods = panel('d-goods', DICT.goods, S.goods, () => rankGoods, {limit:40}); |
| renderOc = panel('d-oc', DICT.country, S.oc, () => rankOc, {limit:30, keepZero:true}); |
| } |
| document.querySelectorAll('#d-party [data-role]').forEach(b => { |
| b.onclick = () => { |
| document.querySelectorAll('#d-party [data-role]').forEach(x => x.classList.remove('on')); |
| b.classList.add('on'); S.role = b.dataset.role; refresh(); |
| }; |
| }); |
| document.querySelectorAll('[data-mode]').forEach(b => { |
| b.onclick = () => { |
| document.querySelectorAll('[data-mode]').forEach(x => x.classList.remove('on')); |
| b.classList.add('on'); S.mode = b.dataset.mode; |
| document.getElementById('unit').textContent = unit(); |
| refresh(); |
| }; |
| }); |
| |
| |
| const slider = document.getElementById('slider'); |
| function syncSlider(){ |
| slider.max = Math.max(0, F.n_months - S.win); |
| slider.value = Math.min(S.m0, +slider.max); |
| S.m0 = +slider.value; |
| } |
| slider.oninput = () => { S.m0 = +slider.value; refresh(); }; |
| document.querySelectorAll('#winseg button').forEach(b => { |
| b.onclick = () => { |
| document.querySelectorAll('#winseg button').forEach(x => x.classList.remove('on')); |
| b.classList.add('on'); S.win = +b.dataset.win; syncSlider(); refresh(); |
| }; |
| }); |
| function ymLabel(off){ |
| const base = F.base_ym, y0 = Math.floor(base/100), m0 = base % 100; |
| const t = (y0*12 + m0 - 1) + off; |
| return Math.floor(t/12) + '-' + String(t%12 + 1).padStart(2, '0'); |
| } |
| |
| const presets = document.getElementById('presets'); |
| Object.keys(PRESETS).forEach(k => { |
| const b = document.createElement('button'); |
| b.textContent = k; b.onclick = () => fit(PRESETS[k]); presets.append(b); |
| }); |
| |
| |
| const routesEl = document.getElementById('routes'); |
| function renderRoutes(){ |
| routesEl.innerHTML = ''; |
| if (!ROUTES.length){ |
| routesEl.innerHTML = '<div class="hint" style="padding:6px">該当なし</div>'; return; |
| } |
| for (const r of ROUTES){ |
| const b = document.createElement('button'); |
| b.className = 'route' + (S.route === r ? ' on' : ''); b.type = 'button'; |
| const up = r.stOut > r.stIn; |
| const conv = `S${r.stIn}→S${r.stOut}` + (up ? ' 加工' : ' 素通り疑い'); |
| b.innerHTML = |
| `<div class="p">${DICT.country[r.x]} → ${DICT.country[r.p]} → JPN</div>` + |
| `<div class="m"><span>流入 ${fmt(r.vIn)}</span><span>対日 ${fmt(r.vOut)}</span>` + |
| `<span class="${up ? 'up' : 'd'}">${conv}</span></div>`; |
| b.onclick = () => { S.route = (S.route === r) ? null : r; renderRoutes(); }; |
| b.onmouseenter = () => { S.hoverRoute = r; }; |
| b.onmouseleave = () => { S.hoverRoute = null; }; |
| routesEl.append(b); |
| } |
| } |
| |
| |
| function refresh(){ |
| S.route = null; S.hoverRoute = null; |
| recompute(); |
| |
| document.getElementById('period').textContent = |
| S.win === 1 ? ymLabel(S.m0) |
| : ymLabel(S.m0) + ' → ' + ymLabel(S.m0 + S.win - 1); |
| document.getElementById('plabel').textContent = |
| S.win === 1 ? '単月' : S.win === 3 ? '四半期' : '12ヶ月'; |
| document.getElementById('total').textContent = fmt(jpTot); |
| const ch = jpPrev ? (jpTot - jpPrev)/jpPrev*100 : 0; |
| document.getElementById('delta').textContent = |
| (jpPrev ? `前期間比 ${ch >= 0 ? '+' : ''}${ch.toFixed(1)}%` : '前期間なし') + |
| ` / 全弧計 ${fmt(grandTot)}`; |
| document.getElementById('srctag').textContent = |
| `${N.toLocaleString()} 行 / ${AGG.size} 弧`; |
| |
| [...ladder.children].forEach((b, i) => { |
| const s = STAGES[i]; |
| b.querySelector('[data-a]').textContent = stageTot[s.id] ? fmt(stageTot[s.id]) : '—'; |
| }); |
| |
| renderRoutes(); |
| renderHs(); renderParty(); renderGoods(); renderOc(); |
| geomDirty = true; |
| } |
| |
| |
| document.getElementById('csv').onclick = () => { |
| const rows = [['from', 'to', 'stage', S.mode === 'q' ? 'qty_kg' : 'value_usd', 'rows']]; |
| [...AGG.values()].sort((a, b) => b.v - a.v).forEach(e => |
| rows.push([DICT.country[e.a], DICT.country[e.b], e.st, e.v.toFixed(2), e.rows])); |
| const blob = new Blob(['\ufeff' + rows.map(r => r.join(',')).join('\n')], |
| {type:'text/csv'}); |
| const a = document.createElement('a'); |
| a.href = URL.createObjectURL(blob); |
| a.download = `flow_${ymLabel(S.m0)}_${S.win}m.csv`; |
| a.click(); URL.revokeObjectURL(a.href); |
| }; |
| |
| |
| resize(); |
| fit(PRESETS['東南アジア→日本']); |
| recompute(); |
| initPanels(); |
| syncSlider(); |
| refresh(); |
| (function loop(t){ draw(t); raf = requestAnimationFrame(loop); })(0); |
| |
| const noGeo = [...new Set(DICT.country)].filter(c => !GEO[c]); |
| if (noGeo.length){ |
| const w = document.getElementById('warn'); |
| w.hidden = false; |
| w.textContent = `座標未登録のため地図に非表示(集計には反映): ${noGeo.join(', ')}`; |
| } |
| }; |
| |
| |
| |
| |
| |
| (function(){ |
| const ready = () => window.FACTS && window.ASIA_LAND; |
| if (ready()){ window.__start(); return; } |
| |
| const ov = document.createElement('div'); |
| ov.id = 'loader'; |
| ov.innerHTML = |
| '<div class="lbox">' + |
| '<h2>データファイルを読み込む</h2>' + |
| '<p>サーバーを立てられない環境向けの読み込み口です。<br>' + |
| '<code>asia_land.js</code> と <code>facts.js</code> を選ぶか、この枠にドロップしてください。</p>' + |
| '<input type="file" id="lfile" multiple accept=".js,.json">' + |
| '<div class="st"><span data-need="ASIA_LAND">asia_land.js</span>' + |
| '<span data-need="FACTS">facts.js</span></div>' + |
| '<p class="err" id="lerr"></p>' + |
| '</div>'; |
| document.body.append(ov); |
| |
| const mark = () => { |
| ov.querySelectorAll('[data-need]').forEach(el => { |
| el.classList.toggle('ok', !!window[el.dataset.need]); |
| }); |
| }; |
| const err = m => { document.getElementById('lerr').textContent = m; }; |
| |
| function ingest(text, name){ |
| try { |
| (0, eval)(text); |
| } catch (e) { |
| err(`${name} の読み込みに失敗: ${e.message}`); return; |
| } |
| mark(); |
| if (ready()){ ov.remove(); window.__start(); } |
| } |
| |
| function take(files){ |
| err(''); |
| [...files].forEach(f => { |
| const r = new FileReader(); |
| r.onload = () => ingest(String(r.result), f.name); |
| r.onerror = () => err(`${f.name} を読めませんでした`); |
| r.readAsText(f, 'utf-8'); |
| }); |
| } |
| |
| document.getElementById('lfile').onchange = e => take(e.target.files); |
| ov.addEventListener('dragover', e => { e.preventDefault(); ov.classList.add('over'); }); |
| ov.addEventListener('dragleave', () => ov.classList.remove('over')); |
| ov.addEventListener('drop', e => { |
| e.preventDefault(); ov.classList.remove('over'); take(e.dataTransfer.files); |
| }); |
| mark(); |
| })(); |
| </script> |
| </body> |
| </html> |
|
|