@joplo-app/plugin-viz 0.0.0-stage → 0.10.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 (43) hide show
  1. package/LICENSE +97 -0
  2. package/dist/catalog.d.ts +8 -0
  3. package/dist/catalog.js +178 -0
  4. package/dist/components.d.ts +236 -0
  5. package/dist/components.js +55 -0
  6. package/dist/effects.d.ts +27 -0
  7. package/dist/effects.js +32 -0
  8. package/dist/guide.d.ts +3 -0
  9. package/dist/guide.js +79 -0
  10. package/dist/labels.json +12 -0
  11. package/dist/plugin.d.ts +6 -0
  12. package/dist/plugin.js +312 -0
  13. package/dist/runtime/bar-chart.d.ts +4 -0
  14. package/dist/runtime/bar-chart.js +204 -0
  15. package/dist/runtime/delta-bar.d.ts +4 -0
  16. package/dist/runtime/delta-bar.js +165 -0
  17. package/dist/runtime/heatmap.d.ts +4 -0
  18. package/dist/runtime/heatmap.js +138 -0
  19. package/dist/runtime/hooks.d.ts +36 -0
  20. package/dist/runtime/hooks.js +54 -0
  21. package/dist/runtime/line-chart.d.ts +4 -0
  22. package/dist/runtime/line-chart.js +197 -0
  23. package/dist/runtime/radar.d.ts +4 -0
  24. package/dist/runtime/radar.js +101 -0
  25. package/dist/runtime/ratio-bar.d.ts +4 -0
  26. package/dist/runtime/ratio-bar.js +77 -0
  27. package/dist/runtime/sankey.d.ts +4 -0
  28. package/dist/runtime/sankey.js +171 -0
  29. package/dist/runtime/scatter-plot.d.ts +4 -0
  30. package/dist/runtime/scatter-plot.js +202 -0
  31. package/dist/runtime/time-lanes.d.ts +4 -0
  32. package/dist/runtime/time-lanes.js +179 -0
  33. package/dist/runtime/tokens.d.ts +11 -0
  34. package/dist/runtime/tokens.js +51 -0
  35. package/dist/runtime/waterfall.d.ts +4 -0
  36. package/dist/runtime/waterfall.js +119 -0
  37. package/dist/runtime.d.ts +2 -0
  38. package/dist/runtime.js +26 -0
  39. package/dist/schema.d.ts +202 -0
  40. package/dist/schema.js +255 -0
  41. package/dist/version.json +3 -0
  42. package/package.json +67 -3
  43. package/README.md +0 -3
