@orangecharts/wrun-chart-host 0.1.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 (198) hide show
  1. package/README.md +72 -0
  2. package/dist/constants/governed-exchanges.constants.d.ts +11 -0
  3. package/dist/constants/governed-exchanges.constants.js +11 -0
  4. package/dist/constants/indicator-sets.constants.d.ts +8 -0
  5. package/dist/constants/indicator-sets.constants.js +8 -0
  6. package/dist/constants/interval.constants.d.ts +34 -0
  7. package/dist/constants/interval.constants.js +121 -0
  8. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.d.ts +28 -0
  9. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.js +65 -0
  10. package/dist/constants/tpo-color-schemes.constants.d.ts +30 -0
  11. package/dist/constants/tpo-color-schemes.constants.js +32 -0
  12. package/dist/host-env.d.ts +102 -0
  13. package/dist/host-env.js +26 -0
  14. package/dist/index.d.ts +93 -0
  15. package/dist/index.js +96 -0
  16. package/dist/indicators/controls/tpo-letter.util.d.ts +2 -0
  17. package/dist/indicators/controls/tpo-letter.util.js +13 -0
  18. package/dist/indicators/controls/tpo-session.util.d.ts +99 -0
  19. package/dist/indicators/controls/tpo-session.util.js +389 -0
  20. package/dist/interfaces/kscript-diagnostics.interface.d.ts +64 -0
  21. package/dist/interfaces/kscript-diagnostics.interface.js +1 -0
  22. package/dist/interfaces/om-panel-data.interface.d.ts +103 -0
  23. package/dist/interfaces/om-panel-data.interface.js +7 -0
  24. package/dist/interfaces/wrun-runtime.interface.d.ts +2659 -0
  25. package/dist/interfaces/wrun-runtime.interface.js +55 -0
  26. package/dist/utility/chart/calendar-interval.util.d.ts +24 -0
  27. package/dist/utility/chart/calendar-interval.util.js +81 -0
  28. package/dist/utility/chart/candle-bar-end.util.d.ts +15 -0
  29. package/dist/utility/chart/candle-bar-end.util.js +23 -0
  30. package/dist/utility/chart/cme-realtime.util.d.ts +24 -0
  31. package/dist/utility/chart/cme-realtime.util.js +53 -0
  32. package/dist/utility/chart/interval-ladder.util.d.ts +27 -0
  33. package/dist/utility/chart/interval-ladder.util.js +92 -0
  34. package/dist/utility/chart/kscript-drawing-prep.util.d.ts +40 -0
  35. package/dist/utility/chart/kscript-drawing-prep.util.js +184 -0
  36. package/dist/utility/chart/kscript-drawing-safe-area.util.d.ts +95 -0
  37. package/dist/utility/chart/kscript-drawing-safe-area.util.js +805 -0
  38. package/dist/utility/chart/kscript-legend-entries.util.d.ts +166 -0
  39. package/dist/utility/chart/kscript-legend-entries.util.js +538 -0
  40. package/dist/utility/chart/kscript-output-role.util.d.ts +32 -0
  41. package/dist/utility/chart/kscript-output-role.util.js +215 -0
  42. package/dist/utility/chart/kscript-provenance-keys.util.d.ts +22 -0
  43. package/dist/utility/chart/kscript-provenance-keys.util.js +56 -0
  44. package/dist/utility/chart/kscript-run-result-normalize.util.d.ts +53 -0
  45. package/dist/utility/chart/kscript-run-result-normalize.util.js +476 -0
  46. package/dist/utility/chart/kscript-run-result-shape.util.d.ts +14 -0
  47. package/dist/utility/chart/kscript-run-result-shape.util.js +66 -0
  48. package/dist/utility/chart/kscript-titancharts-container.util.d.ts +29 -0
  49. package/dist/utility/chart/kscript-titancharts-container.util.js +151 -0
  50. package/dist/utility/chart/kscript-viewport-overlays.util.d.ts +83 -0
  51. package/dist/utility/chart/kscript-viewport-overlays.util.js +443 -0
  52. package/dist/utility/chart/om-panel-push-style.util.d.ts +78 -0
  53. package/dist/utility/chart/om-panel-push-style.util.js +301 -0
  54. package/dist/utility/chart/om-panel.util.d.ts +166 -0
  55. package/dist/utility/chart/om-panel.util.js +815 -0
  56. package/dist/utility/chart/om-series.util.d.ts +42 -0
  57. package/dist/utility/chart/om-series.util.js +330 -0
  58. package/dist/utility/chart/om-target.util.d.ts +11 -0
  59. package/dist/utility/chart/om-target.util.js +24 -0
  60. package/dist/utility/chart/om-widget.util.d.ts +148 -0
  61. package/dist/utility/chart/om-widget.util.js +413 -0
  62. package/dist/utility/chart/overlay-output-budget.util.d.ts +16 -0
  63. package/dist/utility/chart/overlay-output-budget.util.js +109 -0
  64. package/dist/utility/chart/overlay-paint-flags.util.d.ts +24 -0
  65. package/dist/utility/chart/overlay-paint-flags.util.js +55 -0
  66. package/dist/utility/chart/profile-row-height.util.d.ts +21 -0
  67. package/dist/utility/chart/profile-row-height.util.js +110 -0
  68. package/dist/utility/chart/session-interval.util.d.ts +1 -0
  69. package/dist/utility/chart/session-interval.util.js +35 -0
  70. package/dist/utility/chart/technical-script-legend-value.util.d.ts +18 -0
  71. package/dist/utility/chart/technical-script-legend-value.util.js +53 -0
  72. package/dist/utility/chart/trading-day.util.d.ts +70 -0
  73. package/dist/utility/chart/trading-day.util.js +238 -0
  74. package/dist/utility/chart/wrun-legend-name.util.d.ts +25 -0
  75. package/dist/utility/chart/wrun-legend-name.util.js +87 -0
  76. package/dist/utility/chart/wrun-run-result.util.d.ts +977 -0
  77. package/dist/utility/chart/wrun-run-result.util.js +5179 -0
  78. package/dist/utility/chart/wrun-visibility-chart.util.d.ts +25 -0
  79. package/dist/utility/chart/wrun-visibility-chart.util.js +22 -0
  80. package/dist/utility/indicators/heatmap-sensitivity.util.d.ts +62 -0
  81. package/dist/utility/indicators/heatmap-sensitivity.util.js +82 -0
  82. package/dist/utility/number-format.util.d.ts +68 -0
  83. package/dist/utility/number-format.util.js +188 -0
  84. package/dist/utility/v2/opra-governed.util.d.ts +1 -0
  85. package/dist/utility/v2/opra-governed.util.js +13 -0
  86. package/dist/utility/wrun/wrun-auto-row.util.d.ts +54 -0
  87. package/dist/utility/wrun/wrun-auto-row.util.js +96 -0
  88. package/dist/utility/wrun/wrun-batched-marks.util.d.ts +341 -0
  89. package/dist/utility/wrun/wrun-batched-marks.util.js +938 -0
  90. package/dist/utility/wrun/wrun-batched-readout.util.d.ts +75 -0
  91. package/dist/utility/wrun/wrun-batched-readout.util.js +171 -0
  92. package/dist/utility/wrun/wrun-box-segment.util.d.ts +185 -0
  93. package/dist/utility/wrun/wrun-box-segment.util.js +570 -0
  94. package/dist/utility/wrun/wrun-build-memo.util.d.ts +133 -0
  95. package/dist/utility/wrun/wrun-build-memo.util.js +301 -0
  96. package/dist/utility/wrun/wrun-builtin-twin-ports.util.d.ts +25 -0
  97. package/dist/utility/wrun/wrun-builtin-twin-ports.util.js +75 -0
  98. package/dist/utility/wrun/wrun-builtin-twin.util.d.ts +9 -0
  99. package/dist/utility/wrun/wrun-builtin-twin.util.js +40 -0
  100. package/dist/utility/wrun/wrun-canvas-cache.util.d.ts +28 -0
  101. package/dist/utility/wrun/wrun-canvas-cache.util.js +56 -0
  102. package/dist/utility/wrun/wrun-canvas-readout.util.d.ts +69 -0
  103. package/dist/utility/wrun/wrun-canvas-readout.util.js +53 -0
  104. package/dist/utility/wrun/wrun-canvases.util.d.ts +295 -0
  105. package/dist/utility/wrun/wrun-canvases.util.js +1432 -0
  106. package/dist/utility/wrun/wrun-cap-notice.util.d.ts +77 -0
  107. package/dist/utility/wrun/wrun-cap-notice.util.js +296 -0
  108. package/dist/utility/wrun/wrun-card-style.util.d.ts +154 -0
  109. package/dist/utility/wrun/wrun-card-style.util.js +539 -0
  110. package/dist/utility/wrun/wrun-chart-context.util.d.ts +89 -0
  111. package/dist/utility/wrun/wrun-chart-context.util.js +213 -0
  112. package/dist/utility/wrun/wrun-chart-names.util.d.ts +23 -0
  113. package/dist/utility/wrun/wrun-chart-names.util.js +33 -0
  114. package/dist/utility/wrun/wrun-chart-time.util.d.ts +12 -0
  115. package/dist/utility/wrun/wrun-chart-time.util.js +23 -0
  116. package/dist/utility/wrun/wrun-child-overlay-id.util.d.ts +13 -0
  117. package/dist/utility/wrun/wrun-child-overlay-id.util.js +44 -0
  118. package/dist/utility/wrun/wrun-color-facts.util.d.ts +40 -0
  119. package/dist/utility/wrun/wrun-color-facts.util.js +88 -0
  120. package/dist/utility/wrun/wrun-color-settings.lazy.d.ts +26 -0
  121. package/dist/utility/wrun/wrun-color-settings.lazy.js +9 -0
  122. package/dist/utility/wrun/wrun-color-settings.util.d.ts +351 -0
  123. package/dist/utility/wrun/wrun-color-settings.util.js +1363 -0
  124. package/dist/utility/wrun/wrun-footprint-modes.util.d.ts +23 -0
  125. package/dist/utility/wrun/wrun-footprint-modes.util.js +24 -0
  126. package/dist/utility/wrun/wrun-frames.d.ts +217 -0
  127. package/dist/utility/wrun/wrun-frames.js +1021 -0
  128. package/dist/utility/wrun/wrun-handles.util.d.ts +792 -0
  129. package/dist/utility/wrun/wrun-handles.util.js +2050 -0
  130. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.d.ts +156 -0
  131. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.js +342 -0
  132. package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +218 -0
  133. package/dist/utility/wrun/wrun-hosted-wire-caps.js +667 -0
  134. package/dist/utility/wrun/wrun-hosted-wire.util.d.ts +40 -0
  135. package/dist/utility/wrun/wrun-hosted-wire.util.js +27 -0
  136. package/dist/utility/wrun/wrun-hover.util.d.ts +182 -0
  137. package/dist/utility/wrun/wrun-hover.util.js +534 -0
  138. package/dist/utility/wrun/wrun-hud-looks.util.d.ts +246 -0
  139. package/dist/utility/wrun/wrun-hud-looks.util.js +587 -0
  140. package/dist/utility/wrun/wrun-integration-budget.util.d.ts +47 -0
  141. package/dist/utility/wrun/wrun-integration-budget.util.js +122 -0
  142. package/dist/utility/wrun/wrun-latest-snapshot.d.ts +19 -0
  143. package/dist/utility/wrun/wrun-latest-snapshot.js +7 -0
  144. package/dist/utility/wrun/wrun-levels-child.util.d.ts +131 -0
  145. package/dist/utility/wrun/wrun-levels-child.util.js +514 -0
  146. package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +75 -0
  147. package/dist/utility/wrun/wrun-lifecycle.contract.js +9 -0
  148. package/dist/utility/wrun/wrun-market-facts.util.d.ts +116 -0
  149. package/dist/utility/wrun/wrun-market-facts.util.js +238 -0
  150. package/dist/utility/wrun/wrun-output-style.util.d.ts +344 -0
  151. package/dist/utility/wrun/wrun-output-style.util.js +1239 -0
  152. package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +84 -0
  153. package/dist/utility/wrun/wrun-overlay-identity.util.js +142 -0
  154. package/dist/utility/wrun/wrun-overlay-result.util.d.ts +120 -0
  155. package/dist/utility/wrun/wrun-overlay-result.util.js +219 -0
  156. package/dist/utility/wrun/wrun-pack.util.d.ts +70 -0
  157. package/dist/utility/wrun/wrun-pack.util.js +301 -0
  158. package/dist/utility/wrun/wrun-pane-format.util.d.ts +180 -0
  159. package/dist/utility/wrun/wrun-pane-format.util.js +299 -0
  160. package/dist/utility/wrun/wrun-panel-style.lazy.d.ts +25 -0
  161. package/dist/utility/wrun/wrun-panel-style.lazy.js +15 -0
  162. package/dist/utility/wrun/wrun-panel-style.util.d.ts +174 -0
  163. package/dist/utility/wrun/wrun-panel-style.util.js +726 -0
  164. package/dist/utility/wrun/wrun-param-values.util.d.ts +69 -0
  165. package/dist/utility/wrun/wrun-param-values.util.js +230 -0
  166. package/dist/utility/wrun/wrun-refusal.util.d.ts +263 -0
  167. package/dist/utility/wrun/wrun-refusal.util.js +1259 -0
  168. package/dist/utility/wrun/wrun-registry-errors.util.d.ts +199 -0
  169. package/dist/utility/wrun/wrun-registry-errors.util.js +257 -0
  170. package/dist/utility/wrun/wrun-render-select.util.d.ts +336 -0
  171. package/dist/utility/wrun/wrun-render-select.util.js +1157 -0
  172. package/dist/utility/wrun/wrun-script-inputs.util.d.ts +434 -0
  173. package/dist/utility/wrun/wrun-script-inputs.util.js +1971 -0
  174. package/dist/utility/wrun/wrun-setting-words.lazy.d.ts +20 -0
  175. package/dist/utility/wrun/wrun-setting-words.lazy.js +9 -0
  176. package/dist/utility/wrun/wrun-setting-words.util.d.ts +79 -0
  177. package/dist/utility/wrun/wrun-setting-words.util.js +176 -0
  178. package/dist/utility/wrun/wrun-socket-row-store.d.ts +98 -0
  179. package/dist/utility/wrun/wrun-socket-row-store.js +417 -0
  180. package/dist/utility/wrun/wrun-style-flags.util.d.ts +18 -0
  181. package/dist/utility/wrun/wrun-style-flags.util.js +21 -0
  182. package/dist/utility/wrun/wrun-style-words.util.d.ts +148 -0
  183. package/dist/utility/wrun/wrun-style-words.util.js +439 -0
  184. package/dist/utility/wrun/wrun-table-look.util.d.ts +125 -0
  185. package/dist/utility/wrun/wrun-table-look.util.js +225 -0
  186. package/dist/utility/wrun/wrun-text-look.util.d.ts +51 -0
  187. package/dist/utility/wrun/wrun-text-look.util.js +227 -0
  188. package/dist/utility/wrun/wrun-text-params.util.d.ts +106 -0
  189. package/dist/utility/wrun/wrun-text-params.util.js +171 -0
  190. package/dist/utility/wrun/wrun-tpo-rows.util.d.ts +71 -0
  191. package/dist/utility/wrun/wrun-tpo-rows.util.js +438 -0
  192. package/dist/utility/wrun/wrun-twin-compute-inputs.util.d.ts +14 -0
  193. package/dist/utility/wrun/wrun-twin-compute-inputs.util.js +58 -0
  194. package/dist/utility/wrun/wrun-visibility-table.d.ts +437 -0
  195. package/dist/utility/wrun/wrun-visibility-table.js +237 -0
  196. package/dist/utility/wrun/wrun-volume-profile.util.d.ts +37 -0
  197. package/dist/utility/wrun/wrun-volume-profile.util.js +278 -0
  198. package/package.json +38 -0
