/** * Shared CSV export modifiers for chart widgets */ /** * Flattens object array data into CSV-ready rows. * Used by bar, pie, histogram, and timeseries widgets. * * @param data - Array of series, where each series is an array of data objects * @returns CSV rows with headers and values */ export function flattenObjectArrayToCSV>( data: T[][], ): string[][] { const rows: string[][] = [] // Add headers from first data point if available if (data.length > 0 && (data[0]?.length ?? 0) > 0) { const firstDataPoint = data?.[0]?.[0] ?? {} const headers = Object.keys(firstDataPoint) rows.push(headers) } // Add data rows from all series data.forEach((series) => { series.forEach((dataPoint) => { const values = Object.values(dataPoint).map((v) => String(v)) rows.push(values) }) }) return rows } /** * Creates CSV rows for scatterplot data. * Scatterplot uses array format [x, y] instead of objects. * * @param data - Array of series, where each series is an array of [x, y] tuples * @returns CSV rows with ['x', 'y'] headers */ export function scatterplotDataToCSV(data: number[][][]): string[][] { const rows: string[][] = [] // Add headers rows.push(['x', 'y']) // Add data rows from all series data.forEach((series) => { series.forEach((dataPoint) => { rows.push([String(dataPoint[0]), String(dataPoint[1])]) }) }) return rows }