egatari 0.6.17 → 0.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -1
- package/README.ja.md +14 -13
- package/README.md +2 -1
- package/components.lock.json +119 -45
- package/components2.lock.json +639 -1
- package/kit/check-messages.mjs +5 -0
- package/kit/check.mjs +6 -0
- package/kit/core/bookmark.mjs +28 -0
- package/kit/core/build-core.mjs +4 -2
- package/kit/core/check-parity.mjs +4 -0
- package/kit/core/check-rules.mjs +3 -2
- package/kit/core/check-source.mjs +6 -0
- package/kit/core/llm-pack-assemble.mjs +37 -66
- package/kit/core/llm-pack-data.mjs +839 -45
- package/kit/core/llm-pack.mjs +6 -4
- package/kit/core/llm-request.mjs +2 -2
- package/kit/core/new-deck-pack.mjs +38 -44
- package/kit/core/new-deck.mjs +82 -74
- package/kit/core/part-labels.mjs +2 -0
- package/kit/core/purposes.mjs +9 -3
- package/kit/core/sample-info.mjs +17 -3
- package/kit/core/slide-ops.mjs +22 -0
- package/kit/core/tate-mix.mjs +25 -0
- package/kit/core/theme-derive.mjs +56 -17
- package/kit/core/theme-shapes.mjs +15 -1
- package/kit/deck.js +53 -2
- package/kit/instruction-version.json +1 -1
- package/kit/llm-pack.mjs +5 -7
- package/kit/player.json +1 -1
- package/kit/player.lock.json +2 -2
- package/kit/presenter.js +36 -4
- package/kit/tools.js +54 -2
- package/package.json +1 -1
- package/vendor/components/CATALOG.md +24 -22
- package/vendor/components/sha256/038f9d32c89c8212ab25d6e3180023b6fe024a53053ab2ef1443ecab92e3ce43/index.js +205 -0
- package/vendor/components/sha256/07479cb6e5194a01b2a8a9029f3646060f0dbb673f2737d2132738af2eb379e5/README.en.md +44 -0
- package/vendor/components/sha256/07479cb6e5194a01b2a8a9029f3646060f0dbb673f2737d2132738af2eb379e5/index.js +306 -0
- package/vendor/components/sha256/077678d8b14a8065c4f74a9fe1669728c68a0f763bfdca18b304f5c3d863632a/README.en.md +54 -0
- package/vendor/components/sha256/077678d8b14a8065c4f74a9fe1669728c68a0f763bfdca18b304f5c3d863632a/index.js +470 -0
- package/vendor/components/sha256/0840322bf51eeb3c8188ce21ad70af17c13974ac0151065a39c430a3813ee07f/README.en.md +23 -0
- package/vendor/components/sha256/0840322bf51eeb3c8188ce21ad70af17c13974ac0151065a39c430a3813ee07f/index.js +289 -0
- package/vendor/components/sha256/0c25d49d23529879054737aa181951519fe585c4728fd505e0e3c8869953eac7/index.js +934 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/LICENSE-strokesvg.txt +115 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/OFL.txt +93 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/index.js +755 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/strokes-CHANGES.md +20 -0
- package/vendor/components/sha256/0e9e92df8d70eb6d9fcf10916c361769c7cd37baacece8d7eb96bbbc7fbcf942/README.en.md +25 -0
- package/vendor/components/sha256/0e9e92df8d70eb6d9fcf10916c361769c7cd37baacece8d7eb96bbbc7fbcf942/index.js +317 -0
- package/vendor/components/sha256/12ef6a11055407f5cddc40154a49cf2737b0786429fe0fcc94ee1d8245597b40/index.js +244 -0
- package/vendor/components/sha256/13c9ce43396b2f5693ce430a924dae4ddcd1e8a4ad0c9b9e724ff52acb874d0c/index.js +354 -0
- package/vendor/components/sha256/1a0f21def0efbbdef64461cafa40f3883bc7f7122adf79d01f09ca4a245e5dcb/index.js +465 -0
- package/vendor/components/sha256/208175d05b37d326855fad792b10137f785e5d05d91a2cde119d0dbd69d2ed98/index.js +100 -0
- package/vendor/components/sha256/218d88998b70f0732c18d91ccc238b2d63f6b0aaa483883b83685a25468f214a/README.en.md +34 -0
- package/vendor/components/sha256/218d88998b70f0732c18d91ccc238b2d63f6b0aaa483883b83685a25468f214a/index.js +246 -0
- package/vendor/components/sha256/2579bcb46b6525902763cd15a2e08fc5ebd9a2695cdaba63438e5b47907a47e0/README.en.md +24 -0
- package/vendor/components/sha256/2579bcb46b6525902763cd15a2e08fc5ebd9a2695cdaba63438e5b47907a47e0/index.js +554 -0
- package/vendor/components/sha256/2787e410e533e4ad48b0a3c2b652579cf5797f9c51fcf32f5478212a7acef95f/README.en.md +28 -0
- package/vendor/components/sha256/2787e410e533e4ad48b0a3c2b652579cf5797f9c51fcf32f5478212a7acef95f/index.js +214 -0
- package/vendor/components/sha256/2a011338d0fc3c77e102eaa57cde2b6e3726bc279f133174b0b8c9a654263cbc/index.js +824 -0
- package/vendor/components/sha256/2fce5ab0c31dca0aa1586a2c69237870a9cce46c0bc6ab7392c27968d016bd15/index.js +75 -0
- package/vendor/components/sha256/3417efe4722f569690563d9abcea8b144db2f8fa97b83cf0118fb3a34cd4cb2b/README.en.md +23 -0
- package/vendor/components/sha256/3417efe4722f569690563d9abcea8b144db2f8fa97b83cf0118fb3a34cd4cb2b/index.js +150 -0
- package/vendor/components/sha256/3a7962fa87c670447eed54aca08604ef03cd26bf2c334b4667190060e6a17965/README.en.md +18 -0
- package/vendor/components/sha256/3a7962fa87c670447eed54aca08604ef03cd26bf2c334b4667190060e6a17965/index.js +279 -0
- package/vendor/components/sha256/42450c6b36404d66641568f6d6f55e42a4a036d4d9cdf308f709464913b32fe9/README.en.md +42 -0
- package/vendor/components/sha256/42450c6b36404d66641568f6d6f55e42a4a036d4d9cdf308f709464913b32fe9/index.js +272 -0
- package/vendor/components/sha256/45cea6c1abfb3467e3e3dbe7fec58647716999a0c39cf7163531ae389f898d29/README.en.md +30 -0
- package/vendor/components/sha256/45cea6c1abfb3467e3e3dbe7fec58647716999a0c39cf7163531ae389f898d29/index.js +305 -0
- package/vendor/components/sha256/4c8cae404d551a1bb021e8345af5aa45d4f412f13497aa84b142f11a29a365dc/index.js +124 -0
- package/vendor/components/sha256/5521b5b40a43c48c5ae5a50ce87afb3d47bb23b133b16841086d8322364cdd5e/README.en.md +23 -0
- package/vendor/components/sha256/5521b5b40a43c48c5ae5a50ce87afb3d47bb23b133b16841086d8322364cdd5e/index.js +120 -0
- package/vendor/components/sha256/5fe30fd7a84b937a1325d8af2b8e437211efae82ab46d5a433bc4ef07bcaef11/README.en.md +18 -0
- package/vendor/components/sha256/5fe30fd7a84b937a1325d8af2b8e437211efae82ab46d5a433bc4ef07bcaef11/index.js +243 -0
- package/vendor/components/sha256/631f4c4b90c9a41c0f26c9600d5cf2d6447251d04a20f08f48a85aeb4cb0f6fb/index.js +107 -0
- package/vendor/components/sha256/6749f44ed6476a05d4e3026373c188a0e1a19b77a5836579813f62a1477ac35e/README.en.md +31 -0
- package/vendor/components/sha256/6749f44ed6476a05d4e3026373c188a0e1a19b77a5836579813f62a1477ac35e/index.js +271 -0
- package/vendor/components/sha256/701f1deef79fe05eeb18e1119e5a5c4d60a9dfe265e93a4dff79223bc598dbf0/index.js +521 -0
- package/vendor/components/sha256/711cf58fa946c9c0abc953b2f695d02bf5a5a7f73851b09f834e894baf88be38/index.js +122 -0
- package/vendor/components/sha256/728576c57a5ccb39531ad4cb8074fce95480f01603a77dbf57dca0e52199f7c4/index.js +37 -0
- package/vendor/components/sha256/73edc74a2dc4a63b717106c1c1572967b6d306270574e9635d032de16c8ada72/README.en.md +18 -0
- package/vendor/components/sha256/73edc74a2dc4a63b717106c1c1572967b6d306270574e9635d032de16c8ada72/index.js +382 -0
- package/vendor/components/sha256/76bdb63c3e5e14fbb4816bc9f235490817bb5d7be990a826f5077aa08226702c/index.js +162 -0
- package/vendor/components/sha256/7a5adf73fc053b9f2ca56643b5683b42589edb0f63c845ecb7ae7e6b87eccc30/README.en.md +51 -0
- package/vendor/components/sha256/7a5adf73fc053b9f2ca56643b5683b42589edb0f63c845ecb7ae7e6b87eccc30/index.js +230 -0
- package/vendor/components/sha256/7ab483e7fb05f646b062340d70f2749d070b4f64fed7acc0b8a7754d9ef9fded/README.en.md +26 -0
- package/vendor/components/sha256/7ab483e7fb05f646b062340d70f2749d070b4f64fed7acc0b8a7754d9ef9fded/index.js +322 -0
- package/vendor/components/sha256/7af6e5e6dffb618b39f27d883c691ca128f3af06040ee505e75c79c678bfdcfe/README.en.md +18 -0
- package/vendor/components/sha256/7af6e5e6dffb618b39f27d883c691ca128f3af06040ee505e75c79c678bfdcfe/index.js +103 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/ABCJS-ADAPTER.md +101 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/LICENSE-abcjs.txt +21 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/chords.json +89 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/doremi.json +261 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/extras.json +192 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/kaeru.json +521 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/rhythm.json +248 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/ties.json +360 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/index.js +508 -0
- package/vendor/components/sha256/832556ea9c82c292a3ba9e1b332f2405c5c2f969ff8157c91ed80226ec8ac62e/README.en.md +22 -0
- package/vendor/components/sha256/832556ea9c82c292a3ba9e1b332f2405c5c2f969ff8157c91ed80226ec8ac62e/index.js +145 -0
- package/vendor/components/sha256/8a4b6dce84d7a69aaafeb75c1c572fe7904fd49e3169c1c85851ccaeae2a7b11/index.js +41 -0
- package/vendor/components/sha256/8bb511cb90902bc7721e59ca08685b26adbf33539e36a4a45172485d6d3d455b/index.js +250 -0
- package/vendor/components/sha256/8db9c936c36b79b9e3b2be37639487b15502a6937f411ed714621a8091437e9b/README.en.md +21 -0
- package/vendor/components/sha256/8db9c936c36b79b9e3b2be37639487b15502a6937f411ed714621a8091437e9b/index.js +108 -0
- package/vendor/components/sha256/8e5e90c90cb6b06a790b0420e43d79d7da663f1cc19ff06d381ec849b4a8fc57/index.js +312 -0
- package/vendor/components/sha256/a1c2b2fa06e4229815ab82a9202c266ec887a67632a175854c7d136a92389ca3/index.js +69 -0
- package/vendor/components/sha256/a7c4282db79d46557bdbaf84ca519606dc46f3c23455db4a26f663453f85813c/README.en.md +38 -0
- package/vendor/components/sha256/a7c4282db79d46557bdbaf84ca519606dc46f3c23455db4a26f663453f85813c/index.js +414 -0
- package/vendor/components/sha256/aac2f8a23cb1b606e9635061cb288f006588c80a7258540cae846ae677f55ff5/index.js +238 -0
- package/vendor/components/sha256/ace7e82e866b577ad42bbc4b64e11bae18982e3236aadceb6c2e6c20f41234cf/README.en.md +45 -0
- package/vendor/components/sha256/ace7e82e866b577ad42bbc4b64e11bae18982e3236aadceb6c2e6c20f41234cf/index.js +211 -0
- package/vendor/components/sha256/aeb0b9b5991a5c1df7191c397780a699f2c7310123ca210a58486c0a23e44006/index.js +342 -0
- package/vendor/components/sha256/af3355efd6739a56c6cb53990100f41703361c475c123b9b2762421c68509036/index.js +315 -0
- package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/LICENSE-tabler.txt +21 -0
- package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/index.js +4868 -0
- package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/names.txt +4808 -0
- package/vendor/components/sha256/b348ea64cc19632df96a91dfd7c435e4a62095aede72b504d03537be70d6d930/index.js +606 -0
- package/vendor/components/sha256/b53bfa03d1e9feb3cb492f440bf9d100c96eb4fe6b76c42b512a56e9158829cd/index.js +351 -0
- package/vendor/components/sha256/b9443e1cdac4850b1ead9f8449a3061678bbb77fbc53eb9b23d01e1cf50750c3/index.js +619 -0
- package/vendor/components/sha256/bc1e2a02c959ac203819d03c0d25b3776a6a9a4290aae860b26cbbe3d29e876d/index.js +268 -0
- package/vendor/components/sha256/beeb180c0e2250bb04490b092c0a235cae47fca2ed1de2d9ef078a5c14b1a963/index.js +429 -0
- package/vendor/components/sha256/c5fd7713e55e96d75824a21146c0251896d7d9dd028b4d169191433135a97981/index.js +200 -0
- package/vendor/components/sha256/c61987e9994940fc668ed8a010e575ee52027b54ed9c5dd1cc9fdf67649092b3/index.js +404 -0
- package/vendor/components/sha256/ccc8bfe174b6903ec254dfef4f5d1c614271ea39963649add7fcb8d83c081eba/index.js +278 -0
- package/vendor/components/sha256/da267bc65fde0a9279127d5a82f6c253ad74984da6fb11391f00604ad9ffe80d/index.js +93 -0
- package/vendor/components/sha256/dea36cd7010991a572ef394ee32b5e17564aeff09d3ec4ed3820d7e43c9a58be/index.js +484 -0
- package/vendor/components/sha256/df3fdb2aed487d4f6e60a8abab1fe0d1e9fa2a4193fd162edf3c384ea8db5a1b/index.js +180 -0
- package/vendor/components/sha256/e27d517f39eb554c7a9ecea7b59fcf1d347c4deeb3fe2250797aa233bfb7e2b0/index.js +747 -0
- package/vendor/components/sha256/e31f6b058503f7980597c89b3e964c512ee08de7cba3fa99904d499abbb5d0e8/README.en.md +20 -0
- package/vendor/components/sha256/e31f6b058503f7980597c89b3e964c512ee08de7cba3fa99904d499abbb5d0e8/index.js +310 -0
- package/vendor/components/sha256/f4100076a37dbde6046d85969d651feffc925bd984ffe9f333ea9bd2b6816918/README.en.md +30 -0
- package/vendor/components/sha256/f4100076a37dbde6046d85969d651feffc925bd984ffe9f333ea9bd2b6816918/index.js +192 -0
- package/vendor/components/sha256/fa6442fa3c5082cf161c29308fe0ecbd85fa33e8e4bad08c12e7961b46b1365b/README.en.md +24 -0
- package/vendor/components/sha256/fa6442fa3c5082cf161c29308fe0ecbd85fa33e8e4bad08c12e7961b46b1365b/index.js +214 -0
- package/vendor/components-2/manifest-2.16.0.json +2919 -0
- package/vendor/components-2/manifest-2.17.0.json +2852 -0
- package/vendor/components-2/manifest-2.18.0.json +3182 -0
- package/vendor/egatari-spec/SOURCE.json +2 -2
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
// m-boxplot:箱ひげ図。値を小さい順に並べ、中央値・四分位数を求める手順のとおりに組み立てる
|
|
2
|
+
// <m-boxplot xl="記録(回)" style="height:440px">
|
|
3
|
+
// <script type="application/json">{"groups": [{"label": "1 組", "values": [3, 5, 6, 7, 8, 9, 10, 12, 13, 15, 18]}]}
|
|
4
|
+
// (JSON の終わり)</m-boxplot>
|
|
5
|
+
// 段階:build="steps"(既定)は 値の点 → 中央値 → 第1・第3四分位数 → 箱 → ひげ(群が 2 つ以上なら全部の群をそろえて)。
|
|
6
|
+
// build="whole" は群 1 つ = 1 段階で箱ひげ図をまとめて出す
|
|
7
|
+
// 四分位数の求め方(method):jp(既定。中学・数学Ⅰの教科書:前半・後半のそれぞれの中央値。奇数個なら中央値を前半にも後半にも入れない)・
|
|
8
|
+
// inc(表計算の QUARTILE.INC)・exc(QUARTILE.EXC)
|
|
9
|
+
// 軸の属性は m-curves と同じ名前:値の軸は orient="h" なら xr xt xf xl、orient="v" なら yr yt yf yl
|
|
10
|
+
// 見せ方(docs/conventions.md §1):fade(フェードして現れる、既定)・draw(箱の枠と線を引く。ひげは箱から外へ伸びる)
|
|
11
|
+
// 2.0(Egatari spec §6.1 の部品):base の MotionElement・el・mtext・prog・variant・previewOpacity、plot の Plot、
|
|
12
|
+
// 四分位数は boxplot-core(1.x の static stats)を使い、プレイヤーのグローバルは使わない。書き方やデータの知らせ
|
|
13
|
+
// (1.x の console.warn)は mount で ctx.report に。言語は ctx.lang(mount で読む)
|
|
14
|
+
egatariParts.define({name: "boxplot", version: "2.0.7", requires: {base: "1.3.0", "boxplot-core": "1.0.0", plot: "1.1.3"}, element: "m-boxplot"}, function(deps){
|
|
15
|
+
"use strict";
|
|
16
|
+
var base = deps.base, plot = deps.plot;
|
|
17
|
+
// ---- 四分位数(boxplot-core。tools/test-stats.mjs が試す) ----
|
|
18
|
+
var STATS = deps["boxplot-core"];
|
|
19
|
+
// 印の名前の置き場所(m-hist と同じ決まり):線の頭の右(start)か左(end)。名前がほかの線にかからない側を選び、
|
|
20
|
+
// 名前がかかる線の印はそれより下の行に置く。fl:[{x, w}]。各要素に a・L・R・row を書き、行の数を返す。
|
|
21
|
+
// 置き場所がないときは report(code, message)(ctx.report)で知らせる
|
|
22
|
+
function FLAGS(fl, W, report){
|
|
23
|
+
var span = function(f, a){ var l = a === "start" ? f.x + 8 : f.x - 8 - f.w; return [l, l + f.w]; };
|
|
24
|
+
var covers = function(f, g){ return g !== f && g.x > f.L - 6 && g.x < f.R + 6; };
|
|
25
|
+
fl.forEach(function(f){
|
|
26
|
+
var best = null;
|
|
27
|
+
["start", "end"].forEach(function(a){
|
|
28
|
+
var sp = span(f, a);
|
|
29
|
+
if (sp[0] < 2 || sp[1] > W - 2) return;
|
|
30
|
+
var n = fl.filter(function(g){ return g !== f && g.x > sp[0] - 6 && g.x < sp[1] + 6; }).length;
|
|
31
|
+
if (!best || n < best.n) best = {a: a, n: n};
|
|
32
|
+
});
|
|
33
|
+
f.a = best ? best.a : (f.x < W / 2 ? "start" : "end");
|
|
34
|
+
var sp2 = span(f, f.a); f.L = sp2[0]; f.R = sp2[1];
|
|
35
|
+
});
|
|
36
|
+
var lv = fl.map(function(){ return 0; });
|
|
37
|
+
for (var it = 0; it < fl.length; it++) fl.forEach(function(f, i){ fl.forEach(function(g, j){ if (covers(f, g)) lv[i] = Math.max(lv[i], Math.min(fl.length, lv[j] + 1)); }); });
|
|
38
|
+
var order = fl.map(function(f, i){ return i; }).sort(function(a, b){ return lv[a] - lv[b] || a - b; });
|
|
39
|
+
var placed = [], rows = 0;
|
|
40
|
+
order.forEach(function(i){
|
|
41
|
+
var f = fl[i], row = 0;
|
|
42
|
+
placed.forEach(function(o){ if (covers(f, o)) row = Math.max(row, o.row + 1); });
|
|
43
|
+
for (;; row++){
|
|
44
|
+
var clash = placed.some(function(o){
|
|
45
|
+
return (o.row === row && f.L < o.R + 14 && o.L < f.R + 14) || (o.row < row && covers(o, f)) || (o.row > row && covers(f, o));
|
|
46
|
+
});
|
|
47
|
+
if (!clash) break;
|
|
48
|
+
if (row > 12){ report("layout", '[boxplot] no free place for the name "' + (f.text || "") + '" (it may overlap)'); break; }
|
|
49
|
+
}
|
|
50
|
+
f.row = row; placed.push(f); rows = Math.max(rows, row + 1);
|
|
51
|
+
});
|
|
52
|
+
return rows;
|
|
53
|
+
}
|
|
54
|
+
var ON = function(v){ return /^(on|true|yes)$/i.test(String(v || "").trim()); };
|
|
55
|
+
return class extends base.MotionElement {
|
|
56
|
+
static partName = "boxplot"; // the registry name (define meta name), for report prefixes
|
|
57
|
+
static variants = ["fade", "draw"];
|
|
58
|
+
// データの形(kit/shape.js)
|
|
59
|
+
static shape = {
|
|
60
|
+
schema: {type: "object", additionalProperties: false, required: ["groups"], properties: {
|
|
61
|
+
groups: {type: "array", minItems: 1, items: {type: "object", additionalProperties: false, anyOf: [{required: ["values"]}, {required: ["five"]}], properties: {
|
|
62
|
+
label: {type: "string"}, tone: {type: "string"}, values: {type: "array", minItems: 1, items: {type: "number"}},
|
|
63
|
+
five: {type: "array", minItems: 5, maxItems: 5, items: {type: "number"}}}}}}},
|
|
64
|
+
example: {groups: [{label: "1 組", values: [3, 5, 6, 7, 8, 9, 10, 12, 13, 15, 18]}]},
|
|
65
|
+
note: {en: "groups [{label, tone, values}] or [{label, five: [min, q1, median, q3, max]}]; axes go in attributes (xr=\"[0,20]\" xl=\"…\")",
|
|
66
|
+
ja: "groups [{label, tone, values}] か [{label, five: [最小値, 第1四分位数, 中央値, 第3四分位数, 最大値]}]。軸は属性に書く(xr=\"[0,20]\" xl=\"…\")"},
|
|
67
|
+
};
|
|
68
|
+
static props = {
|
|
69
|
+
orient: {type: "string", default: "h", doc: "h(横向きの箱、値の軸が横)か v(縦向き)"},
|
|
70
|
+
xr: {type: "json", default: null, doc: "横軸の範囲"}, yr: {type: "json", default: null, doc: "縦軸の範囲"},
|
|
71
|
+
xt: {type: "json", default: null, doc: "横軸の目盛り"}, yt: {type: "json", default: null, doc: "縦軸の目盛り"},
|
|
72
|
+
xf: {type: "number", default: undefined, doc: "横軸の目盛りの小数桁"}, yf: {type: "number", default: undefined, doc: "縦軸の目盛りの小数桁"},
|
|
73
|
+
xl: {type: "string", default: ""}, yl: {type: "string", default: ""},
|
|
74
|
+
method: {type: "string", default: "jp", doc: "四分位数の求め方:jp(教科書)・inc・exc(表計算の QUARTILE.INC / EXC)"},
|
|
75
|
+
outliers: {type: "string", default: "off", doc: "on:1.5×四分位範囲の外を外れ値(点)にし、ひげはその内側まで"},
|
|
76
|
+
dots: {type: "string", default: "off", doc: "on:値の点を箱の下に並べる(build=\"steps\" ではいつも出る)"},
|
|
77
|
+
build: {type: "string", default: "steps", doc: "steps(求める手順のとおり)か whole(群 1 つ = 1 ステップ)"},
|
|
78
|
+
unit: {type: "string", default: "", doc: "表示する値の後ろに付ける単位"},
|
|
79
|
+
digits: {type: "number", default: undefined, doc: "表示する値の小数桁(書かなければ値の桁+1 まで)"},
|
|
80
|
+
dur: {type: "number", default: 0.8, doc: "1 ステップの動きの秒数"},
|
|
81
|
+
};
|
|
82
|
+
get steps(){
|
|
83
|
+
if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
|
|
84
|
+
return this.plan().steps;
|
|
85
|
+
}
|
|
86
|
+
// 群・四分位数・段階(mount の前にも steps が要るので、データと属性だけから決める)
|
|
87
|
+
plan(){
|
|
88
|
+
if (this._plan) return this._plan;
|
|
89
|
+
var d = this.data || {}, P = this.props, notes = [], S = STATS;
|
|
90
|
+
var method = P.method || "jp", out = ON(P.outliers), build = P.build;
|
|
91
|
+
// 四分位数の求め方は boxplot-core が知っている 3 つだけ(ほかの名前は core が誤りにして図全体が札になるので、ここで既定に戻す)
|
|
92
|
+
if (["jp", "inc", "exc"].indexOf(method) < 0){ notes.push(["attr", '[boxplot] method="' + method + '" is not jp, inc or exc (jp is used)']); method = "jp"; }
|
|
93
|
+
if (["steps", "whole"].indexOf(build) < 0){ notes.push(["attr", '[boxplot] build="' + build + '" is not steps or whole (steps is used)']); build = "steps"; }
|
|
94
|
+
var groups = (d.groups || []).map(function(g, i){
|
|
95
|
+
var o = {i: i, label: g.label || "", tone: g.tone};
|
|
96
|
+
if (Array.isArray(g.values)){
|
|
97
|
+
o.values = g.values.map(Number);
|
|
98
|
+
o.q = S.quartiles(o.values, method);
|
|
99
|
+
} else {
|
|
100
|
+
var f = g.five.map(Number);
|
|
101
|
+
for (var j = 1; j < 5; j++) if (f[j] < f[j - 1]) throw new Error("m-boxplot: five must be in order [min, q1, median, q3, max] (" + f.join(", ") + ")");
|
|
102
|
+
o.q = {min: f[0], q1: f[1], med: f[2], q3: f[3], max: f[4], n: 0};
|
|
103
|
+
}
|
|
104
|
+
o.f = out && o.values ? S.fences(o.q, o.values) : {wlo: o.q.min, whi: o.q.max, out: []};
|
|
105
|
+
return o;
|
|
106
|
+
});
|
|
107
|
+
if (out && groups.some(function(g){ return !g.values; })) notes.push(["data", '[boxplot] outliers="on" needs the values of a group (a group written with "five" has none)']);
|
|
108
|
+
var dec = function(v){ var m = String(v).match(/\.(\d+)$/); return m ? m[1].length : 0; };
|
|
109
|
+
var base = Math.max.apply(null, [0].concat.apply([0], groups.map(function(g){ return g.values ? g.values.map(dec) : [g.q.min, g.q.q1, g.q.med, g.q.q3, g.q.max].map(dec); })));
|
|
110
|
+
var hasVals = groups.some(function(g){ return g.values; }), st = {};
|
|
111
|
+
if (build === "steps"){
|
|
112
|
+
var k = 0;
|
|
113
|
+
if (hasVals) st.dots = ++k;
|
|
114
|
+
st.med = ++k; st.q = ++k; st.box = ++k; st.whisk = ++k;
|
|
115
|
+
groups.forEach(function(g){ g.step = 1; });
|
|
116
|
+
} else groups.forEach(function(g, i){ g.step = i + 1; });
|
|
117
|
+
return (this._plan = {groups: groups, method: method, out: out, build: build, st: st, base: base, notes: notes,
|
|
118
|
+
steps: build === "steps" ? st.whisk : Math.max(1, groups.length)});
|
|
119
|
+
}
|
|
120
|
+
// 数の書き方:digits があればその桁。なければ値そのもの(四分位数は 6.25 のように割り切れるところまで。丸めない)、ただし値の桁より少なくはしない
|
|
121
|
+
fmt(v){
|
|
122
|
+
var P = this.props, pl = this.plan(), s;
|
|
123
|
+
if (P.digits !== undefined && isFinite(P.digits)) s = Math.abs(v).toFixed(P.digits);
|
|
124
|
+
else {
|
|
125
|
+
var a = Math.abs(v), dd = pl.base;
|
|
126
|
+
while (dd < 10 && Math.abs(a * Math.pow(10, dd) - Math.round(a * Math.pow(10, dd))) > 1e-6 * Math.max(1, a * Math.pow(10, dd - 6))) dd++;
|
|
127
|
+
s = a.toFixed(dd);
|
|
128
|
+
}
|
|
129
|
+
var p = s.split("."), i = p[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
130
|
+
return (v < 0 && +s !== 0 ? "−" : "") + i + (p[1] ? "." + p[1] : "");
|
|
131
|
+
}
|
|
132
|
+
mount(ctx){
|
|
133
|
+
var P = this.props, D = base, self = this, pl = this.plan(), S = STATS;
|
|
134
|
+
this.variant = base.variant(this);
|
|
135
|
+
// 書き方とデータの知らせ(plan が集めた [code, message])
|
|
136
|
+
// ctx.report with each code as a literal (lint checks them): plan's notes (attr, data) and the label placer's (layout)
|
|
137
|
+
var report = function(code, msg){ if (code === "attr") ctx.report("attr", msg); else if (code === "layout") ctx.report("layout", msg); else ctx.report("data", msg); };
|
|
138
|
+
pl.notes.forEach(function(n){ report(n[0], n[1]); });
|
|
139
|
+
this.classList.add("plot"); this.style.display = "block";
|
|
140
|
+
var ja = this.ja = /^ja\b/i.test(ctx.lang), hor = P.orient !== "v";
|
|
141
|
+
if (P.orient !== "h" && P.orient !== "v") ctx.report("attr", '[boxplot] orient="' + P.orient + '" is not h or v (h is used)');
|
|
142
|
+
// 字の幅(base の textWidth:テーマの字で測った幅。名前も値も mtext で書く)
|
|
143
|
+
var est = function(s, size){ return base.textWidth(s, size, {font: self, mark: true}); };
|
|
144
|
+
var toneOf = function(c){ return /^(term|emph|muted|accent2|fg|series-[1-4])$/.test(c || "") ? "var(--" + c + ")" : null; };
|
|
145
|
+
var W = this.clientWidth, H = this.clientHeight, G = pl.groups, steps = pl.build === "steps", st = pl.st;
|
|
146
|
+
var N = ja ? {min: "最小値", q1: "第1四分位数", med: "中央値", q3: "第3四分位数", max: "最大値", out: "外れ値"}
|
|
147
|
+
: {min: "Minimum", q1: "First quartile", med: "Median", q3: "Third quartile", max: "Maximum", out: "Outlier"};
|
|
148
|
+
G.forEach(function(g, i){ g.col = toneOf(g.tone) || (G.length > 1 && !g.tone ? "var(--series-" + (i % 4 + 1) + ")" : "var(--term)"); });
|
|
149
|
+
// 値の軸の範囲と目盛り(書かなければ全部の値から切りのよい数に)
|
|
150
|
+
var all = [].concat.apply([], G.map(function(g){ return g.values || [g.q.min, g.q.max]; }));
|
|
151
|
+
var lo = Math.min.apply(null, all), hi = Math.max.apply(null, all);
|
|
152
|
+
var vr = hor ? P.xr : P.yr, vt = hor ? P.xt : P.yt, vf = hor ? P.xf : P.yf, vl = hor ? P.xl : P.yl;
|
|
153
|
+
if (!vr){ var s0 = niceStep((hi - lo || 1) / 6); vr = [S.clean(Math.floor(lo / s0 + 1e-9) * s0), S.clean(Math.ceil(hi / s0 - 1e-9) * s0)]; if (vr[0] === vr[1]) vr = [vr[0] - s0, vr[1] + s0]; }
|
|
154
|
+
if (!vt){
|
|
155
|
+
// 目盛りの数が重ならない間隔まで広げる(横:字の幅+16 px、縦:34 px)
|
|
156
|
+
var labeled0 = G.some(function(g){ return g.label; }), gw0 = labeled0 ? Math.max.apply(null, G.map(function(g){ return est(g.label, 26); })) : 0;
|
|
157
|
+
var room = hor ? W - (labeled0 ? Math.min(W * 0.3, gw0 + 28) : 28) - 28 : H - 120;
|
|
158
|
+
var s1 = niceStep((vr[1] - vr[0]) / (hor ? 8 : 6)), fits = function(sp){
|
|
159
|
+
var n2 = Math.floor((vr[1] - vr[0]) / sp + 1e-9) + 1, w2 = hor ? Math.max(est(vr[0].toFixed(dec(sp)), 24), est(vr[1].toFixed(dec(sp)), 24)) + 16 : 34;
|
|
160
|
+
return n2 * w2 <= room; };
|
|
161
|
+
for (var guard = 0; guard < 20 && !fits(s1); guard++) s1 = niceStep(s1 * 1.01);
|
|
162
|
+
vt = []; for (var u = Math.ceil(vr[0] / s1 - 1e-9) * s1; u <= vr[1] + s1 * 1e-6; u += s1) vt.push(S.clean(u));
|
|
163
|
+
}
|
|
164
|
+
if (vf === undefined) vf = Math.max.apply(null, vt.map(function(x){ var m = String(x).match(/\.(\d+)$/); return m ? m[1].length : 0; }).concat([0]));
|
|
165
|
+
// 群の点:小さい順に置き、前の点と重なる(中心が 2R+1 px より近い)ときは 1 段ずらして積む
|
|
166
|
+
var R = 9;
|
|
167
|
+
var stackDots = function(Vpx){
|
|
168
|
+
G.forEach(function(g){
|
|
169
|
+
g.stack = 0;
|
|
170
|
+
if (!g.values) return;
|
|
171
|
+
var placed = [];
|
|
172
|
+
// 外れ値は点の列に置かず、箱の線の上の白抜きの丸だけで示す(同じ値が 2 つの印に見えないように)
|
|
173
|
+
g.dots = S.sorted(g.values).filter(function(v){ return g.f.out.indexOf(v) < 0; }).map(function(v){
|
|
174
|
+
var x = Vpx(v), lv = 0;
|
|
175
|
+
while (placed.some(function(o){ return o.lv === lv && Math.abs(o.x - x) < 2 * R + 1; })) lv++;
|
|
176
|
+
placed.push({x: x, lv: lv}); g.stack = Math.max(g.stack, lv + 1);
|
|
177
|
+
return {v: v, lv: lv};
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
var showDots = function(g){ return g.values && (steps || ON(P.dots)); };
|
|
182
|
+
var labeled = G.some(function(g){ return g.label; });
|
|
183
|
+
// ---- 並べた値の行(build="steps"、群が 1 つ、横向きで、行に収まるとき) ----
|
|
184
|
+
var g0 = G[0], row = null;
|
|
185
|
+
if (steps && hor && G.length === 1 && g0.values){
|
|
186
|
+
var n = g0.q.n, tw = Math.max.apply(null, g0.q.sorted.map(function(v){ return est(self.fmt(v), 26); }));
|
|
187
|
+
var cw = Math.min(70, (W - 40) / n);
|
|
188
|
+
if (cw >= tw + 10){
|
|
189
|
+
row = {n: n, cw: cw, x0: (W - n * cw) / 2, top: 4, h: 46};
|
|
190
|
+
row.cx = function(i){ return row.x0 + (i + 0.5) * row.cw; };
|
|
191
|
+
// 行の下の名前(中央値・四分位数・最小値・最大値):中心をその位置に、重なれば下の行へ
|
|
192
|
+
var q = g0.q, names = [{k: "med", i: q.imed, st: st.med}, {k: "q1", i: q.iq1, st: st.q}, {k: "q3", i: q.iq3, st: st.q}, {k: "min", i: 0, st: st.whisk}, {k: "max", i: n - 1, st: st.whisk}];
|
|
193
|
+
var put = [], rr = 0;
|
|
194
|
+
names.forEach(function(o){
|
|
195
|
+
o.text = N[o.k]; o.w = est(o.text, 24); o.x = Math.max(o.w / 2 + 2, Math.min(W - o.w / 2 - 2, row.cx(o.i)));
|
|
196
|
+
for (var r = 0; ; r++){ if (!put.some(function(p){ return p.r === r && Math.abs(p.x - o.x) < (p.w + o.w) / 2 + 12; })) { o.r = r; break; } }
|
|
197
|
+
put.push(o); rr = Math.max(rr, o.r + 1);
|
|
198
|
+
});
|
|
199
|
+
row.names = names; row.nrows = rr;
|
|
200
|
+
row.brk = q.halves ? 16 : 0;
|
|
201
|
+
row.bottom = row.top + row.h + row.brk + rr * 30 + 14;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
var top0 = row ? row.bottom : 8;
|
|
205
|
+
// ---- 書き出す値の名前(build="steps") ----
|
|
206
|
+
var flagsOf = function(g, X){
|
|
207
|
+
if (!steps) return [];
|
|
208
|
+
var q = g.q, f = g.f, o = [];
|
|
209
|
+
var name = function(k, v){ return N[k] + " " + self.fmt(v) + self.props.unit; };
|
|
210
|
+
o.push({k: "med", v: q.med, text: name("med", q.med), st: st.med});
|
|
211
|
+
o.push({k: "q1", v: q.q1, text: name("q1", q.q1), st: st.q});
|
|
212
|
+
o.push({k: "q3", v: q.q3, text: name("q3", q.q3), st: st.q});
|
|
213
|
+
o.push({k: "min", v: f.wlo, text: f.wlo === q.min ? name("min", q.min) : self.fmt(f.wlo) + self.props.unit, st: st.whisk});
|
|
214
|
+
o.push({k: "max", v: f.whi, text: f.whi === q.max ? name("max", q.max) : self.fmt(f.whi) + self.props.unit, st: st.whisk});
|
|
215
|
+
o.forEach(function(x){ x.w = est(x.text, 24); x.x = X ? X(x.v) : 0; });
|
|
216
|
+
return o;
|
|
217
|
+
};
|
|
218
|
+
// 外れ値の名前(3 つまで):どの build でも、白抜きの丸のすぐ横に
|
|
219
|
+
G.forEach(function(g){
|
|
220
|
+
g.outLab = g.f.out.length && g.f.out.length <= 3 ? g.f.out.map(function(v){ var t = N.out + " " + self.fmt(v) + self.props.unit; return {v: v, text: t, w: est(t, 24)}; }) : [];
|
|
221
|
+
});
|
|
222
|
+
var gLabW = labeled ? Math.max.apply(null, G.map(function(g){ return est(g.label, 26); })) : 0;
|
|
223
|
+
var m, p, X, Y;
|
|
224
|
+
var bands = [];
|
|
225
|
+
if (hor){
|
|
226
|
+
m = {l: labeled ? Math.min(W * 0.3, gLabW + 28) : 28, r: 28, b: P.xl ? 70 : 44, t: top0};
|
|
227
|
+
X = function(v){ return m.l + (v - vr[0]) / (vr[1] - vr[0]) * (W - m.l - m.r); };
|
|
228
|
+
// 群ごとの帯:名前の行・箱・点の段
|
|
229
|
+
var boxH = 56, avail = H - m.b - top0 - 6;
|
|
230
|
+
var need = function(bh, r){ return G.reduce(function(a, g){ return a + g.rows * 34 + (g.rows ? 10 : 0) + bh + (showDots(g) ? 10 + g.stack * (2 * r + 3) : 0) + 14; }, 0); };
|
|
231
|
+
stackDots(X);
|
|
232
|
+
G.forEach(function(g){ g.flags = flagsOf(g, X); g.rows = g.flags.length ? FLAGS(g.flags, W, report) : 0; });
|
|
233
|
+
while (need(boxH, R) > avail && boxH > 30) boxH -= 2;
|
|
234
|
+
while (need(boxH, R) > avail && R > 5) R -= 0.5;
|
|
235
|
+
if (need(boxH, R) > avail) ctx.report("size", "[boxplot] the element (" + W + "×" + H + " px) is too small for the groups; give it more height");
|
|
236
|
+
var extra = Math.max(0, avail - need(boxH, R)), add = Math.min(extra / G.length, 24);
|
|
237
|
+
boxH = Math.min(boxH + add, 72);
|
|
238
|
+
// 余りは群の間に(30 px まで)、残りは上に。いちばん下の群は軸のすぐ上
|
|
239
|
+
var left = Math.max(0, avail - need(boxH, R)), gap = G.length > 1 ? Math.min(30, left / G.length) : 0;
|
|
240
|
+
var y = top0 + left - gap * (G.length - 1);
|
|
241
|
+
G.forEach(function(g, gi){
|
|
242
|
+
if (gi) y += gap;
|
|
243
|
+
y += g.rows * 34 + (g.rows ? 10 : 0);
|
|
244
|
+
g.c = y + boxH / 2; g.bh = boxH;
|
|
245
|
+
y += boxH;
|
|
246
|
+
if (showDots(g)){ g.dotBase = y + 10 + g.stack * (2 * R + 3) - R - 1; y += 10 + g.stack * (2 * R + 3); }
|
|
247
|
+
y += 14;
|
|
248
|
+
});
|
|
249
|
+
p = plot.Plot(this, {xr: vr, yr: [0, 1], xt: vt, xf: vf, xl: P.xl, noY: true, m: {l: m.l, r: m.r, t: top0, b: m.b}});
|
|
250
|
+
} else {
|
|
251
|
+
m = {l: 0, r: 20, t: 16, b: 44 + (labeled ? 34 : 0) + (P.xl ? 34 : 0)};
|
|
252
|
+
var yw = Math.max.apply(null, vt.map(function(v){ return est(v.toFixed(vf), 24); }).concat([0]));
|
|
253
|
+
m.l = Math.max(64, yw + 22 + (vl ? 40 : 0));
|
|
254
|
+
Y = function(v){ return H - m.b - (v - vr[0]) / (vr[1] - vr[0]) * (H - m.b - m.t); };
|
|
255
|
+
stackDots(Y);
|
|
256
|
+
p = plot.Plot(this, {xr: [0, 1], yr: vr, xt: [], yt: vt, yf: vf, xl: P.xl, yl: vl, m: m});
|
|
257
|
+
// 群ごとの幅:点の列・箱・名前
|
|
258
|
+
var slot = (W - m.l - m.r) / G.length;
|
|
259
|
+
G.forEach(function(g, i){
|
|
260
|
+
g.flags = flagsOf(g, null);
|
|
261
|
+
var lw = Math.max(g.flags.length ? Math.max.apply(null, g.flags.map(function(f){ return f.w; })) + 30 : 0,
|
|
262
|
+
g.outLab.length ? Math.max.apply(null, g.outLab.map(function(o){ return o.w; })) + 20 : 0);
|
|
263
|
+
var dw = showDots(g) ? g.stack * (2 * R + 3) + 10 : 0;
|
|
264
|
+
var bw = Math.max(36, Math.min(80, slot - lw - dw - 24));
|
|
265
|
+
if (lw + dw + bw + 12 > slot) ctx.report("size", "[boxplot] the groups are too narrow for the names; give the element more width");
|
|
266
|
+
var x = m.l + slot * i + (slot - (lw + dw + bw)) / 2;
|
|
267
|
+
g.mid = x + (dw + bw) / 2; // 群の名前は点の列と箱の真ん中の下
|
|
268
|
+
if (showDots(g)){ g.dotBase = x + g.stack * (2 * R + 3) - R + 2; x += dw; }
|
|
269
|
+
g.c = x + bw / 2; g.bh = bw; g.labX = x + bw + 14;
|
|
270
|
+
// 名前:値の高さに置き、重なれば上下にずらす(30 px あける)
|
|
271
|
+
var fl = g.flags.slice().sort(function(a, b){ return Y(b.v) - Y(a.v); });
|
|
272
|
+
fl.forEach(function(f){ f.y = Y(f.v) + 8; });
|
|
273
|
+
for (var it = 0; it < 30; it++){
|
|
274
|
+
var moved = false;
|
|
275
|
+
for (var j = 1; j < fl.length; j++){ if (fl[j - 1].y - fl[j].y < 30){ var d2 = (30 - (fl[j - 1].y - fl[j].y)) / 2; fl[j - 1].y += d2; fl[j].y -= d2; moved = true; } }
|
|
276
|
+
fl.forEach(function(f){ f.y = Math.max(m.t + 24, Math.min(H - m.b - 4, f.y)); });
|
|
277
|
+
if (!moved) break;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
// 読み上げ:最後の形(群ごとの 5 つの数)を 1 文で。図の svg は読ませない。作者の aria-label があればそれを使う(上書きしない)
|
|
282
|
+
p.svg.setAttribute("aria-hidden", "true");
|
|
283
|
+
if (this._aria === undefined) this._aria = this.getAttribute("aria-label");
|
|
284
|
+
this.setAttribute("role", "img");
|
|
285
|
+
if (!this._aria) this.setAttribute("aria-label", this.summary(ja, N));
|
|
286
|
+
var axl = p.svg.querySelectorAll("g.axes > line");
|
|
287
|
+
if (axl.length) axl[0].setAttribute("class", "axis");
|
|
288
|
+
if (!hor && axl.length > 1) axl[1].setAttribute("class", "axis");
|
|
289
|
+
var V = hor ? X : Y;
|
|
290
|
+
// 値の軸の位置 v と、群の向きの位置 c から画面の座標
|
|
291
|
+
var pt = function(v, c){ return hor ? [V(v), c] : [c, V(v)]; };
|
|
292
|
+
var gL = D.el("g", {}, p.svg), gB = D.el("g", {}, p.svg), gD = D.el("g", {}, p.svg), gT = D.el("g", {}, p.svg);
|
|
293
|
+
var items = this.items = [], draw = this.variant === "draw";
|
|
294
|
+
var add2 = function(el, o){ o.el = el; items.push(o); return el; };
|
|
295
|
+
var line = function(par, a, b, attrs){ return D.el("line", Object.assign({x1: a[0].toFixed(1), y1: a[1].toFixed(1), x2: b[0].toFixed(1), y2: b[1].toFixed(1)}, attrs), par); };
|
|
296
|
+
var dur = P.dur;
|
|
297
|
+
G.forEach(function(g){
|
|
298
|
+
var q = g.q, f = g.f, c = g.c, h = g.bh, a = function(k){ return steps ? st[k] : g.step; };
|
|
299
|
+
// 群の名前
|
|
300
|
+
if (g.label){
|
|
301
|
+
var tl = hor ? D.mtext(gT, m.l - 14, c + 9, g.label, {"text-anchor": "end", "font-size": 26, fill: G.length > 1 ? g.col : "var(--fg)", "font-weight": 700})
|
|
302
|
+
: D.mtext(gT, g.mid, H - m.b + 64, g.label, {"text-anchor": "middle", "font-size": 26, fill: G.length > 1 ? g.col : "var(--fg)", "font-weight": 700});
|
|
303
|
+
add2(tl, {st: steps ? 1 : g.step, at: 0, len: 0.4, kind: "fade"});
|
|
304
|
+
}
|
|
305
|
+
// 値の点:h は箱の下に右へ並べて積む、v は箱の左に積む
|
|
306
|
+
if (showDots(g)){
|
|
307
|
+
g.dots.forEach(function(o){
|
|
308
|
+
var cc = hor ? g.dotBase - o.lv * (2 * R + 3) : g.dotBase - o.lv * (2 * R + 3);
|
|
309
|
+
var xy = pt(o.v, cc);
|
|
310
|
+
add2(D.el("circle", {cx: xy[0].toFixed(1), cy: xy[1].toFixed(1), r: R.toFixed(1), fill: g.col, "fill-opacity": 0.85, opacity: 0}, gD),
|
|
311
|
+
{st: steps ? st.dots : g.step, at: 0, len: dur * 0.7, kind: "fade"});
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
var ends = [c - h / 2, c + h / 2], medCol = G.length > 1 || g.tone === "emph" ? "var(--fg)" : "var(--emph)";
|
|
315
|
+
// 中央値と四分位数の線(箱の前に出る。箱が出たら四分位数の線は箱の辺に替わる)
|
|
316
|
+
add2(line(gB, pt(q.med, ends[0]), pt(q.med, ends[1]), {stroke: medCol, "stroke-width": 4.5, "stroke-linecap": "round", opacity: 0}),
|
|
317
|
+
{st: a("med"), at: 0, len: dur * 0.6, kind: "line"});
|
|
318
|
+
if (steps){
|
|
319
|
+
[q.q1, q.q3].forEach(function(v){
|
|
320
|
+
add2(line(gB, pt(v, ends[0]), pt(v, ends[1]), {stroke: "var(--accent2)", "stroke-width": 3.5, "stroke-dasharray": "8 5", opacity: 0}),
|
|
321
|
+
{st: st.q, at: 0, len: dur * 0.6, kind: "fade", until: st.box});
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
// 箱
|
|
325
|
+
var A = pt(q.q1, ends[0]), B = pt(q.q3, ends[1]);
|
|
326
|
+
add2(D.el("rect", {x: Math.min(A[0], B[0]).toFixed(1), y: Math.min(A[1], B[1]).toFixed(1), width: Math.abs(B[0] - A[0]).toFixed(1), height: Math.abs(B[1] - A[1]).toFixed(1),
|
|
327
|
+
fill: g.col, "fill-opacity": 0, stroke: g.col, "stroke-width": 3.5, opacity: 0}, gB), {st: a("box"), at: 0, len: dur, kind: "box"});
|
|
328
|
+
// ひげ:箱の辺から外へ。端に短い線
|
|
329
|
+
[[q.q1, f.wlo], [q.q3, f.whi]].forEach(function(w){
|
|
330
|
+
if (w[0] !== w[1]) add2(line(gB, pt(w[0], c), pt(w[1], c), {stroke: g.col, "stroke-width": 3.5, "stroke-linecap": "round", opacity: 0}), {st: a("whisk"), at: 0, len: dur * 0.7, kind: "line"});
|
|
331
|
+
add2(line(gB, pt(w[1], c - h * 0.28), pt(w[1], c + h * 0.28), {stroke: g.col, "stroke-width": 3.5, "stroke-linecap": "round", opacity: 0}), {st: a("whisk"), at: dur * 0.55, len: dur * 0.3, kind: "fade"});
|
|
332
|
+
});
|
|
333
|
+
f.out.forEach(function(v){
|
|
334
|
+
var xy = pt(v, c);
|
|
335
|
+
add2(D.el("circle", {cx: xy[0].toFixed(1), cy: xy[1].toFixed(1), r: 8, fill: "var(--bg)", stroke: g.col, "stroke-width": 3, opacity: 0}, gB), {st: a("whisk"), at: dur * 0.4, len: dur * 0.4, kind: "fade"});
|
|
336
|
+
});
|
|
337
|
+
// 外れ値の名前:横向きは丸の外側(小さい外れ値は左、大きい外れ値は右)、縦向きは丸の右
|
|
338
|
+
g.outLab.forEach(function(o){
|
|
339
|
+
var xy = pt(o.v, c), x, an;
|
|
340
|
+
if (hor){
|
|
341
|
+
var right = o.v > q.q3;
|
|
342
|
+
x = right ? xy[0] + 18 : xy[0] - 18; an = right ? "start" : "end";
|
|
343
|
+
if (right && x + o.w > W - 2){ x = xy[0] - 18; an = "end"; }
|
|
344
|
+
if (!right && x - o.w < 2){ x = xy[0] + 18; an = "start"; }
|
|
345
|
+
} else { x = xy[0] + 18; an = "start"; }
|
|
346
|
+
add2(D.mtext(gT, x, xy[1] + 8, o.text, {"text-anchor": an, "font-size": 24, fill: "var(--fg)", opacity: 0}), {st: a("whisk"), at: dur * 0.5, len: dur * 0.3, kind: "fade"});
|
|
347
|
+
});
|
|
348
|
+
// 書き出す値:h は箱の上の行に、v は箱の右に。細い線で値の位置とつなぐ
|
|
349
|
+
g.flags.forEach(function(fl){
|
|
350
|
+
var edge = fl.k === "min" || fl.k === "max" ? h * 0.28 : h / 2;
|
|
351
|
+
if (hor){
|
|
352
|
+
var base = c - h / 2 - 14 - fl.row * 34, tip = base - 9, xv = X(fl.v);
|
|
353
|
+
add2(line(gL, [xv, c - edge - 3], [xv, tip], {stroke: "var(--muted)", "stroke-width": 1.5, "stroke-dasharray": "4 4", opacity: 0}), {st: fl.st, at: dur * 0.3, len: dur * 0.4, kind: "fade"});
|
|
354
|
+
add2(D.mtext(gT, fl.a === "start" ? xv + 8 : xv - 8, base, fl.text, {"text-anchor": fl.a, "font-size": 24, fill: "var(--fg)", opacity: 0}), {st: fl.st, at: dur * 0.3, len: dur * 0.4, kind: "fade"});
|
|
355
|
+
} else {
|
|
356
|
+
var yv = Y(fl.v), x0 = c + edge + 3;
|
|
357
|
+
add2(D.el("path", {d: "M" + x0.toFixed(1) + "," + yv.toFixed(1) + "L" + (g.labX - 4).toFixed(1) + "," + (fl.y - 8).toFixed(1), fill: "none", stroke: "var(--muted)", "stroke-width": 1.5, "stroke-dasharray": "4 4", opacity: 0}, gL),
|
|
358
|
+
{st: fl.st, at: dur * 0.3, len: dur * 0.4, kind: "fade"});
|
|
359
|
+
add2(D.mtext(gT, g.labX + 4, fl.y, fl.text, {"text-anchor": "start", "font-size": 24, fill: "var(--fg)", opacity: 0}), {st: fl.st, at: dur * 0.3, len: dur * 0.4, kind: "fade"});
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
// 並べた値の行
|
|
364
|
+
if (row){
|
|
365
|
+
var q = g0.q, gR = D.el("g", {}, p.svg), ty = row.top + 33;
|
|
366
|
+
q.sorted.forEach(function(v, i){
|
|
367
|
+
add2(D.el("text", {x: row.cx(i).toFixed(1), y: ty, "font-size": 26, "text-anchor": "middle", fill: "var(--fg)", opacity: 0, style: "font-variant-numeric:tabular-nums"}, gR), {st: st.dots, at: 0, len: dur * 0.7, kind: "fade"}).textContent = self.fmt(v);
|
|
368
|
+
});
|
|
369
|
+
// 値を囲む枠(位置が x.5 なら前後の 2 つを、x.25 などなら挟む 2 つを)
|
|
370
|
+
var frame = function(i, col, s2){
|
|
371
|
+
var a2 = Math.floor(i + 1e-9), b2 = Math.ceil(i - 1e-9), x1 = row.x0 + a2 * row.cw + 3, x2 = row.x0 + (b2 + 1) * row.cw - 3;
|
|
372
|
+
add2(D.el("rect", {x: x1.toFixed(1), y: row.top + 2, width: Math.max(0, x2 - x1).toFixed(1), height: Math.max(0, row.h - 4), rx: 8, fill: "none", stroke: col, "stroke-width": 3, opacity: 0}, gR), {st: s2, at: 0, len: dur * 0.6, kind: "box"});
|
|
373
|
+
};
|
|
374
|
+
frame(q.imed, "var(--emph)", st.med);
|
|
375
|
+
frame(q.iq1, "var(--accent2)", st.q); frame(q.iq3, "var(--accent2)", st.q);
|
|
376
|
+
frame(0, "var(--fg)", st.whisk); frame(row.n - 1, "var(--fg)", st.whisk);
|
|
377
|
+
// 前半・後半(jp)
|
|
378
|
+
if (q.halves) q.halves.forEach(function(hv){
|
|
379
|
+
var x1 = row.x0 + hv[0] * row.cw + 6, x2 = row.x0 + (hv[1] + 1) * row.cw - 6, yb = row.top + row.h + 9;
|
|
380
|
+
add2(D.el("path", {d: "M" + x1.toFixed(1) + "," + (yb - 6) + "V" + yb + "H" + x2.toFixed(1) + "V" + (yb - 6), fill: "none", stroke: "var(--muted)", "stroke-width": 2, opacity: 0}, gR), {st: st.q, at: 0, len: dur * 0.5, kind: "fade"});
|
|
381
|
+
});
|
|
382
|
+
row.names.forEach(function(o){
|
|
383
|
+
add2(D.mtext(gR, o.x, row.top + row.h + row.brk + 24 + o.r * 30, o.text, {"text-anchor": "middle", "font-size": 24, fill: "var(--fg)", opacity: 0}), {st: o.st, at: dur * 0.2, len: dur * 0.4, kind: "fade"});
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
items.forEach(function(it){
|
|
387
|
+
if (!draw || (it.kind !== "line" && it.kind !== "box")) return;
|
|
388
|
+
it.el.setAttribute("pathLength", 1);
|
|
389
|
+
});
|
|
390
|
+
function niceStep(r){ var e = Math.pow(10, Math.floor(Math.log10(r))), x = r / e; return S.clean((x <= 1 ? 1 : x <= 2 ? 2 : x <= 2.5 ? 2.5 : x <= 5 ? 5 : 10) * e); }
|
|
391
|
+
function dec(v){ var mm = String(v).match(/\.(\d+)$/); return mm ? mm[1].length : 0; }
|
|
392
|
+
}
|
|
393
|
+
// 最後の形を 1 文で(読み上げの名前):「箱ひげ図(記録(回)):1 組 最小値 3、第1四分位数 6、中央値 9、第3四分位数 13、最大値 18」。
|
|
394
|
+
// 群ごとに 5 つの数(outliers="on" なら外れ値も)。N は mount の名前の表
|
|
395
|
+
summary(ja, N){
|
|
396
|
+
var self = this, P = this.props, pl = this.plan(), G = pl.groups, axis = (P.orient === "v" ? P.yl : P.xl) || "";
|
|
397
|
+
var v = function(x){ return self.fmt(x) + P.unit; }, sep = ja ? "、" : ", ";
|
|
398
|
+
var head = (ja ? "箱ひげ図" : "Box plot") + (axis ? (ja ? "(" + axis + ")" : " (" + axis + ")") : "");
|
|
399
|
+
var gs = G.map(function(g, i){
|
|
400
|
+
var q = g.q, five = [["min", q.min], ["q1", q.q1], ["med", q.med], ["q3", q.q3], ["max", q.max]].map(function(o){ return (ja ? N[o[0]] : N[o[0]].toLowerCase()) + " " + v(o[1]); }).join(sep);
|
|
401
|
+
var outs = pl.out && g.f.out.length ? sep + (ja ? N.out : "outliers") + " " + g.f.out.map(v).join(ja ? "・" : ", ") : "";
|
|
402
|
+
var name = g.label || (G.length > 1 ? (ja ? (i + 1) + " 番目の群" : "group " + (i + 1)) : "");
|
|
403
|
+
return (name ? name + (ja ? " " : ": ") : "") + five + outs;
|
|
404
|
+
});
|
|
405
|
+
return head + (ja ? ":" : ": ") + gs.join(ja ? "。" : "; ");
|
|
406
|
+
}
|
|
407
|
+
duration(k){
|
|
408
|
+
var pl = this.plan();
|
|
409
|
+
return k >= 1 && k <= pl.steps ? this.props.dur : 0;
|
|
410
|
+
}
|
|
411
|
+
render(k, t, ctx){
|
|
412
|
+
var D = base, lecture = !!ctx.preview, draw = this.variant === "draw", FAINT = 0.45;
|
|
413
|
+
if (lecture) FAINT = base.previewOpacity(this); // 先の段階の薄さ:テーマの --preview-opacity(ないときは 0.45)
|
|
414
|
+
this.items.forEach(function(it){
|
|
415
|
+
var e = it.st < k ? 1 : it.st > k ? (lecture ? -1 : 0) : D.prog(t, it.at, it.len), op;
|
|
416
|
+
// 箱が出たら消える線(四分位数の仮の線)
|
|
417
|
+
var gone = it.until !== undefined && it.st <= k ? (it.until < k ? 1 : it.until > k ? 0 : D.prog(t, 0, it.len)) : 0;
|
|
418
|
+
if (e < 0){ op = it.until !== undefined ? 0 : FAINT; e = 1; }
|
|
419
|
+
else op = e;
|
|
420
|
+
if (draw && (it.kind === "line" || it.kind === "box")){
|
|
421
|
+
it.el.setAttribute("stroke-dasharray", e >= 1 ? "none" : e.toFixed(4) + " 2");
|
|
422
|
+
op = e > 0 ? (op === FAINT && it.st > k ? FAINT : 1) : 0;
|
|
423
|
+
if (it.kind === "box" && it.el.localName === "rect" && it.el.getAttribute("fill") !== "none") it.el.setAttribute("fill-opacity", (0.14 * e).toFixed(3));
|
|
424
|
+
} else if (it.kind === "box" && it.el.getAttribute("fill") !== "none") it.el.setAttribute("fill-opacity", 0.14);
|
|
425
|
+
it.el.setAttribute("opacity", (op * (1 - gone)).toFixed(3));
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
});
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// expr 1.0.0: the safe arithmetic expression evaluator of Egatari components 2.0 (a library part, Egatari spec §6.1).
|
|
2
|
+
// Parts that evaluate expressions written in attributes or data require it ({expr: "1.0.0"}) and receive what this factory
|
|
3
|
+
// returns: {compile(src, {name, vars, funcs, bareMath}), FUNCS, CONSTS, MAX_LEN, MAX_DEPTH} (the EXPR object below).
|
|
4
|
+
// Spec §6: registry components that evaluate expressions written in attributes MUST restrict what they can do; this one
|
|
5
|
+
// parses and evaluates a tree and never runs text as code. The block between the BEGIN/END lines is tools/expr.js, the
|
|
6
|
+
// one source (tools/expr-sync.mjs keeps it equal; 1.x parts m-sweep and m-curves carry their own copy until they move to 2.0).
|
|
7
|
+
// It uses no import, no network and no global of the player (spec §6.1).
|
|
8
|
+
egatariParts.define({name: "expr", version: "1.0.0", requires: {}}, function(){
|
|
9
|
+
"use strict";
|
|
10
|
+
// ---- BEGIN egatari-expr (source: tools/expr.js; copy with `node tools/expr-sync.mjs`; do not edit inside a component) ----
|
|
11
|
+
// A safe arithmetic expression evaluator (Egatari spec §6: a component that evaluates an expression written in an attribute
|
|
12
|
+
// or in its data MUST restrict what it can do). It parses the text into a tree and evaluates the tree; it never calls eval,
|
|
13
|
+
// new Function or any other way of running text as code. What it accepts (JavaScript's meaning and precedence):
|
|
14
|
+
// numbers 12 1.5 .5 1e-3 (no leading zero such as 010) | the variables the component allows (m-sweep: u c; m-curves: x)
|
|
15
|
+
// Math.<constant> Math.<function>(…) — and, where the component allows it, the same names bare (PI, exp(…)) and its own
|
|
16
|
+
// functions (m-curves: N(x, mean, sd)) | ( ) , between arguments only
|
|
17
|
+
// unary + - ! | ** (right-associative; -a ** b must be written (-a) ** b or -(a ** b)) | * / % | + -
|
|
18
|
+
// < <= > >= | == != === !== | && || | c ? a : b
|
|
19
|
+
// Everything else is refused with a SyntaxError that names the problem: other names (fetch, globalThis, constructor, …),
|
|
20
|
+
// property access other than Math.<listed name>, calls of anything but the listed functions, strings, [ ], { }, =, ;,
|
|
21
|
+
// the comma operator, ^ & | ~ (write ** for a power), more than 2000 characters, nesting deeper than 64.
|
|
22
|
+
// Math.random is not listed (a component draws the same picture for the same step and time).
|
|
23
|
+
var EXPR = (function(){
|
|
24
|
+
"use strict";
|
|
25
|
+
var MAX_LEN = 2000, MAX_DEPTH = 64;
|
|
26
|
+
var FUNCS = ["abs", "acos", "acosh", "asin", "asinh", "atan", "atan2", "atanh", "cbrt", "ceil", "cos", "cosh", "exp", "expm1",
|
|
27
|
+
"floor", "hypot", "log", "log10", "log1p", "log2", "max", "min", "pow", "round", "sign", "sin", "sinh", "sqrt", "tan", "tanh", "trunc"];
|
|
28
|
+
var CONSTS = ["E", "LN10", "LN2", "LOG10E", "LOG2E", "PI", "SQRT1_2", "SQRT2"];
|
|
29
|
+
var M = Math;
|
|
30
|
+
var TOKEN = /\s+|(\d+\.?\d*(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?)|([A-Za-z_$][\w$]*)|(\*\*|===|!==|==|!=|<=|>=|&&|\|\||[-+*/%<>!?:,().])/y;
|
|
31
|
+
var BIN = {
|
|
32
|
+
"||": 1, "&&": 2, "==": 3, "!=": 3, "===": 3, "!==": 3, "<": 4, "<=": 4, ">": 4, ">=": 4, "+": 5, "-": 5, "*": 6, "/": 6, "%": 6
|
|
33
|
+
};
|
|
34
|
+
function own(o, k){ return Object.prototype.hasOwnProperty.call(o, k); }
|
|
35
|
+
function listed(list, k){ return list.indexOf(k) >= 0; }
|
|
36
|
+
function quote(s){ s = String(s); return '"' + (s.length > 60 ? s.slice(0, 57) + "…" : s) + '"'; }
|
|
37
|
+
|
|
38
|
+
function tokenize(src, fail){
|
|
39
|
+
var out = [], i = 0;
|
|
40
|
+
while (i < src.length){
|
|
41
|
+
TOKEN.lastIndex = i;
|
|
42
|
+
var m = TOKEN.exec(src);
|
|
43
|
+
if (!m || m.index !== i){
|
|
44
|
+
var ch = src.charAt(i);
|
|
45
|
+
if (ch === "^") fail("^ is not allowed (write ** for a power)");
|
|
46
|
+
if (ch === "'" || ch === '"' || ch === "`") fail("strings are not allowed");
|
|
47
|
+
if (ch === "[" || ch === "]" || ch === "{" || ch === "}") fail(ch + " is not allowed");
|
|
48
|
+
if (ch === "=") fail("= (assignment) is not allowed");
|
|
49
|
+
fail("the character " + quote(ch) + " is not allowed");
|
|
50
|
+
}
|
|
51
|
+
i = TOKEN.lastIndex;
|
|
52
|
+
if (m[1] !== undefined){
|
|
53
|
+
if (/^0\d/.test(m[1])) fail("a number cannot start with 0 (" + m[1] + ")");
|
|
54
|
+
out.push({k: "num", v: Number(m[1]), s: m[1]});
|
|
55
|
+
}
|
|
56
|
+
else if (m[2] !== undefined) out.push({k: "id", s: m[2]});
|
|
57
|
+
else if (m[3] !== undefined) out.push({k: "op", s: m[3]});
|
|
58
|
+
}
|
|
59
|
+
out.push({k: "end", s: "the end"});
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// parse(src, opt) → tree. Nodes: {t: "num", v} {t: "var", i} {t: "call", f, args} {t: "un", op, a} {t: "bin", op, a, b} {t: "cond", c, a, b}
|
|
64
|
+
function parse(src, opt, fail){
|
|
65
|
+
if (typeof src !== "string") fail("the expression is not text");
|
|
66
|
+
if (src.length > MAX_LEN) fail("longer than " + MAX_LEN + " characters");
|
|
67
|
+
var toks = tokenize(src, fail), p = 0, depth = 0;
|
|
68
|
+
var vars = opt.vars || [], funcs = opt.funcs || {}, bare = !!opt.bareMath;
|
|
69
|
+
function peek(){ return toks[p]; }
|
|
70
|
+
function isOp(s){ var t = toks[p]; return t.k === "op" && t.s === s; }
|
|
71
|
+
function take(s){ if (!isOp(s) && isOp(".")) fail("property access is not allowed (only Math.<name>)"); if (!isOp(s)) fail("expected " + s + " before " + quote(peek().s)); p++; }
|
|
72
|
+
function enter(){ if (++depth > MAX_DEPTH) fail("nested deeper than " + MAX_DEPTH); }
|
|
73
|
+
function ternary(){
|
|
74
|
+
enter();
|
|
75
|
+
var c = binary(1);
|
|
76
|
+
if (isOp("?")){ p++; var a = ternary(); take(":"); var b = ternary(); c = {t: "cond", c: c, a: a, b: b}; }
|
|
77
|
+
depth--;
|
|
78
|
+
return c;
|
|
79
|
+
}
|
|
80
|
+
function binary(min){
|
|
81
|
+
var a = expo();
|
|
82
|
+
for (;;){
|
|
83
|
+
var t = peek(), lv = t.k === "op" && own(BIN, t.s) ? BIN[t.s] : 0;
|
|
84
|
+
if (!lv || lv < min) return a;
|
|
85
|
+
p++;
|
|
86
|
+
a = {t: "bin", op: t.s, a: a, b: binary(lv + 1)};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// ExponentiationExpression: UnaryExpression | UpdateExpression ** ExponentiationExpression (as in JavaScript)
|
|
90
|
+
function expo(){
|
|
91
|
+
var t = peek();
|
|
92
|
+
if (t.k === "op" && (t.s === "-" || t.s === "+" || t.s === "!")){
|
|
93
|
+
var u = unary();
|
|
94
|
+
if (isOp("**")) fail("write (-a) ** b or -(a ** b), not -a ** b");
|
|
95
|
+
return u;
|
|
96
|
+
}
|
|
97
|
+
var b = primary();
|
|
98
|
+
if (isOp("**")){ p++; enter(); var e = expo(); depth--; return {t: "bin", op: "**", a: b, b: e}; }
|
|
99
|
+
return b;
|
|
100
|
+
}
|
|
101
|
+
function unary(){
|
|
102
|
+
var t = peek();
|
|
103
|
+
if (t.k === "op" && (t.s === "-" || t.s === "+" || t.s === "!")){ p++; enter(); var a = unary(); depth--; return {t: "un", op: t.s, a: a}; }
|
|
104
|
+
return primary();
|
|
105
|
+
}
|
|
106
|
+
function call(name, f){
|
|
107
|
+
take("(");
|
|
108
|
+
var args = [];
|
|
109
|
+
if (!isOp(")")) for (;;){ args.push(ternary()); if (isOp(",")){ p++; continue; } break; }
|
|
110
|
+
take(")");
|
|
111
|
+
return {t: "call", name: name, f: f, args: args};
|
|
112
|
+
}
|
|
113
|
+
function mathName(n, shown){
|
|
114
|
+
if (listed(CONSTS, n)) return {t: "num", v: M[n]};
|
|
115
|
+
if (listed(FUNCS, n)){ if (!isOp("(")) fail(shown + " is a function: write " + shown + "(…)"); return call(shown, M[n]); }
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
function primary(){
|
|
119
|
+
var t = peek();
|
|
120
|
+
if (t.k === "num"){ p++; return {t: "num", v: t.v}; }
|
|
121
|
+
if (t.k === "op" && t.s === "("){ p++; var e = ternary(); take(")"); return e; }
|
|
122
|
+
if (t.k !== "id") fail("unexpected " + quote(t.s));
|
|
123
|
+
p++;
|
|
124
|
+
var n = t.s, r;
|
|
125
|
+
if (n === "Math"){
|
|
126
|
+
take(".");
|
|
127
|
+
var m = peek();
|
|
128
|
+
if (m.k !== "id") fail("expected a name after Math.");
|
|
129
|
+
p++;
|
|
130
|
+
if ((r = mathName(m.s, "Math." + m.s))) return r;
|
|
131
|
+
fail("Math." + m.s + " is not allowed");
|
|
132
|
+
}
|
|
133
|
+
if (bare && (r = mathName(n, n))) return r;
|
|
134
|
+
var vi = vars.indexOf(n);
|
|
135
|
+
if (vi >= 0) return {t: "var", i: vi};
|
|
136
|
+
if (own(funcs, n)){ if (!isOp("(")) fail(n + " is a function: write " + n + "(…)"); return call(n, funcs[n]); }
|
|
137
|
+
fail("the name " + n + " is not allowed (allowed: " + allowedNames(opt) + ")");
|
|
138
|
+
}
|
|
139
|
+
var tree = ternary();
|
|
140
|
+
if (isOp(".")) fail("property access is not allowed (only Math.<name>)");
|
|
141
|
+
if (peek().k !== "end") fail("unexpected " + quote(peek().s));
|
|
142
|
+
return tree;
|
|
143
|
+
}
|
|
144
|
+
function allowedNames(opt){
|
|
145
|
+
var a = (opt.vars || []).concat(Object.keys(opt.funcs || {}).map(function(n){ return n + "(…)"; }));
|
|
146
|
+
return a.concat(opt.bareMath ? ["Math functions and constants (sin, PI, …)"] : ["Math.sin, Math.PI, …"]).join(", ");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The tree becomes nested closures over the argument list (no text is run as code)
|
|
150
|
+
function build(n){
|
|
151
|
+
switch (n.t){
|
|
152
|
+
case "num": var v = n.v; return function(){ return v; };
|
|
153
|
+
case "var": var i = n.i; return function(a){ return a[i]; };
|
|
154
|
+
case "call":
|
|
155
|
+
var f = n.f, xs = n.args.map(build), k = xs.length;
|
|
156
|
+
return function(a){ var v2 = new Array(k); for (var j = 0; j < k; j++) v2[j] = xs[j](a); return f.apply(undefined, v2); };
|
|
157
|
+
case "un":
|
|
158
|
+
var u = build(n.a);
|
|
159
|
+
return n.op === "-" ? function(a){ return -u(a); } : n.op === "+" ? function(a){ return +u(a); } : function(a){ return !u(a); };
|
|
160
|
+
case "cond":
|
|
161
|
+
var c = build(n.c), x = build(n.a), y = build(n.b);
|
|
162
|
+
return function(a){ return c(a) ? x(a) : y(a); };
|
|
163
|
+
case "bin":
|
|
164
|
+
var l = build(n.a), r = build(n.b);
|
|
165
|
+
switch (n.op){
|
|
166
|
+
case "+": return function(a){ return l(a) + r(a); };
|
|
167
|
+
case "-": return function(a){ return l(a) - r(a); };
|
|
168
|
+
case "*": return function(a){ return l(a) * r(a); };
|
|
169
|
+
case "/": return function(a){ return l(a) / r(a); };
|
|
170
|
+
case "%": return function(a){ return l(a) % r(a); };
|
|
171
|
+
case "**": return function(a){ return l(a) ** r(a); };
|
|
172
|
+
case "<": return function(a){ return l(a) < r(a); };
|
|
173
|
+
case "<=": return function(a){ return l(a) <= r(a); };
|
|
174
|
+
case ">": return function(a){ return l(a) > r(a); };
|
|
175
|
+
case ">=": return function(a){ return l(a) >= r(a); };
|
|
176
|
+
case "==": return function(a){ return l(a) == r(a); }; // eslint-disable-line eqeqeq
|
|
177
|
+
case "!=": return function(a){ return l(a) != r(a); }; // eslint-disable-line eqeqeq
|
|
178
|
+
case "===": return function(a){ return l(a) === r(a); };
|
|
179
|
+
case "!==": return function(a){ return l(a) !== r(a); };
|
|
180
|
+
case "&&": return function(a){ return l(a) && r(a); };
|
|
181
|
+
case "||": return function(a){ return l(a) || r(a); };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
throw new Error("egatari-expr: unknown node");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// compile(src, {name, vars: ["u", "c"], funcs: {N: fn}, bareMath}) → function(v0, v1, …) that returns the value.
|
|
188
|
+
// A refused expression throws a SyntaxError whose message starts with `<name> = "<src>": ` (the runtime then shows the
|
|
189
|
+
// component's inline placeholder and records it in DECK.componentErrors).
|
|
190
|
+
function compile(src, opt){
|
|
191
|
+
opt = opt || {};
|
|
192
|
+
var fail = function(msg){ throw new SyntaxError((opt.name || "expression") + " = " + quote(src) + ": " + msg); };
|
|
193
|
+
var run = build(parse(src, opt, fail));
|
|
194
|
+
return function(){ return run(arguments); };
|
|
195
|
+
}
|
|
196
|
+
return {compile: compile, FUNCS: FUNCS.slice(), CONSTS: CONSTS.slice(), MAX_LEN: MAX_LEN, MAX_DEPTH: MAX_DEPTH};
|
|
197
|
+
})();
|
|
198
|
+
// ---- END egatari-expr ----
|
|
199
|
+
return EXPR;
|
|
200
|
+
});
|