package/dist/schema.js ADDED
@@ -0,0 +1,255 @@
1
+ import { z } from 'zod';
2
+ // ---------- Step IR Schemas (step.ext.viz) ----------
3
+ const FiniteNum = z.number().finite();
4
+ const KeyOrNum = z.union([z.string().min(1), FiniteNum]);
5
+ export const VizOpS = z.union([
6
+ z.strictObject({
7
+ kind: z.literal('focus'),
8
+ chart: z.string().min(1),
9
+ targets: z.array(z.string().min(1)).nullable(),
10
+ }),
11
+ z.strictObject({
12
+ kind: z.literal('reveal'),
13
+ chart: z.string().min(1),
14
+ cutoff: KeyOrNum.nullable(),
15
+ }),
16
+ z.strictObject({
17
+ kind: z.literal('pinBaseline'),
18
+ chart: z.string().min(1),
19
+ }),
20
+ z.strictObject({
21
+ kind: z.literal('cursor'),
22
+ chart: z.string().min(1),
23
+ value: KeyOrNum.nullable(),
24
+ label: z.string().optional(),
25
+ }),
26
+ ]);
27
+ export const VizChartSnapshotS = z.strictObject({
28
+ focus: z.array(z.string().min(1)).nullable().optional(),
29
+ reveal: KeyOrNum.nullable().optional(),
30
+ cursor: z
31
+ .strictObject({
32
+ value: KeyOrNum,
33
+ label: z.string().optional(),
34
+ })
35
+ .nullable()
36
+ .optional(),
37
+ baselines: z.record(z.string(), FiniteNum).optional(),
38
+ });
39
+ export const VizFrameS = z.strictObject({
40
+ charts: z.record(z.string(), VizChartSnapshotS),
41
+ });
42
+ /** Step slot accepts raw effect ops before frame() lowers them into deterministic per-chart snapshots. */
43
+ export const StepDataS = z.union([
44
+ z.strictObject({ ops: z.array(VizOpS) }),
45
+ VizFrameS,
46
+ ]);
47
+ // ---------- Pure Validators & Math Helpers (shared by compile.node & browser runtime) ----------
48
+ const finite = (n) => typeof n === 'number' && Number.isFinite(n);
49
+ export function ratioError(p, isBinding = () => false) {
50
+ if (p.segments !== undefined) {
51
+ if (isBinding(p.segments))
52
+ return undefined;
53
+ if (!Array.isArray(p.segments) || p.segments.length === 0)
54
+ return 'segments must be a non-empty array';
55
+ let sum = 0;
56
+ for (const [i, seg] of p.segments.entries()) {
57
+ if (!seg || typeof seg !== 'object')
58
+ return `segments[${i}] must be an object`;
59
+ const val = seg.value;
60
+ if (isBinding(val))
61
+ continue;
62
+ if (!finite(val) || val < 0)
63
+ return `segments[${i}].value must be a finite nonnegative number`;
64
+ sum += val;
65
+ }
66
+ if (p.total !== undefined && !isBinding(p.total)) {
67
+ if (!finite(p.total) || p.total <= 0)
68
+ return 'total must be a finite positive number';
69
+ if (sum - p.total > Number.EPSILON * p.total * 4)
70
+ return 'sum of segments must be at most total';
71
+ }
72
+ if (p.threshold !== undefined && !isBinding(p.threshold)) {
73
+ if (!finite(p.threshold) || p.threshold < 0)
74
+ return 'threshold must be a finite nonnegative number';
75
+ if (finite(p.total) && p.threshold > p.total)
76
+ return 'threshold must be at most total';
77
+ }
78
+ return undefined;
79
+ }
80
+ for (const key of ['total', 'left', 'right', 'threshold']) {
81
+ const v = p[key];
82
+ const optional = key === 'right' || key === 'threshold';
83
+ if ((optional && v === undefined) || isBinding(v))
84
+ continue;
85
+ if (!finite(v) || (key === 'total' ? v <= 0 : v < 0)) {
86
+ return `${key} must be a finite ${key === 'total' ? 'positive' : 'nonnegative'} number`;
87
+ }
88
+ }
89
+ if (!finite(p.total))
90
+ return undefined;
91
+ if (finite(p.threshold) && p.threshold > p.total)
92
+ return 'threshold must be at most total';
93
+ const left = finite(p.left) ? p.left : 0;
94
+ const right = finite(p.right) ? p.right : 0;
95
+ if (Math.max(left, right) > p.total || left + right - p.total > Number.EPSILON * p.total) {
96
+ return 'left + right must be at most total';
97
+ }
98
+ return undefined;
99
+ }
100
+ export function ratioParts(p) {
101
+ if (ratioError(p))
102
+ return null;
103
+ if (Array.isArray(p.segments)) {
104
+ const segs = p.segments;
105
+ const segSum = segs.reduce((acc, s) => acc + s.value, 0);
106
+ const total = finite(p.total) ? p.total : Math.max(segSum, 1);
107
+ const remainder = Math.max(0, total - segSum);
108
+ return {
109
+ mode: 'segments',
110
+ total,
111
+ segments: segs,
112
+ remainder,
113
+ percentages: segs.map((s) => (s.value / total) * 100),
114
+ remainderPct: (remainder / total) * 100,
115
+ threshold: finite(p.threshold) ? (p.threshold / total) * 100 : undefined,
116
+ };
117
+ }
118
+ const total = p.total;
119
+ const left = p.left;
120
+ const right = (p.right ?? 0);
121
+ const remainder = Math.max(0, total - (left + right));
122
+ return {
123
+ mode: 'pair',
124
+ total,
125
+ counts: [left, right, remainder],
126
+ percentages: [left, right, remainder].map((n) => (n / total) * 100),
127
+ threshold: finite(p.threshold) ? (p.threshold / total) * 100 : undefined,
128
+ };
129
+ }
130
+ // 2. TimeLanes validation & lane packing (migrated & upgraded from plugin-diagram)
131
+ const TimeVal = z.number().min(-Number.MAX_SAFE_INTEGER).max(Number.MAX_SAFE_INTEGER);
132
+ const NonEmpty = z.string().min(1).refine((v) => !!v.trim(), 'must contain text');
133
+ export const TimeSpanS = z.object({
134
+ id: NonEmpty,
135
+ lane: NonEmpty,
136
+ start: TimeVal,
137
+ end: TimeVal,
138
+ label: z.string().optional(),
139
+ shortLabel: z.string().optional(),
140
+ rangeLabel: z.string().optional(),
141
+ status: z.enum(['ok', 'warn', 'fail', 'info', 'accent']).optional(),
142
+ color: z.string().optional(),
143
+ });
144
+ const TimeLanesChartS = z
145
+ .object({
146
+ domain: z.tuple([TimeVal, TimeVal]),
147
+ spans: z.array(TimeSpanS),
148
+ unit: z.string().default('ms'),
149
+ })
150
+ .superRefine(({ domain: [min, max], spans }, ctx) => {
151
+ if (max <= min) {
152
+ ctx.addIssue({ code: 'custom', path: ['domain'], message: 'domain[1] must be greater than domain[0]' });
153
+ }
154
+ const ids = new Set();
155
+ for (const [index, item] of spans.entries()) {
156
+ const path = ['spans', index];
157
+ if (ids.has(item.id))
158
+ ctx.addIssue({ code: 'custom', path: [...path, 'id'], message: `duplicate id "${item.id}"` });
159
+ ids.add(item.id);
160
+ if (item.end < item.start)
161
+ ctx.addIssue({ code: 'custom', path, message: `"${item.id}" needs end >= start` });
162
+ if (item.start < min || item.end > max) {
163
+ ctx.addIssue({ code: 'custom', path, message: `"${item.id}" must lie inside domain [${min}, ${max}]` });
164
+ }
165
+ }
166
+ });
167
+ export function readTimeLanes(props) {
168
+ const result = TimeLanesChartS.safeParse(props);
169
+ return result.success
170
+ ? { data: result.data }
171
+ : { error: result.error.issues.map((issue) => `${issue.path.join('.')}: ${issue.message}`).join('. ') };
172
+ }
173
+ /** Lane order follows the author. Within a lane overlapping intervals occupy separate sub-rows. */
174
+ export function layoutTimeLanes(spans) {
175
+ const grouped = new Map();
176
+ for (const span of spans) {
177
+ let group = grouped.get(span.lane);
178
+ if (!group) {
179
+ group = [];
180
+ grouped.set(span.lane, group);
181
+ }
182
+ group.push(span);
183
+ }
184
+ return Array.from(grouped, ([lane, group]) => {
185
+ const last = [];
186
+ const bars = [...group]
187
+ .sort((a, b) => a.start - b.start || a.end - b.end)
188
+ .map((span) => {
189
+ let row = last.findIndex((prev) => prev.end < span.start || (prev.end === span.start && prev.start < prev.end));
190
+ if (row < 0)
191
+ row = last.length;
192
+ last[row] = span;
193
+ return { span, row };
194
+ });
195
+ return { lane, bars, rows: Math.max(1, last.length) };
196
+ });
197
+ }
198
+ export function computeParetoFrontier(points, corner) {
199
+ if (points.length === 0)
200
+ return [];
201
+ const wantMinX = corner === 'top-left' || corner === 'bottom-left';
202
+ const wantMaxY = corner === 'top-left' || corner === 'top-right';
203
+ const sorted = [...points].sort((a, b) => (wantMinX ? a.x - b.x : b.x - a.x) || (wantMaxY ? b.y - a.y : a.y - b.y));
204
+ const frontier = [];
205
+ let bestY = wantMaxY ? -Infinity : Infinity;
206
+ for (const pt of sorted) {
207
+ const improves = wantMaxY ? pt.y > bestY : pt.y < bestY;
208
+ if (improves) {
209
+ frontier.push(pt);
210
+ bestY = pt.y;
211
+ }
212
+ }
213
+ // Sort left-to-right by x for smooth polyline rendering
214
+ return frontier.sort((a, b) => a.x - b.x);
215
+ }
216
+ export function computeWaterfall(steps) {
217
+ let running = 0;
218
+ return steps.map((step, index) => {
219
+ const kind = step.kind ?? (index === 0 ? 'start' : 'delta');
220
+ if (kind === 'start') {
221
+ running = step.value;
222
+ return { ...step, index, kind, start: 0, end: running };
223
+ }
224
+ if (kind === 'subtotal' || kind === 'total') {
225
+ const target = finite(step.value) ? step.value : running;
226
+ running = target;
227
+ return { ...step, index, kind, start: 0, end: target };
228
+ }
229
+ const start = running;
230
+ const end = running + step.value;
231
+ running = end;
232
+ return { ...step, index, kind, start, end };
233
+ });
234
+ }
235
+ export function formatDeltaPercent(baseline, current, customLabel) {
236
+ if (customLabel)
237
+ return customLabel;
238
+ const diff = current - baseline;
239
+ if (Math.abs(diff) < 1e-9)
240
+ return '0%';
241
+ if (Math.abs(baseline) < 1e-9)
242
+ return `${diff > 0 ? '+' : '−'}${Math.abs(Math.round(diff * 10) / 10)}`;
243
+ const pct = (diff / Math.abs(baseline)) * 100;
244
+ const rounded = Math.abs(pct) >= 10 ? Math.round(pct) : Math.round(pct * 10) / 10;
245
+ return `${rounded > 0 ? '+' : '−'}${Math.abs(rounded)}%`;
246
+ }
247
+ export function validateChartArray(name, prop, val, isBinding) {
248
+ if (val === undefined)
249
+ return `<${name}> requires ${prop}`;
250
+ if (isBinding(val))
251
+ return undefined;
252
+ if (!Array.isArray(val))
253
+ return `<${name} ${prop}> must be an array`;
254
+ return undefined;
255
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "version": "0.10.0"
3
+ }
package/package.json CHANGED
@@ -1,6 +1,70 @@
1
1
  {
2
2
  "name": "@joplo-app/plugin-viz",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.10.0",
4
+ "type": "module",
5
+ "joplo": {
6
+ "api": 2
7
+ },
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/plugin.d.ts",
11
+ "default": "./dist/plugin.js"
12
+ },
13
+ "./effects": {
14
+ "types": "./dist/effects.d.ts",
15
+ "default": "./dist/effects.js"
16
+ },
17
+ "./runtime": {
18
+ "types": "./dist/runtime.d.ts",
19
+ "default": "./dist/runtime.js"
20
+ },
21
+ "./schema": {
22
+ "types": "./dist/schema.d.ts",
23
+ "default": "./dist/schema.js"
24
+ },
25
+ "./catalog": {
26
+ "types": "./dist/catalog.d.ts",
27
+ "default": "./dist/catalog.js"
28
+ },
29
+ "./guide": {
30
+ "types": "./dist/guide.d.ts",
31
+ "default": "./dist/guide.js"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "dependencies": {
36
+ "@joplo-app/kit": "0.10.0",
37
+ "d3-array": "^3.2.4",
38
+ "d3-sankey": "^0.12.3",
39
+ "d3-scale": "^4.0.2",
40
+ "d3-shape": "^3.2.0",
41
+ "react": "19.3.1",
42
+ "zod": "4.6.7"
43
+ },
44
+ "devDependencies": {
45
+ "@types/d3-array": "^3.2.1",
46
+ "@types/d3-sankey": "^0.12.4",
47
+ "@types/d3-scale": "^4.0.8",
48
+ "@types/d3-shape": "^3.1.6",
49
+ "@types/react": "^19.3.0"
50
+ },
51
+ "description": "Joplo first-party narrative data visualization plugin: DeltaBar, BarChart, RatioBar, LineChart, TimeLanes, ScatterPlot, Heatmap, Sankey, Waterfall and Radar",
52
+ "license": "PolyForm-Shield-1.0.0",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/Joplo-app/Joplo.git",
56
+ "directory": "plugins/viz"
57
+ },
58
+ "engines": {
59
+ "node": ">=22"
60
+ },
61
+ "files": [
62
+ "dist"
63
+ ],
64
+ "publishConfig": {
65
+ "access": "public"
66
+ },
67
+ "scripts": {
68
+ "build": "node ../../scripts/build-package.mjs"
69
+ }
6
70
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.