@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,538 @@
1
+ import { classifyKScriptOutputRole, colorLiteralHasVisibleAlpha } from './kscript-output-role.util.js';
2
+ import { isRecord, kScriptRunOutputFor } from './kscript-run-result-shape.util.js';
3
+ import { densestKScriptOutputKey, KSCRIPT_CONTAINER_OUTPUT_KEY } from './kscript-titancharts-container.util.js';
4
+ /**
5
+ * Multi-value legend for container-native kScript overlays.
6
+ *
7
+ * Titan mounts every v2 run under a synthetic parent (`__kiyoKScriptContainer`,
8
+ * plot 'empty') and hangs the real outputs off `meta.nestedMeta`. The legend
9
+ * hover bridge only ever visits the PARENT row, so a script with several lines
10
+ * (Chandelier long/short stops, EWMA vs equal-weight) used to read one column
11
+ * of whichever child seeded the container. This module resolves one entry per
12
+ * value-bearing child at the hovered timestamp so the legend lists every line.
13
+ *
14
+ * Pure and node-testable: no titan, DOM, or store access.
15
+ */
16
+ /**
17
+ * Slot pool size. The engine folds every legend entry past its visible width
18
+ * into a `+N` chip whose popover lists the hidden slots, so the pool only has
19
+ * to cover what a script plausibly plots, not what fits on one row. Padded
20
+ * empty slots are never counted toward the chip.
21
+ */
22
+ export const KSCRIPT_LEGEND_MAX_ENTRIES = 16;
23
+ /**
24
+ * Plot kinds whose rows carry a per-bar numeric reading worth a legend slot:
25
+ * raw kScript names and their titan-mapped counterparts (line -> spline,
26
+ * histogram -> volume). Markers, drawings, tables, and backgrounds are not
27
+ * readouts.
28
+ */
29
+ const KSCRIPT_LEGEND_VALUE_PLOTS = new Set([
30
+ 'line',
31
+ 'spline',
32
+ 'stepLine',
33
+ 'step',
34
+ 'histogram',
35
+ 'volume',
36
+ 'area',
37
+ 'baseline',
38
+ 'markerLine',
39
+ 'bar',
40
+ 'band',
41
+ 'candle',
42
+ 'hollowCandle',
43
+ 'ohlcBar',
44
+ 'highLow',
45
+ ]);
46
+ /** The candle plot kinds (WRUN-STYLE OT28): rows `[ts, open, high, low,
47
+ * close]`, one legend entry reading the four values. */
48
+ const KSCRIPT_LEGEND_CANDLE_PLOTS = new Set(['candle', 'hollowCandle', 'ohlcBar', 'highLow']);
49
+ /** The column plot kinds whose rows colour by sign against their base
50
+ * column (`[ts, value, base?]`): colors[0] at or above it, colors[1] below. */
51
+ const KSCRIPT_LEGEND_COLUMN_PLOTS = new Set(['bar', 'histogram', 'volume']);
52
+ /** The mark plot kinds (a wrun `point` or `shape` output, a kScript
53
+ * plotShape): one glyph on each bar that has a value. */
54
+ const KSCRIPT_LEGEND_MARK_PLOTS = new Set([
55
+ 'point',
56
+ 'circle',
57
+ 'cross',
58
+ 'triangle',
59
+ 'triangleDown',
60
+ 'diamond',
61
+ 'arrowUp',
62
+ 'arrowDown',
63
+ 'flag',
64
+ 'square',
65
+ 'char',
66
+ ]);
67
+ /**
68
+ * The plot kinds drawn on their own bar only: a column, a candle group and a
69
+ * mark. Their rows are sparse (a bar the plot skips holds no row), so a
70
+ * reading at a bar takes that bar's own row and a bar without one reads as a
71
+ * hole ('-'), never the last drawn value carried forward (TEMPLATE-QA B14
72
+ * B21: a regime histogram, an "absorbed" bar). A line keeps the floor row,
73
+ * titan's rule: it is drawn across the bars between its rows.
74
+ */
75
+ const KSCRIPT_LEGEND_OWN_BAR_PLOTS = new Set([
76
+ ...KSCRIPT_LEGEND_COLUMN_PLOTS,
77
+ ...KSCRIPT_LEGEND_CANDLE_PLOTS,
78
+ ...KSCRIPT_LEGEND_MARK_PLOTS,
79
+ ]);
80
+ const KSCRIPT_LEGEND_FALLBACK_COLOR = '#FFC300';
81
+ const KSCRIPT_LEGEND_DEFAULT_OHLC_LABELS = { open: 'O', high: 'H', low: 'L', close: 'C' };
82
+ /**
83
+ * Value text ink for every kScript legend slot. The plot color rides the
84
+ * slot's `swatch` (titan draws it as a dot before the cell) and the line's name
85
+ * its `label` (titan renders it in the legend title ink), so the reading
86
+ * itself stays in the neutral value ink the OHLC row uses: a dark plot color
87
+ * (a crimson Tenkan reads 1.8:1 on a dark chart) never has to carry text.
88
+ */
89
+ export const KSCRIPT_LEGEND_VALUE_INK = 'var(--legend-value-color)';
90
+ /**
91
+ * Slot id for entry `index`. The first slot keeps today's single-value id so a
92
+ * one-line script is unchanged and the ADX ids (`value-0..2`) stay distinct.
93
+ */
94
+ function kScriptLegendSlotId(index) {
95
+ return index === 0 ? 'value' : `value-${index}`;
96
+ }
97
+ /**
98
+ * The full slot pool. Titan creates one span per initLegendText entry at
99
+ * addOverlay time, before any run result has mounted nestedMeta, and silently
100
+ * drops updates for ids it never created: every slot the multi-entry branch
101
+ * may address has to be born with the static legend.
102
+ */
103
+ export function kScriptLegendSlotIds() {
104
+ return Array.from({ length: KSCRIPT_LEGEND_MAX_ENTRIES }, (_, index) => kScriptLegendSlotId(index));
105
+ }
106
+ /**
107
+ * The slot pool as initLegendText cells. Every slot is born empty and LABELLED
108
+ * (`label: ''`): titan builds a name span + value host only for a cell that
109
+ * carries a label at creation, and later updates fill both.
110
+ */
111
+ export function kScriptLegendSlotCells() {
112
+ return kScriptLegendSlotIds().map((id) => ({
113
+ id,
114
+ text: '',
115
+ label: '',
116
+ color: KSCRIPT_LEGEND_VALUE_INK,
117
+ isStatic: false,
118
+ }));
119
+ }
120
+ function isLegendValuePlot(nested) {
121
+ return [nested.plotType, nested.plot].some((plot) => typeof plot === 'string' && KSCRIPT_LEGEND_VALUE_PLOTS.has(plot));
122
+ }
123
+ /**
124
+ * A plot its run output opted out of the legend (`legend: false`: a sheet
125
+ * range ported from a kScript fillBetween, which paints as before but, like
126
+ * the engine's own 'fillBetween' output, is never a legend line; or an output
127
+ * the sheet keeps off the legend). The engine never emits the field. Titan
128
+ * does not copy it onto the nested meta, so the run output the meta was
129
+ * mounted from is read too.
130
+ */
131
+ function optsOutOfLegend(meta, nested) {
132
+ return nested.legend === false || kScriptRunOutputFor(meta, nested)?.legend === false;
133
+ }
134
+ /**
135
+ * The nested outputs that earn a legend slot, in declaration order, capped at
136
+ * the pool size. `includeUnlisted` also returns the drawn value plots a run
137
+ * output opted out of the legend (`legend: false`) and lifts the cap: the
138
+ * set a series hover walks (a line the legend does not list is still on the
139
+ * chart); the legend itself never passes it.
140
+ */
141
+ export function kScriptLegendLines(meta, options = {}) {
142
+ if (!isRecord(meta) || !Array.isArray(meta.nestedMeta))
143
+ return [];
144
+ const lines = [];
145
+ for (let nestedIndex = 0; nestedIndex < meta.nestedMeta.length; nestedIndex += 1) {
146
+ if (!options.includeUnlisted && lines.length >= KSCRIPT_LEGEND_MAX_ENTRIES)
147
+ break;
148
+ const nested = meta.nestedMeta[nestedIndex];
149
+ if (!isRecord(nested) || !isLegendValuePlot(nested))
150
+ continue;
151
+ if (classifyKScriptOutputRole(nested).isDecorator)
152
+ continue;
153
+ const settings = isRecord(nested.settings) ? nested.settings : undefined;
154
+ if (settings?.visible === false)
155
+ continue;
156
+ if (!options.includeUnlisted && optsOutOfLegend(meta, nested))
157
+ continue;
158
+ lines.push({ meta: nested, nestedIndex });
159
+ }
160
+ return lines;
161
+ }
162
+ function rowTimestamp(row) {
163
+ return Array.isArray(row) && typeof row[0] === 'number' ? row[0] : Number.NEGATIVE_INFINITY;
164
+ }
165
+ /** Binary-search the last row whose timestamp is <= ts (rows ascend by row[0], titan's floorRow rule). */
166
+ export function floorRowAt(rows, ts) {
167
+ if (!Array.isArray(rows) || rows.length === 0 || !Number.isFinite(ts))
168
+ return undefined;
169
+ let lo = 0;
170
+ let hi = rows.length - 1;
171
+ let found = -1;
172
+ while (lo <= hi) {
173
+ const mid = (lo + hi) >>> 1;
174
+ if (rowTimestamp(rows[mid]) <= ts) {
175
+ found = mid;
176
+ lo = mid + 1;
177
+ }
178
+ else {
179
+ hi = mid - 1;
180
+ }
181
+ }
182
+ const row = found >= 0 ? rows[found] : undefined;
183
+ return Array.isArray(row) ? row : undefined;
184
+ }
185
+ function lastRow(rows) {
186
+ if (!Array.isArray(rows) || rows.length === 0)
187
+ return undefined;
188
+ const row = rows[rows.length - 1];
189
+ return Array.isArray(row) ? row : undefined;
190
+ }
191
+ function finiteNumber(value) {
192
+ if (typeof value === 'number')
193
+ return Number.isFinite(value) ? value : undefined;
194
+ if (typeof value === 'string' && value.trim() !== '') {
195
+ const numericValue = Number(value);
196
+ return Number.isFinite(numericValue) ? numericValue : undefined;
197
+ }
198
+ return undefined;
199
+ }
200
+ function stringList(value) {
201
+ return Array.isArray(value)
202
+ ? value.filter((entry) => typeof entry === 'string' && entry.trim().length > 0)
203
+ : [];
204
+ }
205
+ function lineRows(line) {
206
+ const raw = line.meta.rawData;
207
+ if (Array.isArray(raw) && raw.length)
208
+ return raw;
209
+ const active = line.meta.activeData;
210
+ return Array.isArray(active) ? active : undefined;
211
+ }
212
+ /** The run-result output key a legend line was mounted from (the wrun
213
+ * output NAME for a sheet run: the adapter keys `outputs` by it), read the
214
+ * way titan keys a container child (outputKey, then plotKey, then plotId). */
215
+ export function kScriptLegendLineOutputKey(line) {
216
+ for (const key of [line.meta.outputKey, line.meta.plotKey, line.meta.plotId]) {
217
+ if (typeof key === 'string' && key.length > 0)
218
+ return key;
219
+ }
220
+ return undefined;
221
+ }
222
+ /**
223
+ * The declared value format of a legend line (the sheet output's `format`
224
+ * word, WRUN-INPUTS-PLAN 1.3): the adapter stamps it on the run output the
225
+ * child was mounted from (`format`, an FE-only field like `legend`), and
226
+ * titan does not copy it onto the nested meta, so the run output is read
227
+ * too. Undefined = the caller's placement rule.
228
+ */
229
+ export function kScriptLegendLineFormat(meta, line) {
230
+ const own = line.meta.format;
231
+ if (typeof own === 'string' && own.length > 0)
232
+ return own;
233
+ if (!isRecord(meta))
234
+ return undefined;
235
+ const declared = kScriptRunOutputFor(meta, line.meta)?.format;
236
+ return typeof declared === 'string' && declared.length > 0 ? declared : undefined;
237
+ }
238
+ /**
239
+ * The declared format companions of a legend line (WRUN-STYLE OT32): the
240
+ * adapter stamps `formatOptions` ({ decimals, signed, unit }) on the run
241
+ * output beside `format`; titan does not copy it onto the nested meta, so
242
+ * the run output is read too. Undefined = none declared.
243
+ */
244
+ export function kScriptLegendLineFormatOptions(meta, line) {
245
+ const own = line.meta.formatOptions;
246
+ const declared = isRecord(own)
247
+ ? own
248
+ : isRecord(meta)
249
+ ? kScriptRunOutputFor(meta, line.meta)?.formatOptions
250
+ : undefined;
251
+ if (!isRecord(declared))
252
+ return undefined;
253
+ const options = {
254
+ ...(typeof declared.decimals === 'number' ? { decimals: declared.decimals } : {}),
255
+ ...(typeof declared.signed === 'boolean' ? { signed: declared.signed } : {}),
256
+ ...(typeof declared.unit === 'string' ? { unit: declared.unit } : {}),
257
+ };
258
+ return Object.keys(options).length > 0 ? options : undefined;
259
+ }
260
+ function plotKindOf(line) {
261
+ for (const plot of [line.meta.plotType, line.meta.plot]) {
262
+ if (typeof plot === 'string')
263
+ return plot;
264
+ }
265
+ return undefined;
266
+ }
267
+ /** A candle group's legend line (OT28): rows `[ts, open, high, low, close]`. */
268
+ function isCandleLine(line) {
269
+ const plot = plotKindOf(line);
270
+ return plot !== undefined && KSCRIPT_LEGEND_CANDLE_PLOTS.has(plot);
271
+ }
272
+ /** A line's row at `ts` (KSCRIPT_LEGEND_OWN_BAR_PLOTS): the bar's own row for
273
+ * a plot drawn on its own bar only, the floor row for a line; the last row
274
+ * without a timestamp. */
275
+ function lineRowAt(line, ts) {
276
+ const rows = lineRows(line);
277
+ if (ts === undefined)
278
+ return lastRow(rows);
279
+ const row = floorRowAt(rows, ts);
280
+ const plot = plotKindOf(line);
281
+ if (plot !== undefined && KSCRIPT_LEGEND_OWN_BAR_PLOTS.has(plot))
282
+ return row?.[0] === ts ? row : undefined;
283
+ return row;
284
+ }
285
+ /** A stacked column's legend line (OT38): rows `[ts, top, bottom]`, the
286
+ * adapter's `stacked: true` on the run output (titan never copies it). */
287
+ function isStackedLine(meta, line) {
288
+ if (line.meta.stacked === true)
289
+ return true;
290
+ return isRecord(meta) && kScriptRunOutputFor(meta, line.meta)?.stacked === true;
291
+ }
292
+ /** A field the adapter put on the run output, read off the nested meta, its
293
+ * settings, then the run output it was mounted from. */
294
+ function lineField(meta, line, key) {
295
+ const own = line.meta[key];
296
+ if (own !== undefined)
297
+ return own;
298
+ const settings = isRecord(line.meta.settings) ? line.meta.settings : undefined;
299
+ if (settings?.[key] !== undefined)
300
+ return settings[key];
301
+ return isRecord(meta) ? kScriptRunOutputFor(meta, line.meta)?.[key] : undefined;
302
+ }
303
+ /** The colour a line paints `row` in: its per-bar colour (the row's `color`
304
+ * style cell into the output's `color` table, the engine's styled-row
305
+ * channel), else a candle's body colour (close >= open picks colors[0], else
306
+ * colors[1]), else a column's sign colour (the row value against its base
307
+ * column); undefined for a line with one colour. */
308
+ function rowPaint(meta, line, row) {
309
+ const table = stringList(lineField(meta, line, 'color'));
310
+ const properties = lineField(meta, line, 'styleProperties');
311
+ const offset = lineField(meta, line, 'styleDataOffset');
312
+ if (table.length > 0 && Array.isArray(properties) && typeof offset === 'number') {
313
+ const at = properties.indexOf('color');
314
+ const slot = at >= 0 ? finiteNumber(row[offset + at]) : undefined;
315
+ if (slot !== undefined && table[slot] !== undefined)
316
+ return table[slot];
317
+ }
318
+ const colors = stringList(lineField(meta, line, 'colors'));
319
+ const plot = plotKindOf(line);
320
+ if (plot !== undefined && KSCRIPT_LEGEND_CANDLE_PLOTS.has(plot) && colors.length > 0) {
321
+ const open = finiteNumber(row[1]);
322
+ const close = finiteNumber(row[4]);
323
+ if (open !== undefined && close !== undefined)
324
+ return (close >= open ? colors[0] : colors[1]) ?? colors[0];
325
+ }
326
+ if (plot !== undefined && KSCRIPT_LEGEND_COLUMN_PLOTS.has(plot) && colors.length > 0) {
327
+ const value = finiteNumber(row[1]);
328
+ const base = finiteNumber(row[2]) ?? 0;
329
+ if (value !== undefined)
330
+ return (value >= base ? colors[0] : colors[1]) ?? colors[0];
331
+ }
332
+ return undefined;
333
+ }
334
+ /**
335
+ * The colour a legend line is drawn in at `ts` (WRUN-STYLE OT27): what its
336
+ * own output paints the row the reading takes (rowPaint: a per-bar colour, a
337
+ * candle's or a column's sign colour), else the line's own colour (lineColor),
338
+ * also on a row painted fully transparent (a clear rung draws nothing, and
339
+ * titan would show it as a black dot). The row is the reading's (lineRowAt: a
340
+ * column, candle or mark at a bar without its own row shows the line colour);
341
+ * no timestamp reads the last row. It takes no slot: a line's colour is never
342
+ * read from the overlay palette by position.
343
+ */
344
+ export function kScriptLegendLineSwatchAt(meta, line, ts) {
345
+ const row = lineRowAt(line, ts);
346
+ const painted = row === undefined ? undefined : rowPaint(meta, line, row);
347
+ return painted !== undefined && colorLiteralHasVisibleAlpha(painted) ? painted : lineColor(meta, line);
348
+ }
349
+ /**
350
+ * Label ladder per line: the `label=` column (on the child meta or on the run
351
+ * output it was mounted from), then the server contract's per-column labels
352
+ * (positional, so only trusted when the contract covers exactly the mounted
353
+ * outputs or exactly the value lines), then the output title, then a numbered
354
+ * fallback that mirrors labelsFromKScriptContractPlots.
355
+ */
356
+ function lineLabels(meta, lines) {
357
+ const settings = isRecord(meta.settings) ? meta.settings : undefined;
358
+ const settingsMeta = settings && isRecord(settings.meta) ? settings.meta : undefined;
359
+ const contractLabels = stringList(settingsMeta?.plots);
360
+ const nestedCount = Array.isArray(meta.nestedMeta) ? meta.nestedMeta.length : 0;
361
+ return lines.map((line, index) => {
362
+ const own = stringList(line.meta.labels)[0] ?? stringList(kScriptRunOutputFor(meta, line.meta)?.labels)[0];
363
+ if (own)
364
+ return own;
365
+ if (contractLabels.length === nestedCount && contractLabels[line.nestedIndex]) {
366
+ return contractLabels[line.nestedIndex];
367
+ }
368
+ if (contractLabels.length === lines.length && contractLabels[index])
369
+ return contractLabels[index];
370
+ if (typeof line.meta.title === 'string' && line.meta.title.trim())
371
+ return line.meta.title.trim();
372
+ return `Plot ${index + 1}`;
373
+ });
374
+ }
375
+ /**
376
+ * A line's own colour: the first visible colour of its palette as titan
377
+ * mounted it (the nested settings, the child meta), else of the run output it
378
+ * was mounted from, else its first colour, else the control's last resort.
379
+ * Never the overlay palette (`settings.colors`): a sheet with colour settings
380
+ * mounts them there in row order, which names no line (CVD Flow Dominance:
381
+ * Spot, Perpetuals, then the four colours of its one Delta bar).
382
+ */
383
+ function lineColor(meta, line) {
384
+ const settings = isRecord(line.meta.settings) ? line.meta.settings : undefined;
385
+ const output = isRecord(meta) ? kScriptRunOutputFor(meta, line.meta) : undefined;
386
+ const own = [settings?.colors, line.meta.colors, output?.colors, output?.color].flatMap((colors) => stringList(typeof colors === 'string' ? [colors] : colors));
387
+ return own.find(colorLiteralHasVisibleAlpha) ?? own[0] ?? KSCRIPT_LEGEND_FALLBACK_COLOR;
388
+ }
389
+ /**
390
+ * What a readout printing the container row reads paints at `ts`: the output
391
+ * the container's rows come from (densestKScriptOutputKey over the run's
392
+ * outputs, the container's own rule), a legend line or not (a mark, a stats
393
+ * row). Undefined when nothing coloured is read: no output seeds the container
394
+ * (handles only, no run yet), the seed has no row at that bar, or it declares
395
+ * no colour (a stats row at its default, which titan's strip paints from the
396
+ * overlay's palette).
397
+ */
398
+ export function kScriptLegendSeedSwatchAt(meta, ts) {
399
+ if (!isRecord(meta) || !Array.isArray(meta.nestedMeta) || !isRecord(meta.runResult))
400
+ return undefined;
401
+ const outputs = isRecord(meta.runResult.outputs) ? { ...meta.runResult.outputs } : {};
402
+ delete outputs[KSCRIPT_CONTAINER_OUTPUT_KEY];
403
+ const key = densestKScriptOutputKey(outputs);
404
+ const output = key === undefined ? undefined : outputs[key];
405
+ if (!isRecord(output) || stringList([output.colors, output.color].flat()).length === 0)
406
+ return undefined;
407
+ const nestedIndex = meta.nestedMeta.findIndex((nested) => isRecord(nested) && kScriptLegendLineOutputKey({ meta: nested, nestedIndex: 0 }) === key);
408
+ const seed = nestedIndex < 0 ? undefined : { meta: meta.nestedMeta[nestedIndex], nestedIndex };
409
+ return seed === undefined || lineRowAt(seed, ts) === undefined
410
+ ? undefined
411
+ : kScriptLegendLineSwatchAt(meta, seed, ts);
412
+ }
413
+ /** A candle group's entry text (OT28): "O 1.20 H 1.31 L 1.18 C 1.29", each
414
+ * value through the line's format; a hole in any of the four reads '-'. */
415
+ function candleEntryText(row, line, format, labels) {
416
+ const text = (value) => {
417
+ const finite = finiteNumber(value);
418
+ return finite === undefined ? '-' : format(finite, line);
419
+ };
420
+ return [
421
+ `${labels.open} ${text(row?.[1])}`,
422
+ `${labels.high} ${text(row?.[2])}`,
423
+ `${labels.low} ${text(row?.[3])}`,
424
+ `${labels.close} ${text(row?.[4])}`,
425
+ ].join(' ');
426
+ }
427
+ /**
428
+ * One labelled entry per value line at the hovered point's timestamp: the
429
+ * line's name as `label`, its reading as `text` (value ink) and the colour it
430
+ * is drawn in at that bar as `swatch` (kScriptLegendLineSwatchAt: a ladder's
431
+ * per-bar colour, a column's sign colour, else the line colour). Returns an
432
+ * empty list for fewer than two lines so the caller keeps today's unlabelled
433
+ * single-slot readout for one-line scripts. `format` also gets the line it
434
+ * prints, so a caller can honour a per-output format word
435
+ * (kScriptLegendLineFormat); callers that ignore it are unchanged. A candle
436
+ * group's line reads its four values after `options.ohlcLabels` (OT28), a
437
+ * stacked column its own height (OT38). A column, candle group or mark reads
438
+ * the hovered bar's own row, '-' on a bar without one; a line its floor row
439
+ * (lineRowAt).
440
+ */
441
+ export function kScriptLegendEntries(point, meta, format, options = {}) {
442
+ if (!isRecord(meta))
443
+ return [];
444
+ const lines = kScriptLegendLines(meta);
445
+ if (lines.length < 2)
446
+ return [];
447
+ const ts = Array.isArray(point) && typeof point[0] === 'number' ? point[0] : undefined;
448
+ const labels = lineLabels(meta, lines);
449
+ const ohlcLabels = options.ohlcLabels ?? KSCRIPT_LEGEND_DEFAULT_OHLC_LABELS;
450
+ return lines.map((line, index) => {
451
+ const row = lineRowAt(line, ts);
452
+ const value = kScriptLegendLineValueAt(line, ts, { meta });
453
+ return {
454
+ id: kScriptLegendSlotId(index),
455
+ text: isCandleLine(line)
456
+ ? candleEntryText(row, line, format, ohlcLabels)
457
+ : value === undefined
458
+ ? '-'
459
+ : format(value, line),
460
+ label: labels[index],
461
+ color: KSCRIPT_LEGEND_VALUE_INK,
462
+ swatch: kScriptLegendLineSwatchAt(meta, line, ts),
463
+ };
464
+ });
465
+ }
466
+ /** The value a legend line holds at `ts` (lineRowAt: the floor row for a
467
+ * line, titan's rule; the bar's own row for a column, candle group or mark,
468
+ * so a bar without one is a hole; the last row without a timestamp),
469
+ * undefined for a hole. `withinRows` also reads a `ts` past the line's last
470
+ * row as a hole: the legend keeps a line's last reading there, a hover on
471
+ * the plot has nothing drawn to land on. A candle line's value is its close
472
+ * (OT28); a stacked column's its own height, top - bottom (OT38, read off
473
+ * the run output through `meta`). */
474
+ export function kScriptLegendLineValueAt(line, ts, options = {}) {
475
+ if (options.withinRows && ts !== undefined && ts > rowTimestamp(lastRow(lineRows(line))))
476
+ return undefined;
477
+ const row = lineRowAt(line, ts);
478
+ if (row === undefined)
479
+ return undefined;
480
+ if (isCandleLine(line))
481
+ return finiteNumber(row[4]);
482
+ const value = finiteNumber(row[1]);
483
+ if (value !== undefined && isStackedLine(options.meta, line)) {
484
+ const bottom = finiteNumber(row[2]);
485
+ return bottom === undefined ? undefined : value - bottom;
486
+ }
487
+ return value;
488
+ }
489
+ /** The line's values on its last `count` rows at or before `ts`, oldest
490
+ * first (a hole reads NaN): the run-up a sparkline draws to a hovered bar. */
491
+ export function kScriptLegendLineTrailingValues(line, ts, count) {
492
+ const rows = lineRows(line);
493
+ if (!Array.isArray(rows) || rows.length === 0 || !Number.isFinite(ts) || count <= 0)
494
+ return [];
495
+ let lo = 0;
496
+ let hi = rows.length - 1;
497
+ let end = -1;
498
+ while (lo <= hi) {
499
+ const mid = (lo + hi) >>> 1;
500
+ if (rowTimestamp(rows[mid]) <= ts) {
501
+ end = mid;
502
+ lo = mid + 1;
503
+ }
504
+ else {
505
+ hi = mid - 1;
506
+ }
507
+ }
508
+ const values = [];
509
+ for (let at = Math.max(0, end - count + 1); at <= end; at += 1) {
510
+ const row = rows[at];
511
+ values.push(finiteNumber(Array.isArray(row) ? row[1] : undefined) ?? Number.NaN);
512
+ }
513
+ return values;
514
+ }
515
+ /** Slot id for entry `index` (the pool's addressing, exported for callers
516
+ * that place entries after the value lines). */
517
+ export function kScriptLegendSlotIdAt(index) {
518
+ return kScriptLegendSlotId(index);
519
+ }
520
+ /** The colour a legend line paints with (lineColor: its own, never a palette slot). */
521
+ export function kScriptLegendLineColor(meta, line) {
522
+ return lineColor(meta, line);
523
+ }
524
+ /**
525
+ * Pad a legend result to the full slot pool with empty text, name and dot so
526
+ * slots a previous run addressed (a line hidden by a settings toggle) never
527
+ * keep a stale reading. Slots already present are left untouched.
528
+ */
529
+ export function padKScriptLegendSlots(entries) {
530
+ const present = new Set(entries.map((entry) => entry.id));
531
+ const padded = [...entries];
532
+ for (const id of kScriptLegendSlotIds()) {
533
+ if (present.has(id))
534
+ continue;
535
+ padded.push({ id, text: '', label: '', color: KSCRIPT_LEGEND_VALUE_INK, swatch: '' });
536
+ }
537
+ return padded;
538
+ }
@@ -0,0 +1,32 @@
1
+ export type KScriptOutputRoleAppearanceMode = 'full' | 'selection';
2
+ export interface KScriptOutputRole {
3
+ plotType?: string;
4
+ outputRole?: string;
5
+ roleSource: 'declared' | 'inferred';
6
+ paneParentCandidate?: boolean;
7
+ isDeclaredHelper: boolean;
8
+ isContainer: boolean;
9
+ isDecorator: boolean;
10
+ isGuide: boolean;
11
+ isFill: boolean;
12
+ isDrawableChild: boolean;
13
+ isScaleAnchor: boolean;
14
+ invisibleAnchorCompat: boolean;
15
+ canBePaneParent: boolean;
16
+ canFallbackAsPaneParent: boolean;
17
+ canBeOnchartParent: boolean;
18
+ canAnchorStableContainer: boolean;
19
+ emitsPriceDisplay: boolean;
20
+ hasStyleColumns: boolean;
21
+ hasVisiblePalette: boolean;
22
+ visibilityInputs: string[];
23
+ controlledByVisibilityInput: boolean;
24
+ forceOnChart: boolean;
25
+ }
26
+ export declare const KSCRIPT_OFFCHART_MARKER_PARENT_FALLBACK_PLOT_TYPES: Set<string>;
27
+ export declare function isKScriptDeclaredMarkerPlotType(plotType: unknown): plotType is string;
28
+ /** False for a colour literal that paints nothing (alpha 0, `transparent`, empty). */
29
+ export declare function colorLiteralHasVisibleAlpha(color: string): boolean;
30
+ export declare function classifyKScriptOutputRole(output: unknown, options?: {
31
+ appearance?: KScriptOutputRoleAppearanceMode;
32
+ }): KScriptOutputRole;