egatari 0.6.18 → 0.6.20-rc.1

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +33 -1
  2. package/README.ja.md +7 -2
  3. package/README.md +7 -2
  4. package/components.lock.json +40 -12
  5. package/components2.lock.json +534 -1
  6. package/kit/build.mjs +1 -1
  7. package/kit/check-messages.mjs +4 -0
  8. package/kit/check.mjs +6 -2
  9. package/kit/core/bookmark.mjs +13 -0
  10. package/kit/core/build-core.mjs +1 -1
  11. package/kit/core/check-parity.mjs +2 -0
  12. package/kit/core/check-rules.mjs +1 -1
  13. package/kit/core/check-source.mjs +4 -1
  14. package/kit/core/checks.mjs +79 -0
  15. package/kit/core/find-replace.mjs +170 -0
  16. package/kit/core/format.mjs +4 -1
  17. package/kit/core/image-insert.mjs +197 -0
  18. package/kit/core/image-size.mjs +1 -1
  19. package/kit/core/layout-scan.mjs +1 -1
  20. package/kit/core/llm-fix.mjs +1 -1
  21. package/kit/core/llm-pack-assemble.mjs +17 -10
  22. package/kit/core/llm-pack-data.mjs +94 -30
  23. package/kit/core/llm-pack.mjs +2 -1
  24. package/kit/core/part-labels.mjs +1 -0
  25. package/kit/core/portable.mjs +5 -2
  26. package/kit/core/scorm.mjs +30 -6
  27. package/kit/core/script-unclosed.mjs +66 -0
  28. package/kit/core/slide-ops.mjs +5 -1
  29. package/kit/core/template-text.mjs +2 -0
  30. package/kit/deck.js +71 -4
  31. package/kit/instruction-version.json +1 -1
  32. package/kit/layouts.css +8 -0
  33. package/kit/panes.js +17 -1
  34. package/kit/player.json +1 -1
  35. package/kit/player.lock.json +2 -2
  36. package/kit/templates/new-deck.mjs +1 -1
  37. package/kit/tools.js +136 -4
  38. package/kit/track.js +46 -5
  39. package/package.json +1 -1
  40. package/server/editor-ui.js +21 -1
  41. package/server/script-field.js +1 -1
  42. package/vendor/components/CATALOG.md +5 -4
  43. package/vendor/components/sha256/15306d854d67d52ca2b1d66135f0d3296882e6b21fd85c029399b8077c8a7f9d/README.en.md +34 -0
  44. package/vendor/components/sha256/15306d854d67d52ca2b1d66135f0d3296882e6b21fd85c029399b8077c8a7f9d/index.js +398 -0
  45. package/vendor/components/sha256/277ac772315f93737a094cd0f0842726f2435d352d9a90822aebfe0d5d7e79a1/README.en.md +23 -0
  46. package/vendor/components/sha256/277ac772315f93737a094cd0f0842726f2435d352d9a90822aebfe0d5d7e79a1/index.js +294 -0
  47. package/vendor/components/sha256/3be5f64deb1951327b614b29a9b632878fab362687c76e3f48f3af1d1ebbb1cf/README.en.md +47 -0
  48. package/vendor/components/sha256/3be5f64deb1951327b614b29a9b632878fab362687c76e3f48f3af1d1ebbb1cf/index.js +223 -0
  49. package/vendor/components/sha256/7c7b0373cfd38fd5ecb4169021e0b908f0a17be1a29b14f85dd89fdb92e7bf81/README.en.md +44 -0
  50. package/vendor/components/sha256/7c7b0373cfd38fd5ecb4169021e0b908f0a17be1a29b14f85dd89fdb92e7bf81/index.js +306 -0
  51. package/vendor/components/sha256/8a9215c7fea15265c2f3038f3d01a2f72441fc1e931245dee46dcaacf3548a98/README.en.md +38 -0
  52. package/vendor/components/sha256/8a9215c7fea15265c2f3038f3d01a2f72441fc1e931245dee46dcaacf3548a98/index.js +414 -0
  53. package/vendor/components/sha256/91be5c714c046244436569ecac24d9d1eb84504b3e28d8899fc89a5865e8d955/README.en.md +40 -0
  54. package/vendor/components/sha256/91be5c714c046244436569ecac24d9d1eb84504b3e28d8899fc89a5865e8d955/index.js +355 -0
  55. package/vendor/components/sha256/939bdae590ebed3d182b0c9940a35b6c03872505bfa242341990a81cae02fd06/README.en.md +34 -0
  56. package/vendor/components/sha256/939bdae590ebed3d182b0c9940a35b6c03872505bfa242341990a81cae02fd06/index.js +397 -0
  57. package/vendor/components/sha256/9a426812105a44446e8b3f98e0ccfdb6701e8386c08e8aa201b69cb5b52a4c1f/index.js +278 -0
  58. package/vendor/components/sha256/9c5dd0bc0c3279c5f0fea3fdb9d0735870880aef96e8bbf9ecdd77dff0f5ddc8/README.en.md +44 -0
  59. package/vendor/components/sha256/9c5dd0bc0c3279c5f0fea3fdb9d0735870880aef96e8bbf9ecdd77dff0f5ddc8/index.js +551 -0
  60. package/vendor/components/sha256/a5b032657720d3beb64f0b3699954e13b0eb985f36161b9ae046c83fd6048ff5/README.en.md +18 -0
  61. package/vendor/components/sha256/a5b032657720d3beb64f0b3699954e13b0eb985f36161b9ae046c83fd6048ff5/index.js +382 -0
  62. package/vendor/components/sha256/ad4d42a34eff125d8ccd7caf9ac329b7a8092960e7ee872fdeb853bd9c57942f/README.en.md +54 -0
  63. package/vendor/components/sha256/ad4d42a34eff125d8ccd7caf9ac329b7a8092960e7ee872fdeb853bd9c57942f/index.js +470 -0
  64. package/vendor/components/sha256/b35d01e480a203dc27aa18b0d341cc1cbc2dcca69235e909919484f1545b371f/README.en.md +23 -0
  65. package/vendor/components/sha256/b35d01e480a203dc27aa18b0d341cc1cbc2dcca69235e909919484f1545b371f/index.js +150 -0
  66. package/vendor/components-2/manifest-2.20.0.json +3217 -0
  67. package/vendor/components-2/manifest-2.21.0.json +3220 -0
  68. package/vendor/components-2/manifest-2.22.0.json +3240 -0
  69. package/vendor/egatari-spec/SOURCE.json +2 -2
  70. package/vendor/egatari-spec/schema/deck-meta.schema.json +3 -0
  71. package/vendor/egatari-spec/schema/egatari-metadata.schema.json +6 -0
