(
rows: Record<string, unknown>[],
rawSpec: unknown,
options?: { truncated?: boolean }
)
| 296 | } |
| 297 | |
| 298 | export function buildChartPayload( |
| 299 | rows: Record<string, unknown>[], |
| 300 | rawSpec: unknown, |
| 301 | options?: { truncated?: boolean } |
| 302 | ): ChartPayload { |
| 303 | const spec = normalizeChartSpec(rawSpec) |
| 304 | const dataset = inferChartDataset(rows, spec.fields) |
| 305 | |
| 306 | const requiredFields = new Set<string>() |
| 307 | for (const field of Object.values(spec.encoding)) { |
| 308 | if (typeof field === 'string' && field) requiredFields.add(field) |
| 309 | } |
| 310 | for (const field of requiredFields) assertFieldExists(rows, field) |
| 311 | |
| 312 | let data: ChartRenderData |
| 313 | switch (spec.type) { |
| 314 | case 'bar': |
| 315 | data = aggregateByLabel(rows, spec.encoding.x!, spec.encoding.y!) |
| 316 | break |
| 317 | case 'line': |
| 318 | data = buildLineData(rows, spec) |
| 319 | break |
| 320 | case 'pie': |
| 321 | data = aggregateByLabel(rows, spec.encoding.label!, spec.encoding.value!) |
| 322 | break |
| 323 | case 'heatmap': |
| 324 | data = buildHeatmapData(rows, spec) |
| 325 | break |
| 326 | } |
| 327 | |
| 328 | return { |
| 329 | version: 1, |
| 330 | spec, |
| 331 | dataset, |
| 332 | data, |
| 333 | rowCount: rows.length, |
| 334 | truncated: options?.truncated, |
| 335 | } |
| 336 | } |
no test coverage detected