@@ -0,0 +1,815 @@
1
+ import { MAX_PANES } from './om-series.util.js';
2
+ import { invalidPanelStyleWord, invalidSeriesStyleWord, PANEL_SERIES_STYLE_WORDS, PANEL_STYLE_WORDS, } from './om-panel-push-style.util.js';
3
+ import { parseWrunColor } from '../wrun/wrun-color-facts.util.js';
4
+ // The style mappers through their door (wrun-panel-style.lazy.ts): a pure holder the style module
5
+ // binds as it evaluates, so the panel parse every boot path and the frame worker hold carries none.
6
+ import { boundWrunPanelStyle } from '../wrun/wrun-panel-style.lazy.js';
7
+ import { formatWrunNumber, isWrunColorWord, WRUN_FONT_FAMILY_CSS, WRUN_VALUE_FORMATS, wrunFontFamilyCss, wrunFontWeightCss, } from '../wrun/wrun-style-words.util.js';
8
+ const OM_PANEL_MAX_ROWS = 2000;
9
+ export const OM_PANEL_MAX_PER_PANE = 8;
10
+ /** Series columns on every kind but table; a table takes 12 (MX-table-caps). */
11
+ const PANEL_MAX_SERIES = 8;
12
+ const PANEL_MAX_TABLE_COLUMNS = 12;
13
+ /** Frame rows: a table 128 (MX-table-caps), pie and tiles 24, else 2000. */
14
+ const PANEL_MAX_TABLE_ROWS = 128;
15
+ const PANEL_MAX_PIE_ROWS = 24;
16
+ const OM_PANEL_PAYLOAD_ID_PATTERN = /^ompn:c(0|[1-9][0-9]?):[a-z0-9][a-z0-9._-]{0,63}$/;
17
+ const ID_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
18
+ const FRAME_KEYS = new Set(['chartIndex', 'payloadId', 'revision', 'clear', 'panel', 'rows', 'source']);
19
+ const CLEAR_KEYS = new Set(['chartIndex', 'payloadId', 'revision', 'clear']);
20
+ /** The structural panel keys both lanes share. */
21
+ const PANEL_KEYS = new Set([
22
+ 'id',
23
+ 'title',
24
+ 'kind',
25
+ 'x',
26
+ 'place',
27
+ 'orientation',
28
+ 'series',
29
+ 'stacked',
30
+ 'labels',
31
+ 'bins',
32
+ 'hole',
33
+ ]);
34
+ const SERIES_KEYS = new Set(['name', 'color']);
35
+ /** A styled table cell's keys (SC14 SC22 SC35 SC40), both lanes. */
36
+ const CELL_KEYS = new Set([
37
+ 'text',
38
+ 'value',
39
+ 'color',
40
+ 'bar',
41
+ 'spark',
42
+ 'text_color',
43
+ 'background_color',
44
+ 'opacity',
45
+ 'gradient',
46
+ 'align',
47
+ 'valign',
48
+ 'colspan',
49
+ 'rowspan',
50
+ 'font_size',
51
+ 'font_weight',
52
+ ]);
53
+ const BAR_CELL_KEYS = new Set(['value', 'color']);
54
+ const SOURCE_KEYS = new Set(['kind', 'ref', 'computedAtMs']);
55
+ const KINDS = new Set(['bars', 'line', 'scatter', 'histogram', 'pie', 'heatmap', 'table', 'tiles']);
56
+ const X_WORDS = new Set(['time', 'index', 'category', 'number']);
57
+ const ALIGNS = new Set(['left', 'center', 'right']);
58
+ const VALIGNS = new Set(['top', 'middle', 'bottom']);
59
+ const FONT_WEIGHTS = new Set(['normal', 'medium', 'bold']);
60
+ const VALUE_FORMATS = new Set(WRUN_VALUE_FORMATS);
61
+ /** The pane kinds whose `chrome` word reaches the engine as `chromeStyle`. */
62
+ const CHROME_KINDS = new Set(['line', 'bars', 'histogram', 'scatter', 'pie']);
63
+ const DEFAULT_HEATMAP_PALETTE = ['#e06c75', '#7a7f93', '#98c379'];
64
+ /** Chart-shaped panels (bars, curves, scatter, pie, heatmap, tiles) keep one weight below the chart. */
65
+ export const PANEL_CHART_GRID_HEIGHT = 0.22;
66
+ /** The engine's table defaults: 12 px text with 8 px cell padding gives 28 px rows, 8 px edge padding. */
67
+ const PANEL_TABLE_FONT_PX = 12;
68
+ const PANEL_TABLE_CELL_PADDING_PX = 8;
69
+ const PANEL_TABLE_EDGE_PADDING_PX = 8;
70
+ export const PANEL_TABLE_ROW_HEIGHT_PX = PANEL_TABLE_FONT_PX + 2 * PANEL_TABLE_CELL_PADDING_PX;
71
+ /** Pushes the table below the pane's legend pill row so the title never prints over the header. */
72
+ export const PANEL_TABLE_TOP_OFFSET_PX = 24;
73
+ /** The engine refuses panes under 13 percent; half the chart is the most an auto-sized table may take. */
74
+ const PANEL_TABLE_MIN_GRID_HEIGHT = 0.13;
75
+ const PANEL_TABLE_MAX_GRID_HEIGHT = 0.5;
76
+ const PANEL_FALLBACK_CHART_HEIGHT_PX = 800;
77
+ const DAY_MS = 86_400_000;
78
+ export function isPanelClearFrame(frame) {
79
+ return frame.clear === true;
80
+ }
81
+ function isRecord(value) {
82
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
83
+ }
84
+ function finite(value) {
85
+ return typeof value === 'number' && Number.isFinite(value);
86
+ }
87
+ function nonnegativeInteger(value) {
88
+ return finite(value) && Number.isSafeInteger(value) && value >= 0;
89
+ }
90
+ function positiveInteger(value) {
91
+ return nonnegativeInteger(value) && value >= 1;
92
+ }
93
+ function boundedString(value, max) {
94
+ return typeof value === 'string' && value.length > 0 && value.length <= max;
95
+ }
96
+ /** A colour word on both lanes (SC16): `#rrggbb`, `#rrggbbaa` or a theme token. */
97
+ function color(value) {
98
+ return isWrunColorWord(value);
99
+ }
100
+ /** A declared series colour on the wrun lane: a colour word, or the pick of
101
+ * the `param.color` the file bound there (`color: "@ink"`, WRUN-OFFCHART
102
+ * round 2 G). The kit lands the default and records the place; before every
103
+ * run `wrunSheetWithSettings` writes the viewer's pick into it as the
104
+ * settings dialog stores a colour (`#rgb` to `#rrggbbaa`, `rgb()` or
105
+ * `rgba()`), which the panes read as they are. */
106
+ function paintedColor(value) {
107
+ return color(value) || parseWrunColor(value) !== undefined;
108
+ }
109
+ function unitInterval(value) {
110
+ return finite(value) && value >= 0 && value <= 1;
111
+ }
112
+ function spark(value) {
113
+ return Array.isArray(value) && value.length <= 64 && value.every(finite);
114
+ }
115
+ function colorStops(value) {
116
+ return Array.isArray(value) && value.length >= 2 && value.length <= 8 && value.every(color);
117
+ }
118
+ function valueCell(value) {
119
+ return value === null || finite(value);
120
+ }
121
+ /** A bars value cell: a number, a gap, or `{ value, color? }` (SC19). */
122
+ function barCell(value) {
123
+ if (valueCell(value))
124
+ return true;
125
+ if (!isRecord(value) || Object.keys(value).some((key) => !BAR_CELL_KEYS.has(key)))
126
+ return false;
127
+ return 'value' in value && valueCell(value.value) && (value.color === undefined || color(value.color));
128
+ }
129
+ function rowKey(value, x) {
130
+ if (x === 'category')
131
+ return typeof value === 'string';
132
+ if (x === 'time')
133
+ return nonnegativeInteger(value);
134
+ if (x === 'number')
135
+ return finite(value);
136
+ return finite(value) && Number.isSafeInteger(value);
137
+ }
138
+ /** A table cell: text, a number, or a styled cell whose spans stay inside the grid (SC35). */
139
+ function tableCell(value, grid) {
140
+ if (typeof value === 'string' || finite(value))
141
+ return true;
142
+ if (!isRecord(value) || Object.keys(value).some((key) => !CELL_KEYS.has(key)))
143
+ return false;
144
+ return ((value.text === undefined || typeof value.text === 'string') &&
145
+ (value.value === undefined || finite(value.value)) &&
146
+ (value.color === undefined || color(value.color)) &&
147
+ (value.bar === undefined || unitInterval(value.bar)) &&
148
+ (value.spark === undefined || spark(value.spark)) &&
149
+ (value.text_color === undefined || color(value.text_color)) &&
150
+ (value.background_color === undefined || color(value.background_color)) &&
151
+ (value.opacity === undefined || unitInterval(value.opacity)) &&
152
+ (value.gradient === undefined || colorStops(value.gradient)) &&
153
+ (value.align === undefined || (typeof value.align === 'string' && ALIGNS.has(value.align))) &&
154
+ (value.valign === undefined || (typeof value.valign === 'string' && VALIGNS.has(value.valign))) &&
155
+ (value.colspan === undefined || (positiveInteger(value.colspan) && grid.column + value.colspan <= grid.columns)) &&
156
+ (value.rowspan === undefined || (positiveInteger(value.rowspan) && grid.row + value.rowspan <= grid.rows)) &&
157
+ (value.font_size === undefined ||
158
+ (nonnegativeInteger(value.font_size) && value.font_size >= 6 && value.font_size <= 64)) &&
159
+ (value.font_weight === undefined || (typeof value.font_weight === 'string' && FONT_WEIGHTS.has(value.font_weight))));
160
+ }
161
+ function validRow(row, panel, rowIndex, rowCount) {
162
+ if (!Array.isArray(row))
163
+ return false;
164
+ switch (panel.kind) {
165
+ case 'bars':
166
+ return row.length === panel.series.length + 1 && rowKey(row[0], panel.x) && row.slice(1).every(barCell);
167
+ case 'line':
168
+ return row.length === panel.series.length + 1 && rowKey(row[0], panel.x) && row.slice(1).every(valueCell);
169
+ case 'scatter':
170
+ return (row.length >= 3 &&
171
+ row.length <= 6 &&
172
+ (typeof row[0] === 'string' || rowKey(row[0], panel.x === 'number' ? 'number' : 'index')) &&
173
+ finite(row[1]) &&
174
+ finite(row[2]) &&
175
+ (row.length < 4 || (finite(row[3]) && row[3] >= 0)) &&
176
+ (row.length < 5 || color(row[4])) &&
177
+ (row.length < 6 || typeof row[5] === 'string'));
178
+ case 'histogram':
179
+ return row.length === 2 && typeof row[0] === 'string' && finite(row[1]);
180
+ case 'pie':
181
+ return (row.length >= 2 &&
182
+ row.length <= 3 &&
183
+ typeof row[0] === 'string' &&
184
+ finite(row[1]) &&
185
+ (row.length < 3 || color(row[2])));
186
+ case 'heatmap':
187
+ return (row.length === 3 &&
188
+ rowKey(row[0], panel.x) &&
189
+ (typeof row[1] === 'string' || (finite(row[1]) && Number.isSafeInteger(row[1]))) &&
190
+ valueCell(row[2]));
191
+ case 'table': {
192
+ const columns = panel.series.length;
193
+ return (row.length === columns &&
194
+ row.every((cell, column) => tableCell(cell, { columns, rows: rowCount, row: rowIndex, column })));
195
+ }
196
+ case 'tiles':
197
+ return (row.length >= 2 &&
198
+ row.length <= 6 &&
199
+ typeof row[0] === 'string' &&
200
+ ((typeof row[1] === 'string' && row[1].length <= 64) || finite(row[1])) &&
201
+ (row.length < 3 || row[2] === null || typeof row[2] === 'string') &&
202
+ (row.length < 4 || row[3] === null || color(row[3])) &&
203
+ (row.length < 5 || row[4] === null || spark(row[4])) &&
204
+ (row.length < 6 || (typeof row[5] === 'string' && VALUE_FORMATS.has(row[5]))));
205
+ }
206
+ }
207
+ function panelRowCap(kind) {
208
+ if (kind === 'table')
209
+ return PANEL_MAX_TABLE_ROWS;
210
+ if (kind === 'pie' || kind === 'tiles')
211
+ return PANEL_MAX_PIE_ROWS;
212
+ return OM_PANEL_MAX_ROWS;
213
+ }
214
+ function copyCell(cell) {
215
+ if (Array.isArray(cell))
216
+ return [...cell];
217
+ if (cell && typeof cell === 'object') {
218
+ const entry = cell;
219
+ return {
220
+ ...entry,
221
+ ...(entry.spark ? { spark: [...entry.spark] } : {}),
222
+ ...(entry.gradient ? { gradient: [...entry.gradient] } : {}),
223
+ };
224
+ }
225
+ return cell;
226
+ }
227
+ /** Reject an entire frame before it can advance any session revision floor.
228
+ * The row grammar and the style word list are one on both lanes. A push
229
+ * frame's style words are checked here (value, kind, `#rrggbb[aa]` colours)
230
+ * and a bad one is refused by name (`invalid panel.<word>`); a wrun
231
+ * declaration's were checked by the kit, except the app's own bar words. A
232
+ * word outside the list is refused on both lanes. */
233
+ export function parseSetPanelData(value, options = {}) {
234
+ const wrun = options.lane === 'wrun';
235
+ if (!isRecord(value))
236
+ return { ok: false, rejectReason: 'payload is not an object' };
237
+ if (Object.keys(value).some((key) => !FRAME_KEYS.has(key)))
238
+ return { ok: false, rejectReason: 'unknown payload key' };
239
+ if (!nonnegativeInteger(value.chartIndex) || value.chartIndex >= MAX_PANES)
240
+ return { ok: false, rejectReason: 'invalid chartIndex' };
241
+ if (typeof value.payloadId !== 'string' || !OM_PANEL_PAYLOAD_ID_PATTERN.test(value.payloadId))
242
+ return { ok: false, rejectReason: 'invalid payloadId' };
243
+ const parts = value.payloadId.split(':');
244
+ if (parts[1] !== `c${value.chartIndex}`)
245
+ return { ok: false, rejectReason: 'payloadId does not match chartIndex' };
246
+ if (!nonnegativeInteger(value.revision))
247
+ return { ok: false, rejectReason: 'invalid revision stamp' };
248
+ if (value.clear !== undefined && value.clear !== true)
249
+ return { ok: false, rejectReason: 'invalid clear flag (only true is wire-legal)' };
250
+ if (value.clear === true) {
251
+ if (Object.keys(value).some((key) => !CLEAR_KEYS.has(key)))
252
+ return { ok: false, rejectReason: 'clear payload carries baggage' };
253
+ return {
254
+ ok: true,
255
+ data: { chartIndex: value.chartIndex, payloadId: value.payloadId, revision: value.revision, clear: true },
256
+ };
257
+ }
258
+ const raw = value.panel;
259
+ if (!isRecord(raw))
260
+ return { ok: false, rejectReason: 'invalid panel' };
261
+ if (Object.keys(raw).some((key) => !PANEL_KEYS.has(key) && !PANEL_STYLE_WORDS.has(key)))
262
+ return { ok: false, rejectReason: 'unknown panel key' };
263
+ if (typeof raw.id !== 'string' || !ID_PATTERN.test(raw.id))
264
+ return { ok: false, rejectReason: 'invalid panel.id' };
265
+ if (parts[2] !== raw.id)
266
+ return { ok: false, rejectReason: 'payloadId does not match panel.id' };
267
+ if (!boundedString(raw.title, 40))
268
+ return { ok: false, rejectReason: 'invalid panel.title' };
269
+ if (typeof raw.kind !== 'string' || !KINDS.has(raw.kind))
270
+ return { ok: false, rejectReason: 'invalid panel.kind' };
271
+ if (typeof raw.x !== 'string' || !X_WORDS.has(raw.x))
272
+ return { ok: false, rejectReason: 'invalid panel.x' };
273
+ if ((raw.kind === 'heatmap' || raw.kind === 'histogram') && raw.x !== 'category')
274
+ return { ok: false, rejectReason: `${raw.kind} panels use x: category` };
275
+ if (raw.place !== 'below' && raw.place !== 'side')
276
+ return { ok: false, rejectReason: 'invalid panel.place' };
277
+ if (raw.orientation !== undefined && raw.orientation !== 'vertical' && raw.orientation !== 'horizontal')
278
+ return { ok: false, rejectReason: 'invalid panel.orientation' };
279
+ if (raw.stacked !== undefined && typeof raw.stacked !== 'boolean')
280
+ return { ok: false, rejectReason: 'invalid panel.stacked' };
281
+ if (raw.labels !== undefined && typeof raw.labels !== 'boolean')
282
+ return { ok: false, rejectReason: 'invalid panel.labels' };
283
+ if (raw.bins !== undefined && (!nonnegativeInteger(raw.bins) || raw.bins < 2 || raw.bins > 200))
284
+ return { ok: false, rejectReason: 'invalid panel.bins' };
285
+ if (raw.hole !== undefined && (!finite(raw.hole) || raw.hole < 0 || raw.hole > 0.8))
286
+ return { ok: false, rejectReason: 'invalid panel.hole' };
287
+ const kind = raw.kind;
288
+ const badWord = invalidPanelStyleWord(raw, { kind, x: raw.x, place: raw.place }, wrun ? 'wrun' : 'push');
289
+ if (badWord !== undefined)
290
+ return { ok: false, rejectReason: `invalid panel.${badWord}` };
291
+ if (raw.series !== undefined || raw.kind === 'bars' || raw.kind === 'line' || raw.kind === 'table') {
292
+ const seriesCap = raw.kind === 'table' ? PANEL_MAX_TABLE_COLUMNS : PANEL_MAX_SERIES;
293
+ if (!Array.isArray(raw.series) || raw.series.length < 1 || raw.series.length > seriesCap)
294
+ return { ok: false, rejectReason: 'invalid panel.series' };
295
+ for (const series of raw.series) {
296
+ if (!isRecord(series))
297
+ return { ok: false, rejectReason: 'invalid panel.series' };
298
+ if (Object.keys(series).some((key) => !SERIES_KEYS.has(key) && !PANEL_SERIES_STYLE_WORDS.has(key)))
299
+ return { ok: false, rejectReason: 'unknown panel.series key' };
300
+ const seriesColor = wrun ? paintedColor : color;
301
+ if (!boundedString(series.name, 24) || (series.color !== undefined && !seriesColor(series.color)))
302
+ return { ok: false, rejectReason: 'invalid panel.series' };
303
+ const badSeriesWord = wrun ? undefined : invalidSeriesStyleWord(series, kind);
304
+ if (badSeriesWord !== undefined)
305
+ return { ok: false, rejectReason: `invalid panel.series.${badSeriesWord}` };
306
+ }
307
+ }
308
+ const panel = raw;
309
+ if (!Array.isArray(value.rows))
310
+ return { ok: false, rejectReason: 'rows is not an array' };
311
+ const rows = value.rows;
312
+ if (rows.length > panelRowCap(panel.kind))
313
+ return { ok: false, rejectReason: 'too many rows' };
314
+ if (!rows.every((row, index) => validRow(row, panel, index, rows.length)))
315
+ return { ok: false, rejectReason: 'invalid panel row' };
316
+ let source;
317
+ if (value.source !== undefined) {
318
+ if (!isRecord(value.source))
319
+ return { ok: false, rejectReason: 'invalid source' };
320
+ const rawSource = value.source;
321
+ if (Object.keys(rawSource).some((key) => !SOURCE_KEYS.has(key)))
322
+ return { ok: false, rejectReason: 'unknown source key' };
323
+ if (rawSource.kind !== 'watch' &&
324
+ rawSource.kind !== 'push' &&
325
+ rawSource.kind !== 'metric' &&
326
+ rawSource.kind !== 'custom')
327
+ return { ok: false, rejectReason: 'invalid source.kind' };
328
+ if (!boundedString(rawSource.ref, 160))
329
+ return { ok: false, rejectReason: 'invalid source.ref' };
330
+ if (!finite(rawSource.computedAtMs) || rawSource.computedAtMs < 0) {
331
+ return { ok: false, rejectReason: 'invalid source.computedAtMs' };
332
+ }
333
+ source = { kind: rawSource.kind, ref: rawSource.ref, computedAtMs: rawSource.computedAtMs };
334
+ }
335
+ return {
336
+ ok: true,
337
+ data: {
338
+ chartIndex: value.chartIndex,
339
+ payloadId: value.payloadId,
340
+ revision: value.revision,
341
+ panel: { ...panel, ...(panel.series ? { series: panel.series.map((series) => ({ ...series })) } : {}) },
342
+ rows: rows.map((row) => row.map(copyCell)),
343
+ ...(source ? { source } : {}),
344
+ },
345
+ };
346
+ }
347
+ /** The pane's share of the chart: a declared `height_frac` (SC09), else one
348
+ * weight for chart-shaped panels, else the height a table's rows need
349
+ * (header included, at the declared font size and cell padding) between the
350
+ * engine's pane minimum and half the chart. `chartHeightPx` is the chart
351
+ * canvas in CSS px. */
352
+ export function panelGridHeight(panel, rowCount, chartHeightPx) {
353
+ if (finite(panel.height_frac))
354
+ return panel.height_frac;
355
+ if (panel.kind !== 'table')
356
+ return PANEL_CHART_GRID_HEIGHT;
357
+ const rowPx = (panel.font_size ?? PANEL_TABLE_FONT_PX) + 2 * (panel.cell_padding ?? PANEL_TABLE_CELL_PADDING_PX);
358
+ const contentPx = PANEL_TABLE_TOP_OFFSET_PX + 2 * PANEL_TABLE_EDGE_PADDING_PX + (rowCount + 1) * rowPx;
359
+ const height = chartHeightPx !== undefined && chartHeightPx > 0 ? chartHeightPx : PANEL_FALLBACK_CHART_HEIGHT_PX;
360
+ const fraction = Math.min(PANEL_TABLE_MAX_GRID_HEIGHT, Math.max(PANEL_TABLE_MIN_GRID_HEIGHT, contentPx / height));
361
+ return Math.round(fraction * 1000) / 1000;
362
+ }
363
+ function numberSpec(entry) {
364
+ if (entry.format === undefined &&
365
+ entry.decimals === undefined &&
366
+ entry.signed === undefined &&
367
+ entry.unit === undefined)
368
+ return undefined;
369
+ return {
370
+ ...(entry.format !== undefined ? { format: entry.format } : {}),
371
+ ...(entry.decimals !== undefined ? { decimals: entry.decimals } : {}),
372
+ ...(entry.signed !== undefined ? { signed: entry.signed } : {}),
373
+ ...(entry.unit !== undefined ? { unit: entry.unit } : {}),
374
+ };
375
+ }
376
+ /** The engine's rich cell: `textColor` inks plain text, `color` inks a bar or
377
+ * a spark (an undecorated `color` would paint the cell background instead).
378
+ * A number, or a styled cell's `value`, prints through the column's format,
379
+ * else the table's (SC40); no format keeps the raw print. A styled cell's
380
+ * own background, text look, alignment and spans map onto the T2 table keys
381
+ * (SC14 SC22 SC35); a cell without an alignment takes its column's. */
382
+ export function engineTableCell(cell, column = {}, ctx = {}) {
383
+ const spec = column.spec ?? ctx.tableSpec;
384
+ const print = (value) => spec === undefined ? String(value) : formatWrunNumber(value, { ...spec, priceDigits: ctx.priceDigits });
385
+ const style = boundWrunPanelStyle();
386
+ const paneColor = (word) => style.wrunPaneColor(word, ctx.theme);
387
+ if (typeof cell === 'number') {
388
+ if (spec === undefined && column.align === undefined)
389
+ return cell;
390
+ return { text: print(cell), ...(column.align !== undefined ? { align: column.align } : {}) };
391
+ }
392
+ if (typeof cell === 'string')
393
+ return column.align === undefined ? cell : { text: cell, align: column.align };
394
+ if (!cell || typeof cell !== 'object' || Array.isArray(cell))
395
+ return cell;
396
+ const entry = cell;
397
+ const decorated = entry.bar !== undefined || (entry.spark !== undefined && entry.spark.length > 0);
398
+ const text = entry.text !== undefined ? entry.text : entry.value !== undefined ? print(entry.value) : undefined;
399
+ const align = entry.align ?? column.align;
400
+ return {
401
+ ...(text !== undefined ? { text } : {}),
402
+ ...(entry.bar !== undefined ? { bar: entry.bar } : {}),
403
+ ...(entry.spark !== undefined ? { spark: [...entry.spark] } : {}),
404
+ ...(entry.color !== undefined
405
+ ? decorated
406
+ ? { color: paneColor(entry.color) }
407
+ : { textColor: paneColor(entry.color) }
408
+ : {}),
409
+ ...(entry.text_color !== undefined ? { textColor: paneColor(entry.text_color) } : {}),
410
+ ...(entry.background_color !== undefined ? { bgcolor: paneColor(entry.background_color) } : {}),
411
+ ...(entry.opacity !== undefined ? { opacity: entry.opacity } : {}),
412
+ ...(entry.gradient !== undefined ? { gradient: entry.gradient.map(paneColor) } : {}),
413
+ ...(align !== undefined ? { align } : {}),
414
+ ...(entry.valign !== undefined ? { valign: entry.valign } : {}),
415
+ ...(entry.colspan !== undefined ? { colspan: entry.colspan } : {}),
416
+ ...(entry.rowspan !== undefined ? { rowspan: entry.rowspan } : {}),
417
+ ...(entry.font_size !== undefined ? { fontSize: entry.font_size } : {}),
418
+ ...(entry.font_weight !== undefined ? { fontWeight: wrunFontWeightCss(entry.font_weight) } : {}),
419
+ };
420
+ }
421
+ const pad2 = (value) => String(value).padStart(2, '0');
422
+ /** Labels for a time-keyed curve or bars panel, as short as the key set
423
+ * allows: clock times inside one day, dates for day-aligned keys, a year
424
+ * only past a year. `offsetMs` is the chart's display-timezone shift
425
+ * (SC37): the labels print the wall time of key + offset, UTC fields. */
426
+ export function panelTimeLabels(keys, offsetMs = 0) {
427
+ if (keys.length === 0)
428
+ return [];
429
+ const stamps = keys.map((key) => (key < 1e11 ? key * 1000 : key) + offsetMs);
430
+ let min = Infinity;
431
+ let max = -Infinity;
432
+ for (const stamp of stamps) {
433
+ min = Math.min(min, stamp);
434
+ max = Math.max(max, stamp);
435
+ }
436
+ const sameDay = Math.floor(min / DAY_MS) === Math.floor(max / DAY_MS);
437
+ const dayAligned = stamps.every((stamp) => stamp % DAY_MS === 0);
438
+ const withYear = max - min > 366 * DAY_MS;
439
+ return stamps.map((stamp) => {
440
+ const date = new Date(stamp);
441
+ const time = `${pad2(date.getUTCHours())}:${pad2(date.getUTCMinutes())}`;
442
+ if (sameDay)
443
+ return time;
444
+ const day = `${pad2(date.getUTCMonth() + 1)}-${pad2(date.getUTCDate())}`;
445
+ const dated = withYear ? `${date.getUTCFullYear()}-${day}` : day;
446
+ return dayAligned ? dated : `${dated} ${time}`;
447
+ });
448
+ }
449
+ /** A time key in ms (keys under 1e11 are seconds). */
450
+ function timeKeyMs(key) {
451
+ return key < 1e11 ? key * 1000 : key;
452
+ }
453
+ /** A bars value cell's number (a styled cell's `value`). */
454
+ function barCellValue(cell) {
455
+ return isRecord(cell) ? cell.value : cell;
456
+ }
457
+ /** Merge a style patch into engine settings: a `series` array patch merges
458
+ * per index into the mounted series entries instead of replacing them;
459
+ * undefined values leave the key alone. */
460
+ function mergeSettings(target, patch) {
461
+ if (!isRecord(patch))
462
+ return;
463
+ for (const [key, value] of Object.entries(patch)) {
464
+ if (value === undefined)
465
+ continue;
466
+ if (key === 'series' && Array.isArray(value) && Array.isArray(target.series)) {
467
+ mergeSeries(target.series, value);
468
+ continue;
469
+ }
470
+ target[key] = value;
471
+ }
472
+ }
473
+ function mergeSeries(target, patches) {
474
+ if (!Array.isArray(patches))
475
+ return;
476
+ patches.forEach((patch, index) => {
477
+ const entry = target[index];
478
+ if (!entry || !isRecord(patch))
479
+ return;
480
+ for (const [key, value] of Object.entries(patch)) {
481
+ if (value !== undefined)
482
+ entry[key] = value;
483
+ }
484
+ });
485
+ }
486
+ /** Domain keys and presentation strings never enter the engine's numeric axes.
487
+ * The panel title stays on the overlay name (the pane's legend pill); the
488
+ * engine's in-canvas chrome title would print it a second time over that pill.
489
+ * `ctx` (the wrun lane) resolves colour words against the chart palette and
490
+ * hands the style mappers the price digits and display timezone; the push
491
+ * lane passes none and its `#rrggbb` colours pass through unchanged. */
492
+ export function translatePanelToEngine(frame, ctx = {}) {
493
+ const { panel, rows, chrome } = frame;
494
+ const style = boundWrunPanelStyle();
495
+ const paneColor = (word) => style.wrunPaneColor(word, ctx.theme);
496
+ const fontFamily = wrunFontFamilyCss(panel.font_family) ?? WRUN_FONT_FAMILY_CSS.ui;
497
+ const formatDomainValue = style.wrunPanelFormatter(panel, rows, ctx);
498
+ const name = chrome?.title ?? panel.title;
499
+ const hooks = (plot) => ({
500
+ ...(formatDomainValue !== undefined ? { formatDomainValue } : {}),
501
+ ...(formatDomainValue !== undefined && plot === 'curve'
502
+ ? { getYLabel: (value) => formatDomainValue(value, 'axis', -1) }
503
+ : {}),
504
+ });
505
+ /** The engine series entries: name and resolved colour; the style words
506
+ * reach them as the line mapper's per-index patches. */
507
+ const series = panel.series?.map((entry) => ({
508
+ name: entry.name,
509
+ ...(entry.color !== undefined ? { color: paneColor(entry.color) } : {}),
510
+ }));
511
+ const optional = (...keys) => Object.fromEntries(keys.filter((key) => panel[key] !== undefined).map((key) => [key, panel[key]]));
512
+ const chromeStyle = panel.chrome !== undefined && CHROME_KINDS.has(panel.kind) ? { chromeStyle: panel.chrome } : {};
513
+ const finish = (plot, engineRows, settings) => {
514
+ mergeSettings(settings, style.wrunPanelCommonStyle(panel, ctx));
515
+ return { plot, rows: engineRows, settings, name, hooks: hooks(plot) };
516
+ };
517
+ switch (panel.kind) {
518
+ case 'bars': {
519
+ const keys = rows.map((row) => row[0]);
520
+ const xLabels = panel.x === 'time'
521
+ ? panelTimeLabels(keys, ctx.timezoneOffsetMs ?? 0)
522
+ : (panel.x === 'index' || panel.x === 'number') && panel.x_format !== undefined && formatDomainValue
523
+ ? keys.map((key) => formatDomainValue(key, 'x', -1))
524
+ : keys.map(String);
525
+ const coloured = rows.some((row) => row.slice(1).some((cell) => isRecord(cell) && cell.color !== undefined));
526
+ const settings = {
527
+ xLabels,
528
+ series,
529
+ ...optional('stacked', 'orientation', 'labels'),
530
+ ...(coloured
531
+ ? {
532
+ barColors: rows.map((row) => row
533
+ .slice(1)
534
+ .map((cell) => (isRecord(cell) && typeof cell.color === 'string' ? paneColor(cell.color) : null))),
535
+ }
536
+ : {}),
537
+ fontFamily,
538
+ ...chromeStyle,
539
+ };
540
+ const panelRows = rows.map((row, index) => [index, ...row.slice(1).map(barCellValue)]);
541
+ const result = finish('bars', panelRows, settings);
542
+ mergeSettings(result.settings, style.wrunBarsStyle(panel, chrome, rows, ctx));
543
+ return result;
544
+ }
545
+ case 'line': {
546
+ const effective = style.wrunPanelChrome(panel);
547
+ let panelRows;
548
+ let settings;
549
+ if (panel.x === 'time') {
550
+ const sorted = [...rows].sort((a, b) => a[0] - b[0]);
551
+ if (effective === 'grid' || effective === 'none') {
552
+ // The rich curve spaces points by their time (gaps stay gaps) and
553
+ // labels the axis in the chart's display timezone (SC37).
554
+ panelRows = sorted.map((row) => [timeKeyMs(row[0]), ...row.slice(1)]);
555
+ settings = {
556
+ series,
557
+ ...optional('stacked'),
558
+ xFormat: 'time',
559
+ xTimeOffsetMs: ctx.timezoneOffsetMs ?? 0,
560
+ fontFamily,
561
+ ...chromeStyle,
562
+ };
563
+ }
564
+ else {
565
+ // The boxed curve formats its ticks as plain numbers (an epoch reads
566
+ // "1.8B"); a category curve prints the UTC labels it is given.
567
+ panelRows = sorted.map((row, index) => [index, ...row.slice(1)]);
568
+ settings = {
569
+ xLabels: panelTimeLabels(sorted.map((row) => row[0])),
570
+ series,
571
+ ...optional('stacked'),
572
+ fontFamily,
573
+ ...chromeStyle,
574
+ };
575
+ }
576
+ }
577
+ else {
578
+ panelRows = rows.map((row, index) => [panel.x === 'category' ? index : row[0], ...row.slice(1)]);
579
+ settings = {
580
+ ...(panel.x === 'category' ? { xLabels: rows.map((row) => String(row[0])) } : {}),
581
+ series,
582
+ ...optional('stacked'),
583
+ fontFamily,
584
+ ...chromeStyle,
585
+ };
586
+ }
587
+ const result = finish('curve', panelRows, settings);
588
+ const line = style.wrunLineStyle(panel, chrome, rows, ctx);
589
+ mergeSettings(result.settings, line?.settings);
590
+ if (Array.isArray(result.settings.series))
591
+ mergeSeries(result.settings.series, line?.series);
592
+ return result;
593
+ }
594
+ case 'histogram': {
595
+ const labels = rows.map((row) => String(row[0]));
596
+ const bins = panel.bins ?? 0;
597
+ const xLabels = bins > labels.length ? [...labels, ...new Array(bins - labels.length).fill('')] : labels;
598
+ const settings = {
599
+ style: 'bars',
600
+ xLabels,
601
+ ...optional('orientation', 'labels'),
602
+ ...(panel.color !== undefined ? { color: paneColor(panel.color) } : {}),
603
+ fontFamily,
604
+ ...chromeStyle,
605
+ };
606
+ const result = finish('curve', rows.map((row, index) => [index, row[1]]), settings);
607
+ mergeSettings(result.settings, style.wrunHistogramStyle(panel, chrome, rows, ctx));
608
+ return result;
609
+ }
610
+ case 'scatter': {
611
+ const settings = {
612
+ ...(rows.some((row) => row[4] !== undefined)
613
+ ? { pointColors: rows.map((row) => (row[4] !== undefined ? paneColor(row[4]) : null)) }
614
+ : {}),
615
+ ...(rows.some((row) => row[5] !== undefined) ? { pointLabels: rows.map((row) => row[5] ?? '') } : {}),
616
+ ...optional('labels'),
617
+ ...(panel.color !== undefined ? { color: paneColor(panel.color) } : {}),
618
+ fontFamily,
619
+ ...chromeStyle,
620
+ };
621
+ const result = finish('scatter', rows.map((row) => (row[3] !== undefined ? [row[1], row[2], row[3]] : [row[1], row[2]])), settings);
622
+ mergeSettings(result.settings, style.wrunScatterStyle(panel, chrome, rows, ctx));
623
+ return result;
624
+ }
625
+ case 'pie': {
626
+ const settings = {
627
+ labels: rows.map((row) => row[0]),
628
+ ...(rows.some((row) => row[2] !== undefined)
629
+ ? { colors: rows.map((row) => (row[2] !== undefined ? paneColor(row[2]) : null)) }
630
+ : {}),
631
+ ...optional('hole'),
632
+ ...(panel.labels !== undefined ? { labels_on: panel.labels } : {}),
633
+ fontFamily,
634
+ ...chromeStyle,
635
+ };
636
+ const result = finish('piePane', rows.map((row, index) => [index, row[1]]), settings);
637
+ mergeSettings(result.settings, style.wrunPieStyle(panel, chrome, rows, ctx));
638
+ return result;
639
+ }
640
+ case 'heatmap': {
641
+ // The engine's matrix reads sparse `[rowIndex, colIndex, value]` triplets
642
+ // against its row and column label lists; a missing cell stays empty.
643
+ const xKeys = [...new Set(rows.map((row) => row[0]))];
644
+ const yKeys = [...new Set(rows.map((row) => row[1]))];
645
+ const xIndices = new Map(xKeys.map((key, index) => [key, index]));
646
+ const yIndices = new Map(yKeys.map((key, index) => [key, index]));
647
+ const settings = {
648
+ rowLabels: yKeys.map(String),
649
+ colLabels: xKeys.map(String),
650
+ // The signed scale is the engine's own diverging default (SC06).
651
+ ...(panel.scale === 'signed' ? {} : { palette: [...DEFAULT_HEATMAP_PALETTE] }),
652
+ fontFamily,
653
+ };
654
+ const result = finish('matrix', rows.map((row) => [yIndices.get(row[1]), xIndices.get(row[0]), row[2]]), settings);
655
+ mergeSettings(result.settings, style.wrunHeatmapStyle(panel, chrome, {
656
+ x: new Map(xKeys.map((key, index) => [String(key), index])),
657
+ y: new Map(yKeys.map((key, index) => [String(key), index])),
658
+ columns: xKeys.length,
659
+ }, ctx));
660
+ return result;
661
+ }
662
+ case 'table': {
663
+ // The engine's fixed table paints ONE decorator row whose payload holds
664
+ // the grid (header first) and its own anchor; raw cell rows never paint.
665
+ const columns = (panel.series ?? []).map((entry) => ({
666
+ ...(entry.align !== undefined ? { align: entry.align } : {}),
667
+ ...(() => {
668
+ const spec = numberSpec(entry);
669
+ return spec !== undefined ? { spec } : {};
670
+ })(),
671
+ }));
672
+ const cellContext = {
673
+ ...(ctx.theme !== undefined ? { theme: ctx.theme } : {}),
674
+ ...(ctx.priceDigits !== undefined ? { priceDigits: ctx.priceDigits } : {}),
675
+ ...(() => {
676
+ const spec = numberSpec(panel);
677
+ return spec !== undefined ? { tableSpec: spec } : {};
678
+ })(),
679
+ };
680
+ const header = (panel.series ?? []).map((entry, index) => {
681
+ const align = columns[index]?.align;
682
+ if (entry.color === undefined && align === undefined)
683
+ return entry.name;
684
+ return {
685
+ text: entry.name,
686
+ ...(entry.color !== undefined ? { textColor: paneColor(entry.color) } : {}),
687
+ ...(align !== undefined ? { align } : {}),
688
+ };
689
+ });
690
+ const payload = {
691
+ data: [
692
+ header,
693
+ ...rows.map((row) => row.map((cell, index) => engineTableCell(cell, columns[index], cellContext))),
694
+ ],
695
+ position: 'top_left',
696
+ x: 0,
697
+ y: PANEL_TABLE_TOP_OFFSET_PX,
698
+ headerRow: true,
699
+ headerColumn: panel.header_column ?? false,
700
+ fontSize: panel.font_size ?? PANEL_TABLE_FONT_PX,
701
+ cellPadding: panel.cell_padding ?? PANEL_TABLE_CELL_PADDING_PX,
702
+ borderWidth: panel.border_width ?? 1,
703
+ fontFamily,
704
+ };
705
+ mergeSettings(payload, style.wrunTableStyle(panel, ctx));
706
+ return { plot: 'table', rows: [[null, payload]], settings: {}, name, hooks: hooks('table') };
707
+ }
708
+ case 'tiles': {
709
+ const settings = {
710
+ tiles: rows.map((row) => ({
711
+ label: row[0],
712
+ ...(typeof row[1] === 'string' ? { format: 'text' } : {}),
713
+ ...(row[2] !== undefined && row[2] !== null ? { caption: row[2] } : {}),
714
+ ...(row[3] !== undefined && row[3] !== null ? { color: paneColor(row[3]) } : {}),
715
+ ...(row[4] !== undefined && row[4] !== null ? { spark: [...row[4]] } : {}),
716
+ ...(panel.accent === 'auto' && typeof row[1] === 'number' ? { accent: 'auto' } : {}),
717
+ })),
718
+ ...(panel.columns !== undefined ? { columns: panel.columns } : {}),
719
+ fontFamily,
720
+ };
721
+ const result = finish('tiles', rows.map((row, index) => [index, row[1]]), settings);
722
+ mergeSettings(result.settings, style.wrunTilesStyle(panel, rows, ctx));
723
+ return result;
724
+ }
725
+ }
726
+ }
727
+ /** Settings that describe the carved pane and its legend chrome, set at
728
+ * addOverlay and never rewritten by an in-place refresh (the engine also
729
+ * rewrites `gridHeight` to the fraction it carved), plus the focus stamps
730
+ * pane-maximize.util writes while a pane is maximized (SC10) and the pane
731
+ * anchor the engine reads once at addOverlay (C6 `insertBeforePaneId`, the
732
+ * wrun mount's: a later run's anchor is neither written nor deleted, so an
733
+ * anchor change never churns a mounted pane). */
734
+ export const PANEL_PANE_LAYOUT_KEYS = new Set([
735
+ 'gridHeight',
736
+ 'hideSettingsBtn',
737
+ 'hideCloseBtn',
738
+ 'hideVisibilityBtn',
739
+ 'hideMergePaneBtn',
740
+ 'hidePinToChartBtn',
741
+ 'legendRemoveLabel',
742
+ 'paneMaximized',
743
+ 'expandedView',
744
+ 'insertBeforePaneId',
745
+ ]);
746
+ /** A mounted panel overlay can take new rows in place when it is the same
747
+ * pane: same engine species, same axis ownership, same chrome and the height
748
+ * it was carved with (a table whose row count moved its height is re-carved). */
749
+ export function samePanelPane(mounted, next) {
750
+ return (mounted.plot === next.plot &&
751
+ (mounted.hidePriceScale ?? false) === (next.hidePriceScale ?? false) &&
752
+ (mounted.chromeStyle ?? null) === (next.chromeStyle ?? null) &&
753
+ Math.abs(mounted.gridHeight - next.gridHeight) < 0.005);
754
+ }
755
+ /** The caption text a pane legend prints from `settings.customTitle` (a
756
+ * function form, the mount's, or a string). */
757
+ function captionText(value) {
758
+ if (typeof value === 'string')
759
+ return value;
760
+ if (typeof value !== 'function')
761
+ return undefined;
762
+ try {
763
+ const text = value();
764
+ return typeof text === 'string' ? text : undefined;
765
+ }
766
+ catch {
767
+ return undefined;
768
+ }
769
+ }
770
+ const PANEL_HOOK_KEYS = ['formatDomainValue', 'getYLabel'];
771
+ /** Refresh a mounted panel overlay from a new frame: the row-level settings
772
+ * (labels, series, palette, style) move onto the engine's meta, the run's
773
+ * name and formatter hooks follow (the legend is poked when its title or
774
+ * caption moved), then the rows swap through the engine's own update path.
775
+ * The pane keeps its place in the stack and whatever the reader did to it;
776
+ * a remove + add sent every refreshed pane to the bottom, and two panels
777
+ * updating leapfrogged. The wrun child mount and the om push lane share
778
+ * this one path. */
779
+ export function refreshPanelOverlayInPlace(tc, id, next, mountedSettings) {
780
+ const meta = tc.metadata.find((candidate) => candidate.id === id);
781
+ if (!meta)
782
+ throw new Error(`panel overlay '${id}' is not mounted`);
783
+ const settings = (meta.settings ??= {});
784
+ const captionBefore = captionText(settings.customTitle);
785
+ for (const key of Object.keys(mountedSettings)) {
786
+ if (!PANEL_PANE_LAYOUT_KEYS.has(key) && !(key in next.settings))
787
+ delete settings[key];
788
+ }
789
+ for (const [key, value] of Object.entries(next.settings)) {
790
+ if (!PANEL_PANE_LAYOUT_KEYS.has(key))
791
+ settings[key] = value;
792
+ }
793
+ let legendMoved = captionText(settings.customTitle) !== captionBefore;
794
+ if (next.name !== undefined && meta.name !== next.name) {
795
+ meta.name = next.name;
796
+ legendMoved = true;
797
+ }
798
+ if (next.hooks !== undefined) {
799
+ for (const key of PANEL_HOOK_KEYS) {
800
+ const hook = next.hooks[key];
801
+ if (typeof hook === 'function')
802
+ meta[key] = hook;
803
+ else
804
+ delete meta[key];
805
+ }
806
+ }
807
+ if (legendMoved)
808
+ meta.legends?.refresh?.();
809
+ if (typeof tc.updateData === 'function') {
810
+ tc.updateData(id, next.data, { shouldResetRange: true });
811
+ return;
812
+ }
813
+ meta.rawData = next.data;
814
+ meta.data = next.data;
815
+ }