@johnmorrisdotca/kyogijo-stadiums 0.0.0-stage → 0.4.0

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 (321) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +293 -0
  4. package/README.md +600 -2
  5. package/dist/annotate.d.ts +47 -0
  6. package/dist/annotate.js +349 -0
  7. package/dist/atlas-entry.d.ts +4 -0
  8. package/dist/atlas-entry.js +3 -0
  9. package/dist/atlas.d.ts +131 -0
  10. package/dist/atlas.js +149 -0
  11. package/dist/background.d.ts +56 -0
  12. package/dist/background.js +127 -0
  13. package/dist/ballpark.d.ts +48 -0
  14. package/dist/ballpark.js +129 -0
  15. package/dist/canvas-entry.d.ts +2 -0
  16. package/dist/canvas-entry.js +2 -0
  17. package/dist/canvas.d.ts +85 -0
  18. package/dist/canvas.js +279 -0
  19. package/dist/charts-entry.d.ts +10 -0
  20. package/dist/charts-entry.js +8 -0
  21. package/dist/charts.baseball.d.ts +30 -0
  22. package/dist/charts.baseball.js +152 -0
  23. package/dist/charts.curling.d.ts +52 -0
  24. package/dist/charts.curling.js +196 -0
  25. package/dist/charts.d.ts +172 -0
  26. package/dist/charts.js +508 -0
  27. package/dist/charts.network.d.ts +66 -0
  28. package/dist/charts.network.js +64 -0
  29. package/dist/charts.soccer.d.ts +43 -0
  30. package/dist/charts.soccer.js +191 -0
  31. package/dist/charts.types.d.ts +396 -0
  32. package/dist/charts.types.js +1 -0
  33. package/dist/colour.d.ts +29 -0
  34. package/dist/colour.js +97 -0
  35. package/dist/common.d.ts +14 -0
  36. package/dist/common.js +32 -0
  37. package/dist/court.d.ts +89 -0
  38. package/dist/court.js +248 -0
  39. package/dist/courts.style.d.ts +20 -0
  40. package/dist/courts.style.js +54 -0
  41. package/dist/cricket.board.d.ts +18 -0
  42. package/dist/cricket.board.js +67 -0
  43. package/dist/curling.board.d.ts +24 -0
  44. package/dist/curling.board.js +147 -0
  45. package/dist/d3-entry.d.ts +3 -0
  46. package/dist/d3-entry.js +2 -0
  47. package/dist/d3.d.ts +45 -0
  48. package/dist/d3.js +56 -0
  49. package/dist/draw.d.ts +52 -0
  50. package/dist/draw.js +705 -0
  51. package/dist/element-define.d.ts +2 -0
  52. package/dist/element-define.js +4 -0
  53. package/dist/element-entry.d.ts +1 -0
  54. package/dist/element-entry.js +2 -0
  55. package/dist/element.d.ts +39 -0
  56. package/dist/element.js +321 -0
  57. package/dist/eras-entry.d.ts +2 -0
  58. package/dist/eras-entry.js +2 -0
  59. package/dist/eras.d.ts +204 -0
  60. package/dist/eras.js +396 -0
  61. package/dist/events-entry.d.ts +3 -0
  62. package/dist/events-entry.js +3 -0
  63. package/dist/events.d.ts +54 -0
  64. package/dist/events.data.d.ts +59 -0
  65. package/dist/events.data.js +297 -0
  66. package/dist/events.js +127 -0
  67. package/dist/features.d.ts +31 -0
  68. package/dist/features.js +284 -0
  69. package/dist/field.score.d.ts +5 -0
  70. package/dist/field.score.js +33 -0
  71. package/dist/fields/afl.d.ts +11 -0
  72. package/dist/fields/afl.js +103 -0
  73. package/dist/fields/cricket.d.ts +9 -0
  74. package/dist/fields/cricket.js +81 -0
  75. package/dist/fields/fieldhockey.d.ts +6 -0
  76. package/dist/fields/fieldhockey.js +81 -0
  77. package/dist/fields/gaelic.d.ts +10 -0
  78. package/dist/fields/gaelic.js +86 -0
  79. package/dist/fields/handball.d.ts +6 -0
  80. package/dist/fields/handball.js +85 -0
  81. package/dist/fields/lacrosse.d.ts +17 -0
  82. package/dist/fields/lacrosse.js +169 -0
  83. package/dist/fields/rugby.d.ts +10 -0
  84. package/dist/fields/rugby.js +130 -0
  85. package/dist/fields.d.ts +12 -0
  86. package/dist/fields.js +44 -0
  87. package/dist/fields.kit.d.ts +63 -0
  88. package/dist/fields.kit.js +108 -0
  89. package/dist/fields.style.d.ts +4 -0
  90. package/dist/fields.style.js +19 -0
  91. package/dist/fields.types.d.ts +155 -0
  92. package/dist/fields.types.js +1 -0
  93. package/dist/format.d.ts +10 -0
  94. package/dist/format.js +28 -0
  95. package/dist/formations.d.ts +18 -0
  96. package/dist/formations.js +211 -0
  97. package/dist/frames-entry.d.ts +8 -0
  98. package/dist/frames-entry.js +7 -0
  99. package/dist/frames.d.ts +199 -0
  100. package/dist/frames.js +271 -0
  101. package/dist/frames.providers.d.ts +16 -0
  102. package/dist/frames.providers.js +302 -0
  103. package/dist/geojson-entry.d.ts +2 -0
  104. package/dist/geojson-entry.js +2 -0
  105. package/dist/geojson.d.ts +79 -0
  106. package/dist/geojson.js +147 -0
  107. package/dist/geometry-entry.d.ts +7 -0
  108. package/dist/geometry-entry.js +5 -0
  109. package/dist/geometry.d.ts +295 -0
  110. package/dist/geometry.js +361 -0
  111. package/dist/grounds-entry.d.ts +4 -0
  112. package/dist/grounds-entry.js +3 -0
  113. package/dist/grounds.d.ts +134 -0
  114. package/dist/grounds.data.d.ts +2 -0
  115. package/dist/grounds.data.js +1849 -0
  116. package/dist/grounds.js +74 -0
  117. package/dist/heat.colour.d.ts +37 -0
  118. package/dist/heat.colour.js +54 -0
  119. package/dist/heat.d.ts +43 -0
  120. package/dist/heat.js +614 -0
  121. package/dist/heat.stats.d.ts +123 -0
  122. package/dist/heat.stats.js +413 -0
  123. package/dist/heat.types.d.ts +145 -0
  124. package/dist/heat.types.js +1 -0
  125. package/dist/heat.zones.d.ts +54 -0
  126. package/dist/heat.zones.js +424 -0
  127. package/dist/homeside.d.ts +16 -0
  128. package/dist/homeside.js +43 -0
  129. package/dist/index.d.ts +47 -0
  130. package/dist/index.js +29 -0
  131. package/dist/layers-entry.d.ts +19 -0
  132. package/dist/layers-entry.js +11 -0
  133. package/dist/layers.d.ts +41 -0
  134. package/dist/layers.env.d.ts +72 -0
  135. package/dist/layers.env.js +79 -0
  136. package/dist/layers.js +533 -0
  137. package/dist/layers.marks.d.ts +45 -0
  138. package/dist/layers.marks.js +168 -0
  139. package/dist/layers.types.d.ts +226 -0
  140. package/dist/layers.types.js +1 -0
  141. package/dist/layout.d.ts +97 -0
  142. package/dist/layout.js +381 -0
  143. package/dist/layout.style.d.ts +6 -0
  144. package/dist/layout.style.js +17 -0
  145. package/dist/layouts-entry.d.ts +5 -0
  146. package/dist/layouts-entry.js +4 -0
  147. package/dist/leagues/aussie.d.ts +57 -0
  148. package/dist/leagues/aussie.js +36 -0
  149. package/dist/leagues/baseball.d.ts +58 -0
  150. package/dist/leagues/baseball.js +171 -0
  151. package/dist/leagues/basketball.d.ts +67 -0
  152. package/dist/leagues/basketball.js +128 -0
  153. package/dist/leagues/courts.d.ts +212 -0
  154. package/dist/leagues/courts.js +114 -0
  155. package/dist/leagues/cricket.d.ts +45 -0
  156. package/dist/leagues/cricket.js +34 -0
  157. package/dist/leagues/curling.d.ts +64 -0
  158. package/dist/leagues/curling.js +53 -0
  159. package/dist/leagues/fieldhockey.d.ts +48 -0
  160. package/dist/leagues/fieldhockey.js +29 -0
  161. package/dist/leagues/fields.d.ts +10 -0
  162. package/dist/leagues/fields.js +6 -0
  163. package/dist/leagues/football.d.ts +8 -0
  164. package/dist/leagues/football.js +82 -0
  165. package/dist/leagues/gaelic.d.ts +50 -0
  166. package/dist/leagues/gaelic.js +29 -0
  167. package/dist/leagues/handball.d.ts +45 -0
  168. package/dist/leagues/handball.js +32 -0
  169. package/dist/leagues/hockey.d.ts +63 -0
  170. package/dist/leagues/hockey.js +166 -0
  171. package/dist/leagues/lacrosse.d.ts +103 -0
  172. package/dist/leagues/lacrosse.js +175 -0
  173. package/dist/leagues/rugby.d.ts +59 -0
  174. package/dist/leagues/rugby.js +72 -0
  175. package/dist/leagues/sources.d.ts +18 -0
  176. package/dist/leagues/sources.js +95 -0
  177. package/dist/leagues/types.d.ts +43 -0
  178. package/dist/leagues/types.js +5 -0
  179. package/dist/leagues-entry.d.ts +23 -0
  180. package/dist/leagues-entry.js +15 -0
  181. package/dist/leagues.d.ts +172 -0
  182. package/dist/leagues.js +462 -0
  183. package/dist/look.d.ts +50 -0
  184. package/dist/look.js +186 -0
  185. package/dist/mark.d.ts +35 -0
  186. package/dist/mark.js +59 -0
  187. package/dist/mount-entry.d.ts +2 -0
  188. package/dist/mount-entry.js +2 -0
  189. package/dist/mount.d.ts +63 -0
  190. package/dist/mount.js +167 -0
  191. package/dist/mow.d.ts +36 -0
  192. package/dist/mow.js +111 -0
  193. package/dist/names.d.ts +77 -0
  194. package/dist/names.js +97 -0
  195. package/dist/outline.d.ts +25 -0
  196. package/dist/outline.js +112 -0
  197. package/dist/parks-entry.d.ts +4 -0
  198. package/dist/parks-entry.js +3 -0
  199. package/dist/parks.d.ts +200 -0
  200. package/dist/parks.data.d.ts +2 -0
  201. package/dist/parks.data.js +4489 -0
  202. package/dist/parks.js +92 -0
  203. package/dist/plot-entry.d.ts +2 -0
  204. package/dist/plot-entry.js +2 -0
  205. package/dist/plot.d.ts +74 -0
  206. package/dist/plot.js +68 -0
  207. package/dist/png-entry.d.ts +2 -0
  208. package/dist/png-entry.js +2 -0
  209. package/dist/png.d.ts +26 -0
  210. package/dist/png.js +46 -0
  211. package/dist/ranges.d.ts +33 -0
  212. package/dist/ranges.js +178 -0
  213. package/dist/ranges.names.d.ts +31 -0
  214. package/dist/ranges.names.js +65 -0
  215. package/dist/react-entry.d.ts +2 -0
  216. package/dist/react-entry.js +2 -0
  217. package/dist/react.d.ts +53 -0
  218. package/dist/react.js +58 -0
  219. package/dist/replay-entry.d.ts +5 -0
  220. package/dist/replay-entry.js +4 -0
  221. package/dist/replay.control.d.ts +47 -0
  222. package/dist/replay.control.js +184 -0
  223. package/dist/replay.d.ts +170 -0
  224. package/dist/replay.dom.d.ts +21 -0
  225. package/dist/replay.dom.js +388 -0
  226. package/dist/replay.js +579 -0
  227. package/dist/replay.types.d.ts +152 -0
  228. package/dist/replay.types.js +1 -0
  229. package/dist/restyle.d.ts +21 -0
  230. package/dist/restyle.js +70 -0
  231. package/dist/road.d.ts +26 -0
  232. package/dist/road.draw.d.ts +6 -0
  233. package/dist/road.draw.js +269 -0
  234. package/dist/road.js +59 -0
  235. package/dist/road.model.d.ts +63 -0
  236. package/dist/road.model.js +215 -0
  237. package/dist/road.style.d.ts +7 -0
  238. package/dist/road.style.js +26 -0
  239. package/dist/road.types.d.ts +122 -0
  240. package/dist/road.types.js +1 -0
  241. package/dist/scene-entry.d.ts +7 -0
  242. package/dist/scene-entry.js +5 -0
  243. package/dist/scene.css.d.ts +44 -0
  244. package/dist/scene.css.js +296 -0
  245. package/dist/scene.d.ts +27 -0
  246. package/dist/scene.fit.d.ts +12 -0
  247. package/dist/scene.fit.js +18 -0
  248. package/dist/scene.flat.d.ts +38 -0
  249. package/dist/scene.flat.js +167 -0
  250. package/dist/scene.js +420 -0
  251. package/dist/scene.path.d.ts +40 -0
  252. package/dist/scene.path.js +357 -0
  253. package/dist/scene.types.d.ts +172 -0
  254. package/dist/scene.types.js +1 -0
  255. package/dist/scene.xml.d.ts +31 -0
  256. package/dist/scene.xml.js +81 -0
  257. package/dist/score.d.ts +70 -0
  258. package/dist/score.js +212 -0
  259. package/dist/sets.board.d.ts +22 -0
  260. package/dist/sets.board.js +89 -0
  261. package/dist/sheet.d.ts +18 -0
  262. package/dist/sheet.js +38 -0
  263. package/dist/special.d.ts +44 -0
  264. package/dist/special.js +204 -0
  265. package/dist/special.style.d.ts +8 -0
  266. package/dist/special.style.js +36 -0
  267. package/dist/strings.d.ts +581 -0
  268. package/dist/strings.js +411 -0
  269. package/dist/style.d.ts +35 -0
  270. package/dist/style.js +177 -0
  271. package/dist/surfaces/baseball.d.ts +33 -0
  272. package/dist/surfaces/baseball.js +208 -0
  273. package/dist/surfaces/baseball.walls.d.ts +16 -0
  274. package/dist/surfaces/baseball.walls.js +150 -0
  275. package/dist/surfaces/basketball.d.ts +8 -0
  276. package/dist/surfaces/basketball.js +199 -0
  277. package/dist/surfaces/curling.d.ts +23 -0
  278. package/dist/surfaces/curling.js +128 -0
  279. package/dist/surfaces/field.d.ts +12 -0
  280. package/dist/surfaces/field.js +163 -0
  281. package/dist/surfaces/football.d.ts +12 -0
  282. package/dist/surfaces/football.js +208 -0
  283. package/dist/surfaces/hockey.d.ts +10 -0
  284. package/dist/surfaces/hockey.js +166 -0
  285. package/dist/surfaces/net.d.ts +7 -0
  286. package/dist/surfaces/net.js +151 -0
  287. package/dist/surfaces/soccer-providers.d.ts +124 -0
  288. package/dist/surfaces/soccer-providers.js +334 -0
  289. package/dist/surfaces/soccer-spec.d.ts +138 -0
  290. package/dist/surfaces/soccer-spec.js +175 -0
  291. package/dist/surfaces/soccer.d.ts +25 -0
  292. package/dist/surfaces/soccer.js +270 -0
  293. package/dist/surfaces/surface.d.ts +180 -0
  294. package/dist/surfaces/surface.js +49 -0
  295. package/dist/team.common.d.ts +75 -0
  296. package/dist/team.common.js +97 -0
  297. package/dist/team.formation.d.ts +24 -0
  298. package/dist/team.formation.js +121 -0
  299. package/dist/team.geometry.d.ts +66 -0
  300. package/dist/team.geometry.js +304 -0
  301. package/dist/team.layers.d.ts +16 -0
  302. package/dist/team.layers.js +32 -0
  303. package/dist/team.motion.d.ts +9 -0
  304. package/dist/team.motion.js +325 -0
  305. package/dist/team.network.d.ts +43 -0
  306. package/dist/team.network.js +215 -0
  307. package/dist/team.shape.d.ts +12 -0
  308. package/dist/team.shape.js +217 -0
  309. package/dist/team.types.d.ts +189 -0
  310. package/dist/team.types.js +1 -0
  311. package/dist/text.d.ts +31 -0
  312. package/dist/text.js +88 -0
  313. package/dist/themes.d.ts +14 -0
  314. package/dist/themes.js +24 -0
  315. package/dist/types.d.ts +565 -0
  316. package/dist/types.js +1 -0
  317. package/dist/units.d.ts +45 -0
  318. package/dist/units.js +90 -0
  319. package/dist/version.d.ts +2 -0
  320. package/dist/version.js +2 -0
  321. package/package.json +234 -4