@@ -0,0 +1,294 @@
1
+ // m-contrast 2.0.0: a contrast figure, earlier work against this work, for positioning a study (handoff #71).
2
+ // kind "answers" (default): questions and their answers. A row per question (1 to 3): the question on the left, the
3
+ // earlier answer in the middle ({"text", "by"}, or {"unknown": true, "near"}: a dashed box with 不明 / Unknown and what is
4
+ // known under nearby conditions), an arrow, and this work's answer on the right with its basis, framed in --emph.
5
+ // Columns are headed 問い / 従来 / 本研究 (heads, selfLabel). Steps (order "build"): the questions → the earlier
6
+ // answers → the arrows and this work's answers; order "row": a row per step; order "whole": everything at once.
7
+ // kind "assumptions": the assumptions of near studies (2 to 3 rows of chips, each with what it yields), then this work's
8
+ // row: the assumptions it keeps, those it drops (written 外した / dropped, struck through) and those it adds (written
9
+ // 加えた / added, in --emph), and what it newly yields. Steps: the studies → this work's row with what it keeps and
10
+ // drops → what it adds → its result (order "whole": at once).
11
+ // This work is written out (selfLabel, default 本研究 / This work) and framed, never colour alone. The readable table
12
+ // (data-readable) gives the final state. Boxes and chips are data-part boxes and their texts labels (Studio's geometry
13
+ // check). Text is size px (≥ 24); when the figure does not fit at 24 px the part reports size with what it needs.
14
+ // Colours: earlier boxes --muted, unknown dashed --muted, this work --emph, chips --term, lines --line, notes --muted.
15
+ egatariParts.define({name: "contrast", version: "2.0.0", requires: {base: "1.3.0"}, element: "m-contrast"}, function(deps){
16
+ "use strict";
17
+ var base = deps.base;
18
+ var T = {ja: {self: "本研究", answers: ["問い", "従来", null], assumptions: ["研究", "仮定", "得られること"], unknown: "不明",
19
+ dropped: "外した", added: "加えた", sep: ":", label: "対比の図:", list: "、"},
20
+ en: {self: "This work", answers: ["Question", "Earlier", null], assumptions: ["Study", "Assumptions", "Yields"], unknown: "Unknown",
21
+ dropped: "dropped", added: "added", sep: ": ", label: "Contrast: ", list: ", "}};
22
+ var STR = {type: "string"}, STRS = {type: "array", items: STR};
23
+ // lines no wider than maxW by measure (as m-matrix 2.1): "\n" breaks; Latin runs, "7 割" and 、。)」 stay together;
24
+ // a break after 、。 late in the line, or after hiragana before a kanji, is preferred
25
+ var TOKEN = /[A-Za-z0-9.,%+\-\/]+(?: [^\sA-Za-z0-9、。,.)」])?[、。,.)」]*|\S[、。,.・)」]*|\s+/g;
26
+ var HIRA = /[ぁ-ゟ]/, WORD = /[一-鿿゠-ヿA-Za-z0-9]/;
27
+ function breakAt(t){
28
+ var n = t.length, i;
29
+ for (i = n - 1; i >= n * 0.6; i--) if (/[、。,.]/.test(t[i - 1]) && i < n) return i;
30
+ for (i = n - 1; i >= n * 0.35; i--) if (HIRA.test(t[i - 1]) && (WORD.test(t[i]) || (t[i] === " " && WORD.test(t[i + 1] || "")))) return i;
31
+ return -1;
32
+ }
33
+ function wrap(s, maxW, measure){
34
+ var out = [];
35
+ String(s).split("\n").forEach(function(part){
36
+ var cur = "", toks = part.match(TOKEN) || [];
37
+ toks.forEach(function(tok){
38
+ if (/^\s+$/.test(tok)) { if (cur) cur += " "; return; }
39
+ if (!cur || measure(cur + tok) <= maxW) { cur += tok; return; }
40
+ var line = cur.replace(/ +$/, ""), rest = "", p = breakAt(line + tok);
41
+ if (p > 0 && p < line.length) { rest = line.slice(p).replace(/^ +/, ""); line = line.slice(0, p).replace(/ +$/, ""); }
42
+ out.push(line); cur = rest + tok;
43
+ });
44
+ out.push(cur.replace(/ +$/, ""));
45
+ });
46
+ return out;
47
+ }
48
+ // balanced: the narrowest width that keeps the number of lines (no one-character last line)
49
+ function balanced(s, full, measure){
50
+ var ls = wrap(s, full, measure);
51
+ if (ls.length < 2) return ls;
52
+ var lo = full / ls.length, hi = full;
53
+ for (var it = 0; it < 12; it++) { var mid = (lo + hi) / 2; if (wrap(s, mid, measure).length > ls.length) lo = mid; else hi = mid; }
54
+ return wrap(s, hi, measure);
55
+ }
56
+ var str = function(v){ return typeof v === "string" && v.trim() ? v.trim() : ""; };
57
+ var arr = function(v){ return Array.isArray(v) ? v.map(String).filter(function(s){ return s.trim(); }) : []; };
58
+ return class extends base.MotionElement {
59
+ static partName = "contrast";
60
+ static shape = {
61
+ schema: {type: "object", additionalProperties: false, properties: {
62
+ kind: {type: "string", enum: ["answers", "assumptions"]},
63
+ heads: {type: "array", minItems: 3, maxItems: 3, items: STR},
64
+ selfLabel: STR,
65
+ rows: {type: "array", minItems: 1, maxItems: 3, items: {type: "object", additionalProperties: false, required: ["q", "before", "after"], properties: {
66
+ q: STR, after: STR, basis: STR,
67
+ before: {type: "object", additionalProperties: false, properties: {text: STR, by: STR, unknown: {type: "boolean"}, near: STR}}}}},
68
+ studies: {type: "array", minItems: 1, maxItems: 3, items: {type: "object", additionalProperties: false, required: ["label", "chips", "result"], properties: {
69
+ label: STR, chips: STRS, result: STR}}},
70
+ self: {type: "object", additionalProperties: false, required: ["result"], properties: {dropped: STRS, kept: STRS, added: STRS, result: STR}}}},
71
+ example: {kind: "answers", rows: [{q: "差はどこにあるか", before: {unknown: true, near: "国の間の差は [2]"}, after: "7 割は学校の間", basis: "3 年分の全数データ"}]},
72
+ note: {en: "answers: rows [{\"q\", \"before\": {\"text\", \"by\"} or {\"unknown\": true, \"near\"}, \"after\", \"basis\"?}]. assumptions: studies [{\"label\", \"chips\", \"result\"}], self {\"kept\", \"dropped\", \"added\", \"result\"}",
73
+ ja: "answers:rows は [{\"q\", \"before\": {\"text\", \"by\"} か {\"unknown\": true, \"near\"}, \"after\", \"basis\"?}]。assumptions:studies は [{\"label\", \"chips\", \"result\"}]、self は {\"kept\", \"dropped\", \"added\", \"result\"}"},
74
+ };
75
+ static props = {
76
+ order: {type: "string", default: "", doc: "出し方:build(既定。answers は列ごと、assumptions は研究 → 本研究)・row(answers の行ごと)・whole"},
77
+ dur: {type: "number", default: 0.6, doc: "1 ステップの秒数"},
78
+ size: {type: "number", default: 28, doc: "字の大きさ(px、24 以上。入らなければ 24 まで縮める)"},
79
+ };
80
+ get steps(){
81
+ if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
82
+ return this.plan().steps;
83
+ }
84
+ // what is drawn and when; from the data alone
85
+ plan(){
86
+ if (this._plan) return this._plan;
87
+ var d = this.data || {}, bad = [], lang = this.closest && this.closest("[lang]") ? this.closest("[lang]").getAttribute("lang") : "ja";
88
+ var L = /^en\b/i.test(lang) ? T.en : T.ja, kind = d.kind || "answers", order = this.props.order || "build", selfLabel = str(d.selfLabel) || L.self;
89
+ if (kind !== "answers" && kind !== "assumptions") { bad.push("kind is answers or assumptions, not " + kind); kind = "answers"; }
90
+ var heads = (L[kind]).map(function(h, i){ var v = Array.isArray(d.heads) ? str(d.heads[i]) : ""; return v || h || selfLabel; });
91
+ if (d.heads !== undefined && (!Array.isArray(d.heads) || d.heads.length !== 3)) bad.push("heads is 3 texts (" + L[kind].map(function(h){ return h || selfLabel; }).join(", ") + ")");
92
+ var steps = 1, rows = [];
93
+ if (kind === "answers") {
94
+ if (["build", "row", "whole"].indexOf(order) < 0) bad.push("order for answers is build, row or whole, not " + order);
95
+ var rs = Array.isArray(d.rows) ? d.rows : [];
96
+ if (rs.length < 1 || rs.length > 3) bad.push("answers needs 1 to 3 rows (a question each); split the slide for more");
97
+ rs.slice(0, 3).forEach(function(r, i){
98
+ r = r || {};
99
+ var b = r.before && typeof r.before === "object" ? r.before : {}, unknown = b.unknown === true;
100
+ if (!str(r.q)) bad.push("row " + (i + 1) + " has no question (q)");
101
+ if (!str(r.after)) bad.push("row " + (i + 1) + " has no answer of this work (after)");
102
+ if (!unknown && !str(b.text)) bad.push("row " + (i + 1) + ": before is {\"text\", \"by\"} or {\"unknown\": true, \"near\"}");
103
+ var at = function(col){ return order === "whole" ? 1 : order === "row" ? i + 1 : col; };
104
+ rows.push({q: str(r.q), before: unknown ? {unknown: true, near: str(b.near)} : {text: str(b.text), by: str(b.by)},
105
+ after: str(r.after), basis: str(r.basis), sQ: at(1), sB: at(2), sA: at(3)});
106
+ });
107
+ steps = order === "whole" ? 1 : order === "row" ? Math.max(1, rows.length) : 3;
108
+ } else {
109
+ if (["build", "whole"].indexOf(order) < 0) bad.push("order for assumptions is build or whole, not " + order);
110
+ var whole = order === "whole", ss = Array.isArray(d.studies) ? d.studies : [], me = d.self && typeof d.self === "object" ? d.self : null;
111
+ if (ss.length < 1 || ss.length > 3) bad.push("assumptions needs 1 to 3 near studies (studies)");
112
+ ss.slice(0, 3).forEach(function(s, i){
113
+ s = s || {};
114
+ if (!str(s.label) || !arr(s.chips).length) bad.push("study " + (i + 1) + " needs a label and its assumptions (chips)");
115
+ rows.push({label: str(s.label), chips: arr(s.chips).map(function(c){ return {text: c, step: 1}; }), result: str(s.result), sL: 1, sR: 1});
116
+ });
117
+ if (!me) bad.push("assumptions needs this work's row (self: {\"kept\", \"dropped\", \"added\", \"result\"})");
118
+ me = me || {};
119
+ var added = arr(me.added), sAdd = whole ? 1 : 3, sRes = whole ? 1 : added.length ? 4 : 3;
120
+ if (!arr(me.dropped).length && !added.length) bad.push("this work's row drops or adds at least one assumption (self.dropped, self.added)");
121
+ rows.push({self: true, label: selfLabel, result: str(me.result), sL: whole ? 1 : 2, sR: sRes,
122
+ chips: arr(me.kept).map(function(c){ return {text: c, step: whole ? 1 : 2}; })
123
+ .concat(arr(me.dropped).map(function(c){ return {text: c, step: whole ? 1 : 2, dropped: true}; }))
124
+ .concat(added.map(function(c){ return {text: c, step: sAdd, added: true}; }))});
125
+ steps = whole ? 1 : sRes;
126
+ }
127
+ return (this._plan = {kind: kind, L: L, bad: bad, heads: heads, rows: rows, steps: steps, selfLabel: selfLabel});
128
+ }
129
+ mount(ctx){
130
+ var self = this, P = this.props, D = base, pl = this.plan();
131
+ this.style.display = "block"; this.style.position = "relative";
132
+ var W = this.clientWidth, H = this.clientHeight;
133
+ if (!(P.dur >= 0)) { ctx.report("attr", "[contrast] dur must be ≥ 0; using 0.6"); P.dur = 0.6; }
134
+ if (!(P.size >= 24)) { ctx.report("attr", "[contrast] size must be ≥ 24; using 28"); P.size = 28; }
135
+ pl.bad.forEach(function(m){ ctx.report("data", "[contrast] " + m); });
136
+ if (!pl.rows.length) return;
137
+ var fit = null;
138
+ for (var fs = Math.round(P.size); fs >= 24 && !fit; fs -= 2) fit = this._fit(fs, W, H);
139
+ if (!fit) {
140
+ fit = this._fit(24, W, H, true);
141
+ ctx.report("size", "[contrast] the figure needs " + Math.ceil(fit.needW) + " × " + Math.ceil(fit.needH) + " px at 24 px; the element is " + W + " × " + H + " px (fewer rows, shorter texts, or a larger element)");
142
+ }
143
+ var G = this.geo = fit, fsz = G.fs, lh = G.lh;
144
+ 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);
145
+ var gL = D.el("g", {}, svg), gI = D.el("g", {}, svg);
146
+ // the header: a label over each column, a line under them
147
+ this.items = [];
148
+ var add = function(step, g){ self.items.push({step: step, g: g}); return g; };
149
+ var gH = add(0, D.el("g", {}, gI));
150
+ G.heads.forEach(function(h){
151
+ var t = D.el("text", {x: h.x.toFixed(1), y: h.y.toFixed(1), "font-size": fsz, "font-weight": 700, "text-anchor": "middle", fill: "var(--fg)"}, gH);
152
+ t.textContent = h.s;
153
+ });
154
+ D.el("line", {x1: G.x0, x2: G.x1, y1: G.headLine, y2: G.headLine, stroke: "var(--line)", "stroke-width": 2.5}, gL);
155
+ G.seps.forEach(function(y){ D.el("line", {x1: G.x0, x2: G.x1, y1: y, y2: y, stroke: "var(--line)", "stroke-width": 1.2}, gL); });
156
+ // a box with its lines of text; lines are {s, tone: fg|muted|emph, bold, strike}
157
+ var text = function(g, b, id){
158
+ var n = b.lines.length, top = b.y + b.h / 2 - (n - 1) * lh / 2 + fsz * 0.36, left = b.align === "start";
159
+ b.lines.forEach(function(l, i){
160
+ var t = D.el("text", {x: (left ? b.x + G.pad : b.x + b.w / 2).toFixed(1), y: (top + i * lh).toFixed(1), "font-size": fsz,
161
+ "text-anchor": left ? "start" : "middle", fill: "var(--" + (l.tone || "fg") + ")", "font-weight": l.bold ? 700 : 400,
162
+ "data-part": "label", "data-part-id": id, "data-part-of": "box"}, g);
163
+ t.textContent = l.s;
164
+ });
165
+ };
166
+ G.boxes.forEach(function(b, i){
167
+ var g = add(b.step, D.el("g", {}, gI)), id = b.id || "b" + i;
168
+ var r = D.el("rect", {x: b.x.toFixed(1), y: b.y.toFixed(1), width: b.w.toFixed(1), height: b.h.toFixed(1), rx: b.rx || 10,
169
+ fill: b.fill || "none", stroke: b.stroke || "none", "stroke-width": b.sw || 0, "data-part": "box", "data-part-id": id}, g);
170
+ if (b.dash) r.setAttribute("stroke-dasharray", "9 6");
171
+ text(g, b, id);
172
+ if (b.strike) D.el("line", {x1: (b.x + b.strike[0]).toFixed(1), x2: (b.x + b.w - b.strike[1]).toFixed(1), y1: (b.y + b.h / 2).toFixed(1), y2: (b.y + b.h / 2).toFixed(1),
173
+ stroke: "var(--fg)", "stroke-width": 2.5}, g);
174
+ });
175
+ G.arrows.forEach(function(a){
176
+ var g = add(a.step, D.el("g", {}, gI)), hd = fsz * 0.42;
177
+ D.el("line", {x1: a.x1.toFixed(1), x2: (a.x2 - hd).toFixed(1), y1: a.y.toFixed(1), y2: a.y.toFixed(1), stroke: "var(--muted)", "stroke-width": 3}, g);
178
+ D.el("path", {d: "M" + (a.x2 - hd).toFixed(1) + "," + (a.y - hd * 0.6).toFixed(1) + "L" + a.x2.toFixed(1) + "," + a.y.toFixed(1) + "L" + (a.x2 - hd).toFixed(1) + "," + (a.y + hd * 0.6).toFixed(1) + "Z", fill: "var(--muted)"}, g);
179
+ });
180
+ G.frames.forEach(function(f){
181
+ add(f.step, D.el("rect", {x: f.x.toFixed(1), y: f.y.toFixed(1), width: f.w.toFixed(1), height: f.h.toFixed(1), rx: 8, fill: "none", stroke: "var(--emph)", "stroke-width": 4}, gI));
182
+ });
183
+ // the readable table: the final state
184
+ var L = pl.L, tbl = document.createElement("table");
185
+ tbl.className = "sr-only"; tbl.setAttribute("data-readable", "");
186
+ tbl.setAttribute("style", "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);border:0");
187
+ var tr = function(list, th){ var e = document.createElement("tr"); list.forEach(function(s, i){ var td = document.createElement(th || i === 0 ? "th" : "td"); td.textContent = s; e.appendChild(td); }); tbl.appendChild(e); };
188
+ tr(pl.heads, true);
189
+ pl.rows.forEach(function(r){
190
+ if (pl.kind === "answers") {
191
+ var b = r.before.unknown ? L.unknown + (r.before.near ? "(" + r.before.near + ")" : "") : r.before.text + (r.before.by ? " " + r.before.by : "");
192
+ tr([r.q, b, r.after + (r.basis ? "(" + r.basis + ")" : "")]);
193
+ } else {
194
+ tr([r.label, r.chips.map(function(c){ return (c.dropped ? L.dropped + L.sep : c.added ? L.added + L.sep : "") + c.text; }).join(L.list), r.result]);
195
+ }
196
+ });
197
+ this.appendChild(tbl);
198
+ if (this._aria === undefined) this._aria = this.getAttribute("aria-label");
199
+ this.setAttribute("role", "img");
200
+ if (!this._aria) this.setAttribute("aria-label", L.label + pl.heads.join(" / "));
201
+ }
202
+ // the geometry at font size fs; null when it does not fit W × H (unless force)
203
+ _fit(fs, W, H, force){
204
+ var pl = this.plan(), D = base, self = this, pad = Math.round(fs * 0.5), lh = Math.ceil(fs * 1.3);
205
+ var tw = function(s, b){ return D.textWidth(s, fs, {font: self}) * (b ? 1.06 : 1); };
206
+ var m = function(t){ return tw(t); }, mb = function(t){ return tw(t, true); };
207
+ var lines = function(s, w, tone, bold){ return s ? balanced(s, w, bold ? mb : m).map(function(x){ return {s: x, tone: tone, bold: bold}; }) : []; };
208
+ var arrowW = Math.round(fs * 2.2), x0 = 2, x1 = W - 2, inner = x1 - x0, hh = Math.ceil(fs * 1.5), gap = Math.round(fs * 0.45);
209
+ var boxH = function(n){ return Math.ceil(n * lh + fs * 0.7); };
210
+ var boxes = [], arrows = [], frames = [], heads = [], seps = [], rowsH = [], cols, needW = inner;
211
+ if (pl.kind === "answers") {
212
+ var qW = Math.round((inner - arrowW) * 0.27), bW = Math.round((inner - arrowW) * 0.33), aW = inner - arrowW - qW - bW - gap;
213
+ cols = [x0, x0 + qW + gap, x0 + qW + gap + bW + arrowW];
214
+ var widths = [qW, bW, aW];
215
+ pl.heads.forEach(function(h, i){ heads.push({s: h, x: cols[i] + widths[i] / 2}); if (tw(h, true) > widths[i]) needW = Math.max(needW, inner * tw(h, true) / widths[i]); });
216
+ pl.rows.forEach(function(r, i){
217
+ var tw2 = function(w){ return w - 2 * pad; };
218
+ var q = lines(r.q, tw2(qW), "fg");
219
+ var b = r.before.unknown ? [{s: pl.L.unknown, tone: "muted", bold: true}].concat(lines(r.before.near, tw2(bW), "muted"))
220
+ : lines(r.before.text, tw2(bW), "fg").concat(lines(r.before.by, tw2(bW), "muted"));
221
+ var a = lines(r.after, tw2(aW), "fg").concat(lines(r.basis, tw2(aW), "muted"));
222
+ rowsH.push({q: q, b: b, a: a, h: boxH(Math.max(q.length, b.length, a.length))});
223
+ });
224
+ } else {
225
+ var labW = Math.max.apply(null, pl.rows.map(function(r){ return tw(r.label, true); })) + 2 * pad;
226
+ labW = Math.min(Math.ceil(labW), Math.round(inner * 0.22));
227
+ var chipH = Math.ceil(fs * 1.45), cpad = Math.round(fs * 0.45), cgap = Math.round(fs * 0.35);
228
+ var pre = function(c){ return c.dropped ? pl.L.dropped + " " : c.added ? pl.L.added + " " : ""; };
229
+ var chipW = function(c){ return Math.ceil(tw(pre(c) + c.text, !!pre(c)) + 2 * cpad); };
230
+ // the chips column as wide as the longest row of chips needs, leaving the result at least a quarter
231
+ var nat = Math.max.apply(null, pl.rows.map(function(r){ return r.chips.reduce(function(a, c){ return a + chipW(c) + cgap; }, -cgap); }));
232
+ var cW = Math.max(fs * 4, Math.min(nat, inner - labW - arrowW - gap - Math.round(inner * 0.24))), rW = inner - labW - gap - cW - arrowW;
233
+ cols = [x0, x0 + labW + gap, x0 + labW + gap + cW + arrowW];
234
+ [labW, cW, rW].forEach(function(w, i){ heads.push({s: pl.heads[i], x: cols[i] + w / 2}); });
235
+ pl.rows.forEach(function(r){
236
+ // chips flow into lines inside the chips column
237
+ var cx = 0, ln = 0, placed = [];
238
+ r.chips.forEach(function(c){
239
+ var p = pre(c), w = chipW(c);
240
+ if (w > cW) needW = Math.max(needW, inner + w - cW);
241
+ if (cx > 0 && cx + w > cW) { ln++; cx = 0; }
242
+ placed.push({c: c, pre: p, dx: cx, ln: ln, w: Math.min(w, cW), sx: c.dropped ? cpad + tw(p, true) : 0}); cx += w + cgap;
243
+ });
244
+ var end = placed.reduce(function(a, q){ return Math.max(a, q.dx + q.w); }, 0);
245
+ var nl = placed.length ? ln + 1 : 0, chipsH = nl * chipH + Math.max(0, nl - 1) * cgap;
246
+ var lab = lines(r.label, labW - 2 * pad, r.self ? "emph" : "fg", true), res = lines(r.result, rW - 2 * pad, "fg", !!r.self);
247
+ rowsH.push({placed: placed, end: end, chipsH: chipsH, lab: lab, res: res, h: Math.max(boxH(Math.max(lab.length, res.length)), chipsH + fs * 0.7)});
248
+ });
249
+ var chipGeo = {chipH: chipH, cgap: cgap, cpad: cpad, cW: cW, labW: labW, rW: rW};
250
+ }
251
+ var bodyH = rowsH.reduce(function(s, r){ return s + r.h; }, 0) + gap * 2 * rowsH.length;
252
+ var needH = 4 + hh + bodyH + 4;
253
+ if (!force && (needH > H || needW > inner + 0.5)) return null;
254
+ // fill the element's height: rows grow, each to at most twice its height (no wide empty band under the figure)
255
+ if (needH < H) { var each = Math.min((H - needH) / rowsH.length, Math.min.apply(null, rowsH.map(function(r){ return r.h; }))); rowsH.forEach(function(r){ r.h += each; }); needH += each * rowsH.length; }
256
+ var y = 4 + hh, headLine = y;
257
+ heads.forEach(function(h){ h.y = 4 + hh / 2 + fs * 0.36; });
258
+ rowsH.forEach(function(R, i){
259
+ var top = y + gap, h = R.h, mid = top + h / 2, r = pl.rows[i];
260
+ if (i > 0) seps.push(y);
261
+ if (pl.kind === "answers") {
262
+ var qW2 = cols[1] - gap - cols[0], bW2 = cols[2] - arrowW - cols[1], aW2 = x1 - cols[2];
263
+ boxes.push({id: "q" + i, x: cols[0], y: top, w: qW2, h: h, lines: R.q, align: "start", step: r.sQ});
264
+ boxes.push({id: "b" + i, x: cols[1], y: top, w: bW2, h: h, lines: R.b, step: r.sB, stroke: "var(--muted)", sw: 2.2, dash: !!r.before.unknown});
265
+ boxes.push({id: "a" + i, x: cols[2], y: top, w: aW2, h: h, lines: R.a, step: r.sA, stroke: "var(--emph)", sw: 4});
266
+ arrows.push({x1: cols[1] + bW2 + 8, x2: cols[2] - 8, y: mid, step: r.sA});
267
+ } else {
268
+ var g = chipGeo;
269
+ boxes.push({id: "l" + i, x: cols[0], y: top, w: g.labW, h: h, lines: R.lab, align: "start", step: r.sL});
270
+ var cy0 = mid - R.chipsH / 2;
271
+ R.placed.forEach(function(p, j){
272
+ var c = p.c, l = p.pre ? [{s: p.pre + c.text, tone: c.added ? "emph" : "muted", bold: true}] : [{s: c.text, tone: "fg"}];
273
+ boxes.push({id: "c" + i + "-" + j, x: cols[1] + p.dx, y: cy0 + p.ln * (g.chipH + g.cgap), w: p.w, h: g.chipH, lines: l, step: c.step, rx: g.chipH / 2,
274
+ stroke: c.added ? "var(--emph)" : c.dropped ? "var(--muted)" : "var(--term)", sw: c.added ? 3 : 2, dash: !!c.dropped,
275
+ fill: c.added ? "var(--emph-tint)" : c.dropped ? "none" : "var(--term-tint)", strike: c.dropped ? [p.sx, g.cpad] : null});
276
+ });
277
+ boxes.push({id: "r" + i, x: cols[2], y: top, w: g.rW, h: h, lines: R.res, step: r.sR});
278
+ arrows.push({x1: cols[1] + R.end + 10, x2: cols[2] - 8, y: mid, step: r.sR});
279
+ if (r.self) frames.push({x: x0, y: top - gap * 0.6, w: inner, h: h + gap * 1.2, step: r.sL});
280
+ }
281
+ y = top + h + gap;
282
+ });
283
+ return {fs: fs, pad: pad, lh: lh, x0: x0, x1: x1, heads: heads, headLine: headLine, seps: seps, boxes: boxes, arrows: arrows, frames: frames, needW: needW, needH: needH};
284
+ }
285
+ duration(k){ return k >= 1 && k <= this.plan().steps ? this.props.dur : 0; }
286
+ render(k, t, ctx){
287
+ if (!this.items) return;
288
+ var D = base, P = this.props, N = this.plan().steps, faint = ctx && (ctx.preview || ctx.mode === "lecture") ? base.previewOpacity(this) : 0;
289
+ k = Math.max(0, Math.min(k, N));
290
+ var e = k === 0 ? 1 : D.prog(t, 0, P.dur);
291
+ this.items.forEach(function(o){ var v = o.step < k ? 1 : o.step > k ? 0 : e; o.g.setAttribute("opacity", Math.max(v, o.step > k ? faint : 0).toFixed(3)); });
292
+ }
293
+ };
294
+ });
@@ -0,0 +1,47 @@
1
+ ---
2
+ summary: Lines drawn one at a time on shared axes, each named at its end (no legend)
3
+ steps: largest step (default: number of series)
4
+ props:
5
+ xr: x range
6
+ yr: y range
7
+ xt: x ticks
8
+ yt: y ticks
9
+ xf: x tick decimals
10
+ yf: y tick decimals
11
+ xl: x title
12
+ yl: y title
13
+ n: samples
14
+ dur: draw seconds
15
+ lw: line width
16
+ size: text px (≥ 24; 0 = default)
17
+ example: [{"label": "Demand D", "f": "10 - x"}, {"label": "Supply S", "f": "2 + x"}, {"label": "Equilibrium", "pts": [[4, 0], [4, 6]], "dash": true}]
18
+ ---
19
+
20
+ One comparison at a time: mean, median, quantiles; losses.
21
+
22
+ Series: a JSON array in a child script. One series = one step; same `step` = together.
23
+
24
+ | Field | Meaning |
25
+ |---|---|
26
+ | `label` | line name |
27
+ | `f` | expression in x (see below) |
28
+ | `pts` | points `[[x, y], …]` |
29
+ | `tone` | `term` `emph` `accent2` `muted` `fg` `series-1..4` |
30
+ | `dash` | dashed |
31
+ | `area` / `areaLabel` | shaded span `[a, b]` (several; null = edge), labels |
32
+ | `labelAt` / `anchor` / `dx` / `dy` / `ly` | name at x = `labelAt` (default: end); `anchor` `right` `left` `above` `below`; `ly` = y |
33
+
34
+ Text ticks: `xt='["Apr","May"]'` (x = 1, 2) or `[[0, "FY2023"], …]`.
35
+
36
+ `f` is arithmetic, not code: numbers, `x`, `Math` functions/constants (`sin(x)`, `PI`; no `random`), `N(x, mean, sd)`, `+ - * / % **`, comparisons, `&& || !`, `? :`. Powers: `**`. An unreadable `f` shows a card.
37
+
38
+ <!-- llm: omit -->
39
+ - `xr` and `yr` default to `[0, 1]`. When one is not written and a line goes outside it, the line is cut flat at the edge, so the part reports it per series and axis (`ctx.report`; Studio's check warns; from 2.3.0). A written range that cuts a line is taken as intended.
40
+ - `area`: shades to y = 0; several spans; rejection regions, p-values; `areaLabel` per span
41
+ Older forms (still work; not described to the AI)
42
+
43
+ - `color` = `tone`; `at` = `labelAt` (only without `labelAt`)
44
+ - `anchor` `start` `middle` `end`: raw text-anchor
45
+ - Without `step`, series i is step i + 1. Showing later steps faintly draws later lines at `--preview-opacity`
46
+ - The demo is `demo.html`. Series fields match `m-scatter` (conventions §3)
47
+ <!-- llm: end -->
@@ -0,0 +1,223 @@
1
+ // m-curves:同じ座標軸に線を 1 本ずつ引く。線の名前は凡例でなく線の端に直接書く
2
+ // <m-curves xr="[-3,3]" yr="[0,0.5]" xt="[-2,0,2]" yt="[0,0.2,0.4]" xl="*y*" yl="密度" style="height:380px">
3
+ // <script type="application/json">[{"label":"予測分布","f":"N(x,0,1)"},{"label":"中央値","pts":[[0,0],[0,0.4]],"dash":true}]
4
+ // (JSON の終わり)</m-curves>
5
+ // 1 系列 = 1 段階(書いた順。系列の step で変えられ、同じ step の系列は一緒に引く)。f は x の式(Math の関数と N(x,平均,標準偏差) が使える)、pts は点列
6
+ // 2.0(Egatari spec §6.1 の部品):base の MotionElement・el・mtext・prog・previewOpacity、plot の Plot、expr の compile を使い、
7
+ // プレイヤーのグローバルは使わない(1.x は式の評価器 egatari-expr を写して持っていた)。破線の mask の id は
8
+ // curves-2-2-1-<n>m<系列>:このパーツの名前と版に、このパーツだけの数え(1.x は mc<n>m<系列> で、DECK.Plot の数えを進めていた)
9
+ egatariParts.define({name: "curves", version: "2.4.0", requires: {base: "1.3.0", expr: "1.0.0", plot: "1.1.3"}, element: "m-curves"}, function(deps){
10
+ "use strict";
11
+ var base = deps.base, plot = deps.plot, expr = deps.expr;
12
+ var ID = "curves-2-3-1-"; // name and version of this part, dots replaced (spec §6.1 Identifiers)
13
+ var count = 0; // this part's own counter for its ids (deterministic: the order of mounting)
14
+ // 線の名前(26px)の下付き・上付きの分の高さ(px)。base 1.1.0 で mtext の下付き・上付きは 24px 以上になり、下付きの字の下端は基線から
15
+ // 添字の 0.75 倍まで、上付きの字の上端は 1.4 倍まで届く(Source Sans 3 で測った値。BIZ UDPGothic ではそれぞれ 0.5 倍と 1.2 倍)。
16
+ // 字の下の余白(字の 0.12 倍)・上の高さ(0.88 倍)を超える分。記法がなければ 0
17
+ function subX(s, fs){ var ss = Math.max(24, Math.round(fs * 0.75)); return /_./.test(s) ? Math.max(0, 0.75 * ss - 0.12 * fs) : 0; }
18
+ function supX(s, fs){ var ss = Math.max(24, Math.round(fs * 0.75)); return /\^./.test(s) ? Math.max(0, 1.4 * ss - 0.88 * fs) : 0; }
19
+ return class extends base.MotionElement {
20
+ static partName = "curves"; // the registry name (define meta name), for report prefixes
21
+ // データの形(kit/shape.js):線の配列。軸(xr yr xt yt xl yl)は属性に書く(JSON の中ではない)
22
+ static shape = {
23
+ schema: {type: "array", items: {type: "object", additionalProperties: false, anyOf: [{required: ["f"]}, {required: ["pts"]}],
24
+ properties: {label: {type: "string"}, f: {type: "string"}, pts: {type: "array", items: {type: "array", items: {type: "number"}, minItems: 2, maxItems: 2}},
25
+ tone: {type: "string"}, step: {type: "integer"}, labelAt: {type: "number"},
26
+ color: {type: "string"}, dash: {type: "boolean"}, xr: {type: "array", items: {type: "number"}, minItems: 2, maxItems: 2},
27
+ area: {type: "array"}, areaLabel: {type: ["string", "array"]},
28
+ at: {type: "number"}, anchor: {enum: ["right", "left", "above", "below", "top", "bottom", "start", "middle", "end"]}, dx: {type: "number"}, dy: {type: "number"}, ly: {type: "number"}}}},
29
+ example: [{label: "需要 D", f: "10 - x"}, {label: "供給 S", f: "2 + x"}, {label: "均衡", pts: [[4, 0], [4, 6]], dash: true}],
30
+ // よくある書き違い(指示の部品の一覧に書く)
31
+ note: {en: "the data is the array itself, not {\"series\": […]}; axes go in attributes (xr=\"[0,10]\" xl=\"…\"); the expression field is f in x (not fn, not u)",
32
+ ja: "データは配列そのもの({\"series\": […]} ではない)。軸は属性に書く(xr=\"[0,10]\" xl=\"…\")。式の欄は f で、変数は x(fn・u ではない)"},
33
+ };
34
+ static props = {
35
+ xr: {type: "json", default: [0, 1], doc: "横軸の範囲"}, yr: {type: "json", default: [0, 1], doc: "縦軸の範囲"},
36
+ xt: {type: "json", default: []}, yt: {type: "json", default: []},
37
+ xf: {type: "number", default: undefined, doc: "横軸の目盛りの小数桁"}, yf: {type: "number", default: undefined, doc: "縦軸の目盛りの小数桁"},
38
+ xl: {type: "string", default: ""}, yl: {type: "string", default: ""},
39
+ n: {type: "number", default: 300, doc: "f を描く分割数"},
40
+ dur: {type: "number", default: 1.0, doc: "1 本を引く秒数"},
41
+ lw: {type: "number", default: 3.5, doc: "線の太さ"},
42
+ size: {type: "number", default: 0, doc: "字の大きさ(24 以上。0 = 既定)"},
43
+ };
44
+ // 系列 i の段階:step(書いてあれば)、なければ i + 1。段階の数はその最大(step がなければ系列の数で、今までと同じ)
45
+ stepOf(s, i){ return s && s.step !== undefined ? s.step : i + 1; }
46
+ get steps(){
47
+ if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
48
+ var self = this, m = 0;
49
+ (this.data || []).forEach(function(s, i){ m = Math.max(m, self.stepOf(s, i)); });
50
+ return m;
51
+ }
52
+ mount(ctx){
53
+ var P = this.props, D = base, self = this, S = this.data || [];
54
+ this.classList.add("plot"); this.style.display = "block";
55
+ // size:字の大きさ(24 未満は 24 にして知らせる)。0 なら 2.0 と同じ大きさと余白
56
+ var SZ = P.size > 0 ? Math.max(24, P.size) : 0, K = SZ ? SZ / 26 : 1, F = SZ || 26;
57
+ if (P.size > 0 && P.size < 24) ctx.report("attr", "[curves] size " + P.size + " is under 24 px; 24 is used");
58
+ // 字の目盛り(2.4.0):xt に字("4月"。i 番目は x = i + 1)か [x, "字"] があれば、目盛りは自分で描く(plot は数だけを書く)
59
+ var XT = Array.isArray(P.xt) ? P.xt : [], named = XT.some(function(v){ return typeof v === "string" || Array.isArray(v); });
60
+ var ticks = named ? XT.map(function(v, i){ return typeof v === "string" ? [i + 1, v] : Array.isArray(v) ? [+v[0], String(v[1] === undefined ? v[0] : v[1])] : [+v, null]; }) : null;
61
+ var p = this.p = plot.Plot(this, {xr: P.xr, yr: P.yr, xt: named ? [] : P.xt, yt: P.yt, xf: P.xf, yf: P.yf, xl: P.xl, yl: P.yl, fs: SZ || undefined, m: {l: Math.round(86 * K), r: 24, t: Math.round(24 * K), b: Math.round(64 * K)}});
62
+ if (ticks) {
63
+ // plot 1.1.3 と同じ線と位置(字の大きさ fs のときは 6 + fs 下、なければ 30 下)。数の目盛りは xf の桁で書く
64
+ var ax = p.svg.querySelector(".axes") || p.svg, yb = p.H - p.m.b, dX = SZ ? 6 + SZ : 30;
65
+ ticks.forEach(function(t){
66
+ var x = p.x(t[0]), lab = t[1] !== null ? t[1] : (P.xf !== undefined ? t[0].toFixed(P.xf) : String(t[0])).replace("-", "−");
67
+ D.el("line", {x1: x, y1: yb, x2: x, y2: yb + 6, stroke: "var(--fg)", "stroke-width": 1.5}, ax);
68
+ D.mtext(ax, x, yb + dX, lab, SZ ? {"text-anchor": "middle", "font-size": SZ} : {"text-anchor": "middle"});
69
+ });
70
+ }
71
+ var uid = ID + (++count), defs = D.el("defs", {}, p.svg);
72
+ var N = function(x, m, s){ return Math.exp(-0.5 * Math.pow((x - m) / s, 2)) / (s * Math.sqrt(2 * Math.PI)); };
73
+ var x0 = p.m.l, x1 = p.W - p.m.r, y0 = p.m.t, y1 = p.H - p.m.b;
74
+ // 系列ごとに:点列、線、(破線なら)描く範囲の mask、端の名前
75
+ this.items = S.map(function(s, i){
76
+ var pts;
77
+ var raw;
78
+ if (s.pts){ raw = s.pts; pts = s.pts.map(function(q){ return [p.x(q[0]), p.y(q[1])]; }); }
79
+ else {
80
+ // 式は木にしてから計算する(コードとしては実行しない。Egatari spec §6)。書けない式は SyntaxError で、base が受け止めて札を出す(spec §6.1 Containment)
81
+ var g = expr.compile(s.f, {name: "[" + i + "].f", vars: ["x"], funcs: {N: N}, bareMath: true}), a = s.xr ? s.xr[0] : P.xr[0], b = s.xr ? s.xr[1] : P.xr[1];
82
+ pts = []; raw = [];
83
+ for (var j = 0; j <= P.n; j++){ var v = a + (b - a) * j / P.n, w = g(v); raw.push([v, w]); pts.push([p.x(v), p.y(w)]); }
84
+ }
85
+ // 軸の範囲を書かなかったとき(既定の [0, 1])に線が範囲の外に出るなら知らせる(切れて平らな線に見えるため。書いた範囲で切るのは意図とみなす)
86
+ [["xr", 0], ["yr", 1]].forEach(function(ax){
87
+ if (self.hasAttribute(ax[0])) return;
88
+ var r = P[ax[0]], lo = Math.min(r[0], r[1]), hi = Math.max(r[0], r[1]), tol = (hi - lo) * 1e-9, vs = raw.map(function(q){ return q[ax[1]]; }).filter(function(v){ return typeof v === "number" && isFinite(v); });
89
+ var out = vs.filter(function(v){ return v < lo - tol || v > hi + tol; });
90
+ if (out.length) ctx.report("range", "[curves] series " + i + " (" + JSON.stringify(s.label || "") + "): " + out.length + " of " + vs.length + " points are outside " + ax[0] + " " + JSON.stringify(r) + " (the default; values from " + +Math.min.apply(null, vs).toPrecision(4) + " to " + +Math.max.apply(null, vs).toPrecision(4) + "), so the line is cut at the edge. Write " + ax[0] + " to cover them");
91
+ });
92
+ // 有限でない点(NaN・±Infinity)は線を切り、有限な区間ごとに M から引き直す(1 つの NaN で線全体が消えない)
93
+ var d = "", pen = false, gap = false;
94
+ pts.forEach(function(q){
95
+ if (isFinite(q[0]) && isFinite(q[1])){ d += (pen ? "L" : "M") + q[0].toFixed(1) + "," + q[1].toFixed(1); pen = true; }
96
+ else { pen = false; gap = true; }
97
+ });
98
+ if (gap) ctx.report("data", "[curves] series " + i + " (" + JSON.stringify(s.label || "") + ") has points that are not finite numbers (NaN or ±Infinity, e.g. log or division by zero); the line is broken there");
99
+ pts = pts.filter(function(q){ return isFinite(q[0]) && isFinite(q[1]); });
100
+ var col = colorOf(s.tone || s.color, i);
101
+ var it = {step: self.stepOf(s, i)};
102
+ if (s.dash){
103
+ // 破線は dasharray を描画に使えないので、同じ線を mask にして引く
104
+ var mk = D.el("mask", {id: uid + "m" + i, maskUnits: "userSpaceOnUse", x: 0, y: 0, width: p.W, height: p.H}, defs);
105
+ it.mpath = D.el("path", {d: d, fill: "none", stroke: "#fff", "stroke-width": P.lw + 6, pathLength: 1, "stroke-dasharray": "1 2"}, mk);
106
+ var gg = D.el("g", {mask: "url(#" + uid + "m" + i + ")", "clip-path": p.clip}, p.svg);
107
+ it.path = D.el("path", {d: d, fill: "none", stroke: col, "stroke-width": P.lw, "stroke-dasharray": "10 8", "stroke-linecap": "butt"}, gg);
108
+ it.draw = it.mpath;
109
+ } else {
110
+ it.path = D.el("path", {d: d, fill: "none", stroke: col, "stroke-width": P.lw, "clip-path": p.clip, pathLength: 1, "stroke-dasharray": "1 2", "stroke-linejoin": "round"}, p.svg);
111
+ it.draw = it.path;
112
+ }
113
+ // 面積(area:[a, b] か [[a, b], …]。null は軸の端):線と横軸(y = 0、範囲の外なら下の端)のあいだを、その区間だけ薄く塗る。
114
+ // 棄却域・p 値・信頼区間・確率など。線が引き終わってから出る(名前と同じ時に)。areaLabel は区間ごとに中に書く(字の列なら区間の順に)
115
+ if (s.area !== undefined){
116
+ var A = Array.isArray(s.area) && s.area.length && Array.isArray(s.area[0]) ? s.area : [s.area];
117
+ var yb = p.y(Math.max(Math.min(P.yr[0], P.yr[1]), Math.min(Math.max(P.yr[0], P.yr[1]), 0))), ad = "", segs = [];
118
+ A.forEach(function(r){
119
+ if (!Array.isArray(r) || r.length !== 2 || r.some(function(v){ return v !== null && !(typeof v === "number" && isFinite(v)); })){ ctx.report("data", "[curves] series " + i + ": area " + JSON.stringify(r) + " is not [from, to] (numbers, or null for the edge)"); return; }
120
+ var la = p.x(r[0] === null ? P.xr[0] : r[0]), lb = p.x(r[1] === null ? P.xr[1] : r[1]);
121
+ if (la > lb){ var tt = la; la = lb; lb = tt; }
122
+ var seg = pts.filter(function(q){ return isFinite(q[0]) && isFinite(q[1]) && q[0] >= la - 0.01 && q[0] <= lb + 0.01; });
123
+ if (seg.length < 2){ ctx.report("data", "[curves] series " + i + ": area " + JSON.stringify(r) + " has no part of the line in it"); return; }
124
+ ad += "M" + seg[0][0].toFixed(1) + "," + yb.toFixed(1) + seg.map(function(q){ return "L" + q[0].toFixed(1) + "," + q[1].toFixed(1); }).join("") + "L" + seg[seg.length - 1][0].toFixed(1) + "," + yb.toFixed(1) + "Z";
125
+ segs.push({w: seg[seg.length - 1][0] - seg[0][0], seg: seg});
126
+ });
127
+ if (ad){
128
+ it.area = D.el("path", {d: ad, fill: col, "fill-opacity": 0.28, stroke: "none", "clip-path": p.clip, opacity: 0}, p.svg);
129
+ p.svg.insertBefore(it.area, s.dash ? it.path.parentNode : it.path); // 線の下に
130
+ // 区間の名前:区間の真ん中の上(線より上、字の高さぶん離す)。細い区間でも読めるように線の上に置き、背景色で縁取る
131
+ it.areaLabs = [];
132
+ segs.forEach(function(o, j){
133
+ var t = Array.isArray(s.areaLabel) ? s.areaLabel[j] : s.areaLabel;
134
+ if (!t) return;
135
+ var mid = o.seg[Math.floor(o.seg.length / 2)], top = Math.min.apply(null, o.seg.map(function(q){ return q[1]; }));
136
+ var tx = Math.max(x0 + 4, Math.min(x1 - 4, mid[0])), ty = Math.max(y0 + F, top - 10);
137
+ it.areaLabs.push(D.mtext(p.svg, tx, ty, t, {"text-anchor": "middle", fill: col, stroke: "var(--bg)", "stroke-width": 6, "paint-order": "stroke", "font-size": F, "font-weight": 700, opacity: 0}));
138
+ });
139
+ }
140
+ }
141
+ // 名前の位置:labelAt(横軸の値)の点、なければ描画範囲の中の最後の点。at は labelAt の古い名前(labelAt がないときだけ)
142
+ var inside = pts.filter(function(q){ return q[0] >= x0 - .5 && q[0] <= x1 + .5 && q[1] >= y0 - .5 && q[1] <= y1 + .5; });
143
+ var end = inside.length ? inside[inside.length - 1] : pts[pts.length - 1];
144
+ var lat = s.labelAt !== undefined ? s.labelAt : s.at;
145
+ if (!end){ it.lx = it.ly = it.x0 = it.x1 = 0; it.anchor = "start"; it.col = col; it.label = ""; return it; } // 有限な点がない:名前は書かない
146
+ if (lat !== undefined){
147
+ var ax = p.x(lat), best = pts[0];
148
+ pts.forEach(function(q){ if (Math.abs(q[0] - ax) < Math.abs(best[0] - ax)) best = q; });
149
+ end = best;
150
+ }
151
+ // anchor:right(点の右から書く)・left(点の左で終わる)・above / below(点の上・下に中央そろえ、約 0.9 字ぶん離す)。
152
+ // start / middle / end は古い書き方(text-anchor そのもの)で、今までどおり
153
+ var an = {top: "above", bottom: "below"}[s.anchor] || s.anchor; // top / bottom は above / below の別名
154
+ var side = {right: "start", left: "end", above: "middle", below: "middle"}, anchor = side[an] || an || (end[0] > x1 - 160 ? "end" : "start");
155
+ var ly0 = an === "above" ? end[1] - Math.round(14 * K) : an === "below" ? end[1] + Math.round(32 * K) : end[1] - Math.round(12 * K);
156
+ if (an === "below" && ly0 > y1 - 4) ly0 = end[1] - Math.round(14 * K); // 下に入らなければ上へ
157
+ it.lx = end[0] + (anchor === "end" ? -8 : anchor === "start" ? 10 : 0) + (s.dx || 0);
158
+ it.ly = (s.ly !== undefined ? p.y(s.ly) : ly0) + (s.dy || 0);
159
+ // 字の幅の見積もり(26px。base の textWidth:テーマの字で測った幅。記法の * _ ^ を除く)
160
+ var w = D.textWidth(s.label || "", F, {font: self, mark: true});
161
+ it.x0 = anchor === "end" ? it.lx - w : anchor === "middle" ? it.lx - w / 2 : it.lx; it.x1 = it.x0 + w;
162
+ it.anchor = anchor; it.col = col; it.label = s.label || "";
163
+ return it;
164
+ });
165
+ // 名前どうしが重ならないように、横に重なる名前を下へずらす。先に描く高さ(上は 26、下は横軸の 6 px 上)に収めてから離し、
166
+ // 下に収まらなければ下から上へ押し戻す(目盛りの数字にかからない)。上付きのある名前は上端を、下付きのある名前は下端を
167
+ // その分だけ内に、上の名前の下付き・下の名前の上付きの分だけ間を広げる(記法がなければ今までと同じ)
168
+ var min = Math.round(32 * K), lo = F, hi = y1 - 6, ov = function(u, v){ return u.x0 < v.x1 && v.x0 < u.x1; };
169
+ var loOf = function(it){ return lo + supX(it.label, F); }, hiOf = function(it){ return hi - subX(it.label, F); };
170
+ var gap = function(u, v){ return min + subX(u.label, F) + supX(v.label, F); }; // u が上、v が下
171
+ var g = self.items.filter(function(it){ return it.label; });
172
+ g.forEach(function(it){ it.ly = Math.min(hiOf(it), Math.max(loOf(it), it.ly)); });
173
+ g.sort(function(u, v){ return u.ly - v.ly; });
174
+ g.forEach(function(it, j){
175
+ for (var q = 0; q < j; q++){
176
+ var o = g[q];
177
+ if (ov(it, o) && it.ly - o.ly < gap(o, it)) it.ly = o.ly + gap(o, it);
178
+ }
179
+ });
180
+ for (var j = g.length - 1; j >= 0; j--){
181
+ var it = g[j];
182
+ it.ly = Math.min(hiOf(it), it.ly);
183
+ for (var q = j + 1; q < g.length; q++) if (ov(it, g[q]) && g[q].ly - it.ly < gap(it, g[q])) it.ly = g[q].ly - gap(it, g[q]);
184
+ }
185
+ g.forEach(function(it){ it.ly = Math.max(loOf(it), it.ly); });
186
+ this.items.forEach(function(it){
187
+ it.lab = D.mtext(p.svg, it.lx, it.ly, it.label, {"text-anchor": it.anchor, fill: it.col, "font-size": F, "font-weight": 700, opacity: 0});
188
+ });
189
+ // tone:テーマの色の名前か var(--…)。知らない名前は既定の色(series-1..4 の順)にして知らせる
190
+ function colorOf(c, i){
191
+ var def = "var(--series-" + (i % 4 + 1) + ")";
192
+ if (!c) return def;
193
+ if (c.indexOf("var(") === 0) return c;
194
+ var nm = c.replace(/^--/, "");
195
+ if (/^(term|emph|accent2|muted|fg|line|series-[1-4])$/.test(nm)) return "var(--" + nm + ")";
196
+ ctx.report("attr", "[curves] series " + i + ": tone " + JSON.stringify(c) + " is not term, emph, accent2, muted, fg or series-1..4 (" + def.slice(6, -1) + " is used)");
197
+ return def;
198
+ }
199
+ }
200
+ duration(k){ return k >= 1 ? this.props.dur * 0.7 + 0.6 : 0; }
201
+ render(k, t, ctx){
202
+ var dur = this.props.dur, preview = !!ctx.preview, PV = preview ? base.previewOpacity(this) : 0;
203
+ // 先を薄く見せる表示:後の段階の線と名前を、引き終えた形のままテーマの --preview-opacity(なければ 0.45)で描く
204
+ this.items.forEach(function(it){
205
+ if (preview && it.step > k){
206
+ it.draw.setAttribute("stroke-dashoffset", (0).toFixed(4));
207
+ it.path.setAttribute("opacity", PV);
208
+ it.lab.setAttribute("opacity", PV.toFixed(3));
209
+ if (it.area) it.area.setAttribute("opacity", PV.toFixed(3));
210
+ (it.areaLabs || []).forEach(function(e){ e.setAttribute("opacity", PV.toFixed(3)); });
211
+ return;
212
+ }
213
+ var pd = it.step < k ? 1 : it.step === k ? base.prog(t, 0, dur) : 0;
214
+ var pl = it.step < k ? 1 : it.step === k ? base.prog(t, dur * 0.7, 0.6) : 0;
215
+ it.draw.setAttribute("stroke-dashoffset", (1 - pd).toFixed(4));
216
+ it.path.setAttribute("opacity", pd > 0 ? 1 : 0);
217
+ it.lab.setAttribute("opacity", pl.toFixed(3));
218
+ if (it.area) it.area.setAttribute("opacity", pl.toFixed(3));
219
+ (it.areaLabs || []).forEach(function(e){ e.setAttribute("opacity", pl.toFixed(3)); });
220
+ });
221
+ }
222
+ };
223
+ });
@@ -0,0 +1,44 @@
1
+ ---
2
+ summary: Scatter plot; point series by step, then a trend line (least squares or 2 points)
3
+ steps: series + trend (default 1 series = 1 step, lines 1 step)
4
+ props:
5
+ xr: x range
6
+ yr: y range
7
+ xt: x ticks
8
+ yt: y ticks
9
+ xf: x tick decimals
10
+ yf: y tick decimals
11
+ xl: x title
12
+ yl: y title
13
+ r: point radius px
14
+ dur: step seconds
15
+ example: {"series": [{"label": "Sunny", "points": [[24, 210], [28, 380], [31, 560], {"x": 34, "y": 690, "label": "Heatwave"}]}], "trend": "linear"}
16
+ ---
17
+
18
+ Relation of two quantities (heat vs sales). Points, then trend.
19
+
20
+ Child JSON: `{"series": [...], "trend": ...}`.
21
+
22
+ - series: `points` (`[x, y]` or `{"x", "y", "label", "anchor", "tone"}`), `label` (series name, top with marker), `tone` (`term` `emph` `accent2` `muted` `fg` `series-1..4`), `step` (1 series = 1 step; same = together), `at` (start s)
23
+ - Point `label` placed clear of overlaps (`anchor` = first try: `right` `left` `above` `below`); outliers only
24
+ - Marker shape per series; repeats after 4, so ≤ 4 series
25
+ - trend: `"linear"` (least squares on all points), or `{"fit": "linear", "series": 0, "label", "tone", "dash", "step", "at", "digits"}`, or `{"pts": [[x0, y0], [x1, y1]]}` (segment; exactly 2 points; vertical OK); several = array
26
+ - `series`: fitted series index from 0, or array (default all)
27
+ - `label`: `{r}` correlation, `{a}` slope, `{b}` intercept (`digits`, default 2), e.g. `"Trend (r = {r})"`
28
+ - `step`: default after the last series
29
+ - Clipped to the plot; lines outside reported. Vertical `pts`: `{a}` `{b}` empty; `{r}` only for fits
30
+ - Axes: `xr` `yr` range, `xt` `yt` ticks, `xf` `yf` decimals, `xl` `yl` titles; range/ticks auto if omitted
31
+ - `variant`: `fade`, `sweep`, `grow`
32
+ - Show later steps faintly (`ctx.preview`): later points and lines faint
33
+
34
+ The demo is `demo.html`. All numbers are fictional.
35
+
36
+ <!-- llm: omit -->
37
+ Notes
38
+
39
+ - Marker shapes: circle, square, triangle, diamond (colour not relied on); repeat after 4. Point labels try right → left → above → below
40
+ - Fits span the fitted x range, `pts` the 2 points
41
+ - Step length `dur` (default 1 s): points in 0.7 s, then point and series names; line names after the line is drawn
42
+ - `sweep` only orders points; `grow` only scales them; nothing moves (conventions §1.2). No rise-from-below variant: it would move more than variant 0
43
+ - Text width estimated from characters; no room for a name is reported (`ctx.report`)
44
+ <!-- llm: end -->