egatari 0.6.17 → 0.6.19
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 +68 -1
- package/README.ja.md +14 -13
- package/README.md +2 -1
- package/components.lock.json +153 -54
- package/components2.lock.json +1001 -1
- package/kit/build.mjs +1 -1
- package/kit/check-messages.mjs +9 -0
- package/kit/check.mjs +8 -0
- package/kit/core/bookmark.mjs +28 -0
- package/kit/core/build-core.mjs +5 -3
- package/kit/core/check-parity.mjs +6 -0
- package/kit/core/check-rules.mjs +4 -3
- package/kit/core/check-source.mjs +10 -1
- package/kit/core/checks.mjs +79 -0
- package/kit/core/find-replace.mjs +170 -0
- package/kit/core/format.mjs +4 -1
- package/kit/core/image-insert.mjs +197 -0
- package/kit/core/image-size.mjs +1 -1
- package/kit/core/layout-scan.mjs +1 -1
- package/kit/core/llm-fix.mjs +1 -1
- package/kit/core/llm-pack-assemble.mjs +40 -67
- package/kit/core/llm-pack-data.mjs +882 -52
- 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 +3 -0
- package/kit/core/portable.mjs +5 -2
- package/kit/core/purposes.mjs +9 -3
- package/kit/core/sample-info.mjs +17 -3
- package/kit/core/scorm.mjs +30 -6
- package/kit/core/script-unclosed.mjs +49 -0
- package/kit/core/slide-ops.mjs +27 -1
- package/kit/core/tate-mix.mjs +25 -0
- package/kit/core/template-text.mjs +2 -0
- package/kit/core/theme-derive.mjs +56 -17
- package/kit/core/theme-shapes.mjs +15 -1
- package/kit/deck.js +124 -6
- package/kit/instruction-version.json +1 -1
- package/kit/layouts.css +8 -0
- package/kit/llm-pack.mjs +5 -7
- package/kit/panes.js +17 -1
- 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/kit/track.js +46 -5
- package/package.json +1 -1
- package/server/editor-ui.js +21 -1
- package/vendor/components/CATALOG.md +25 -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/15306d854d67d52ca2b1d66135f0d3296882e6b21fd85c029399b8077c8a7f9d/README.en.md +34 -0
- package/vendor/components/sha256/15306d854d67d52ca2b1d66135f0d3296882e6b21fd85c029399b8077c8a7f9d/index.js +398 -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/277ac772315f93737a094cd0f0842726f2435d352d9a90822aebfe0d5d7e79a1/README.en.md +23 -0
- package/vendor/components/sha256/277ac772315f93737a094cd0f0842726f2435d352d9a90822aebfe0d5d7e79a1/index.js +294 -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/3be5f64deb1951327b614b29a9b632878fab362687c76e3f48f3af1d1ebbb1cf/README.en.md +47 -0
- package/vendor/components/sha256/3be5f64deb1951327b614b29a9b632878fab362687c76e3f48f3af1d1ebbb1cf/index.js +223 -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/7c7b0373cfd38fd5ecb4169021e0b908f0a17be1a29b14f85dd89fdb92e7bf81/README.en.md +44 -0
- package/vendor/components/sha256/7c7b0373cfd38fd5ecb4169021e0b908f0a17be1a29b14f85dd89fdb92e7bf81/index.js +306 -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/939bdae590ebed3d182b0c9940a35b6c03872505bfa242341990a81cae02fd06/README.en.md +34 -0
- package/vendor/components/sha256/939bdae590ebed3d182b0c9940a35b6c03872505bfa242341990a81cae02fd06/index.js +397 -0
- package/vendor/components/sha256/9c5dd0bc0c3279c5f0fea3fdb9d0735870880aef96e8bbf9ecdd77dff0f5ddc8/README.en.md +44 -0
- package/vendor/components/sha256/9c5dd0bc0c3279c5f0fea3fdb9d0735870880aef96e8bbf9ecdd77dff0f5ddc8/index.js +551 -0
- package/vendor/components/sha256/a1c2b2fa06e4229815ab82a9202c266ec887a67632a175854c7d136a92389ca3/index.js +69 -0
- package/vendor/components/sha256/a5b032657720d3beb64f0b3699954e13b0eb985f36161b9ae046c83fd6048ff5/README.en.md +18 -0
- package/vendor/components/sha256/a5b032657720d3beb64f0b3699954e13b0eb985f36161b9ae046c83fd6048ff5/index.js +382 -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/components-2/manifest-2.20.0.json +3217 -0
- package/vendor/components-2/manifest-2.21.0.json +3220 -0
- package/vendor/egatari-spec/SOURCE.json +2 -2
- package/vendor/egatari-spec/schema/deck-meta.schema.json +3 -0
- package/vendor/egatari-spec/schema/egatari-metadata.schema.json +6 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Network layers and the forward pass; neurons whole or split into Σ | f
|
|
3
|
+
steps: input + 1 per whole, 2 per split layer
|
|
4
|
+
props:
|
|
5
|
+
dur: s per step
|
|
6
|
+
digits: decimals
|
|
7
|
+
edges: plain | weight
|
|
8
|
+
size: text px
|
|
9
|
+
example: {"layers": [{"n": 2, "label": "Input"}, {"n": 1, "label": "Output", "act": "step", "form": "split"}], "weights": [[[1, 1]]], "biases": [[-1.5]], "values": {"x": [1, 1], "z": [[0.5]], "a": [[1]]}}
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Values from a script; checked: z = W·a + b, a = f(z). Curves: m-curves. 480px+.
|
|
13
|
+
<!-- llm: omit -->
|
|
14
|
+
- layer: `n`, `label`, `names` (left of inputs, right of outputs), `act` (identity, step, relu, sigmoid, tanh, softmax), `form` (combined | split). `weights[l]`: rows = next layer, columns = previous; `biases[l]`; `values.x`, `values.z[l]`, `values.a[l]`.
|
|
15
|
+
- steps (default: input, then a per layer; split layers z then a): `{"show": "x"}`, `{"layer": l, "phase": "z" | "a"}`, `{"open": l}`, `{"focus": [l, i]}` (incoming weights and the z = w·x + b line), `note`. `"delta"`/`"grad"` are reserved (backpropagation; reported, not drawn).
|
|
16
|
+
- Checks (data): shapes, z = W·a_prev + b, a = f(z) (softmax per layer), within the precision written. `edges="weight"`: width |w|, positive `--term` solid, negative `--emph` dashed. Up to 8 neurons per layer (fewer if the height is short), else head, "⋮", tail. No answer preview.
|
|
17
|
+
<!-- llm: end -->
|
|
18
|
+
<!-- llm: omit -->
|
|
19
|
+
Notes: keep values to about 2 decimals; draw activation curves beside it with m-curves.
|
|
20
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// m-nnet:ニューラルネットの図。層ごとのニューロンの数から列を並べ(座標は書かない)、入力の値から層ごとに前向きの計算を進める。
|
|
2
|
+
// ニューロンは 2 通り:まとめた丸(ふつうの図)と、丸を Σ(重み付きの和 z = w·x + b)と f(活性化 a = f(z))に分けた形(教える用)。
|
|
3
|
+
// <m-nnet style="height:520px"><script type="application/json">
|
|
4
|
+
// {"layers": [{"n": 2, "label": "入力層", "names": ["*x*_1", "*x*_2"]}, {"n": 1, "label": "出力層", "act": "step", "form": "split"}],
|
|
5
|
+
// "weights": [[[1, 1]]], "biases": [[-1.5]], "values": {"x": [1, 1], "z": [[0.5]], "a": [[1]]}}
|
|
6
|
+
// (JSON の終わり)</m-nnet>
|
|
7
|
+
// 値は計算を走らせたスクリプトの出力(重み・バイアス・z・a)。パーツは計算をし直して確かめるだけ(z = W·a_前 + b、a = f(z)。合わなければ data)。
|
|
8
|
+
// 活性化関数の曲線は描かない(m-curves で描く)。分けた形の f の側には関数の名前(ReLU、σ など)を出す。
|
|
9
|
+
// ステップ(steps を書かないとき):1 入力の値、続いて層ごとに、まとめた層は 1 ステップ(a が出る)、分けた層は 2 ステップ(z、次に a)。
|
|
10
|
+
// steps を書くとき:{"show": "x"}、{"layer": l, "phase": "z" | "a"}、{"open": l}(まとめた丸を Σ|f に開く)、{"focus": [l, i]}(ニューロン i に入る辺と重み)。
|
|
11
|
+
// "phase": "delta" と "grad"(誤差逆伝播)は予約。今は data で知らせて飛ばす。
|
|
12
|
+
// 色:正の重み --term(実線)、負の重み --emph(点線)。今計算している層に入る辺を濃くする。値の出たニューロンは --accent2 の縁。
|
|
13
|
+
egatariParts.define({name: "nnet", version: "2.1.0", requires: {base: "1.3.0"}, element: "m-nnet"}, function(deps){
|
|
14
|
+
"use strict";
|
|
15
|
+
var base = deps.base;
|
|
16
|
+
var NUM = {type: "number"}, VEC = {type: "array", items: NUM}, MAT = {type: "array", items: VEC};
|
|
17
|
+
var ACT = {
|
|
18
|
+
identity: {ja: "恒等", en: "identity", f: function(z){ return z; }},
|
|
19
|
+
step: {ja: "ステップ", en: "step", f: function(z){ return z >= 0 ? 1 : 0; }},
|
|
20
|
+
relu: {ja: "ReLU", en: "ReLU", f: function(z){ return Math.max(0, z); }},
|
|
21
|
+
sigmoid: {ja: "σ", en: "σ", f: function(z){ return 1 / (1 + Math.exp(-z)); }},
|
|
22
|
+
tanh: {ja: "tanh", en: "tanh", f: function(z){ return Math.tanh(z); }},
|
|
23
|
+
softmax: {ja: "softmax", en: "softmax", f: null},
|
|
24
|
+
};
|
|
25
|
+
function dec(v){ var s = String(v).split(".")[1]; return s ? s.length : 0; }
|
|
26
|
+
return class extends base.MotionElement {
|
|
27
|
+
static partName = "nnet";
|
|
28
|
+
static preview = "none";
|
|
29
|
+
static shape = {
|
|
30
|
+
schema: {type: "object", additionalProperties: false, required: ["layers"], properties: {
|
|
31
|
+
layers: {type: "array", minItems: 2, maxItems: 6, items: {type: "object", additionalProperties: false, required: ["n"], properties: {
|
|
32
|
+
n: {type: "integer"}, label: {type: "string"}, names: {type: "array", items: {type: "string"}},
|
|
33
|
+
act: {type: "string", enum: ["identity", "step", "relu", "sigmoid", "tanh", "softmax"]}, form: {type: "string", enum: ["combined", "split"]}}}},
|
|
34
|
+
weights: {type: "array", items: MAT},
|
|
35
|
+
biases: {type: "array", items: VEC},
|
|
36
|
+
values: {type: "object", additionalProperties: false, properties: {x: VEC, z: MAT, a: MAT}},
|
|
37
|
+
steps: {type: "array", maxItems: 40, items: {type: "object", additionalProperties: false, properties: {
|
|
38
|
+
show: {type: "string", enum: ["x"]}, layer: {type: "integer"}, phase: {type: "string", enum: ["z", "a", "delta", "grad"]},
|
|
39
|
+
open: {type: "integer"}, focus: {type: "array", minItems: 2, maxItems: 2, items: {type: "integer"}}, note: {type: "string"}}}}}},
|
|
40
|
+
example: {layers: [{n: 2, label: "入力層"}, {n: 1, label: "出力層", act: "step", form: "split"}],
|
|
41
|
+
weights: [[[1, 1]]], biases: [[-1.5]], values: {x: [1, 1], z: [[0.5]], a: [[1]]}},
|
|
42
|
+
};
|
|
43
|
+
static props = {
|
|
44
|
+
dur: {type: "number", default: 0.8, doc: "1 ステップの秒数"},
|
|
45
|
+
digits: {type: "number", default: 2, doc: "値の小数の桁"},
|
|
46
|
+
edges: {type: "string", default: "plain", doc: "辺の描き方:plain(細い線)か weight(重みの大きさで太さ、符号で実線と点線)"},
|
|
47
|
+
size: {type: "number", default: 24, doc: "字の大きさ(px、24 以上)"},
|
|
48
|
+
};
|
|
49
|
+
get steps(){
|
|
50
|
+
if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
|
|
51
|
+
return this.plan().states.length - 1;
|
|
52
|
+
}
|
|
53
|
+
// ステップごとの状態(データだけから決まる)と確かめた結果
|
|
54
|
+
plan(){
|
|
55
|
+
if (this._plan) return this._plan;
|
|
56
|
+
var d = this.data || {}, L = d.layers || [], bad = [], V = d.values || {};
|
|
57
|
+
var nL = L.length;
|
|
58
|
+
// 形の確かめ:重み W[l] は n(l+1) 行 × n(l) 列、バイアス b[l] は n(l+1)、z[l]・a[l] は n(l+1)、x は n(0)
|
|
59
|
+
var dims = function(m, r, c){ return Array.isArray(m) && m.length === r && m.every(function(row){ return !c || row.length === c; }); };
|
|
60
|
+
if (d.weights && (d.weights.length !== nL - 1 || d.weights.some(function(W, l){ return !dims(W, L[l + 1].n, L[l].n); }))) bad.push("weights must be " + (nL - 1) + " matrices, n(next) rows × n(previous) columns");
|
|
61
|
+
if (d.biases && (d.biases.length !== nL - 1 || d.biases.some(function(b, l){ return b.length !== L[l + 1].n; }))) bad.push("biases must be " + (nL - 1) + " vectors of n(next)");
|
|
62
|
+
if (V.x && V.x.length !== (L[0] || {}).n) bad.push("values.x must have " + (L[0] || {}).n + " numbers");
|
|
63
|
+
["z", "a"].forEach(function(k){ if (V[k] && (V[k].length !== nL - 1 || V[k].some(function(v, l){ return v.length !== L[l + 1].n; }))) bad.push("values." + k + " must be " + (nL - 1) + " vectors, one per layer after the input"); });
|
|
64
|
+
L.forEach(function(ly, l){
|
|
65
|
+
if (ly.n < 1 || ly.n > 64) bad.push("layers[" + l + "].n must be 1–64");
|
|
66
|
+
if (ly.names && ly.names.length !== ly.n) bad.push("layers[" + l + "].names must have " + ly.n + " names");
|
|
67
|
+
});
|
|
68
|
+
var ok = !bad.length;
|
|
69
|
+
// 計算の確かめ:z = W·a_前 + b、a = f(z)。値が丸めて書かれていれば、その桁の分だけ許す
|
|
70
|
+
if (ok && d.weights && V.x && V.z) for (var l = 0; l < nL - 1; l++){
|
|
71
|
+
var prev = l === 0 ? V.x : V.a && V.a[l - 1];
|
|
72
|
+
if (!prev) break;
|
|
73
|
+
var dp = Math.max.apply(null, prev.map(dec).concat([0])), dz = Math.max.apply(null, V.z[l].map(dec).concat([0]));
|
|
74
|
+
V.z[l].forEach(function(z, i){
|
|
75
|
+
var W = d.weights[l][i], s = W.reduce(function(a, w, j){ return a + w * prev[j]; }, 0) + (d.biases ? d.biases[l][i] : 0);
|
|
76
|
+
var tol = 1e-6 * Math.max(1, Math.abs(s)) + (dz ? 0.51 * Math.pow(10, -dz) : 0) + (dp ? W.reduce(function(a, w){ return a + Math.abs(w); }, 0) * 0.51 * Math.pow(10, -dp) : 0);
|
|
77
|
+
if (Math.abs(s - z) > tol) bad.push("layer " + (l + 1) + " neuron " + (i + 1) + ": z is " + z + " but W·a + b = " + +s.toFixed(6));
|
|
78
|
+
});
|
|
79
|
+
var act = L[l + 1].act;
|
|
80
|
+
if (act && V.a){
|
|
81
|
+
var zs = V.z[l], da = Math.max.apply(null, V.a[l].map(dec).concat([0])), want;
|
|
82
|
+
if (act === "softmax"){ var m = Math.max.apply(null, zs), ex = zs.map(function(z){ return Math.exp(z - m); }), se = ex.reduce(function(a, b){ return a + b; }, 0); want = ex.map(function(e){ return e / se; }); }
|
|
83
|
+
else want = zs.map(ACT[act].f);
|
|
84
|
+
V.a[l].forEach(function(a, i){
|
|
85
|
+
var t = 1e-6 + (da ? 0.51 * Math.pow(10, -da) : 0) + (dz && act !== "step" ? 0.51 * Math.pow(10, -dz) : 0);
|
|
86
|
+
if (Math.abs(want[i] - a) > t) bad.push("layer " + (l + 1) + " neuron " + (i + 1) + ": a is " + a + " but " + ACT[act].en + "(z) = " + +want[i].toFixed(6));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// ステップの列
|
|
91
|
+
var steps = d.steps;
|
|
92
|
+
if (!steps){
|
|
93
|
+
steps = [{show: "x"}];
|
|
94
|
+
for (var q = 1; q < nL; q++){
|
|
95
|
+
if (L[q].form === "split") steps.push({layer: q, phase: "z"}, {layer: q, phase: "a"});
|
|
96
|
+
else steps.push({layer: q, phase: "a"});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
var st = {x: false, z: {}, a: {}, form: L.map(function(ly){ return ly.form === "split" ? "split" : "combined"; }), active: -1, focus: null, open: -1, note: ""};
|
|
100
|
+
var states = [st];
|
|
101
|
+
steps.forEach(function(s, j){
|
|
102
|
+
var n = {x: st.x, z: Object.assign({}, st.z), a: Object.assign({}, st.a), form: st.form.slice(), active: -1, focus: null, open: -1, note: s.note || ""};
|
|
103
|
+
if (s.show === "x") n.x = true;
|
|
104
|
+
if (s.layer !== undefined){
|
|
105
|
+
if (s.layer < 1 || s.layer >= nL) bad.push("steps[" + j + "].layer must be 1–" + (nL - 1));
|
|
106
|
+
else if (s.phase === "delta" || s.phase === "grad") bad.push("steps[" + j + "].phase \"" + s.phase + "\" (backpropagation) is reserved and not drawn yet");
|
|
107
|
+
else { n.x = true; n.active = s.layer; if (s.phase !== "a" || n.form[s.layer] === "combined") n.z[s.layer] = true; if (s.phase !== "z") { n.a[s.layer] = true; n.z[s.layer] = true; } }
|
|
108
|
+
}
|
|
109
|
+
if (s.open !== undefined){
|
|
110
|
+
if (s.open < 1 || s.open >= nL) bad.push("steps[" + j + "].open must be 1–" + (nL - 1));
|
|
111
|
+
else { n.form[s.open] = "split"; n.open = s.open; }
|
|
112
|
+
}
|
|
113
|
+
if (s.focus){
|
|
114
|
+
var fl = s.focus[0], fi = s.focus[1];
|
|
115
|
+
if (fl < 1 || fl >= nL || fi < 0 || fi >= L[fl].n) bad.push("steps[" + j + "].focus must be [layer 1–" + (nL - 1) + ", neuron 0–n−1]");
|
|
116
|
+
else if (!d.weights) bad.push("steps[" + j + "].focus needs weights");
|
|
117
|
+
else n.focus = [fl, fi];
|
|
118
|
+
}
|
|
119
|
+
states.push(n); st = n;
|
|
120
|
+
});
|
|
121
|
+
return (this._plan = {L: L, states: states, bad: bad, V: V});
|
|
122
|
+
}
|
|
123
|
+
fmt(v){
|
|
124
|
+
var dg = Math.max(0, Math.min(6, Math.round(this.props.digits))), s = Math.abs(v).toFixed(dg);
|
|
125
|
+
return (v < 0 && +s !== 0 ? "−" : "") + s;
|
|
126
|
+
}
|
|
127
|
+
mount(ctx){
|
|
128
|
+
var self = this, P = this.props, D = base, pl = this.plan(), d = this.data, L = pl.L, V = pl.V, ja = /^ja\b/i.test(ctx.lang || "ja");
|
|
129
|
+
this.style.display = "block"; this.style.position = "relative";
|
|
130
|
+
var W = this.clientWidth, H = this.clientHeight;
|
|
131
|
+
var svg = this.svg = D.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true", style: "display:block;overflow:visible"}, this);
|
|
132
|
+
if (!(P.dur >= 0)){ ctx.report("attr", "[nnet] dur must be ≥ 0; using 0.8"); P.dur = 0.8; }
|
|
133
|
+
if (!(P.size >= 24)){ ctx.report("attr", "[nnet] size must be ≥ 24; using 24"); P.size = 24; }
|
|
134
|
+
if (["plain", "weight"].indexOf(P.edges) < 0){ ctx.report("attr", "[nnet] edges must be plain or weight; using plain"); P.edges = "plain"; }
|
|
135
|
+
pl.bad.forEach(function(m){ ctx.report("data", "[nnet] " + m); });
|
|
136
|
+
var fs = P.size, tw = function(s){ return D.textWidth(s, fs, {font: self}); };
|
|
137
|
+
// ニューロンの大きさ:値の字(とΣ・関数の名前)が入る丸。分けた形は左右に 2 つ分
|
|
138
|
+
var all = [].concat(V.x || [], [].concat.apply([], V.z || []), [].concat.apply([], V.a || []));
|
|
139
|
+
var valW = Math.max(tw("−0." + "0".repeat(Math.max(0, Math.round(P.digits)))), ...all.map(function(v){ return tw(self.fmt(v)); }));
|
|
140
|
+
var actW = Math.max(tw("Σ"), ...L.map(function(ly){ return ly.act ? tw(ACT[ly.act][ja ? "ja" : "en"]) : 0; }));
|
|
141
|
+
var r = Math.ceil(Math.max(fs * 1.1, valW / 2 + 6)), halfW = Math.ceil(Math.max(valW, actW) + 14);
|
|
142
|
+
var gap = 8, capH = Math.ceil(fs * 1.6), labH = L.some(function(ly){ return ly.label; }) ? Math.ceil(fs * 1.6) : 0;
|
|
143
|
+
var noteH = pl.states.some(function(s){ return s.note; }) || pl.states.some(function(s){ return s.focus; }) ? Math.ceil(fs * 1.7) : 0;
|
|
144
|
+
var top = capH + 4, bottom = H - labH - noteH - 4, slotsMax = Math.max(1, Math.floor((bottom - top + gap) / (2 * r + gap)));
|
|
145
|
+
// 横:名前の列(入力の左・出力の右)を取ってから、層の列を等間隔に。分けた形にもなりうる層は幅を広く取る
|
|
146
|
+
var nameW = function(l){ return L[l].names ? Math.max.apply(null, L[l].names.map(function(s){ return tw(s); })) + 16 : 0; };
|
|
147
|
+
var leftW = nameW(0), rightW = nameW(L.length - 1);
|
|
148
|
+
var colW = L.map(function(ly, l){ var sp = pl.states.some(function(s){ return s.form[l] === "split"; }); return sp ? 2 * halfW : 2 * r; });
|
|
149
|
+
var span = W - leftW - rightW - colW[0] / 2 - colW[L.length - 1] / 2 - 8;
|
|
150
|
+
var xs = L.map(function(ly, l){ return leftW + colW[0] / 2 + 4 + (L.length > 1 ? span * l / (L.length - 1) : 0); });
|
|
151
|
+
for (var l2 = 1; l2 < L.length; l2++) if (xs[l2] - xs[l2 - 1] < (colW[l2] + colW[l2 - 1]) / 2 + 60){
|
|
152
|
+
ctx.report("layout", "[nnet] the layers are too close (" + Math.round(xs[l2] - xs[l2 - 1]) + " px between columns " + l2 + " and " + (l2 + 1) + "); use fewer layers, shorter names or a wider element"); break;
|
|
153
|
+
}
|
|
154
|
+
if (slotsMax < 2 || span < 0) ctx.report("size", "[nnet] the element (" + W + "×" + H + " px) is too small; give it about " + Math.ceil(top + 4 * (2 * r + gap) + labH + noteH + 8) + " px of height");
|
|
155
|
+
// 縦:見せるニューロン(多ければ先頭と末尾、間に ⋮)
|
|
156
|
+
this.cols = L.map(function(ly, l){
|
|
157
|
+
var n = ly.n, k = Math.min(n, 8, slotsMax), idx;
|
|
158
|
+
if (n <= k) idx = Array.from({length: n}, function(_, i){ return i; });
|
|
159
|
+
else { var head = Math.ceil((k - 1) / 2), tail = k - 1 - head; idx = Array.from({length: head}, function(_, i){ return i; }).concat([-1], Array.from({length: tail}, function(_, i){ return n - tail + i; })); }
|
|
160
|
+
var h = idx.length * 2 * r + (idx.length - 1) * gap, y0 = top + (bottom - top - h) / 2 + r;
|
|
161
|
+
return idx.map(function(i, j){ return {i: i, x: xs[l], y: y0 + j * (2 * r + gap)}; });
|
|
162
|
+
});
|
|
163
|
+
var gE = D.el("g", {}, svg), gN = D.el("g", {}, svg), gT = D.el("g", {}, svg);
|
|
164
|
+
// 辺
|
|
165
|
+
var maxW = d.weights ? Math.max.apply(null, [].concat.apply([], [].concat.apply([], d.weights)).map(Math.abs).concat([1e-9])) : 1;
|
|
166
|
+
this.edgeEls = [];
|
|
167
|
+
for (var l = 1; l < L.length; l++) this.cols[l].forEach(function(t){
|
|
168
|
+
if (t.i < 0) return;
|
|
169
|
+
self.cols[l - 1].forEach(function(s){
|
|
170
|
+
if (s.i < 0) return;
|
|
171
|
+
var w = d.weights ? d.weights[l - 1][t.i][s.i] : null, wt = P.edges === "weight" && w !== null;
|
|
172
|
+
var x1 = s.x + colHalf(l - 1), x2 = t.x - colHalf(l);
|
|
173
|
+
// data-part:Studio の形の検査(R-geometry)が測る印。ニューロンの枠(n<層>-<番号>)とその中の数・横の名前、層のあいだの線(辺)と重みの札(縁取りあり)
|
|
174
|
+
var eg = D.el("g", {"data-part": "edge", "data-part-id": "n" + (l - 1) + "-" + s.i + ">n" + l + "-" + t.i, "data-part-from": "n" + (l - 1) + "-" + s.i, "data-part-to": "n" + l + "-" + t.i}, gE);
|
|
175
|
+
var e = D.el("line", {"data-part-line": "", x1: x1.toFixed(1), y1: s.y.toFixed(1), x2: x2.toFixed(1), y2: t.y.toFixed(1),
|
|
176
|
+
stroke: wt ? (w < 0 ? "var(--emph)" : "var(--term)") : "var(--line)", "stroke-width": wt ? (1 + 5 * Math.abs(w) / maxW).toFixed(2) : 1.5,
|
|
177
|
+
"stroke-dasharray": wt && w < 0 ? "8 6" : "none", opacity: 0.35}, eg);
|
|
178
|
+
self.edgeEls.push({el: e, l: l, s: s, t: t, w: w});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
function colHalf(l){ return pl.states.some(function(s){ return s.form[l] === "split"; }) ? halfW : r; }
|
|
182
|
+
// ニューロン
|
|
183
|
+
this.neurons = this.cols.map(function(col, l){
|
|
184
|
+
return col.map(function(c){
|
|
185
|
+
if (c.i < 0){ var t = D.el("text", {x: c.x.toFixed(1), y: (c.y + fs * 0.36).toFixed(1), "font-size": fs, "text-anchor": "middle", fill: "var(--muted)"}, gN); t.textContent = "⋮"; return {dots: true}; }
|
|
186
|
+
var g = D.el("g", {}, gN);
|
|
187
|
+
var nid = "n" + l + "-" + c.i;
|
|
188
|
+
var body = D.el("rect", {y: (c.y - r).toFixed(1), height: 2 * r, rx: r, fill: "var(--bg)", stroke: "var(--fg)", "stroke-width": 2, "data-part": "box", "data-part-id": nid}, g);
|
|
189
|
+
var ring = D.el("rect", {y: (c.y - r - 4).toFixed(1), height: 2 * r + 8, rx: r + 4, fill: "none", stroke: "var(--accent2)", "stroke-width": 4, opacity: 0}, g);
|
|
190
|
+
var div = D.el("line", {x1: c.x, x2: c.x, y1: (c.y - r + 6).toFixed(1), y2: (c.y + r - 6).toFixed(1), stroke: "var(--fg)", "stroke-width": 2, opacity: 0}, g);
|
|
191
|
+
var mk = function(){ return D.el("text", {x: c.x, y: (c.y + fs * 0.36).toFixed(1), "font-size": fs, "text-anchor": "middle", fill: "var(--fg)", opacity: 0, style: "font-variant-numeric:tabular-nums", "data-part": "label", "data-part-id": nid, "data-part-of": "box"}, g); };
|
|
192
|
+
var o = {c: c, l: l, body: body, ring: ring, div: div, one: mk(), zT: mk(), aT: mk(), sig: mk(), fn: mk()};
|
|
193
|
+
o.sig.textContent = "Σ"; o.fn.textContent = L[l].act ? ACT[L[l].act][ja ? "ja" : "en"] : "f";
|
|
194
|
+
if (l === 0 && V.x) o.one.textContent = self.fmt(V.x[c.i]);
|
|
195
|
+
if (l > 0){
|
|
196
|
+
if (V.z) o.zT.textContent = self.fmt(V.z[l - 1][c.i]);
|
|
197
|
+
if (V.a) o.one.textContent = o.aT.textContent = self.fmt(V.a[l - 1][c.i]);
|
|
198
|
+
}
|
|
199
|
+
return o;
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
this.r = r; this.halfW = halfW;
|
|
203
|
+
// 名前(入力の左、出力の右)、層の名前(下)、分けた層の見出し(上:Σ → ReLU)
|
|
204
|
+
var names = function(l, side){
|
|
205
|
+
if (!L[l].names) return;
|
|
206
|
+
self.cols[l].forEach(function(c){ if (c.i < 0) return; D.mtext(gT, side < 0 ? c.x - colHalf(l) - 10 : c.x + colHalf(l) + 10, c.y + fs * 0.36, L[l].names[c.i], {"font-size": fs, "text-anchor": side < 0 ? "end" : "start", "data-part": "label", "data-part-id": "n" + l + "-" + c.i, "data-part-of": "box", "data-part-at": "outside"}); });
|
|
207
|
+
};
|
|
208
|
+
names(0, -1); if (L.length > 1) names(L.length - 1, 1);
|
|
209
|
+
L.forEach(function(ly, l){
|
|
210
|
+
if (ly.label){ var t = D.el("text", {x: xs[l].toFixed(1), y: (H - noteH - fs * 0.5).toFixed(1), "font-size": fs, "text-anchor": "middle", fill: "var(--muted)"}, gT); t.textContent = ly.label; }
|
|
211
|
+
});
|
|
212
|
+
this.caps = L.map(function(ly, l){
|
|
213
|
+
if (l === 0) return null;
|
|
214
|
+
var t = D.el("text", {x: xs[l].toFixed(1), y: (fs * 1.1).toFixed(1), "font-size": fs, "text-anchor": "middle", fill: "var(--muted)", opacity: 0}, gT);
|
|
215
|
+
t.textContent = "Σ → " + (ly.act ? ACT[ly.act][ja ? "ja" : "en"] : "f");
|
|
216
|
+
return t;
|
|
217
|
+
});
|
|
218
|
+
// 重みの札(focus)と下の行(説明・式)
|
|
219
|
+
this.wlabels = [];
|
|
220
|
+
if (d.weights) pl.states.forEach(function(s){
|
|
221
|
+
if (!s.focus) return;
|
|
222
|
+
var key = s.focus.join(":");
|
|
223
|
+
if (self.wlabels.some(function(x){ return x.key === key; })) return;
|
|
224
|
+
var tl = s.focus[0], ti = s.focus[1], tgt = self.cols[tl].filter(function(c){ return c.i === ti; })[0], g = D.el("g", {opacity: 0}, gT);
|
|
225
|
+
if (tgt) self.cols[tl - 1].forEach(function(src){
|
|
226
|
+
if (src.i < 0) return;
|
|
227
|
+
var w = d.weights[tl - 1][ti][src.i], x1 = src.x + colHalf(tl - 1), x2 = tgt.x - colHalf(tl), f = 0.22;
|
|
228
|
+
var x = x1 + (x2 - x1) * f, y = src.y + (tgt.y - src.y) * f - fs * 0.45;
|
|
229
|
+
var t = D.el("text", {x: x.toFixed(1), y: y.toFixed(1), "font-size": fs, "text-anchor": "middle", fill: w < 0 ? "var(--emph)" : "var(--term)", stroke: "var(--bg)", "stroke-width": 6, "paint-order": "stroke", style: "font-variant-numeric:tabular-nums", "data-part": "label", "data-part-id": "n" + (tl - 1) + "-" + src.i + ">n" + tl + "-" + ti, "data-part-of": "edge", "data-part-halo": ""}, g);
|
|
230
|
+
t.textContent = self.fmt(w);
|
|
231
|
+
});
|
|
232
|
+
self.wlabels.push({key: key, g: g, l: tl, i: ti});
|
|
233
|
+
});
|
|
234
|
+
this.noteText = noteH ? D.el("text", {x: 4, y: (H - fs * 0.5).toFixed(1), "font-size": fs, fill: "var(--fg)", opacity: 0}, gT) : null;
|
|
235
|
+
this.noteFor = function(s){
|
|
236
|
+
if (s.note) return s.note;
|
|
237
|
+
if (!s.focus) return "";
|
|
238
|
+
var tl = s.focus[0], ti = s.focus[1], prev = tl === 1 ? V.x : V.a && V.a[tl - 2], w = d.weights[tl - 1][ti];
|
|
239
|
+
if (!prev || !V.z) return "";
|
|
240
|
+
var terms = w.map(function(x, j){ return self.fmt(x) + "×" + self.fmt(prev[j]); }), b = d.biases ? d.biases[tl - 1][ti] : null;
|
|
241
|
+
var s2 = (terms.length > 4 ? terms.slice(0, 3).concat(["…"]) : terms).join(" + ") + (b !== null ? " + " + self.fmt(b) : "") + " = " + self.fmt(V.z[tl - 1][ti]);
|
|
242
|
+
return "z = " + s2.replace(/\+ −/g, "− ");
|
|
243
|
+
};
|
|
244
|
+
if (noteH){
|
|
245
|
+
var long = pl.states.map(function(s, k){ return tw(self.noteFor(s)) > W - 8 ? k : -1; }).filter(function(k){ return k >= 0; });
|
|
246
|
+
if (long.length) ctx.report("layout", "[nnet] the note of step " + long.join(", ") + " is wider than the element; shorten it (it is cut with …)");
|
|
247
|
+
}
|
|
248
|
+
this.fit = function(s){ if (tw(s) <= W - 8) return s; var a = Array.from(s); while (a.length && tw(a.join("") + "…") > W - 8) a.pop(); return a.join("") + "…"; };
|
|
249
|
+
if (this._aria === undefined) this._aria = this.getAttribute("aria-label");
|
|
250
|
+
this.setAttribute("role", "img");
|
|
251
|
+
if (!this._aria) this.setAttribute("aria-label", this.summary(ja));
|
|
252
|
+
this.model = {r: r, cols: this.cols.map(function(c){ return c.map(function(x){ return x.i; }); })};
|
|
253
|
+
}
|
|
254
|
+
summary(ja){
|
|
255
|
+
var pl = this.plan(), L = pl.L, V = pl.V, self = this;
|
|
256
|
+
var s = (ja ? "ニューラルネット(" : "Neural network (") + L.map(function(ly){ return ly.n; }).join("–") + (ja ? ")" : ")");
|
|
257
|
+
var lab = function(l, i){ return L[l].names ? L[l].names[i].replace(/[*_^]/g, "") : (ja ? "ニューロン " : "neuron ") + (i + 1); };
|
|
258
|
+
if (V.x) s += (ja ? "。入力:" : ". Input: ") + V.x.map(function(v, i){ return lab(0, i) + " " + self.fmt(v); }).join(ja ? "、" : ", ");
|
|
259
|
+
if (V.a) { var o = L.length - 1; s += (ja ? "。出力:" : ". Output: ") + V.a[o - 1].map(function(v, i){ return lab(o, i) + " " + self.fmt(v); }).join(ja ? "、" : ", "); }
|
|
260
|
+
return s;
|
|
261
|
+
}
|
|
262
|
+
duration(k){ return k >= 1 && k < this.plan().states.length ? this.props.dur : 0; }
|
|
263
|
+
render(k, t, ctx){
|
|
264
|
+
if (!this.neurons) return;
|
|
265
|
+
var self = this, pl = this.plan(), D = base, P = this.props, r = this.r, hw = this.halfW;
|
|
266
|
+
k = Math.max(0, Math.min(k, pl.states.length - 1));
|
|
267
|
+
var cur = pl.states[k], prev = pl.states[Math.max(0, k - 1)], e = k === 0 ? 1 : D.prog(t, 0, P.dur);
|
|
268
|
+
var mix = function(a, b){ return D.lerp(a ? 1 : 0, b ? 1 : 0, e); };
|
|
269
|
+
this.neurons.forEach(function(col, l){
|
|
270
|
+
col.forEach(function(o){
|
|
271
|
+
if (o.dots) return;
|
|
272
|
+
// 形:まとめた丸 ↔ 分けた形(open は幅を広げ、仕切りを出す)
|
|
273
|
+
var sp = mix(prev.form[l] === "split", cur.form[l] === "split"), w = D.lerp(2 * r, 2 * hw, sp), x = o.c.x;
|
|
274
|
+
o.body.setAttribute("x", (x - w / 2).toFixed(1)); o.body.setAttribute("width", w.toFixed(1)); o.body.setAttribute("rx", D.lerp(r, Math.min(r, 14), sp).toFixed(1));
|
|
275
|
+
o.ring.setAttribute("x", (x - w / 2 - 4).toFixed(1)); o.ring.setAttribute("width", (w + 8).toFixed(1)); o.ring.setAttribute("rx", D.lerp(r + 4, Math.min(r + 4, 18), sp).toFixed(1));
|
|
276
|
+
o.div.setAttribute("opacity", sp.toFixed(3));
|
|
277
|
+
var lx = x - w / 4, rx = x + w / 4;
|
|
278
|
+
o.zT.setAttribute("x", lx.toFixed(1)); o.sig.setAttribute("x", lx.toFixed(1)); o.aT.setAttribute("x", rx.toFixed(1)); o.fn.setAttribute("x", rx.toFixed(1));
|
|
279
|
+
var hasX = l === 0 ? mix(prev.x, cur.x) : 0;
|
|
280
|
+
var zOn = l > 0 ? mix(prev.z[l], cur.z[l]) : 0, aOn = l > 0 ? mix(prev.a[l], cur.a[l]) : 0;
|
|
281
|
+
// まとめた丸:入力は x、ほかは a。分けた形:左は Σ → z、右は関数の名前 → a
|
|
282
|
+
var one = l === 0 ? hasX : aOn * Math.max(0, 1 - 2 * sp);
|
|
283
|
+
o.one.setAttribute("opacity", (o.one.textContent ? one : 0).toFixed(3));
|
|
284
|
+
// 同じ場所の字は重ねない:前半で消し、後半で出す
|
|
285
|
+
var inn = function(v){ return Math.max(0, 2 * v - 1); }, out = function(v){ return Math.max(0, 1 - 2 * v); };
|
|
286
|
+
o.zT.setAttribute("opacity", (o.zT.textContent ? sp * inn(zOn) : 0).toFixed(3));
|
|
287
|
+
o.sig.setAttribute("opacity", (sp * out(zOn)).toFixed(3));
|
|
288
|
+
o.aT.setAttribute("opacity", (o.aT.textContent ? sp * inn(aOn) : 0).toFixed(3));
|
|
289
|
+
o.fn.setAttribute("opacity", (sp * out(aOn)).toFixed(3));
|
|
290
|
+
o.ring.setAttribute("opacity", (l === 0 ? hasX : Math.max(zOn * sp, aOn)).toFixed(3));
|
|
291
|
+
});
|
|
292
|
+
if (self.caps[l]) self.caps[l].setAttribute("opacity", mix(prev.form[l] === "split", cur.form[l] === "split").toFixed(3));
|
|
293
|
+
});
|
|
294
|
+
// 辺:今計算している層に入る辺は濃く、focus のときはそのニューロンに入る辺だけ濃く
|
|
295
|
+
this.edgeEls.forEach(function(x){
|
|
296
|
+
var lvl = function(s){ return s.focus ? (s.focus[0] === x.l && s.focus[1] === x.t.i ? 1 : 0.12) : s.active === x.l ? 1 : 0.35; };
|
|
297
|
+
x.el.setAttribute("opacity", D.lerp(k === 0 ? lvl(cur) : lvl(prev), lvl(cur), e).toFixed(3));
|
|
298
|
+
});
|
|
299
|
+
this.wlabels.forEach(function(x){
|
|
300
|
+
var on = function(s){ return s.focus && s.focus[0] === x.l && s.focus[1] === x.i; };
|
|
301
|
+
x.g.setAttribute("opacity", mix(on(prev) && k > 0, on(cur)).toFixed(3));
|
|
302
|
+
});
|
|
303
|
+
if (this.noteText){
|
|
304
|
+
var n = this.noteFor(cur);
|
|
305
|
+
this.noteText.textContent = n ? this.fit(n) : "";
|
|
306
|
+
this.noteText.setAttribute("opacity", n ? (k === 0 ? 1 : D.prog(t, 0, P.dur * 0.5)).toFixed(3) : 0);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Waterfall: a baseline bar, then increases and decreases stacked from the previous bar's end, then a total bar
|
|
3
|
+
steps: max item step (default 1 bar = 1 step)
|
|
4
|
+
props:
|
|
5
|
+
unit: value unit
|
|
6
|
+
digits: decimals
|
|
7
|
+
per-step: bars per step (0 = all)
|
|
8
|
+
dur: grow seconds
|
|
9
|
+
stagger: bar offset (s)
|
|
10
|
+
min: axis start (0 = from 0; > 0 cuts the axis, with a break mark)
|
|
11
|
+
ticks: ticks and grid
|
|
12
|
+
links: dotted links between bars
|
|
13
|
+
al: axis title
|
|
14
|
+
example: {"items": [{"label": "Last year", "value": 120, "total": true}, {"label": "Sales", "value": 45}, {"label": "Costs", "value": -30}, {"label": "This year", "total": true}]}
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
For what was added and taken away between a baseline and a total: profit bridges, budget against actual, population change.
|
|
18
|
+
|
|
19
|
+
Child JSON: `{"items": [...]}`.
|
|
20
|
+
|
|
21
|
+
- item: `label` (`\n` = 2 lines), `value`, `total` (true = a total bar from 0), `tone`, `step`, `at`
|
|
22
|
+
- The first total's `value` is the baseline; change bars start at the running total; a later total's `value` is checked (data)
|
|
23
|
+
- `min`: axis start for small changes on large values (break mark)
|
|
24
|
+
<!-- llm: omit -->
|
|
25
|
+
- `min` works when every bar end and total is ≥ `min`; total bars then rise from the axis bottom
|
|
26
|
+
<!-- llm: end -->
|
|
27
|
+
<!-- llm: omit -->
|
|
28
|
+
- Colours: totals `--term`, increases `--accent2`, decreases `--emph`; values sit above each bar's upper end, changes with `+` or `−` (direction is not shown by colour alone). Dotted links (`--muted`, `links="false"` to hide) join the bars at the running total.
|
|
29
|
+
- The preview view draws later bars, values and links fully grown at `--preview-opacity`.
|
|
30
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// m-diverge:ウォーターフォール。基準の棒から始め、増減を前の棒の端から上下に積み、最後に合計の棒を立てる
|
|
2
|
+
// <m-diverge unit="億円" style="height:420px"><script type="application/json">
|
|
3
|
+
// {"items": [{"label": "前年の利益", "value": 120, "total": true},
|
|
4
|
+
// {"label": "売上の増加", "value": 45}, {"label": "原価の増加", "value": -30},
|
|
5
|
+
// {"label": "今年の利益", "total": true}]}
|
|
6
|
+
// (JSON の終わり)</m-diverge>
|
|
7
|
+
// 合計の棒(total: true)は 0 から、増減の棒は前の棒の端(それまでの累計)から伸びる。最初の合計の棒の value が基準。
|
|
8
|
+
// 後の合計の棒は累計から決め、value を書いたら累計と合うかを確かめる(合わなければ data で知らせ、累計を描く)
|
|
9
|
+
// 色:合計 --term、増加 --accent2、減少 --emph(item の tone で変えられる)。値の数字は棒の端の外(増加と合計は上、減少は下)に、
|
|
10
|
+
// 増減には符号を付けて書く。隣の棒とは累計の高さで点線をつなぐ
|
|
11
|
+
// ステップ:既定は棒 1 本 = 1 ステップ(per-step で n 本ずつ、item の step で直接)。軸・目盛り・棒の名前はステップ 0 から見える
|
|
12
|
+
egatariParts.define({name: "diverge", version: "2.0.1", requires: {base: "1.3.0"}, element: "m-diverge"}, function(deps){
|
|
13
|
+
"use strict";
|
|
14
|
+
var base = deps.base;
|
|
15
|
+
return class extends base.MotionElement {
|
|
16
|
+
static partName = "diverge";
|
|
17
|
+
static shape = {
|
|
18
|
+
schema: {type: "object", additionalProperties: false, required: ["items"], properties: {
|
|
19
|
+
items: {type: "array", minItems: 1, items: {type: "object", required: ["label"], properties: {
|
|
20
|
+
label: {type: "string"}, value: {type: "number"}, total: {type: "boolean"}, tone: {type: "string"}, step: {type: "integer"}, at: {type: "number"}}}}}},
|
|
21
|
+
example: {items: [{label: "前年", value: 120, total: true}, {label: "増加", value: 45}, {label: "減少", value: -30}, {label: "今年", total: true}]},
|
|
22
|
+
};
|
|
23
|
+
static props = {
|
|
24
|
+
unit: {type: "string", default: "", doc: "値の単位(数字の後ろに付ける。億円、人、% など)"},
|
|
25
|
+
digits: {type: "number", default: 0, doc: "値の小数桁"},
|
|
26
|
+
"per-step": {type: "number", default: 1, doc: "1 ステップに出す棒の数(0 = 全部をステップ 1 で)"},
|
|
27
|
+
dur: {type: "number", default: 0.8, doc: "1 本が伸びる秒数"},
|
|
28
|
+
stagger: {type: "number", default: 0.3, doc: "同じステップの棒の出だしのずれ(秒)"},
|
|
29
|
+
min: {type: "number", default: 0, doc: "値の軸の始まり(0 = 0 から。正の値なら軸をそこから始め、切れ目の印を付ける。棒の端がすべてそれ以上のときだけ)"},
|
|
30
|
+
ticks: {type: "bool", default: true, doc: "値の軸の目盛りと格子"},
|
|
31
|
+
links: {type: "bool", default: true, doc: "隣の棒を累計の高さの点線でつなぐ"},
|
|
32
|
+
al: {type: "string", default: "", doc: "値の軸の名前"},
|
|
33
|
+
};
|
|
34
|
+
get steps(){
|
|
35
|
+
if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
|
|
36
|
+
var m = 0;
|
|
37
|
+
this.plan().bars.forEach(function(b){ m = Math.max(m, b.step); });
|
|
38
|
+
return m;
|
|
39
|
+
}
|
|
40
|
+
// 棒ごとの始め・終わり(累計)、ステップと開始時刻。データだけから決める(mount の前にも steps が要る)
|
|
41
|
+
plan(){
|
|
42
|
+
if (this._plan) return this._plan;
|
|
43
|
+
var d = this.data || {}, P = this.props, per = P["per-step"], seen = {}, run = 0, bad = [];
|
|
44
|
+
var bars = (d.items || []).map(function(it, i){
|
|
45
|
+
var step = it.step !== undefined ? it.step : per > 0 ? Math.floor(i / per) + 1 : 1;
|
|
46
|
+
var j = seen[step] = (seen[step] || 0) + 1, at = it.at !== undefined ? it.at : +((j - 1) * P.stagger).toFixed(3);
|
|
47
|
+
var b = {it: it, i: i, step: step, at: at, total: !!it.total};
|
|
48
|
+
if (b.total){
|
|
49
|
+
// 最初の棒か、それまでに増減がない合計の棒は value が基準になる。それ以外は累計(value は確かめに使う)
|
|
50
|
+
var given = typeof it.value === "number" && isFinite(it.value);
|
|
51
|
+
if (given && i > 0 && Math.abs(it.value - run) > 1e-6 * Math.max(1, Math.abs(run))) bad.push({label: it.label, given: it.value, run: run});
|
|
52
|
+
if (given && i === 0) run = it.value;
|
|
53
|
+
b.from = 0; b.to = run; b.v = run;
|
|
54
|
+
} else {
|
|
55
|
+
var v = +it.value || 0;
|
|
56
|
+
b.from = run; b.to = run + v; b.v = v; run = b.to;
|
|
57
|
+
}
|
|
58
|
+
return b;
|
|
59
|
+
});
|
|
60
|
+
return (this._plan = {bars: bars, bad: bad});
|
|
61
|
+
}
|
|
62
|
+
mount(ctx){
|
|
63
|
+
var P = this.props, D = base, self = this, pl = this.plan();
|
|
64
|
+
this.style.display = "block"; this.style.position = "relative";
|
|
65
|
+
var W = this.clientWidth, H = this.clientHeight;
|
|
66
|
+
var svg = D.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true", style: "display:block;overflow:visible"}, this);
|
|
67
|
+
if (!pl.bars.length){ ctx.report("data", "[diverge] items is empty"); return; }
|
|
68
|
+
if (pl.bars[0].total && typeof pl.bars[0].it.value !== "number") ctx.report("data", "[diverge] the first total bar needs a value (the baseline)");
|
|
69
|
+
pl.bad.forEach(function(x){ ctx.report("data", "[diverge] the total \"" + x.label + "\" is " + x.given + " but the running total is " + +x.run.toFixed(6) + "; drawing the running total"); });
|
|
70
|
+
var est = function(s, size, mk){ return D.textWidth(s, size, {font: self, mark: !!mk}); };
|
|
71
|
+
// 値の軸:棒の端(0 を含む)から。上は値の数字の分、下は負の棒の数字の分をあける
|
|
72
|
+
var ends = [0];
|
|
73
|
+
pl.bars.forEach(function(b){ ends.push(b.from, b.to); });
|
|
74
|
+
var hi = Math.max.apply(null, ends), lo = Math.min.apply(null, ends);
|
|
75
|
+
// min(正):軸をそこから始める(合計の棒は軸の下の端から)。増減の棒の端と合計がすべて min 以上のときだけ
|
|
76
|
+
var cut = 0;
|
|
77
|
+
if (P.min > 0){
|
|
78
|
+
var low = Math.min.apply(null, pl.bars.map(function(b){ return b.total ? b.to : Math.min(b.from, b.to); }));
|
|
79
|
+
if (low < P.min) ctx.report("data", "[diverge] min " + P.min + " is above the lowest bar end (" + +low.toFixed(6) + "); the axis starts at 0");
|
|
80
|
+
else { cut = P.min; lo = cut; }
|
|
81
|
+
}
|
|
82
|
+
this.cut = cut;
|
|
83
|
+
var lines = Math.max.apply(null, pl.bars.map(function(b){ return String(b.it.label || "").split("\n").length; }).concat([1]));
|
|
84
|
+
var m = {l: 16, r: 16, t: 44, b: 18 + 30 * lines};
|
|
85
|
+
// 目盛りの字の幅は軸が決まってから測るので、ここでは大きめの見積もりで決め、後で広げる
|
|
86
|
+
this.y0 = m.t; this.y1 = H - m.b;
|
|
87
|
+
var span = Math.max(1, this.y1 - this.y0), room = Math.min(0.3, 40 / span);
|
|
88
|
+
var top = hi > 0 ? nice(hi / (1 - room)) : 0, bot = lo < 0 ? -nice(-lo / (1 - room)) : 0;
|
|
89
|
+
if (cut){ bot = cut; top = cut + nice(Math.max(hi - cut, 1e-9) / (1 - room)); }
|
|
90
|
+
if (top === 0 && bot === 0) top = 1;
|
|
91
|
+
this.top = top; this.bot = bot;
|
|
92
|
+
var st = tickStep(top - bot), dec = decs(st);
|
|
93
|
+
var tickVals = [];
|
|
94
|
+
if (cut) tickVals.push(cut);
|
|
95
|
+
for (var v = Math.ceil(bot / st - 1e-9) * st; v <= top + 1e-9; v += st) if (!cut || v > cut + st * 0.4) tickVals.push(+v.toFixed(8));
|
|
96
|
+
var tickW = Math.max.apply(null, tickVals.map(function(x){ return est(self.fmt(x, dec, ""), 24); }).concat([0]));
|
|
97
|
+
if (P.ticks) m.l = 24 + tickW + (P.al ? 36 : 0);
|
|
98
|
+
this.x0 = m.l; this.x1 = W - m.r;
|
|
99
|
+
if (this.x1 < this.x0 || this.y1 < this.y0){
|
|
100
|
+
ctx.report("size", "[diverge] the element (" + W + "×" + H + " px) is too small for the bars; give it a size, e.g. style=\"height:420px\"");
|
|
101
|
+
this.x1 = Math.max(this.x0, this.x1); this.y1 = Math.max(this.y0, this.y1);
|
|
102
|
+
}
|
|
103
|
+
var y = this.y = function(v){ return self.y1 - (v - bot) / (top - bot) * (self.y1 - self.y0); };
|
|
104
|
+
var gT = D.el("g", {}, svg);
|
|
105
|
+
if (P.ticks) tickVals.forEach(function(v){
|
|
106
|
+
if (Math.abs(v - (cut || 0)) > 1e-9) D.el("line", {x1: self.x0, x2: self.x1, y1: y(v).toFixed(1), y2: y(v).toFixed(1), stroke: "var(--line)", "stroke-width": 1.2, opacity: 0.8}, gT);
|
|
107
|
+
var tx = D.el("text", {x: self.x0 - 10, y: (y(v) + 8).toFixed(1), "font-size": 24, "text-anchor": "end", style: "font-variant-numeric:tabular-nums"}, gT);
|
|
108
|
+
tx.textContent = self.fmt(v, dec, "");
|
|
109
|
+
});
|
|
110
|
+
if (P.al) D.mtext(svg, 20, (this.y0 + this.y1) / 2, P.al, {"text-anchor": "middle", fill: "var(--muted)", transform: "rotate(-90 20 " + (this.y0 + this.y1) / 2 + ")"});
|
|
111
|
+
var gK = D.el("g", {}, svg), gB = D.el("g", {}, svg), gL = D.el("g", {}, svg);
|
|
112
|
+
var yb0 = y(cut || 0);
|
|
113
|
+
D.el("line", {x1: this.x0, x2: this.x1, y1: yb0.toFixed(1), y2: yb0.toFixed(1), stroke: "var(--fg)", "stroke-width": 1.5}, svg);
|
|
114
|
+
// 軸が 0 から始まらないときの切れ目の印:基線の左の端に斜めの 2 本
|
|
115
|
+
if (cut) [-5, 5].forEach(function(dx){ D.el("line", {x1: (self.x0 + 8 + dx - 6).toFixed(1), y1: (yb0 + 10).toFixed(1), x2: (self.x0 + 8 + dx + 6).toFixed(1), y2: (yb0 - 10).toFixed(1), stroke: "var(--fg)", "stroke-width": 2}, svg); });
|
|
116
|
+
var n = pl.bars.length, slot = Math.max(0, this.x1 - this.x0) / n, bw = Math.min(110, slot * 0.64);
|
|
117
|
+
this.bw = bw;
|
|
118
|
+
this.items = pl.bars.map(function(b, i){
|
|
119
|
+
var c = self.x0 + slot * (i + 0.5);
|
|
120
|
+
String(b.it.label || "").split("\n").forEach(function(s, j){ D.mtext(gL, c, self.y1 + 34 + j * 30, s, {"text-anchor": "middle", "font-size": 26}); });
|
|
121
|
+
var col = tone(b.it.tone || (b.total ? "term" : b.v < 0 ? "emph" : "accent2"));
|
|
122
|
+
var o = {b: b, c: c, col: col, rect: D.el("rect", {rx: 3, fill: col, opacity: 0}, gB),
|
|
123
|
+
val: D.el("text", {"font-size": 26, "font-weight": 700, "text-anchor": "middle", fill: b.total ? "var(--fg)" : col, opacity: 0, style: "font-variant-numeric:tabular-nums"}, gL)};
|
|
124
|
+
o.val.textContent = b.total ? self.fmt(b.v) : (b.v < 0 ? "−" : "+") + self.fmt(Math.abs(b.v));
|
|
125
|
+
// 前の棒の端(累計)から、この棒の左の辺までの点線
|
|
126
|
+
if (i > 0 && P.links) o.link = D.el("line", {stroke: "var(--muted)", "stroke-width": 2, "stroke-dasharray": "6 5", opacity: 0}, gK);
|
|
127
|
+
return o;
|
|
128
|
+
});
|
|
129
|
+
if (this._aria === undefined) this._aria = this.getAttribute("aria-label");
|
|
130
|
+
this.setAttribute("role", "img");
|
|
131
|
+
if (!this._aria) this.setAttribute("aria-label", this.summary(/^ja\b/i.test(ctx.lang || "ja")));
|
|
132
|
+
function nice(v){ var e = Math.pow(10, Math.floor(Math.log10(v))), x = v / e;
|
|
133
|
+
return [1, 1.2, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10].filter(function(s){ return s >= x - 1e-9; })[0] * e; }
|
|
134
|
+
function tickStep(M){ var r = M / 4.5, e = Math.pow(10, Math.floor(Math.log10(r))), x = r / e;
|
|
135
|
+
return (x <= 1 ? 1 : x <= 2 ? 2 : x <= 2.5 ? 2.5 : x <= 5 ? 5 : 10) * e; }
|
|
136
|
+
function decs(s){ var k = 0; while (Math.abs(s * Math.pow(10, k) - Math.round(s * Math.pow(10, k))) > 1e-6 && k < 6) k++; return k; }
|
|
137
|
+
function tone(t){ return /^(term|emph|muted|accent2|fg|series-[1-4])$/.test(t) ? "var(--" + t + ")" : "var(--term)"; }
|
|
138
|
+
}
|
|
139
|
+
// 読み上げ:「前年の利益 120億円、売上の増加 +45億円、原価の増加 −30億円、今年の利益 135億円」。棒が 16 本より多ければ初めの 16 本と本数
|
|
140
|
+
summary(ja){
|
|
141
|
+
var self = this, P = this.props, bars = this.plan().bars;
|
|
142
|
+
var head = P.al || (ja ? "増減の内訳" : "Changes");
|
|
143
|
+
if (P.unit) head += ja ? "(" + P.unit + ")" : " (" + P.unit + ")";
|
|
144
|
+
var gs = bars.slice(0, 16).map(function(b){
|
|
145
|
+
var lab = String(b.it.label || "").replace(/\s*\n\s*/g, " ");
|
|
146
|
+
return (lab ? lab + " " : "") + (b.total ? self.fmt(b.v) : (b.v < 0 ? "−" : "+") + self.fmt(Math.abs(b.v)));
|
|
147
|
+
});
|
|
148
|
+
if (bars.length > 16) gs.push(ja ? "…(全 " + bars.length + " 本)" : "… (" + bars.length + " bars)");
|
|
149
|
+
return head + (ja ? ":" : ": ") + gs.join(ja ? "、" : "; ");
|
|
150
|
+
}
|
|
151
|
+
fmt(v, digits, unit){
|
|
152
|
+
var dg = digits !== undefined ? digits : this.props.digits;
|
|
153
|
+
var s = Math.abs(v).toFixed(dg).split("."), i = s[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
154
|
+
var neg = v < 0 && +Math.abs(v).toFixed(dg) !== 0;
|
|
155
|
+
return (neg ? "−" : "") + i + (s[1] ? "." + s[1] : "") + (unit !== undefined ? unit : this.props.unit);
|
|
156
|
+
}
|
|
157
|
+
duration(k){
|
|
158
|
+
var P = this.props, d = 0;
|
|
159
|
+
this.plan().bars.forEach(function(b){ if (b.step === k) d = Math.max(d, b.at + P.dur + 0.35); });
|
|
160
|
+
return d;
|
|
161
|
+
}
|
|
162
|
+
render(k, t, ctx){
|
|
163
|
+
if (!this.items) return;
|
|
164
|
+
var P = this.props, D = base, self = this, y = this.y, w = this.bw;
|
|
165
|
+
var preview = !!ctx.preview, PV = preview ? base.previewOpacity(this) : 1;
|
|
166
|
+
this.items.forEach(function(o, i){
|
|
167
|
+
var b = o.b, ahead = preview && b.step > k;
|
|
168
|
+
var e = b.step < k || ahead ? 1 : b.step > k ? 0 : D.prog(t, b.at, P.dur);
|
|
169
|
+
// 合計は 0 から、増減は前の累計から伸びる
|
|
170
|
+
var a = b.total ? self.cut : b.from, z = a + (b.to - a) * e, ya = y(a), yz = y(z);
|
|
171
|
+
o.rect.setAttribute("x", (o.c - w / 2).toFixed(1)); o.rect.setAttribute("width", w.toFixed(1));
|
|
172
|
+
o.rect.setAttribute("y", Math.min(ya, yz).toFixed(1)); o.rect.setAttribute("height", Math.abs(yz - ya).toFixed(1));
|
|
173
|
+
o.rect.setAttribute("opacity", ahead ? PV : e > 0 ? 1 : 0);
|
|
174
|
+
var vo = ahead ? PV : b.step < k ? 1 : b.step > k ? 0 : D.prog(t, b.at + P.dur, 0.35);
|
|
175
|
+
// 値の数字は棒の上の端の上(減少も:下の端は次の棒へつなぐ点線と基線の側なので空けておく)。
|
|
176
|
+
// 棒がすべて 0 より下にあるときだけ、下の端の下
|
|
177
|
+
var ye = y(b.to), yb = y(b.total ? self.cut : b.from), under = Math.max(b.from, b.to) <= 0 && Math.min(b.from, b.to) < 0;
|
|
178
|
+
var yt = under ? Math.max(ye, yb) + 32 : Math.min(ye, yb) - 12;
|
|
179
|
+
o.val.setAttribute("x", o.c.toFixed(1)); o.val.setAttribute("y", yt.toFixed(1));
|
|
180
|
+
o.val.setAttribute("opacity", vo.toFixed(3));
|
|
181
|
+
if (o.link){
|
|
182
|
+
// 点線は前の棒の右の辺からこの棒の左の辺まで、前の棒の終わりの高さ(= この棒の始まり、合計なら同じ累計)で
|
|
183
|
+
var p = self.items[i - 1], lv = y(p.b.to);
|
|
184
|
+
o.link.setAttribute("x1", (p.c + w / 2).toFixed(1)); o.link.setAttribute("x2", (o.c - w / 2).toFixed(1));
|
|
185
|
+
o.link.setAttribute("y1", lv.toFixed(1)); o.link.setAttribute("y2", lv.toFixed(1));
|
|
186
|
+
var lo = ahead ? PV : b.step < k ? 1 : b.step > k ? 0 : D.prog(t, b.at, 0.3);
|
|
187
|
+
o.link.setAttribute("opacity", lo.toFixed(3));
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Contours of f(x, y) by level, gradient arrows, a descent path by point
|
|
3
|
+
steps: levels (all: 1) → gradient → path points
|
|
4
|
+
props:
|
|
5
|
+
dur: s per step
|
|
6
|
+
size: text px
|
|
7
|
+
by: level or all
|
|
8
|
+
example: {"f":"x**2 + 3*y**2","range":{"x":[-3,3],"y":[-2,2]},"levels":[1,2,4,8],"gradient":{"at":[[1,1]]},"path":[[2.5,1.5],[1.5,0.3],[0.9,0.06]],"monotone":true}
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`f` (arithmetic in x, y) or a script's `grid`; `levels` (≤8), `gradient.at`, `path` from a script. Size: with `equal` and a square range, about 740 px wide for 440 px high (a panel on the right); to fill the width, widen x or set `equal: false`.
|
|
12
|
+
<!-- llm: omit -->
|
|
13
|
+
- `range` {x, y}; `equal` (default true). `grid.values` row 0 = min y.
|
|
14
|
+
- Level labels on the lines. `gradient`: `scale`, `tangent`. `path` ≤30 points; `monotone` reports a rise.
|
|
15
|
+
- Colours: contours term, gradient accent2, path fg, current point emph. No preview of later steps.
|
|
16
|
+
<!-- llm: end -->
|
|
17
|
+
|
|
18
|
+
Demo: `demo.html`.
|
|
19
|
+
|
|
20
|
+
<!-- llm: omit -->
|
|
21
|
+
Notes
|
|
22
|
+
|
|
23
|
+
- A formula f is sampled on 97 × 73; pass narrow valleys as `grid`.
|
|
24
|
+
<!-- llm: end -->
|