@opendata-ai/openchart-engine 1.2.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 (85) hide show
  1. package/dist/index.d.ts +366 -0
  2. package/dist/index.js +4227 -0
  3. package/dist/index.js.map +1 -0
  4. package/package.json +62 -0
  5. package/src/__test-fixtures__/specs.ts +124 -0
  6. package/src/__tests__/axes.test.ts +114 -0
  7. package/src/__tests__/compile-chart.test.ts +337 -0
  8. package/src/__tests__/dimensions.test.ts +151 -0
  9. package/src/__tests__/legend.test.ts +113 -0
  10. package/src/__tests__/scales.test.ts +109 -0
  11. package/src/annotations/__tests__/compute.test.ts +454 -0
  12. package/src/annotations/compute.ts +603 -0
  13. package/src/charts/__tests__/registry.test.ts +110 -0
  14. package/src/charts/bar/__tests__/compute.test.ts +294 -0
  15. package/src/charts/bar/__tests__/labels.test.ts +75 -0
  16. package/src/charts/bar/compute.ts +205 -0
  17. package/src/charts/bar/index.ts +33 -0
  18. package/src/charts/bar/labels.ts +132 -0
  19. package/src/charts/column/__tests__/compute.test.ts +277 -0
  20. package/src/charts/column/compute.ts +282 -0
  21. package/src/charts/column/index.ts +33 -0
  22. package/src/charts/column/labels.ts +108 -0
  23. package/src/charts/dot/__tests__/compute.test.ts +344 -0
  24. package/src/charts/dot/compute.ts +257 -0
  25. package/src/charts/dot/index.ts +46 -0
  26. package/src/charts/dot/labels.ts +97 -0
  27. package/src/charts/line/__tests__/compute.test.ts +437 -0
  28. package/src/charts/line/__tests__/labels.test.ts +93 -0
  29. package/src/charts/line/area.ts +288 -0
  30. package/src/charts/line/compute.ts +177 -0
  31. package/src/charts/line/index.ts +68 -0
  32. package/src/charts/line/labels.ts +144 -0
  33. package/src/charts/pie/__tests__/compute.test.ts +276 -0
  34. package/src/charts/pie/compute.ts +234 -0
  35. package/src/charts/pie/index.ts +49 -0
  36. package/src/charts/pie/labels.ts +142 -0
  37. package/src/charts/registry.ts +64 -0
  38. package/src/charts/scatter/__tests__/compute.test.ts +304 -0
  39. package/src/charts/scatter/__tests__/trendline.test.ts +191 -0
  40. package/src/charts/scatter/compute.ts +124 -0
  41. package/src/charts/scatter/index.ts +41 -0
  42. package/src/charts/scatter/trendline.ts +100 -0
  43. package/src/charts/utils.ts +120 -0
  44. package/src/compile.ts +368 -0
  45. package/src/compiler/__tests__/compile.test.ts +87 -0
  46. package/src/compiler/__tests__/normalize.test.ts +210 -0
  47. package/src/compiler/__tests__/validate.test.ts +440 -0
  48. package/src/compiler/index.ts +47 -0
  49. package/src/compiler/normalize.ts +269 -0
  50. package/src/compiler/types.ts +148 -0
  51. package/src/compiler/validate.ts +581 -0
  52. package/src/graphs/__tests__/community.test.ts +228 -0
  53. package/src/graphs/__tests__/compile-graph.test.ts +315 -0
  54. package/src/graphs/__tests__/encoding.test.ts +314 -0
  55. package/src/graphs/community.ts +92 -0
  56. package/src/graphs/compile-graph.ts +291 -0
  57. package/src/graphs/encoding.ts +302 -0
  58. package/src/graphs/types.ts +98 -0
  59. package/src/index.ts +74 -0
  60. package/src/layout/axes.ts +194 -0
  61. package/src/layout/dimensions.ts +199 -0
  62. package/src/layout/gridlines.ts +84 -0
  63. package/src/layout/scales.ts +426 -0
  64. package/src/legend/compute.ts +186 -0
  65. package/src/tables/__tests__/bar-column.test.ts +147 -0
  66. package/src/tables/__tests__/category-colors.test.ts +153 -0
  67. package/src/tables/__tests__/compile-table.test.ts +208 -0
  68. package/src/tables/__tests__/format-cells.test.ts +126 -0
  69. package/src/tables/__tests__/heatmap.test.ts +124 -0
  70. package/src/tables/__tests__/pagination.test.ts +78 -0
  71. package/src/tables/__tests__/search.test.ts +94 -0
  72. package/src/tables/__tests__/sort.test.ts +107 -0
  73. package/src/tables/__tests__/sparkline.test.ts +122 -0
  74. package/src/tables/bar-column.ts +94 -0
  75. package/src/tables/category-colors.ts +67 -0
  76. package/src/tables/compile-table.ts +420 -0
  77. package/src/tables/format-cells.ts +110 -0
  78. package/src/tables/heatmap.ts +121 -0
  79. package/src/tables/pagination.ts +46 -0
  80. package/src/tables/search.ts +66 -0
  81. package/src/tables/sort.ts +69 -0
  82. package/src/tables/sparkline.ts +113 -0
  83. package/src/tables/utils.ts +16 -0
  84. package/src/tooltips/__tests__/compute.test.ts +328 -0
  85. package/src/tooltips/compute.ts +231 -0
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Bar column computation for inline bar visualization in table cells.
3
+ *
4
+ * Computes bar width as a proportion of the max value.
5
+ * Supports negative values with bidirectional bars.
6
+ */
7
+
8
+ import type { BarColumnConfig, ResolvedTheme } from '@opendata-ai/openchart-core';
9
+
10
+ const NEGATIVE_BAR_COLOR = '#c44e52';
11
+
12
+ /**
13
+ * Compute the bar percentage, offset, and color for a single cell value.
14
+ *
15
+ * barPercent is 0-1. barOffset is 0-1 (left edge position).
16
+ * When the column has negative values, bars extend bidirectionally from a zero line.
17
+ */
18
+ export function computeBarCell(
19
+ value: number,
20
+ config: BarColumnConfig,
21
+ columnMax: number,
22
+ columnMin: number,
23
+ theme: ResolvedTheme,
24
+ _darkMode: boolean,
25
+ ): { barPercent: number; barOffset: number; barColor: string; isNegative: boolean } {
26
+ const barColor = config.color ?? theme.colors.categorical[0];
27
+ const hasNegatives = columnMin < 0;
28
+
29
+ if (!Number.isFinite(value)) {
30
+ return { barPercent: 0, barOffset: 0, barColor, isNegative: false };
31
+ }
32
+
33
+ if (!hasNegatives) {
34
+ // Positive-only column: simple left-to-right bars
35
+ const maxValue = config.maxValue ?? columnMax;
36
+ if (maxValue <= 0) {
37
+ return { barPercent: 0, barOffset: 0, barColor, isNegative: false };
38
+ }
39
+ const barPercent = Math.max(0, Math.min(1, value / maxValue));
40
+ return { barPercent, barOffset: 0, barColor, isNegative: false };
41
+ }
42
+
43
+ // Bidirectional: zero line position proportional to data range
44
+ const maxPos = config.maxValue ?? columnMax;
45
+ const absMin = Math.abs(columnMin);
46
+ const totalRange = maxPos + absMin;
47
+ if (totalRange === 0) {
48
+ return { barPercent: 0, barOffset: 0, barColor, isNegative: false };
49
+ }
50
+
51
+ const zeroPos = absMin / totalRange;
52
+
53
+ if (value >= 0) {
54
+ const barPercent = value / totalRange;
55
+ return { barPercent, barOffset: zeroPos, barColor, isNegative: false };
56
+ }
57
+
58
+ // Negative value: red bar extending left from zero
59
+ const barPercent = Math.abs(value) / totalRange;
60
+ return {
61
+ barPercent,
62
+ barOffset: zeroPos - barPercent,
63
+ barColor: config.color ?? NEGATIVE_BAR_COLOR,
64
+ isNegative: true,
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Compute the column max and min from data for bar scaling.
70
+ */
71
+ export function computeColumnMax(data: Record<string, unknown>[], key: string): number {
72
+ let max = 0;
73
+ for (const row of data) {
74
+ const val = row[key];
75
+ if (typeof val === 'number' && Number.isFinite(val) && val > max) {
76
+ max = val;
77
+ }
78
+ }
79
+ return max;
80
+ }
81
+
82
+ /**
83
+ * Compute the column minimum from data (for negative bar support).
84
+ */
85
+ export function computeColumnMin(data: Record<string, unknown>[], key: string): number {
86
+ let min = 0;
87
+ for (const row of data) {
88
+ const val = row[key];
89
+ if (typeof val === 'number' && Number.isFinite(val) && val < min) {
90
+ min = val;
91
+ }
92
+ }
93
+ return min;
94
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Category color assignment for table columns.
3
+ *
4
+ * Maps categorical values to colors using explicit mappings or
5
+ * theme categorical palette, with AA-contrast text colors.
6
+ */
7
+
8
+ import type { CellStyle, ColumnConfig, ResolvedTheme } from '@opendata-ai/openchart-core';
9
+ import { adaptColorForDarkMode } from '@opendata-ai/openchart-core';
10
+ import { accessibleTextColor } from './utils';
11
+
12
+ /**
13
+ * Compute category-colored cell styles for a column.
14
+ *
15
+ * Uses column.categoryColors for explicit value-to-color mappings.
16
+ * Unmapped values get colors from the theme's categorical palette.
17
+ *
18
+ * Returns a Map keyed by original data index with background and text colors.
19
+ */
20
+ export function computeCategoryColors(
21
+ data: Record<string, unknown>[],
22
+ column: ColumnConfig,
23
+ theme: ResolvedTheme,
24
+ darkMode: boolean,
25
+ ): Map<number, CellStyle> {
26
+ const result = new Map<number, CellStyle>();
27
+ const explicitMap = column.categoryColors;
28
+ if (!explicitMap) return result;
29
+
30
+ const categoricalPalette = theme.colors.categorical;
31
+ let nextPaletteIndex = 0;
32
+ const autoAssigned = new Map<string, string>();
33
+ const lightBg = '#ffffff';
34
+ const darkBg = theme.colors.background;
35
+
36
+ for (let i = 0; i < data.length; i++) {
37
+ const raw = data[i][column.key];
38
+ if (raw == null) continue;
39
+
40
+ const key = String(raw);
41
+ let bg: string;
42
+
43
+ if (explicitMap[key]) {
44
+ bg = explicitMap[key];
45
+ } else if (autoAssigned.has(key)) {
46
+ bg = autoAssigned.get(key)!;
47
+ } else {
48
+ // Assign from categorical palette
49
+ bg = categoricalPalette[nextPaletteIndex % categoricalPalette.length];
50
+ nextPaletteIndex++;
51
+ autoAssigned.set(key, bg);
52
+ }
53
+
54
+ // Dark mode adaptation
55
+ if (darkMode) {
56
+ bg = adaptColorForDarkMode(bg, lightBg, darkBg);
57
+ }
58
+
59
+ const textColor = accessibleTextColor(bg);
60
+ result.set(i, {
61
+ backgroundColor: bg,
62
+ color: textColor,
63
+ });
64
+ }
65
+
66
+ return result;
67
+ }
@@ -0,0 +1,420 @@
1
+ /**
2
+ * Table compilation pipeline.
3
+ *
4
+ * Takes a NormalizedTableSpec and produces a fully resolved TableLayout:
5
+ * resolve columns -> build search index -> sort data -> filter by search ->
6
+ * paginate -> format visible cells -> apply visual enhancements -> return
7
+ */
8
+
9
+ import type {
10
+ CellStyle,
11
+ ColumnConfig,
12
+ CompileTableOptions,
13
+ PaginationState,
14
+ ResolvedColumn,
15
+ ResolvedTheme,
16
+ TableCell,
17
+ TableLayout,
18
+ TableRow,
19
+ } from '@opendata-ai/openchart-core';
20
+ import { computeChrome, estimateTextWidth } from '@opendata-ai/openchart-core';
21
+
22
+ import type { NormalizedTableSpec } from '../compiler/types';
23
+ import { computeBarCell, computeColumnMax, computeColumnMin } from './bar-column';
24
+ import { computeCategoryColors } from './category-colors';
25
+ import { formatCell } from './format-cells';
26
+ import { computeHeatmapColors } from './heatmap';
27
+ import { paginateData } from './pagination';
28
+ import { buildSearchIndex, filterBySearch } from './search';
29
+ import { sortData } from './sort';
30
+ import { computeSparklineForRow, type SparklineData } from './sparkline';
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // Column resolution
34
+ // ---------------------------------------------------------------------------
35
+
36
+ /**
37
+ * Determine the cell type for a column based on its config.
38
+ * Precedence: sparkline > bar > heatmap > image > flag > categoryColors > text
39
+ */
40
+ function determineCellType(col: ColumnConfig): ResolvedColumn['cellType'] {
41
+ if (col.sparkline) return 'sparkline';
42
+ if (col.bar) return 'bar';
43
+ if (col.heatmap) return 'heatmap';
44
+ if (col.image) return 'image';
45
+ if (col.flag) return 'flag';
46
+ if (col.categoryColors) return 'category';
47
+ return 'text';
48
+ }
49
+
50
+ /**
51
+ * Infer alignment for a column.
52
+ * Explicit align wins. Otherwise: right for numeric data, left for everything else.
53
+ */
54
+ function inferAlignment(
55
+ col: ColumnConfig,
56
+ data: Record<string, unknown>[],
57
+ ): 'left' | 'center' | 'right' {
58
+ if (col.align) return col.align;
59
+
60
+ // Check first non-null value in the data
61
+ for (const row of data) {
62
+ const val = row[col.key];
63
+ if (val != null) {
64
+ return typeof val === 'number' ? 'right' : 'left';
65
+ }
66
+ }
67
+ return 'left';
68
+ }
69
+
70
+ /**
71
+ * Estimate the needed width for a column by measuring header and data values.
72
+ * Samples up to 100 rows for estimation.
73
+ */
74
+ function estimateColumnWidth(
75
+ col: ColumnConfig,
76
+ data: Record<string, unknown>[],
77
+ fontSize: number,
78
+ ): number {
79
+ const MIN_WIDTH = 60;
80
+ const PADDING = 24; // cell padding
81
+
82
+ // Visual columns get fixed widths (they render graphics, not text)
83
+ if (col.sparkline) return 140;
84
+ if (col.image) return (col.image.width ?? 24) + PADDING;
85
+ if (col.flag) return 60;
86
+
87
+ // Header width
88
+ const label = col.label ?? col.key;
89
+ const headerWidth = estimateTextWidth(label, fontSize, 600) + PADDING;
90
+
91
+ // Sample data values
92
+ const sampleSize = Math.min(100, data.length);
93
+ let maxDataWidth = 0;
94
+
95
+ for (let i = 0; i < sampleSize; i++) {
96
+ const val = data[i][col.key];
97
+ const text = val == null ? '' : String(val);
98
+ const width = estimateTextWidth(text, fontSize, 400) + PADDING;
99
+ if (width > maxDataWidth) maxDataWidth = width;
100
+ }
101
+
102
+ return Math.max(MIN_WIDTH, headerWidth, maxDataWidth);
103
+ }
104
+
105
+ /**
106
+ * Resolve all columns: compute widths, types, alignment.
107
+ */
108
+ function resolveColumns(
109
+ columns: ColumnConfig[],
110
+ data: Record<string, unknown>[],
111
+ totalWidth: number,
112
+ theme: ResolvedTheme,
113
+ ): ResolvedColumn[] {
114
+ const fontSize = theme.fonts.sizes.body;
115
+
116
+ // Compute natural widths and identify fixed-width visual columns.
117
+ // Visual columns (sparkline, image, flag) get fixed sizes; only text
118
+ // columns participate in proportional scaling to fill the container.
119
+ const isFixed = columns.map((col) => !!(col.sparkline || col.image || col.flag));
120
+
121
+ const naturalWidths = columns.map((col) => {
122
+ if (col.width) {
123
+ // Parse explicit width
124
+ if (col.width.endsWith('px')) {
125
+ return parseInt(col.width, 10) || 100;
126
+ }
127
+ if (col.width.endsWith('%')) {
128
+ return (parseFloat(col.width) / 100) * totalWidth || 100;
129
+ }
130
+ return parseInt(col.width, 10) || 100;
131
+ }
132
+ return estimateColumnWidth(col, data, fontSize);
133
+ });
134
+
135
+ // Fixed columns keep their natural width; remaining space goes to text columns
136
+ const fixedTotal = naturalWidths.reduce((sum, w, i) => sum + (isFixed[i] ? w : 0), 0);
137
+ const flexTotal = naturalWidths.reduce((sum, w, i) => sum + (isFixed[i] ? 0 : w), 0);
138
+ const remainingWidth = totalWidth - fixedTotal;
139
+ const flexScale = flexTotal > 0 && remainingWidth > 0 ? remainingWidth / flexTotal : 1;
140
+
141
+ return columns.map((col, i) => ({
142
+ key: col.key,
143
+ label: col.label ?? col.key,
144
+ width: Math.max(60, isFixed[i] ? naturalWidths[i] : Math.round(naturalWidths[i] * flexScale)),
145
+ sortable: col.sortable ?? true,
146
+ align: inferAlignment(col, data),
147
+ cellType: determineCellType(col),
148
+ }));
149
+ }
150
+
151
+ // ---------------------------------------------------------------------------
152
+ // Cell building
153
+ // ---------------------------------------------------------------------------
154
+
155
+ /**
156
+ * Build a fully resolved TableCell from a data value and column config.
157
+ */
158
+ function buildCell(
159
+ value: unknown,
160
+ column: ColumnConfig,
161
+ resolvedColumn: ResolvedColumn,
162
+ heatmapStyle: CellStyle | undefined,
163
+ categoryStyle: CellStyle | undefined,
164
+ barData:
165
+ | { barPercent: number; barOffset: number; barColor: string; isNegative: boolean }
166
+ | undefined,
167
+ sparklineData: SparklineData | null,
168
+ ): TableCell {
169
+ const base = formatCell(value, column);
170
+
171
+ // Apply font variant for number columns
172
+ if (typeof value === 'number') {
173
+ base.style = { ...base.style, fontVariant: 'tabular-nums' };
174
+ }
175
+
176
+ const cellType = resolvedColumn.cellType;
177
+
178
+ switch (cellType) {
179
+ case 'heatmap': {
180
+ const merged = heatmapStyle ? { ...base.style, ...heatmapStyle } : base.style;
181
+ return {
182
+ ...base,
183
+ cellType: 'heatmap',
184
+ style: merged,
185
+ };
186
+ }
187
+ case 'category': {
188
+ const merged = categoryStyle ? { ...base.style, ...categoryStyle } : base.style;
189
+ return {
190
+ ...base,
191
+ cellType: 'category',
192
+ style: merged,
193
+ };
194
+ }
195
+ case 'bar': {
196
+ return {
197
+ ...base,
198
+ cellType: 'bar',
199
+ barWidth: barData?.barPercent ?? 0,
200
+ barOffset: barData?.barOffset ?? 0,
201
+ barColor: barData?.barColor ?? '#ccc',
202
+ isNegative: barData?.isNegative ?? false,
203
+ };
204
+ }
205
+ case 'sparkline': {
206
+ return {
207
+ ...base,
208
+ cellType: 'sparkline',
209
+ sparklineData,
210
+ };
211
+ }
212
+ case 'image': {
213
+ const src = typeof value === 'string' ? value : '';
214
+ const imgConfig = column.image ?? {};
215
+ return {
216
+ ...base,
217
+ cellType: 'image',
218
+ src,
219
+ imageWidth: imgConfig.width ?? 24,
220
+ imageHeight: imgConfig.height ?? 24,
221
+ rounded: imgConfig.rounded ?? false,
222
+ };
223
+ }
224
+ case 'flag': {
225
+ const code = typeof value === 'string' ? value : '';
226
+ return {
227
+ ...base,
228
+ cellType: 'flag',
229
+ countryCode: code,
230
+ };
231
+ }
232
+ default: {
233
+ return {
234
+ ...base,
235
+ cellType: 'text',
236
+ };
237
+ }
238
+ }
239
+ }
240
+
241
+ // ---------------------------------------------------------------------------
242
+ // Main pipeline
243
+ // ---------------------------------------------------------------------------
244
+
245
+ /**
246
+ * Compile a normalized table spec into a TableLayout.
247
+ *
248
+ * Pipeline:
249
+ * 1. Resolve columns (widths, types, alignment)
250
+ * 2. Build search index
251
+ * 3. Sort data
252
+ * 4. Filter by search
253
+ * 5. Paginate
254
+ * 6. Format visible cells and apply visual enhancements
255
+ * 7. Return TableLayout
256
+ */
257
+ export function compileTableLayout(
258
+ spec: NormalizedTableSpec,
259
+ options: CompileTableOptions,
260
+ theme: ResolvedTheme,
261
+ ): TableLayout {
262
+ const data = spec.data;
263
+ const darkMode = theme.isDark;
264
+
265
+ // 1. Resolve columns
266
+ const resolvedColumns = resolveColumns(spec.columns, data, options.width, theme);
267
+
268
+ // 2. Build search index (over full dataset, using original indices)
269
+ const searchIndex = spec.search
270
+ ? buildSearchIndex(data, spec.columns)
271
+ : new Map<number, string>();
272
+
273
+ // 3. Track original indices through the pipeline
274
+ let currentData = data;
275
+ let originalIndices = data.map((_, i) => i);
276
+
277
+ // 4. Sort
278
+ if (options.sort) {
279
+ const sorted = sortData(currentData, options.sort);
280
+ // Map sorted originalIndices back through our current index mapping
281
+ originalIndices = sorted.originalIndices.map((i) => originalIndices[i]);
282
+ currentData = sorted.data;
283
+ }
284
+
285
+ // 5. Filter by search
286
+ if (spec.search && options.search) {
287
+ const filtered = filterBySearch(currentData, options.search, searchIndex, originalIndices);
288
+ currentData = filtered.data;
289
+ originalIndices = filtered.indices;
290
+ }
291
+
292
+ const totalFiltered = currentData.length;
293
+
294
+ // 6. Paginate
295
+ let pageSize = 0;
296
+ let currentPage = 0;
297
+ let paginationState: PaginationState | undefined;
298
+
299
+ if (spec.pagination) {
300
+ pageSize =
301
+ options.pageSize ?? (typeof spec.pagination === 'object' ? spec.pagination.pageSize : 25);
302
+ currentPage = options.page ?? 0;
303
+ const paginated = paginateData(currentData, currentPage, pageSize);
304
+
305
+ // Slice indices too
306
+ const start = paginated.page * pageSize;
307
+ const end = start + paginated.rows.length;
308
+ const pageIndices = originalIndices.slice(start, end);
309
+
310
+ currentData = paginated.rows;
311
+ originalIndices = pageIndices;
312
+
313
+ paginationState = {
314
+ page: paginated.page,
315
+ pageSize,
316
+ totalRows: paginated.totalRows,
317
+ totalPages: paginated.totalPages,
318
+ };
319
+ }
320
+
321
+ // 7. Pre-compute visual enhancements for visible data columns
322
+ // We need heatmap/category colors computed over the FULL dataset, then
323
+ // applied only to visible rows.
324
+ const heatmapMaps = new Map<string, Map<number, CellStyle>>();
325
+ const categoryMaps = new Map<string, Map<number, CellStyle>>();
326
+ const barMaxes = new Map<string, number>();
327
+ const barMins = new Map<string, number>();
328
+
329
+ for (let c = 0; c < spec.columns.length; c++) {
330
+ const col = spec.columns[c];
331
+ const resolved = resolvedColumns[c];
332
+
333
+ if (resolved.cellType === 'heatmap' && col.heatmap) {
334
+ heatmapMaps.set(col.key, computeHeatmapColors(data, col, theme, darkMode));
335
+ }
336
+ if (resolved.cellType === 'category' && col.categoryColors) {
337
+ categoryMaps.set(col.key, computeCategoryColors(data, col, theme, darkMode));
338
+ }
339
+ if (resolved.cellType === 'bar' && col.bar) {
340
+ barMaxes.set(col.key, computeColumnMax(data, col.key));
341
+ barMins.set(col.key, computeColumnMin(data, col.key));
342
+ }
343
+ }
344
+
345
+ // 8. Build rows from visible data
346
+ const rows: TableRow[] = currentData.map((row, i) => {
347
+ const origIdx = originalIndices[i];
348
+ const rowId = spec.rowKey ? String(row[spec.rowKey] ?? origIdx) : String(origIdx);
349
+
350
+ const cells: TableCell[] = spec.columns.map((col, c) => {
351
+ const resolved = resolvedColumns[c];
352
+ const value = row[col.key];
353
+
354
+ // Lookup visual enhancement data
355
+ const heatmapStyle = heatmapMaps.get(col.key)?.get(origIdx);
356
+ const categoryStyle = categoryMaps.get(col.key)?.get(origIdx);
357
+
358
+ let barData:
359
+ | { barPercent: number; barOffset: number; barColor: string; isNegative: boolean }
360
+ | undefined;
361
+ if (resolved.cellType === 'bar' && col.bar && typeof value === 'number') {
362
+ barData = computeBarCell(
363
+ value,
364
+ col.bar,
365
+ barMaxes.get(col.key) ?? 0,
366
+ barMins.get(col.key) ?? 0,
367
+ theme,
368
+ darkMode,
369
+ );
370
+ }
371
+
372
+ let sparklineData: SparklineData | null = null;
373
+ if (resolved.cellType === 'sparkline' && col.sparkline) {
374
+ sparklineData = computeSparklineForRow(row, col.key, col.sparkline, theme, darkMode);
375
+ }
376
+
377
+ return buildCell(value, col, resolved, heatmapStyle, categoryStyle, barData, sparklineData);
378
+ });
379
+
380
+ return { id: rowId, cells, data: row };
381
+ });
382
+
383
+ // 9. Compute chrome
384
+ const chrome = computeChrome(
385
+ {
386
+ title: spec.chrome.title,
387
+ subtitle: spec.chrome.subtitle,
388
+ source: spec.chrome.source,
389
+ byline: spec.chrome.byline,
390
+ footer: spec.chrome.footer,
391
+ },
392
+ theme,
393
+ options.width,
394
+ options.measureText,
395
+ );
396
+
397
+ // 10. Build a11y
398
+ const titleText = spec.chrome.title?.text ?? '';
399
+ const caption = titleText ? `Table: ${titleText}` : `Data table with ${data.length} rows`;
400
+
401
+ return {
402
+ chrome,
403
+ columns: resolvedColumns,
404
+ rows,
405
+ sort: options.sort,
406
+ pagination: paginationState,
407
+ search: {
408
+ enabled: spec.search,
409
+ placeholder: 'Search...',
410
+ query: options.search ?? '',
411
+ },
412
+ stickyFirstColumn: spec.stickyFirstColumn,
413
+ compact: spec.compact,
414
+ a11y: {
415
+ caption,
416
+ summary: `${resolvedColumns.length} columns, ${totalFiltered} rows`,
417
+ },
418
+ theme,
419
+ };
420
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Cell value formatting for table columns.
3
+ *
4
+ * Handles number formatting (d3-format), date formatting, and
5
+ * null/undefined values. Produces the formattedValue string and
6
+ * base style for each cell.
7
+ */
8
+
9
+ import type { CellStyle, ColumnConfig, TableCellBase } from '@opendata-ai/openchart-core';
10
+ import { formatDate, formatNumber } from '@opendata-ai/openchart-core';
11
+ import { format as d3Format } from 'd3-format';
12
+
13
+ /**
14
+ * Check if a value is numeric (finite number or parseable numeric string).
15
+ */
16
+ function isNumericValue(value: unknown): value is number {
17
+ if (typeof value === 'number') return Number.isFinite(value);
18
+ return false;
19
+ }
20
+
21
+ /**
22
+ * Check if a value is a date.
23
+ */
24
+ function isDateValue(value: unknown): boolean {
25
+ if (value instanceof Date) return !Number.isNaN(value.getTime());
26
+ return false;
27
+ }
28
+
29
+ /**
30
+ * Format a raw cell value into a display string with styling.
31
+ *
32
+ * Formatting precedence:
33
+ * 1. null/undefined -> ""
34
+ * 2. column.format (d3-format string) for numbers
35
+ * 3. Auto-format: numbers via formatNumber, dates via formatDate
36
+ * 4. Fallback: String(value)
37
+ */
38
+ export function formatCell(value: unknown, column: ColumnConfig): TableCellBase {
39
+ const style: CellStyle = {};
40
+
41
+ // Null/undefined -> empty
42
+ if (value == null) {
43
+ return {
44
+ value,
45
+ formattedValue: '',
46
+ style,
47
+ };
48
+ }
49
+
50
+ // If column has a d3-format string and value is numeric
51
+ if (column.format && isNumericValue(value)) {
52
+ try {
53
+ const formatter = d3Format(column.format);
54
+ return {
55
+ value,
56
+ formattedValue: formatter(value),
57
+ style,
58
+ };
59
+ } catch {
60
+ // Fall through to auto-format if format string is invalid
61
+ }
62
+ }
63
+
64
+ // Auto-format numbers
65
+ if (isNumericValue(value)) {
66
+ return {
67
+ value,
68
+ formattedValue: formatNumber(value),
69
+ style,
70
+ };
71
+ }
72
+
73
+ // Auto-format dates
74
+ if (isDateValue(value)) {
75
+ return {
76
+ value,
77
+ formattedValue: formatDate(value as Date),
78
+ style,
79
+ };
80
+ }
81
+
82
+ // String and everything else
83
+ return {
84
+ value,
85
+ formattedValue: String(value),
86
+ style,
87
+ };
88
+ }
89
+
90
+ /**
91
+ * Format a value into a string for search indexing.
92
+ * Uses d3-format for numeric columns, otherwise String().
93
+ */
94
+ export function formatValueForSearch(value: unknown, column: ColumnConfig): string {
95
+ if (value == null) return '';
96
+
97
+ if (column.format && isNumericValue(value)) {
98
+ try {
99
+ return d3Format(column.format)(value);
100
+ } catch {
101
+ // Fall through
102
+ }
103
+ }
104
+
105
+ if (isNumericValue(value)) {
106
+ return formatNumber(value);
107
+ }
108
+
109
+ return String(value);
110
+ }