@contractspec/lib.presentation-runtime-core 3.7.6 → 3.8.3

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 (39) hide show
  1. package/README.md +60 -54
  2. package/dist/browser/index.js +473 -1
  3. package/dist/browser/table.js +21 -0
  4. package/dist/browser/visualization.echarts.js +148 -0
  5. package/dist/browser/visualization.js +307 -0
  6. package/dist/browser/visualization.model.builders.js +290 -0
  7. package/dist/browser/visualization.model.helpers.js +199 -0
  8. package/dist/browser/visualization.model.js +306 -0
  9. package/dist/browser/visualization.types.js +0 -0
  10. package/dist/browser/visualization.utils.js +65 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.js +473 -1
  13. package/dist/node/index.js +473 -1
  14. package/dist/node/table.js +21 -0
  15. package/dist/node/visualization.echarts.js +148 -0
  16. package/dist/node/visualization.js +307 -0
  17. package/dist/node/visualization.model.builders.js +290 -0
  18. package/dist/node/visualization.model.helpers.js +199 -0
  19. package/dist/node/visualization.model.js +306 -0
  20. package/dist/node/visualization.types.js +0 -0
  21. package/dist/node/visualization.utils.js +65 -0
  22. package/dist/table.d.ts +99 -0
  23. package/dist/table.js +22 -0
  24. package/dist/visualization.d.ts +3 -0
  25. package/dist/visualization.echarts.d.ts +3 -0
  26. package/dist/visualization.echarts.js +149 -0
  27. package/dist/visualization.js +308 -0
  28. package/dist/visualization.model.builders.d.ts +20 -0
  29. package/dist/visualization.model.builders.js +291 -0
  30. package/dist/visualization.model.d.ts +3 -0
  31. package/dist/visualization.model.helpers.d.ts +60 -0
  32. package/dist/visualization.model.helpers.js +200 -0
  33. package/dist/visualization.model.js +307 -0
  34. package/dist/visualization.model.test.d.ts +1 -0
  35. package/dist/visualization.types.d.ts +74 -0
  36. package/dist/visualization.types.js +1 -0
  37. package/dist/visualization.utils.d.ts +5 -0
  38. package/dist/visualization.utils.js +66 -0
  39. package/package.json +121 -5
