@tidal-ts/chart 0.25.0 → 0.27.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.
package/dist/prepare.d.ts CHANGED
@@ -9,8 +9,9 @@ import type { ChartSeries } from './types.js';
9
9
  *
10
10
  * Three moves, in order:
11
11
  *
12
- * 1. {@link foldSources} — join the comparison series under the compare prefix
13
- * where a config asks for it, then fold every study spec onto its source so
12
+ * 1. {@link foldSources} — join the other symbols' series (the comparison, a
13
+ * locked symbol) where a config asks for them, then fold every study spec
14
+ * onto its source so
14
15
  * the derived columns exist before the chart reads them. A source coarser
15
16
  * than the axis is folded at its own grain and then held across the axis
16
17
  * ({@link SourceHold}).
@@ -28,6 +29,15 @@ import type { ChartSeries } from './types.js';
28
29
  export interface SourceInput {
29
30
  series: ChartSeries;
30
31
  compare?: ChartSeries | null;
32
+ /**
33
+ * The series of each **locked symbol** a compare on this source reads
34
+ * (`SeriesConfig.compareOn`), keyed by the symbol: `{ SPY: … }`. Each is
35
+ * joined like the comparison, under `pinnedPrefix(symbol)` (`cmp_@SPY_close`),
36
+ * and only when a config on the source reads that prefix. Same grain and
37
+ * keys as `series`, as for `compare`; a key `isPinnableSymbol` refuses is
38
+ * never joined.
39
+ */
40
+ pinned?: Readonly<Record<string, ChartSeries>>;
31
41
  /** Set when `series` (and `compare`) are coarser than the axis: a daily
32
42
  * curve on an intraday chart. Pass them at their own grain. */
33
43
  hold?: SourceHold;
@@ -49,10 +59,13 @@ export interface SourceInput {
49
59
  * - **A restated row wins before anything reads it.** Rows sharing a key are
50
60
  * reduced to the last one before the join and the studies, so a study never
51
61
  * counts a restatement as an extra bar.
52
- * - **The comparison joins on the coarse keys**, exactly as it does unheld: a
53
- * day the source has no row for is a gap in the comparison too, and a
54
- * comparison keyed at another time of day (04:00 against 00:00) matches
55
- * nothing. Pass both at the same grain, keyed the same way.
62
+ * - **The comparison joins on the coarse keys**, exactly as it does unheld:
63
+ * each source row takes the comparison's latest row at or before it, so a
64
+ * day the comparison has no row for carries its previous day. That is also
65
+ * why both must be keyed the same way: a comparison keyed at another time of
66
+ * day (04:00 against 00:00) would pair every day with the comparison's day
67
+ * BEFORE, with nothing to show it. Pass both at the same grain, keyed the
68
+ * same way.
56
69
  * - **Warm-up comes from the coarse rows.** A 20-day study needs 20 coarse
57
70
  * rows before the grid starts; rows before the grid cost nothing in the
58
71
  * result. Pass the source with the look-back its longest study needs.
@@ -98,8 +111,9 @@ export interface SourceFacts {
98
111
  */
99
112
  export declare function foldKey(configs: readonly SeriesConfig[]): string;
100
113
  /**
101
- * The fold: each source, joined with its comparison under the compare prefix
102
- * when any config on it asks (a compare-bound spec, or a raw compare leg), then
114
+ * The fold: each source, joined with the other symbols' series it reads (its
115
+ * comparison under the compare prefix when a compare-bound spec or a raw
116
+ * compare leg asks, each locked symbol under its own), as of each row, then
103
117
  * every study spec on it applied so the derived columns exist, then, for a
104
118
  * source with a {@link SourceHold}, held across the axis. A spec whose
105
119
  * inputs are missing is skipped by the engine and simply yields no column — the
package/dist/prepare.js CHANGED
@@ -1,4 +1,4 @@
1
- import { applyDerivedReport, COMPARE_PREFIX, holdAcrossGrid, joinUnderPrefix, usesCompare, } from '@tidal-ts/core';
1
+ import { applyDerivedReport, COMPARE_PREFIX, holdAcrossGrid, isPinnableSymbol, pinnedPrefix, readsColumn, readsPrefix, } from '@tidal-ts/core';
2
2
  import { configColumns, configSpecs } from './series.js';
3
3
  import { finiteColumns, lastRow } from './seriesFacts.js';
4
4
  /** The series with one row per key, the last of each run: a restatement
@@ -19,16 +19,35 @@ const specsBySource = (configs) => {
19
19
  (m[c.source] ??= []).push(...configSpecs(c));
20
20
  return m;
21
21
  };
22
+ /** Per source, the RAW compare columns its configs read, sorted: what decides
23
+ * which joins a spec-less config needs (the comparison's, a locked symbol's). */
24
+ const rawCompareColumns = (configs) => {
25
+ const m = {};
26
+ for (const c of configs)
27
+ if (c.source && !c.derive && c.column.startsWith(COMPARE_PREFIX))
28
+ (m[c.source] ??= new Set()).add(c.column);
29
+ return Object.fromEntries(Object.keys(m)
30
+ .sort()
31
+ .map((k) => [k, [...m[k]].sort()]));
32
+ };
22
33
  /** Sources with a config reading a RAW compare column — a split pair's leg,
23
34
  * which carries no spec at all but needs the compare join exactly as a
24
35
  * compare-bound spec does. (Asking only the specs left such a leg reading a
25
36
  * column nothing ever joined on: it drew nothing and, having no `derive`, was
26
37
  * filtered out of the panel too — an invisible, unremovable leg.) */
27
- const rawCompareSources = (configs) => [
28
- ...new Set(configs
29
- .filter((c) => c.source && !c.derive && c.column.startsWith(COMPARE_PREFIX))
30
- .map((c) => c.source)),
31
- ].sort();
38
+ const rawCompareSources = (configs) => Object.entries(rawCompareColumns(configs))
39
+ .filter(([, cols]) => cols.some(readsComparison))
40
+ .map(([source]) => source);
41
+ /** The prefix every locked symbol's columns share (`cmp_@`), which the
42
+ * comparison's own columns never start with. */
43
+ const PINNED_ANY = pinnedPrefix('').slice(0, -1);
44
+ /** Whether a column is the bar's COMPARISON's — under the compare prefix but
45
+ * not a locked symbol's, which has a join of its own. */
46
+ const readsComparison = (column) => column.startsWith(COMPARE_PREFIX) && !column.startsWith(PINNED_ANY);
47
+ /** Whether a spec reads the comparison anywhere — `usesCompare` minus the
48
+ * locked symbols' columns, so a compare locked to `SPY` does not pull the
49
+ * bar's comparison onto the series too. */
50
+ const specReadsComparison = (spec) => readsColumn(spec, readsComparison);
32
51
  /**
33
52
  * A stable string of everything {@link foldSources} depends on in the configs —
34
53
  * the specs per source and the raw-compare set — so a host can key its fold
@@ -43,11 +62,15 @@ export function foldKey(configs) {
43
62
  const sorted = Object.fromEntries(Object.keys(bySource)
44
63
  .sort()
45
64
  .map((source) => [source, bySource[source].map((spec) => JSON.stringify(spec)).sort()]));
46
- return JSON.stringify([sorted, rawCompareSources(configs)]);
65
+ // The raw compare COLUMNS, not just the sources: a raw leg reading a locked
66
+ // symbol (`cmp_@SPY_close`) needs that symbol's join, which a source already
67
+ // joining the comparison would not otherwise re-fold for.
68
+ return JSON.stringify([sorted, rawCompareColumns(configs)]);
47
69
  }
48
70
  /**
49
- * The fold: each source, joined with its comparison under the compare prefix
50
- * when any config on it asks (a compare-bound spec, or a raw compare leg), then
71
+ * The fold: each source, joined with the other symbols' series it reads (its
72
+ * comparison under the compare prefix when a compare-bound spec or a raw
73
+ * compare leg asks, each locked symbol under its own), as of each row, then
51
74
  * every study spec on it applied so the derived columns exist, then, for a
52
75
  * source with a {@link SourceHold}, held across the axis. A spec whose
53
76
  * inputs are missing is skipped by the engine and simply yields no column — the
@@ -68,6 +91,7 @@ export function foldSources(inputs, configs) {
68
91
  export function foldSourcesReport(inputs, configs) {
69
92
  const specs = specsBySource(configs);
70
93
  const rawCompare = new Set(rawCompareSources(configs));
94
+ const rawColumns = rawCompareColumns(configs);
71
95
  const sources = {};
72
96
  const reports = {};
73
97
  for (const [key, input] of Object.entries(inputs)) {
@@ -76,17 +100,29 @@ export function foldSourcesReport(inputs, configs) {
76
100
  // A coarse source is folded before it is held, so a restated row has to go
77
101
  // first, or the join pairs it with nothing and a study counts it as a bar
78
102
  // (the hold alone would only pick the later row after both had happened).
79
- let series = input.hold ? lastPerKey(input.series) : input.series;
80
- const compare = input.hold && input.compare ? lastPerKey(input.compare) : input.compare;
81
- // A compare-bound leg needs the compare entity's columns on the PRIMARY
82
- // series before the fold — a left exact-key join, every conflicting column
83
- // prefixed. Only when asked and the compare series exists; otherwise the
84
- // fold's own skip leaves the spread as a gap (compare off ⇒ not drawn).
85
- // Rename-then-join lives in core (`joinUnderPrefix`; F-charts-14 for why
86
- // not the library's `onConflict: 'prefix'`).
87
- if (compare && (rawCompare.has(key) || mine.some(usesCompare)))
88
- series = joinUnderPrefix(series, compare);
89
- const folded = applyDerivedReport(series, mine);
103
+ const series = input.hold ? lastPerKey(input.series) : input.series;
104
+ // The other symbols' series this source's configs read, keyed by the
105
+ // prefix their columns carry, for the engine's as-of join (core's
106
+ // `applyDerivedReport`). Each only when asked: the comparison when a
107
+ // compare-bound spec or a raw compare leg reads it (otherwise the fold's
108
+ // own skip leaves the spread as a gap — compare off ⇒ not drawn), a locked
109
+ // symbol when a config reads its prefix, so a symbol the host still holds
110
+ // but nothing reads any more costs no join. The as-of join takes the latest
111
+ // row at or before each of this source's rows, so a restated row on the
112
+ // other side resolves to its last version with no `lastPerKey` of its own.
113
+ const others = {};
114
+ if (input.compare && (rawCompare.has(key) || mine.some(specReadsComparison)))
115
+ others[COMPARE_PREFIX] = input.compare;
116
+ for (const symbol of Object.keys(input.pinned ?? {}).sort()) {
117
+ if (!isPinnableSymbol(symbol))
118
+ continue;
119
+ const prefix = pinnedPrefix(symbol);
120
+ const asked = (rawColumns[key] ?? []).some((col) => col.startsWith(prefix)) ||
121
+ mine.some((spec) => readsPrefix(spec, prefix));
122
+ if (asked)
123
+ others[prefix] = input.pinned[symbol];
124
+ }
125
+ const folded = applyDerivedReport(series, mine, undefined, others);
90
126
  const out = input.hold
91
127
  ? holdAcrossGrid(folded.series, input.hold.grid, input.hold.grainMs)
92
128
  : folded.series;
package/dist/series.d.ts CHANGED
@@ -336,6 +336,23 @@ export interface SeriesConfig {
336
336
  * no cycle.
337
337
  */
338
338
  compareWith?: CompareWith;
339
+ /**
340
+ * The symbol a compare's other side (inB) is **locked** to — `SPY` — in
341
+ * place of the bar's comparison. Absent ⇒ inB reads the comparison, as
342
+ * before. Set on the ROOT of a chain, like {@link compareFn}. What it means,
343
+ * which the HOST builds at render time as it does the rest of a compare: the
344
+ * automatic other side is the same chain on this symbol, and a wiring with
345
+ * `side: 'comparison'` reads the wired config on this symbol. A wiring with
346
+ * `side: 'primary'` reads no other symbol, so the lock changes nothing there.
347
+ *
348
+ * Unlike the rest of a compare it never SLEEPS: it does not depend on the
349
+ * bar, so it draws with no comparison named. The host fetches the symbol,
350
+ * hands it to the fold as `SourceInput.pinned`, and rebinds the chain onto
351
+ * its columns with `underPrefix(input, pinnedPrefix(symbol))`
352
+ * (`cmp_@SPY_close`). Saved with the config and
353
+ * carried by a preset, the one symbol a preset carries.
354
+ */
355
+ compareOn?: string;
339
356
  }
340
357
  /** A compare's hand-wired other side — see {@link SeriesConfig.compareWith}. */
341
358
  export interface CompareWith {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/chart",
3
- "version": "0.25.0",
3
+ "version": "0.27.0",
4
4
  "private": false,
5
5
  "description": "TimeSeriesChart — a multi-row time-series chart on @pond-ts/charts, plus the series and settings vocabulary and the prepare step. Theme, colours and settings arrive as props.",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "react": "^18.2.0 || ^19.0.0"
39
39
  },
40
40
  "dependencies": {
41
- "@tidal-ts/core": "0.25.0"
41
+ "@tidal-ts/core": "0.27.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@pond-ts/charts": "^0.78.0",