package/dist/layers.js ADDED
@@ -0,0 +1,533 @@
1
+ // Data layers: points, lines, arrows and text laid on a surface in the surface's own frame (feet, or any unit, or a provider's
2
+ // frame), cut to the surface's outline, upright when the surface is turned, with a title and a description for each layer and a
3
+ // table that stands for the drawing. Every mark is a plain SVG element in the drawing's own frame, so the same options always give
4
+ // the same string. Written for this package; mplsoccer and hockey_rink were read for the list of charts to match, and nothing of
5
+ // either is used.
6
+ import { safeColour, safeLogo } from "./colour.js";
7
+ import { FRAMES } from "./frames.js";
8
+ import { KYOGIJO_HEAT_STYLE, KYOGIJO_TEAM_STYLE } from "./style.js";
9
+ import { drawTeam, isTeamLayer, teamTables } from "./team.layers.js";
10
+ import { drawHeat, drawLegend, heatTable } from "./heat.js";
11
+ import { clamp01, envFor, isNum, newMade, opacityOf, own, place, toArray, unitOfLayer } from "./layers.env.js";
12
+ import { safeClass } from "./common.js";
13
+ import { esc, num } from "./format.js";
14
+ import { curveBetween, curvePath, headPath, isPolygon, polylinePath, rampColour, shapePath, taperPath, wavePath, } from "./layers.marks.js";
15
+ import { KYOGIJO_STRINGS, fill } from "./strings.js";
16
+ import { FEET_PER } from "./units.js";
17
+ /**
18
+ * How the standard outcomes look. A word that is not here draws as a filled mark; `style.outcomes` of a layer adds words and changes these.
19
+ * A hollow point is drawn open and a hollow line is dashed, so that an outcome is never told by colour alone.
20
+ */
21
+ export const OUTCOMES = Object.freeze({
22
+ goal: { scale: 1.3 },
23
+ scored: { scale: 1.3 },
24
+ made: {},
25
+ hit: {},
26
+ complete: {},
27
+ success: {},
28
+ won: {},
29
+ shot: { hollow: true },
30
+ missed: { hollow: true },
31
+ miss: { hollow: true },
32
+ saved: { hollow: true },
33
+ blocked: { hollow: true },
34
+ incomplete: { hollow: true },
35
+ failed: { hollow: true },
36
+ out: { hollow: true },
37
+ lost: { hollow: true },
38
+ });
39
+ const NEUTRAL = "var(--ky-layer)";
40
+ const HOME = "var(--ky-layer-home)";
41
+ const AWAY = "var(--ky-layer-away)";
42
+ const INK = "var(--ky-layer-ink)";
43
+ /** A scale from a datum's value to a number. */
44
+ function scaleOf(scale, values, area) {
45
+ const finite = values.filter(isNum);
46
+ const [d0, d1] = scale.domain ?? (finite.length === 0 ? [0, 1] : [Math.min(...finite), Math.max(...finite)]);
47
+ const [r0, r1] = scale.range;
48
+ const useArea = scale.area ?? area;
49
+ return (value) => {
50
+ const t = isNum(value) ? clamp01(d1 === d0 ? 0.5 : (value - d0) / (d1 - d0)) : 0;
51
+ return useArea ? Math.sqrt(r0 * r0 + (r1 * r1 - r0 * r0) * t) : r0 + (r1 - r0) * t;
52
+ };
53
+ }
54
+ const outcomeOf = (word, own_) => {
55
+ if (typeof word !== "string" || word === "")
56
+ return {};
57
+ const key = word.toLowerCase();
58
+ return { ...(own(OUTCOMES, key) ? OUTCOMES[key] : {}), ...(own_ !== undefined && own(own_, key) ? own_[key] : own_ !== undefined && own(own_, word) ? own_[word] : {}) };
59
+ };
60
+ /** The colour of a mark, as a CSS colour: its own, the layer's rule, the team's, or the layer's plain one. */
61
+ function colourFor(paint, d, neutral, domain) {
62
+ const mine = safeColour(d.color);
63
+ if (mine !== undefined)
64
+ return mine;
65
+ const team = d.team === "home" ? HOME : d.team === "away" ? AWAY : undefined;
66
+ if (paint === undefined)
67
+ return team ?? neutral;
68
+ if (typeof paint === "string")
69
+ return safeColour(paint) ?? neutral;
70
+ if (paint.by === "team")
71
+ return (d.team === "home" ? safeColour(paint.home) ?? HOME : d.team === "away" ? safeColour(paint.away) ?? AWAY : safeColour(paint.other)) ?? neutral;
72
+ if (paint.by === "value")
73
+ return isNum(d.value) ? rampColour(paint.stops, domain(d.value)) ?? neutral : safeColour(paint.missing) ?? neutral;
74
+ const key = typeof d.outcome === "string" ? d.outcome : "";
75
+ return (own(paint.colors, key) ? safeColour(paint.colors[key]) : undefined) ?? safeColour(paint.other) ?? neutral;
76
+ }
77
+ /** The class and the custom property that give a mark its colour, when the layer's own does not already. */
78
+ function colourAttrs(expr, layerExpr, neutral) {
79
+ if (expr === HOME)
80
+ return { cls: "ky-h", css: "" };
81
+ if (expr === AWAY)
82
+ return { cls: "ky-a", css: "" };
83
+ if (expr === neutral || expr === layerExpr)
84
+ return { cls: "", css: "" };
85
+ return { cls: "", css: `--c:${expr}` };
86
+ }
87
+ const attrs = (cls, css, opacity) => `${cls === "" ? "" : ` class="${cls}"`}${css === "" ? "" : ` style="${css}"`}${opacity === undefined ? "" : ` opacity="${num(opacity)}"`}`;
88
+ const titled = (tag, open, title) => (typeof title === "string" && title !== "" ? `<${tag}${open}><title>${esc(title)}</title></${tag}>` : `<${tag}${open}/>`);
89
+ const fixedPaint = (paint) => (typeof paint === "string" ? safeColour(paint) : undefined);
90
+ /** Count a mark that is drawn: its outcome, its team and its value. */
91
+ function tally(made, d) {
92
+ made.drawn += 1;
93
+ if (typeof d.outcome === "string" && d.outcome !== "")
94
+ made.outcomes.set(d.outcome, (made.outcomes.get(d.outcome) ?? 0) + 1);
95
+ if (typeof d.team === "string" && d.team !== "")
96
+ made.teams.set(d.team, (made.teams.get(d.team) ?? 0) + 1);
97
+ if (isNum(d.value)) {
98
+ made.low = Math.min(made.low, d.value);
99
+ made.high = Math.max(made.high, d.value);
100
+ }
101
+ }
102
+ /** Points. */
103
+ function drawPoints(layer, env) {
104
+ const made = newMade();
105
+ const style = layer.style ?? {};
106
+ const unit = unitOfLayer(layer, env);
107
+ const k = FEET_PER[unit];
108
+ const data = toArray(layer.data, "a points layer");
109
+ const keep = [];
110
+ const raw = [];
111
+ data.forEach((d, i) => {
112
+ if (typeof d === "object" && d !== null && isNum(d.x) && isNum(d.y)) {
113
+ keep.push(i);
114
+ raw.push([d.x, d.y]);
115
+ }
116
+ else
117
+ made.skipped += 1;
118
+ });
119
+ const at = place(layer, env, raw, unit);
120
+ const values = keep.map((i) => data[i].value).filter(isNum);
121
+ const sizeFn = typeof style.size === "object" && style.size !== null ? scaleOf(style.size, values, true) : undefined;
122
+ const fixed = typeof style.size === "number" && style.size > 0 ? style.size * k : env.base * (isNum(style.scale) && style.scale > 0 ? style.scale : 1);
123
+ const domain = typeof style.fill === "object" && style.fill.by === "value" ? scaleOf({ domain: style.fill.domain, range: [0, 1] }, values, false) : () => 0;
124
+ const layerExpr = fixedPaint(style.fill);
125
+ const edge = style.stroke === "none" ? "none" : safeColour(style.stroke);
126
+ const width = Math.max(0.05, fixed * 0.12);
127
+ made.style = `--w:${num(width)}${layerExpr === undefined ? "" : `;--c:${layerExpr}`}${edge === "none" ? ";--ky-layer-edge:none" : edge === undefined ? "" : `;--ky-layer-edge:${edge}`}`;
128
+ made.opacity = opacityOf(style.opacity);
129
+ const out = [];
130
+ keep.forEach((i, j) => {
131
+ const d = data[i];
132
+ tally(made, d);
133
+ const [x, y] = at[j];
134
+ const outcome = outcomeOf(d.outcome, style.outcomes);
135
+ const shape = d.shape ?? outcome.shape ?? style.shape ?? "circle";
136
+ const w = (isNum(d.size) && d.size > 0 ? d.size * k : sizeFn === undefined ? fixed : Math.max(0.05, sizeFn(d.value) * k)) * (outcome.scale ?? 1);
137
+ const { cls, css } = colourAttrs(colourFor(style.fill, d, NEUTRAL, domain), layerExpr, NEUTRAL);
138
+ const classes = `${cls}${outcome.hollow === true ? `${cls === "" ? "" : " "}ky-hol` : ""}`;
139
+ const open = attrs(classes, css, opacityOf(d.opacity));
140
+ const role = d.team === "home" ? "home" : d.team === "away" ? "away" : undefined;
141
+ const logo = shape === "logo" ? safeLogo(d.logo) ?? (role === undefined ? undefined : safeLogo(style.logos?.[role]) ?? env.logos[role]) : undefined;
142
+ if (logo !== undefined) {
143
+ const side = w * 1.8;
144
+ out.push(titled("image", `${open} href="${esc(logo)}" x="${num(x - side / 2)}" y="${num(y - side / 2)}" width="${num(side)}" height="${num(side)}" preserveAspectRatio="xMidYMid meet"${env.turned ? ` transform="rotate(-90 ${num(x)} ${num(y)})"` : ""}`, d.title));
145
+ }
146
+ else if (shape !== "logo" && isPolygon(shape))
147
+ out.push(titled("path", `${open} d="${shapePath(shape, x, y, w, env.turned)}"`, d.title));
148
+ else
149
+ out.push(titled("circle", `${open} cx="${num(x)}" cy="${num(y)}" r="${num(w / 2)}"`, d.title));
150
+ });
151
+ made.marks = out.join("");
152
+ return made;
153
+ }
154
+ /** Lines and arrows. */
155
+ function drawLines(layer, env, index) {
156
+ const made = newMade();
157
+ const style = layer.style ?? {};
158
+ const unit = unitOfLayer(layer, env);
159
+ const k = FEET_PER[unit];
160
+ const data = toArray(layer.data, `a ${layer.kind} layer`);
161
+ const keep = [];
162
+ const counts = [];
163
+ const raw = [];
164
+ data.forEach((d, i) => {
165
+ let path;
166
+ if (typeof d === "object" && d !== null) {
167
+ if (Array.isArray(d.points) && d.points.length >= 2 && d.points.every((p) => Array.isArray(p) && isNum(p[0]) && isNum(p[1])))
168
+ path = d.points.map((p) => [p[0], p[1]]);
169
+ else if (isNum(d.x) && isNum(d.y) && isNum(d.x2) && isNum(d.y2))
170
+ path = [[d.x, d.y], [d.x2, d.y2]];
171
+ }
172
+ if (path === undefined)
173
+ made.skipped += 1;
174
+ else {
175
+ keep.push(i);
176
+ counts.push(path.length);
177
+ raw.push(...path);
178
+ }
179
+ });
180
+ const at = place(layer, env, raw, unit);
181
+ const values = keep.map((i) => data[i].value).filter(isNum);
182
+ const widthFn = typeof style.width === "object" && style.width !== null ? scaleOf(style.width, values, false) : undefined;
183
+ const fixed = typeof style.width === "number" && style.width > 0 ? style.width * k : env.base * 0.28 * (isNum(style.scale) && style.scale > 0 ? style.scale : 1);
184
+ const domain = typeof style.color === "object" && style.color.by === "value" ? scaleOf({ domain: style.color.domain, range: [0, 1] }, values, false) : () => 0;
185
+ const layerExpr = fixedPaint(style.color);
186
+ const headKind = style.head ?? (layer.kind === "arrows" ? "triangle" : "none");
187
+ const comet = style.comet === true ? {} : typeof style.comet === "object" && style.comet !== null ? style.comet : undefined;
188
+ made.style = `${widthFn === undefined ? `stroke-width:${num(fixed)}` : ""}${layerExpr === undefined ? "" : `${widthFn === undefined ? ";" : ""}--c:${layerExpr}`}`;
189
+ made.opacity = opacityOf(style.opacity);
190
+ const out = [];
191
+ const defs = [];
192
+ let cursor = 0;
193
+ keep.forEach((i, j) => {
194
+ const d = data[i];
195
+ const pts = at.slice(cursor, cursor + counts[j]);
196
+ cursor += counts[j];
197
+ const first = pts[0];
198
+ const last = pts[pts.length - 1];
199
+ const w = isNum(d.width) && d.width > 0 ? d.width * k : widthFn === undefined ? fixed : Math.max(0.05, widthFn(d.value) * k);
200
+ const bow = isNum(d.curve) ? d.curve : isNum(style.curve) ? style.curve : 0;
201
+ const wavy = d.wavy ?? (style.wavy !== undefined && style.wavy !== false);
202
+ const wave = typeof style.wavy === "object" && style.wavy !== null ? style.wavy : {};
203
+ const outcome = outcomeOf(d.outcome, style.outcomes);
204
+ const expr = colourFor(style.color, d, NEUTRAL, domain);
205
+ const { cls, css } = colourAttrs(expr, layerExpr, NEUTRAL);
206
+ const dash = style.dash === undefined || style.dash === "auto" ? (outcome.hollow === true ? "dashed" : "solid") : style.dash;
207
+ const dashes = dash === "dashed" ? ` stroke-dasharray="${num(w * 3)} ${num(w * 2.2)}"` : dash === "dotted" ? ` stroke-dasharray="0.01 ${num(w * 2.2)}"` : "";
208
+ const strokeWidth = widthFn === undefined && !(isNum(d.width) && d.width > 0) ? "" : ` stroke-width="${num(w)}"`;
209
+ const headLength = headKind === "none" ? 0 : (isNum(style.headSize) && style.headSize > 0 ? style.headSize : 3.4) * w;
210
+ let shaft;
211
+ let head = "";
212
+ let tapered;
213
+ let curveLength;
214
+ if (pts.length === 2) {
215
+ const curve = curveBetween(first, last, bow);
216
+ curveLength = curve.length;
217
+ const hl = Math.min(headLength, curveLength * 0.5);
218
+ if (headKind !== "none" && hl > 0)
219
+ head = headPath(last, curve.tangent(curve.length), hl, hl * 0.5, headKind === "open");
220
+ const shortened = headKind === "triangle" && hl > 0 ? curve.trim(hl * 0.9) : curve;
221
+ if (comet?.taper === true && !wavy)
222
+ tapered = taperPath(shortened, w * 1.7);
223
+ shaft = wavy ? wavePath(shortened, wave.amplitude ?? w * 1.2, wave.wavelength ?? w * 8) : curvePath(shortened);
224
+ }
225
+ else {
226
+ let run = 0;
227
+ for (let m = 1; m < pts.length; m += 1)
228
+ run += Math.hypot(pts[m][0] - pts[m - 1][0], pts[m][1] - pts[m - 1][1]);
229
+ curveLength = run;
230
+ const before = pts[pts.length - 2];
231
+ const seg = Math.hypot(last[0] - before[0], last[1] - before[1]);
232
+ const u = seg === 0 ? [1, 0] : [(last[0] - before[0]) / seg, (last[1] - before[1]) / seg];
233
+ const hl = Math.min(headLength, seg * 0.9);
234
+ if (headKind !== "none" && hl > 0)
235
+ head = headPath(last, u, hl, hl * 0.5, headKind === "open");
236
+ const cut = headKind === "triangle" && hl > 0 ? [last[0] - u[0] * hl * 0.9, last[1] - u[1] * hl * 0.9] : undefined;
237
+ shaft = polylinePath(pts, cut);
238
+ }
239
+ if (curveLength === 0) {
240
+ made.skipped += 1;
241
+ return;
242
+ }
243
+ tally(made, d);
244
+ let gradient = "";
245
+ let shaftClass = "ky-ln";
246
+ if (comet !== undefined) {
247
+ const gid = `${index}-g${j}`;
248
+ const from = isNum(comet.from) ? clamp01(comet.from) : 0.05;
249
+ defs.push(`<linearGradient id="${gid}" gradientUnits="userSpaceOnUse" x1="${num(first[0])}" y1="${num(first[1])}" x2="${num(last[0])}" y2="${num(last[1])}"><stop offset="0" style="stop-color:${expr};stop-opacity:${num(from)}"/><stop offset="1" style="stop-color:${expr}"/></linearGradient>`);
250
+ if (tapered !== undefined) {
251
+ shaft = tapered;
252
+ shaftClass = "ky-cm";
253
+ gradient = `fill:url(#${gid})`;
254
+ }
255
+ else
256
+ gradient = `stroke:url(#${gid})`;
257
+ }
258
+ const headEl = head === "" ? "" : `<path class="${headKind === "open" ? "ky-ln" : "ky-ah"}" d="${head}"${headKind === "open" ? ` stroke-width="${num(w)}"` : ""}/>`;
259
+ const title = typeof d.title === "string" && d.title !== "" ? `<title>${esc(d.title)}</title>` : "";
260
+ const opacity = opacityOf(d.opacity);
261
+ // A line with a head is a group (its colour and opacity on the group); a line alone carries them itself.
262
+ const wrap = headEl !== "";
263
+ const pathClass = [shaftClass, wrap ? "" : cls].filter((c) => c !== "").join(" ");
264
+ const pathStyle = [wrap ? "" : css, gradient].filter((c) => c !== "").join(";");
265
+ const pathOpen = `${attrs(pathClass, pathStyle, wrap ? undefined : opacity)} d="${shaft}"${tapered === undefined ? strokeWidth + dashes : ""}`;
266
+ if (wrap)
267
+ out.push(`<g${attrs(cls, css, opacity)}>${title}<path${pathOpen}/>${headEl}</g>`);
268
+ else
269
+ out.push(title === "" ? `<path${pathOpen}/>` : `<path${pathOpen}>${title}</path>`);
270
+ });
271
+ made.marks = out.join("");
272
+ made.defs = defs.join("");
273
+ return made;
274
+ }
275
+ /** Text. */
276
+ function drawText(layer, env) {
277
+ const made = newMade();
278
+ const style = layer.style ?? {};
279
+ const unit = unitOfLayer(layer, env);
280
+ const k = FEET_PER[unit];
281
+ const data = toArray(layer.data, "a text layer");
282
+ const keep = [];
283
+ const raw = [];
284
+ data.forEach((d, i) => {
285
+ if (typeof d === "object" && d !== null && isNum(d.x) && isNum(d.y) && (typeof d.text === "string" || isNum(d.text)) && String(d.text) !== "") {
286
+ keep.push(i);
287
+ raw.push([d.x, d.y]);
288
+ }
289
+ else
290
+ made.skipped += 1;
291
+ });
292
+ const at = place(layer, env, raw, unit);
293
+ const size = isNum(style.size) && style.size > 0 ? style.size * k : env.base * 1.5;
294
+ const layerExpr = fixedPaint(style.color);
295
+ const values = keep.map((i) => data[i].value).filter(isNum);
296
+ const domain = typeof style.color === "object" && style.color.by === "value" ? scaleOf({ domain: style.color.domain, range: [0, 1] }, values, false) : () => 0;
297
+ made.style = `font-size:${num(size)}px${layerExpr === undefined ? "" : `;--c:${layerExpr}`}${isNum(style.weight) ? `;font-weight:${Math.round(style.weight)}` : ""}`;
298
+ const base = style.anchor === "start" || style.anchor === "end" ? style.anchor : "middle";
299
+ made.attr = ` text-anchor="${base}"`;
300
+ made.opacity = opacityOf(style.opacity);
301
+ const out = [];
302
+ keep.forEach((i, j) => {
303
+ const d = data[i];
304
+ tally(made, d);
305
+ const [x, y] = at[j];
306
+ const s = isNum(d.size) && d.size > 0 ? d.size * k : size;
307
+ const { cls, css } = colourAttrs(colourFor(style.color, d, INK, domain), layerExpr, INK);
308
+ const anchor = d.anchor === "start" || d.anchor === "middle" || d.anchor === "end" ? d.anchor : base;
309
+ // The letters are turned back by the quarter the surface was turned, so that they read upright, and by the caller's own turn on top.
310
+ const angle = (env.turned ? -90 : 0) + (isNum(d.rotate) ? d.rotate : 0);
311
+ const classes = ["ky-lt", style.halo === false ? "ky-nh" : "", cls].filter((c) => c !== "").join(" ");
312
+ const open = `${attrs(classes, `${s === size ? "" : `font-size:${num(s)}px`}${s !== size && css !== "" ? ";" : ""}${css}`, opacityOf(d.opacity))}${anchor === base ? "" : ` text-anchor="${anchor}"`} x="${num(x)}" y="${num(y + s * 0.36)}"${angle === 0 ? "" : ` transform="rotate(${num(angle)} ${num(x)} ${num(y)})"`}`;
313
+ out.push(`<text${open}>${esc(String(d.text))}${typeof d.title === "string" && d.title !== "" ? `<title>${esc(d.title)}</title>` : ""}</text>`);
314
+ });
315
+ made.marks = out.join("");
316
+ return made;
317
+ }
318
+ const plural = (words, kind, n) => words[`${kind === "lines" ? "line" : kind === "arrows" ? "arrow" : kind === "text" ? "text" : "point"}${n === 1 ? "One" : "Many"}`];
319
+ const tidy = (text) => text.replace(/[.。]\s*$/, "");
320
+ /** A list of counts, `goal 3, shot 21`, the most first. */
321
+ function listOf(counts, words) {
322
+ return [...counts.entries()]
323
+ .map(([name, n], order) => ({ name, n, order }))
324
+ .sort((a, b) => b.n - a.n || a.order - b.order)
325
+ .map(({ name, n }) => fill(words.item, { name, n }))
326
+ .join(words.listSep);
327
+ }
328
+ function summarise(layer, made, words, stop, gap) {
329
+ // A heat layer wrote its own title and sentences.
330
+ if (made.title !== undefined) {
331
+ const title = typeof layer.title === "string" && layer.title !== "" ? layer.title : made.title;
332
+ return { title, description: typeof layer.description === "string" && layer.description !== "" ? layer.description : made.text ?? "" };
333
+ }
334
+ const noun = typeof layer.name === "string" && layer.name.trim() !== "" ? layer.name.trim() : plural(words, layer.kind, made.drawn);
335
+ const title = typeof layer.title === "string" && layer.title !== "" ? layer.title : fill(words.count, { n: made.drawn, noun });
336
+ if (typeof layer.description === "string" && layer.description !== "")
337
+ return { title, description: layer.description };
338
+ const parts = [];
339
+ if (made.outcomes.size > 0)
340
+ parts.push(fill(words.outcomes, { list: listOf(made.outcomes, words) }));
341
+ if (made.teams.size > 0)
342
+ parts.push(fill(words.teams, { list: listOf(made.teams, words) }));
343
+ if (made.high >= made.low)
344
+ parts.push(fill(words.values, { min: Math.round(made.low * 1000) / 1000, max: Math.round(made.high * 1000) / 1000 }));
345
+ if (made.skipped > 0)
346
+ parts.push(fill(words.skipped, { n: made.skipped }));
347
+ void stop;
348
+ return { title, description: parts.join(gap) };
349
+ }
350
+ /** The layers of a drawing as markup, and a sentence for each. `turned` says whether the surface group is turned a quarter. */
351
+ export function renderLayers(o, layers, turned, range) {
352
+ if (layers === undefined || layers.length === 0)
353
+ return { svg: "", summaries: [] };
354
+ if (!Array.isArray(layers))
355
+ throw new TypeError("layers is a list of layers");
356
+ const env = envFor(o, turned, range);
357
+ const clipId = `${env.id}-lc`;
358
+ const { stop, gap } = env.words.said;
359
+ const said = [];
360
+ const groups = [];
361
+ const rules = [];
362
+ const known = new Map();
363
+ let clipped = false;
364
+ /** The layers made so far, kept in order so that a legend for a layer drawn after it can be filled in last. */
365
+ const slots = [];
366
+ layers.forEach((layer, index) => {
367
+ if (typeof layer !== "object" || layer === null)
368
+ throw new TypeError(`layer ${index} is not a layer`);
369
+ const name = safeClass(layer.id).replace(/ /g, "-");
370
+ const gid = name === "" ? `${env.id}-l${index}` : `${env.id}-${name}`;
371
+ const finish = (made) => {
372
+ if (made.css !== undefined && made.css !== "")
373
+ rules.push(made.css);
374
+ if (layer.hidden === true)
375
+ return;
376
+ const { title, description } = summarise(layer, made, env.words.layers, stop, gap);
377
+ said[index] = description === "" ? `${tidy(title)}${stop}` : `${tidy(title)}${stop}${gap}${description}`;
378
+ const clip = layer.clip !== "none" && layer.kind !== "legend";
379
+ clipped = clipped || clip;
380
+ const cls = made.cls ?? (layer.kind === "points" ? "ky-pts" : layer.kind === "text" ? "ky-txt" : "ky-lns");
381
+ // Cropped to a range, a clipped layer is also cut to the range: its group is held by a group with the range's clip.
382
+ const [cut0, cut1] = clip && env.cropped ? [`<g clip-path="url(#${clipId}-r)">`, "</g>"] : ["", ""];
383
+ groups[index] =
384
+ `${cut0}<g id="${gid}" class="ky-layer ${cls}" data-kind="${layer.kind}"${clip ? ` clip-path="url(#${clipId})"` : ""}${made.attr}${made.style === "" ? "" : ` style="${made.style}"`}${made.opacity === undefined ? "" : ` opacity="${num(made.opacity)}"`}>` +
385
+ `<title id="${gid}-t">${esc(title)}</title>${description === "" ? "" : `<desc id="${gid}-d">${esc(description)}</desc>`}${made.defs === "" ? "" : `<defs>${made.defs}</defs>`}${made.marks}</g>${cut1}${made.after ?? ""}`;
386
+ };
387
+ switch (layer.kind) {
388
+ case "points":
389
+ finish(drawPoints(layer, env));
390
+ return;
391
+ case "lines":
392
+ case "arrows":
393
+ finish(drawLines(layer, env, gid));
394
+ return;
395
+ case "text":
396
+ finish(drawText(layer, env));
397
+ return;
398
+ case "heatmap":
399
+ case "hexbin":
400
+ case "zones":
401
+ case "contour":
402
+ finish(drawHeat(layer, env, gid, known));
403
+ return;
404
+ case "network":
405
+ case "formation":
406
+ case "voronoi":
407
+ case "delaunay":
408
+ case "hull":
409
+ case "flow":
410
+ case "sonar":
411
+ finish(drawTeam(layer, env));
412
+ return;
413
+ case "legend":
414
+ // A legend may be for a layer drawn after it, so it is drawn once the others are.
415
+ slots[index] = () => finish(drawLegend(layer, env, gid, known));
416
+ return;
417
+ default:
418
+ throw new TypeError(`layer ${index} has the kind "${String(layer.kind)}": use points, lines, arrows, text, network, formation, voronoi, delaunay, hull, flow, sonar, heatmap, hexbin, zones, contour or legend`);
419
+ }
420
+ });
421
+ slots.forEach((later) => later?.());
422
+ // The summaries and the groups are in the order of the layers, whichever order they were made in.
423
+ const summaries = said.filter((text) => text !== undefined);
424
+ const placed = groups.filter((g) => g !== undefined);
425
+ groups.length = 0;
426
+ groups.push(...placed);
427
+ if (groups.length === 0)
428
+ return { svg: "", summaries };
429
+ // Cropped to a range, a layer is cut to the range as well as to the surface's outline (see the group each clipped layer is put in).
430
+ const shown = env.shown;
431
+ const rangeClip = env.cropped ? `<clipPath id="${clipId}-r"><rect x="${num(shown.x)}" y="${num(shown.y)}" width="${num(shown.w)}" height="${num(shown.h)}"/></clipPath>` : "";
432
+ const defs = clipped ? `<defs>${rangeClip}<clipPath id="${clipId}"><path d="${env.outline.d}"/></clipPath></defs>` : "";
433
+ // The heat layers' rules go beside them, unless the page supplies the stylesheet (KYOGIJO_STYLE has them); the dark set of a ramp is the layer's own and always comes.
434
+ const heated = groups.some((g) => g.includes('class="ky-layer ky-hm') || g.includes('class="ky-lgw"'));
435
+ const teamed = groups.some((g) => g.includes('class="ky-layer ky-tm'));
436
+ const sheet = `${env.o.style === "none" || !heated ? "" : KYOGIJO_HEAT_STYLE}${env.o.style === "none" || !teamed ? "" : KYOGIJO_TEAM_STYLE}${rules.join("")}`;
437
+ const style = sheet === "" ? "" : `<style>${sheet}</style>`;
438
+ return { svg: `<g class="ky-layers" data-layers="${groups.length}">${defs}${style}${groups.join("")}</g>`, summaries };
439
+ }
440
+ /**
441
+ * The layers of a drawing as one SVG group, in the surface's own frame, for a caller who composes the drawing's parts. Put it inside the
442
+ * surface's group (the one with the quarter turn, when the drawing is tall) and the marks lie on the surface, cut to its outline, with
443
+ * the text still upright. The same options and layers always give the same string; `drawKyogijo` draws exactly this when it is given `layers`.
444
+ * @example
445
+ * ```ts
446
+ * import { drawLayers } from "@johnmorrisdotca/kyogijo-stadiums";
447
+ * const group = drawLayers({ sport: "nhl", id: "rink" }, [
448
+ * { kind: "points", name: "shots", data: [{ x: 169, y: 20.5, outcome: "goal", team: "home" }, { x: 150, y: 40, outcome: "shot", team: "home" }] },
449
+ * ]);
450
+ * console.log(group.startsWith('<g class="ky-layers"'), group.includes("2 shots"));
451
+ * // true true
452
+ * ```
453
+ */
454
+ export function drawLayers(options, layers) {
455
+ return renderLayers(options, layers).svg;
456
+ }
457
+ /** The unit of a frame's numbers, for a layer's `from` that names or is a frame. */
458
+ const frameUnit = (from) => (from === undefined ? undefined : typeof from === "string" ? FRAMES[from]?.unit : from.unit);
459
+ const cell = (value) => (isNum(value) ? String(Math.round(value * 1000) / 1000) : typeof value === "string" ? esc(value) : "");
460
+ /**
461
+ * An HTML table for each layer: the data the drawing shows, as text, for a screen reader or for a page that wants its numbers. The
462
+ * coordinates are the ones given (in the layer's frame and unit), and a caption says which. Hidden layers are in it too. Every cell is escaped.
463
+ * @example
464
+ * ```ts
465
+ * import { layerTable } from "@johnmorrisdotca/kyogijo-stadiums";
466
+ * const html = layerTable([{ kind: "points", name: "shots", from: "nhl-api", data: [{ x: 69, y: 22, outcome: "goal", title: "Goal, 2nd period" }] }]);
467
+ * console.log(html.includes("<th scope=\"col\">Outcome</th>"), html.includes("Goal, 2nd period"));
468
+ * // true true
469
+ * ```
470
+ */
471
+ export function layerTable(layers, options = {}) {
472
+ const words = KYOGIJO_STRINGS[options.language === "ja" ? "ja" : "en"];
473
+ const w = words.layers;
474
+ const maxRows = isNum(options.maxRows) && options.maxRows >= 0 ? Math.floor(options.maxRows) : 500;
475
+ const cls = safeClass(options.className) || "ky-table";
476
+ if (!Array.isArray(layers))
477
+ throw new TypeError("layers is a list of layers");
478
+ const surfaceEnv = options.surface === undefined ? undefined : envFor({ ...options.surface, language: options.language ?? options.surface.language }, false);
479
+ return layers
480
+ .map((layer, index) => {
481
+ if (layer.kind === "legend")
482
+ return "";
483
+ const render = (t, kind) => {
484
+ const shown = t.rows.slice(0, maxRows);
485
+ const more = t.rows.length > shown.length ? fill(w.tableMore, { shown: shown.length, total: t.rows.length }) : "";
486
+ const note = [t.note, more].filter((part) => part !== "").join(words.said.gap);
487
+ const body = shown.map((row) => `<tr><th scope="row">${cell(row[0])}</th>${row.slice(1).map((value) => `<td>${cell(value)}</td>`).join("")}</tr>`).join("");
488
+ return `<table class="${cls}" data-kind="${kind}"><caption>${esc(t.title)}${note === "" ? "" : ` <small>${esc(note)}</small>`}</caption><thead><tr>${t.head.map((name, i) => `<th scope="col">${esc(i === 0 ? w.tableNo : name)}</th>`).join("")}</tr></thead><tbody>${body}</tbody></table>`;
489
+ };
490
+ if (surfaceEnv !== undefined && (layer.kind === "heatmap" || layer.kind === "hexbin" || layer.kind === "zones" || layer.kind === "contour"))
491
+ return render(heatTable(layer, surfaceEnv), layer.kind);
492
+ if (surfaceEnv !== undefined && isTeamLayer(layer))
493
+ return teamTables(layer, surfaceEnv).map((t) => render(t, layer.kind)).join("");
494
+ const data = layer.kind === "formation" ? (layer.data ?? []) : toArray(layer.data, `layer ${index}`);
495
+ const columns = [];
496
+ const has = (key) => data.some((d) => typeof d === "object" && d !== null && d[key] !== undefined && d[key] !== null);
497
+ const t = words.team;
498
+ if (layer.kind === "text")
499
+ columns.push([w.tableText, "text", (d) => d.text]);
500
+ if (layer.kind === "network")
501
+ columns.push([t.tableFrom, "from", (d) => d.from], [t.tableTo, "to", (d) => d.to]);
502
+ if (layer.kind === "formation")
503
+ columns.push([t.tablePosition, "position", (d) => d.position], [t.tableNumber, "number", (d) => d.number], [t.tableName, "name", (d) => d.name]);
504
+ else {
505
+ columns.push([w.tableX, "x", (d) => d.x]);
506
+ columns.push([w.tableY, "y", (d) => d.y]);
507
+ }
508
+ if (layer.kind === "network" || layer.kind === "flow" || layer.kind === "sonar") {
509
+ columns.push([w.tableToX, "x2", (d) => d.x2]);
510
+ columns.push([w.tableToY, "y2", (d) => d.y2]);
511
+ }
512
+ if (layer.kind === "lines" || layer.kind === "arrows") {
513
+ columns.push([w.tableToX, "x2", (d) => d.x2]);
514
+ columns.push([w.tableToY, "y2", (d) => d.y2]);
515
+ if (has("points"))
516
+ columns.push([w.tablePath, "points", (d) => (Array.isArray(d.points) ? d.points.map((p) => (Array.isArray(p) ? `(${cell(p[0])}, ${cell(p[1])})` : "")).join(" ") : "")]);
517
+ }
518
+ for (const [head, key] of [[w.tableValue, "value"], [w.tableTeam, "team"], [w.tableOutcome, "outcome"], [w.tableNote, "title"]])
519
+ if (has(key))
520
+ columns.push([head, key, (d) => d[key]]);
521
+ const rows = data.slice(0, maxRows);
522
+ const names = typeof layer.name === "string" && layer.name.trim() !== "" ? layer.name.trim() : plural(w, layer.kind, data.length);
523
+ const title = typeof layer.title === "string" && layer.title !== "" ? layer.title : fill(w.count, { n: data.length, noun: names });
524
+ const frame = layer.from === undefined ? undefined : typeof layer.from === "string" ? layer.from : layer.from.name;
525
+ // A frame's numbers are in the frame's own unit (tenths of a foot for the NBA's); without one they are in the layer's.
526
+ const unit = frameUnit(layer.from) ?? (layer.units === "rulebook" ? "rulebook" : layer.units ?? "ft");
527
+ const note = [frame === undefined ? undefined : fill(w.tableFrame, { frame, unit }), data.length > rows.length ? fill(w.tableMore, { shown: rows.length, total: data.length }) : undefined].filter((part) => part !== undefined).join(words.said.gap);
528
+ const head = `<tr><th scope="col">${esc(w.tableNo)}</th>${columns.map(([name]) => `<th scope="col">${esc(name)}</th>`).join("")}</tr>`;
529
+ const body = rows.map((d, n) => `<tr><th scope="row">${n + 1}</th>${columns.map(([, , read]) => `<td>${typeof d === "object" && d !== null ? cell(read(d)) : ""}</td>`).join("")}</tr>`).join("");
530
+ return `<table class="${cls}" data-kind="${layer.kind}"><caption>${esc(title)}${note === "" ? "" : ` <small>${esc(note)}</small>`}</caption><thead>${head}</thead><tbody>${body}</tbody></table>`;
531
+ })
532
+ .join("");
533
+ }
@@ -0,0 +1,45 @@
1
+ import type { MarkShape } from "./layers.types.ts";
2
+ /** A place, `[x, y]`, in the drawing's frame. */
3
+ export type P = readonly [number, number];
4
+ /**
5
+ * A colour a fraction `t` (0 to 1) of the way through `stops`, as a hex colour, or the nearest stop when one cannot be read without a
6
+ * browser (`var(--x)`, a name that is not a common one). Stops that are not colours are ignored; with none left, `undefined`.
7
+ */
8
+ export declare function rampColour(stops: readonly string[], t: number): string | undefined;
9
+ /**
10
+ * The path of a polygon shape of width `width` centred on `(cx, cy)`. When the surface is turned a quarter, the corners are turned
11
+ * back so that the shape stands upright as the eye sees it.
12
+ */
13
+ export declare function shapePath(shape: Exclude<MarkShape, "circle">, cx: number, cy: number, width: number, turned: boolean): string;
14
+ /** True for a shape drawn as a polygon, not a circle. */
15
+ export declare const isPolygon: (shape: string) => shape is Exclude<MarkShape, "circle">;
16
+ /** A line from one place to another, straight or bowed, that can be walked by distance. */
17
+ export interface Curve {
18
+ from: P;
19
+ to: P;
20
+ /** The control point of the bow, or `undefined` for a straight line. */
21
+ control?: P;
22
+ /** The length of the line. */
23
+ length: number;
24
+ /** The place a distance `s` along the line. */
25
+ at(s: number): P;
26
+ /** The unit direction of the line a distance `s` along it. */
27
+ tangent(s: number): P;
28
+ /** The same line with its last `by` cut off (so that an arrowhead's tip can stand where the line was to end). */
29
+ trim(by: number): Curve;
30
+ }
31
+ /** A line from `from` to `to` bowed by `bow` times its length (zero for straight). Positive bows to the right of the way it goes as the surface is drawn wide. */
32
+ export declare function curveBetween(from: P, to: P, bow: number): Curve;
33
+ /** The path of a straight or bowed line. */
34
+ export declare function curvePath(curve: Curve): string;
35
+ /**
36
+ * A wave along a line: a chain of quadratic curves, each half a wavelength long, that leans to one side and then the other, so
37
+ * that it starts and ends on the line. The wavelength is stretched a little so that a whole number of waves fits.
38
+ */
39
+ export declare function wavePath(curve: Curve, amplitude: number, wavelength: number): string;
40
+ /** The path of a head at `tip`, pointing along the unit direction `u`: a filled triangle (closed) or an open chevron. */
41
+ export declare function headPath(tip: P, u: P, headLength: number, halfWidth: number, open: boolean): string;
42
+ /** The outline of a comet along a line: a shape that grows from a point at the start to `widest` wide at the end. */
43
+ export declare function taperPath(curve: Curve, widest: number, steps?: number): string;
44
+ /** A polyline's path. */
45
+ export declare const polylinePath: (points: readonly P[], last?: P) => string;