@@ -0,0 +1,306 @@
1
+ // src/visualization.utils.ts
2
+ function formatVisualizationValue(value, format) {
3
+ if (value == null)
4
+ return "—";
5
+ if (format === "currency" && typeof value === "number") {
6
+ return new Intl.NumberFormat(undefined, {
7
+ style: "currency",
8
+ currency: "USD"
9
+ }).format(value);
10
+ }
11
+ if (format === "percentage" && typeof value === "number") {
12
+ return `${(value * 100).toFixed(1)}%`;
13
+ }
14
+ if ((format === "date" || format === "dateTime") && value) {
15
+ const date = value instanceof Date ? value : new Date(String(value));
16
+ return Number.isNaN(date.getTime()) ? String(value) : new Intl.DateTimeFormat(undefined, {
17
+ dateStyle: "medium",
18
+ timeStyle: format === "dateTime" ? "short" : undefined
19
+ }).format(date);
20
+ }
21
+ return String(value);
22
+ }
23
+ function resolveVisualizationRows(data, resultPath) {
24
+ const value = resultPath ? getAtPath(data, resultPath) : data;
25
+ const candidate = value ?? data;
26
+ if (Array.isArray(candidate))
27
+ return candidate.map(asRow);
28
+ if (Array.isArray(getAtPath(candidate, "items"))) {
29
+ return getAtPath(candidate, "items").map(asRow);
30
+ }
31
+ if (Array.isArray(getAtPath(candidate, "rows"))) {
32
+ return getAtPath(candidate, "rows").map(asRow);
33
+ }
34
+ if (Array.isArray(getAtPath(candidate, "data"))) {
35
+ return getAtPath(candidate, "data").map(asRow);
36
+ }
37
+ return candidate && typeof candidate === "object" ? [asRow(candidate)] : [];
38
+ }
39
+ function getAtPath(source, path) {
40
+ if (!path || source == null)
41
+ return source;
42
+ return path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean).reduce((current, segment) => {
43
+ if (current == null || typeof current !== "object")
44
+ return;
45
+ return current[segment];
46
+ }, source);
47
+ }
48
+ function toNumber(value) {
49
+ if (typeof value === "number" && Number.isFinite(value))
50
+ return value;
51
+ if (typeof value === "string" && value.trim()) {
52
+ const parsed = Number(value);
53
+ return Number.isFinite(parsed) ? parsed : null;
54
+ }
55
+ return null;
56
+ }
57
+ function asRow(value) {
58
+ return value && typeof value === "object" ? value : { value };
59
+ }
60
+
61
+ // src/visualization.model.helpers.ts
62
+ function createVisualizationMaps(spec) {
63
+ return {
64
+ dimensions: new Map((spec.visualization.dimensions ?? []).map((dimension) => [
65
+ dimension.key,
66
+ dimension
67
+ ])),
68
+ measures: new Map((spec.visualization.measures ?? []).map((measure) => [
69
+ measure.key,
70
+ measure
71
+ ]))
72
+ };
73
+ }
74
+ function createVisualizationBaseModel(spec, rows, maps) {
75
+ const config = spec.visualization;
76
+ return {
77
+ kind: config.kind,
78
+ title: config.title ?? spec.meta.title,
79
+ description: config.description ?? spec.meta.description,
80
+ summary: `${spec.meta.title ?? spec.meta.key}: ${rows.length} row${rows.length === 1 ? "" : "s"}`,
81
+ warnings: rows.length === 0 ? ["No visualization rows available."] : [],
82
+ palette: config.palette,
83
+ legend: config.legend,
84
+ tooltip: config.tooltip,
85
+ drilldown: config.drilldown,
86
+ thresholds: config.thresholds ?? [],
87
+ annotations: (config.annotations ?? []).map((annotation) => resolveAnnotation(annotation, rows[0] ?? {})),
88
+ table: createTable(config.table?.caption, config, rows)
89
+ };
90
+ }
91
+ function createMeasureSeries(measureKey, measures, rows, dimension, config) {
92
+ const measure = measures.get(measureKey);
93
+ if (!measure)
94
+ return null;
95
+ return {
96
+ key: config?.key ?? measure.key,
97
+ label: config?.label ?? measure.label,
98
+ type: config?.type,
99
+ color: config?.color ?? measure.color,
100
+ stack: config?.stack,
101
+ smooth: config?.smooth,
102
+ points: rows.map((row, index) => ({
103
+ id: `${measure.key}-${index}`,
104
+ raw: row,
105
+ x: dimension ? readDimensionValue(row, dimension) : index,
106
+ y: numericValue(row, measure),
107
+ value: numericValue(row, measure)
108
+ }))
109
+ };
110
+ }
111
+ function defaultSeries(keys) {
112
+ return keys.map((key) => ({ key, label: key, measure: key }));
113
+ }
114
+ function createAxis(dimension) {
115
+ if (!dimension)
116
+ return;
117
+ return {
118
+ key: dimension.key,
119
+ label: dimension.label,
120
+ type: dimension.type === "time" ? "time" : dimension.type === "number" ? "number" : "category",
121
+ format: dimension.format
122
+ };
123
+ }
124
+ function createValueAxis(key, measures) {
125
+ const measure = key ? measures.get(key) : undefined;
126
+ if (!measure)
127
+ return;
128
+ return {
129
+ key: measure.key,
130
+ label: measure.label,
131
+ type: "number",
132
+ format: measure.format
133
+ };
134
+ }
135
+ function readValue(row, measure) {
136
+ return measure ? getAtPath(row, measure.dataPath) : undefined;
137
+ }
138
+ function readDimensionValue(row, dimension) {
139
+ return dimension ? getAtPath(row, dimension.dataPath) : undefined;
140
+ }
141
+ function numericValue(row, measure) {
142
+ return toNumber(readValue(row, measure));
143
+ }
144
+ function numericPathValue(row, dimension) {
145
+ return toNumber(readDimensionValue(row, dimension));
146
+ }
147
+ function stringValue(row, dimension) {
148
+ const value = readDimensionValue(row, dimension);
149
+ return value == null ? undefined : String(value);
150
+ }
151
+ function createTable(caption, config, rows) {
152
+ const dimensions = config.dimensions ?? [];
153
+ const measures = config.measures ?? [];
154
+ return {
155
+ caption,
156
+ columns: [
157
+ ...dimensions.map((dimension) => ({
158
+ key: dimension.key,
159
+ label: dimension.label
160
+ })),
161
+ ...measures.map((measure) => ({
162
+ key: measure.key,
163
+ label: measure.label
164
+ }))
165
+ ],
166
+ rows: rows.map((row) => ({
167
+ ...Object.fromEntries(dimensions.map((dimension) => [
168
+ dimension.key,
169
+ readDimensionValue(row, dimension)
170
+ ])),
171
+ ...Object.fromEntries(measures.map((measure) => [measure.key, readValue(row, measure)]))
172
+ }))
173
+ };
174
+ }
175
+ function resolveAnnotation(annotation, row) {
176
+ return {
177
+ key: annotation.key,
178
+ label: annotation.label,
179
+ kind: annotation.kind,
180
+ color: annotation.color,
181
+ x: annotation.xDataPath ? getAtPath(row, annotation.xDataPath) : undefined,
182
+ y: annotation.yDataPath ? toNumber(getAtPath(row, annotation.yDataPath)) : undefined,
183
+ start: annotation.startDataPath ? getAtPath(row, annotation.startDataPath) : undefined,
184
+ end: annotation.endDataPath ? getAtPath(row, annotation.endDataPath) : undefined
185
+ };
186
+ }
187
+
188
+ // src/visualization.model.builders.ts
189
+ function buildMetricModel(base, config, rows, maps) {
190
+ const measure = maps.measures.get(config.measure);
191
+ const comparison = config.comparisonMeasure ? maps.measures.get(config.comparisonMeasure) : undefined;
192
+ const currentRow = rows.at(-1) ?? {};
193
+ const sparkline = config.sparkline ? createMeasureSeries(config.sparkline.measure ?? config.measure, maps.measures, rows, maps.dimensions.get(config.sparkline.dimension)) : null;
194
+ return {
195
+ ...base,
196
+ series: sparkline ? [sparkline] : [],
197
+ metric: {
198
+ label: measure?.label ?? config.measure,
199
+ value: readValue(currentRow, measure),
200
+ comparisonValue: comparison ? readValue(currentRow, comparison) : undefined,
201
+ format: measure?.format
202
+ }
203
+ };
204
+ }
205
+ function buildCartesianModel(base, config, rows, maps) {
206
+ const series = (config.series ?? defaultSeries(config.yMeasures ?? [])).map((seriesItem) => createMeasureSeries(seriesItem.measure, maps.measures, rows, maps.dimensions.get(config.xDimension), seriesItem)).filter((item) => Boolean(item));
207
+ return {
208
+ ...base,
209
+ series,
210
+ xAxis: createAxis(maps.dimensions.get(config.xDimension)),
211
+ yAxis: createValueAxis(config.yMeasures?.[0], maps.measures)
212
+ };
213
+ }
214
+ function buildDistributionModel(base, config, rows, maps) {
215
+ const nameDimension = maps.dimensions.get(config.nameDimension);
216
+ const valueMeasure = maps.measures.get(config.valueMeasure);
217
+ return {
218
+ ...base,
219
+ series: [
220
+ {
221
+ key: valueMeasure?.key ?? config.valueMeasure,
222
+ label: valueMeasure?.label ?? config.valueMeasure,
223
+ type: config.kind,
224
+ points: rows.map((row, index) => ({
225
+ id: `${config.kind}-${index}`,
226
+ raw: row,
227
+ name: stringValue(row, nameDimension),
228
+ value: numericValue(row, valueMeasure)
229
+ }))
230
+ }
231
+ ]
232
+ };
233
+ }
234
+ function buildHeatmapModel(base, config, rows, maps) {
235
+ const xDimension = maps.dimensions.get(config.xDimension);
236
+ const yDimension = maps.dimensions.get(config.yDimension);
237
+ const valueMeasure = maps.measures.get(config.valueMeasure);
238
+ return {
239
+ ...base,
240
+ series: [
241
+ {
242
+ key: config.valueMeasure,
243
+ label: valueMeasure?.label ?? config.valueMeasure,
244
+ type: "heatmap",
245
+ points: rows.map((row, index) => ({
246
+ id: `heatmap-${index}`,
247
+ raw: row,
248
+ name: stringValue(row, yDimension),
249
+ x: readDimensionValue(row, xDimension),
250
+ y: numericValue(row, valueMeasure),
251
+ value: numericValue(row, valueMeasure)
252
+ }))
253
+ }
254
+ ],
255
+ xAxis: createAxis(xDimension),
256
+ yAxis: createAxis(yDimension)
257
+ };
258
+ }
259
+ function buildGeoModel(base, config, rows, maps) {
260
+ return {
261
+ ...base,
262
+ series: [
263
+ {
264
+ key: "geo",
265
+ label: "Geo",
266
+ type: config.variant ?? "scatter",
267
+ points: rows.map((row, index) => ({
268
+ id: `geo-${index}`,
269
+ raw: row,
270
+ name: config.nameDimension ? stringValue(row, maps.dimensions.get(config.nameDimension)) : undefined,
271
+ value: config.valueMeasure ? numericValue(row, maps.measures.get(config.valueMeasure)) : undefined,
272
+ latitude: numericPathValue(row, maps.dimensions.get(config.latitudeDimension ?? "")),
273
+ longitude: numericPathValue(row, maps.dimensions.get(config.longitudeDimension ?? ""))
274
+ }))
275
+ }
276
+ ],
277
+ geo: {
278
+ mode: config.mode ?? "chart",
279
+ variant: config.variant ?? "scatter",
280
+ geoJson: config.geoJson
281
+ }
282
+ };
283
+ }
284
+
285
+ // src/visualization.model.ts
286
+ function createVisualizationModel(spec, data) {
287
+ const rows = resolveVisualizationRows(data, spec.source.resultPath);
288
+ const maps = createVisualizationMaps(spec);
289
+ const base = createVisualizationBaseModel(spec, rows, maps);
290
+ switch (spec.visualization.kind) {
291
+ case "metric":
292
+ return buildMetricModel(base, spec.visualization, rows, maps);
293
+ case "cartesian":
294
+ return buildCartesianModel(base, spec.visualization, rows, maps);
295
+ case "pie":
296
+ case "funnel":
297
+ return buildDistributionModel(base, spec.visualization, rows, maps);
298
+ case "heatmap":
299
+ return buildHeatmapModel(base, spec.visualization, rows, maps);
300
+ case "geo":
301
+ return buildGeoModel(base, spec.visualization, rows, maps);
302
+ }
303
+ }
304
+ export {
305
+ createVisualizationModel
306
+ };
File without changes
@@ -0,0 +1,65 @@
1
+ // src/visualization.utils.ts
2
+ function formatVisualizationValue(value, format) {
3
+ if (value == null)
4
+ return "—";
5
+ if (format === "currency" && typeof value === "number") {
6
+ return new Intl.NumberFormat(undefined, {
7
+ style: "currency",
8
+ currency: "USD"
9
+ }).format(value);
10
+ }
11
+ if (format === "percentage" && typeof value === "number") {
12
+ return `${(value * 100).toFixed(1)}%`;
13
+ }
14
+ if ((format === "date" || format === "dateTime") && value) {
15
+ const date = value instanceof Date ? value : new Date(String(value));
16
+ return Number.isNaN(date.getTime()) ? String(value) : new Intl.DateTimeFormat(undefined, {
17
+ dateStyle: "medium",
18
+ timeStyle: format === "dateTime" ? "short" : undefined
19
+ }).format(date);
20
+ }
21
+ return String(value);
22
+ }
23
+ function resolveVisualizationRows(data, resultPath) {
24
+ const value = resultPath ? getAtPath(data, resultPath) : data;
25
+ const candidate = value ?? data;
26
+ if (Array.isArray(candidate))
27
+ return candidate.map(asRow);
28
+ if (Array.isArray(getAtPath(candidate, "items"))) {
29
+ return getAtPath(candidate, "items").map(asRow);
30
+ }
31
+ if (Array.isArray(getAtPath(candidate, "rows"))) {
32
+ return getAtPath(candidate, "rows").map(asRow);
33
+ }
34
+ if (Array.isArray(getAtPath(candidate, "data"))) {
35
+ return getAtPath(candidate, "data").map(asRow);
36
+ }
37
+ return candidate && typeof candidate === "object" ? [asRow(candidate)] : [];
38
+ }
39
+ function getAtPath(source, path) {
40
+ if (!path || source == null)
41
+ return source;
42
+ return path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean).reduce((current, segment) => {
43
+ if (current == null || typeof current !== "object")
44
+ return;
45
+ return current[segment];
46
+ }, source);
47
+ }
48
+ function toNumber(value) {
49
+ if (typeof value === "number" && Number.isFinite(value))
50
+ return value;
51
+ if (typeof value === "string" && value.trim()) {
52
+ const parsed = Number(value);
53
+ return Number.isFinite(parsed) ? parsed : null;
54
+ }
55
+ return null;
56
+ }
57
+ function asRow(value) {
58
+ return value && typeof value === "object" ? value : { value };
59
+ }
60
+ export {
61
+ toNumber,
62
+ resolveVisualizationRows,
63
+ getAtPath,
64
+ formatVisualizationValue
65
+ };
package/dist/index.d.ts CHANGED
@@ -10,6 +10,11 @@ export type ListFetcher<TVars, TItem> = (vars: TVars) => Promise<{
10
10
  totalItems?: number;
11
11
  totalPages?: number;
12
12
  }>;
13
+ export type { ContractTableCellAlign, ContractTableCellRenderModel, ContractTableColumnRenderModel, ContractTableController, ContractTableExecutionMode, ContractTableExpandedState, ContractTableInitialState, ContractTablePaginationState, ContractTablePinningState, ContractTablePinState, ContractTableRowRenderModel, ContractTableRowSelectionState, ContractTableSelectionMode, ContractTableSizingState, ContractTableSort, ContractTableState, ContractTableVisibilityState, } from './table';
14
+ export { createEmptyTableState } from './table';
15
+ export type { ContractVisualizationAnnotationModel, ContractVisualizationAxisModel, ContractVisualizationGeoModel, ContractVisualizationMetricModel, ContractVisualizationPoint, ContractVisualizationRenderModel, ContractVisualizationSeriesModel, ContractVisualizationTableAlternative, } from './visualization';
16
+ export { createVisualizationModel, formatVisualizationValue, } from './visualization';
17
+ export { buildVisualizationEChartsOption } from './visualization.echarts';
13
18
  export interface NextAliasOptions {
14
19
  uiKitWeb?: string;
15
20
  uiKitNative?: string;