|
| 1 | +/** Pure helpers — Flutter `ChartData` parity for Quick Charts (#1899). */ |
| 2 | + |
| 3 | +export type QuickChartType = "bar" | "line" | "pie"; |
| 4 | + |
| 5 | +export type ChartAggregation = "none" | "sum" | "count" | "avg" | "min" | "max"; |
| 6 | + |
| 7 | +export type ChartPoint = { |
| 8 | + label: string; |
| 9 | + value: number; |
| 10 | + time?: Date | null; |
| 11 | +}; |
| 12 | + |
| 13 | +export type ChartSeries = { |
| 14 | + points: ChartPoint[]; |
| 15 | + droppedRows: number; |
| 16 | +}; |
| 17 | + |
| 18 | +export const MAX_LINE_POINTS = 50; |
| 19 | +export const DEFAULT_TOP_N = 20; |
| 20 | +export const PIE_TOP_N = 8; |
| 21 | +export const OTHER_LABEL = "Other"; |
| 22 | +export const TOP_N_CHOICES: (number | null)[] = [8, 10, 20, 50, null]; |
| 23 | + |
| 24 | +export function parseNumber(raw: string): number | null { |
| 25 | + const text = raw.trim(); |
| 26 | + if (!text) return null; |
| 27 | + const value = Number(text); |
| 28 | + if (!Number.isFinite(value)) return null; |
| 29 | + return value; |
| 30 | +} |
| 31 | + |
| 32 | +/** ISO-ish date / timestamp; null when it does not fit. */ |
| 33 | +export function parseTime(raw: string): Date | null { |
| 34 | + const s = raw.trim(); |
| 35 | + if (s.length < 10) return null; |
| 36 | + if (!/^\d{4}-\d{2}-\d{2}/.test(s)) return null; |
| 37 | + const d = new Date(s); |
| 38 | + return Number.isNaN(d.getTime()) ? null : d; |
| 39 | +} |
| 40 | + |
| 41 | +export function numericColumns( |
| 42 | + columns: string[], |
| 43 | + rows: string[][], |
| 44 | +): number[] { |
| 45 | + const result: number[] = []; |
| 46 | + for (let c = 0; c < columns.length; c++) { |
| 47 | + let seen = 0; |
| 48 | + let ok = true; |
| 49 | + for (const row of rows) { |
| 50 | + if (c >= row.length) continue; |
| 51 | + const cell = (row[c] ?? "").trim(); |
| 52 | + if (!cell || cell === "NULL") continue; |
| 53 | + if (parseNumber(cell) == null) { |
| 54 | + ok = false; |
| 55 | + break; |
| 56 | + } |
| 57 | + seen++; |
| 58 | + } |
| 59 | + if (ok && seen > 0) result.push(c); |
| 60 | + } |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +export function isTimeColumn(rows: string[][], labelColumn: number): boolean { |
| 65 | + let seen = 0; |
| 66 | + for (const row of rows) { |
| 67 | + if (labelColumn >= row.length) continue; |
| 68 | + const cell = (row[labelColumn] ?? "").trim(); |
| 69 | + if (!cell || cell === "NULL") continue; |
| 70 | + if (!parseTime(cell)) return false; |
| 71 | + seen++; |
| 72 | + } |
| 73 | + return seen > 0; |
| 74 | +} |
| 75 | + |
| 76 | +export function defaultAggregation(opts: { |
| 77 | + rows: string[][]; |
| 78 | + labelColumn: number | null; |
| 79 | + valueColumn: number; |
| 80 | +}): ChartAggregation { |
| 81 | + const { rows, labelColumn, valueColumn } = opts; |
| 82 | + if (labelColumn == null) return "none"; |
| 83 | + const seen = new Set<string>(); |
| 84 | + for (const row of rows) { |
| 85 | + if (labelColumn >= row.length || valueColumn >= row.length) continue; |
| 86 | + if (parseNumber(row[valueColumn] ?? "") == null) continue; |
| 87 | + const label = row[labelColumn] ?? ""; |
| 88 | + if (seen.has(label)) return "sum"; |
| 89 | + seen.add(label); |
| 90 | + } |
| 91 | + return "none"; |
| 92 | +} |
| 93 | + |
| 94 | +function parsePoints( |
| 95 | + rows: string[][], |
| 96 | + labelColumn: number | null, |
| 97 | + valueColumn: number, |
| 98 | +): ChartPoint[] { |
| 99 | + const points: ChartPoint[] = []; |
| 100 | + for (let i = 0; i < rows.length; i++) { |
| 101 | + const row = rows[i] ?? []; |
| 102 | + if (valueColumn >= row.length) continue; |
| 103 | + const v = parseNumber(row[valueColumn] ?? ""); |
| 104 | + if (v == null) continue; |
| 105 | + const label = |
| 106 | + labelColumn != null && labelColumn < row.length |
| 107 | + ? (row[labelColumn] ?? "") |
| 108 | + : String(i + 1); |
| 109 | + points.push({ label, value: v, time: parseTime(label) }); |
| 110 | + } |
| 111 | + return points; |
| 112 | +} |
| 113 | + |
| 114 | +function aggregate( |
| 115 | + points: ChartPoint[], |
| 116 | + aggregation: ChartAggregation, |
| 117 | +): ChartPoint[] { |
| 118 | + const groups = new Map< |
| 119 | + string, |
| 120 | + { sum: number; count: number; min: number; max: number; time?: Date | null } |
| 121 | + >(); |
| 122 | + for (const p of points) { |
| 123 | + let g = groups.get(p.label); |
| 124 | + if (!g) { |
| 125 | + g = { |
| 126 | + sum: 0, |
| 127 | + count: 0, |
| 128 | + min: Number.POSITIVE_INFINITY, |
| 129 | + max: Number.NEGATIVE_INFINITY, |
| 130 | + time: p.time, |
| 131 | + }; |
| 132 | + groups.set(p.label, g); |
| 133 | + } |
| 134 | + g.sum += p.value; |
| 135 | + g.count += 1; |
| 136 | + g.min = Math.min(g.min, p.value); |
| 137 | + g.max = Math.max(g.max, p.value); |
| 138 | + } |
| 139 | + const out: ChartPoint[] = []; |
| 140 | + for (const [label, g] of groups) { |
| 141 | + let value = g.sum; |
| 142 | + switch (aggregation) { |
| 143 | + case "count": |
| 144 | + value = g.count; |
| 145 | + break; |
| 146 | + case "avg": |
| 147 | + value = g.count ? g.sum / g.count : 0; |
| 148 | + break; |
| 149 | + case "min": |
| 150 | + value = g.min; |
| 151 | + break; |
| 152 | + case "max": |
| 153 | + value = g.max; |
| 154 | + break; |
| 155 | + case "sum": |
| 156 | + case "none": |
| 157 | + value = g.sum; |
| 158 | + break; |
| 159 | + } |
| 160 | + out.push({ label, value, time: g.time }); |
| 161 | + } |
| 162 | + return out; |
| 163 | +} |
| 164 | + |
| 165 | +export function buildChartSeries(opts: { |
| 166 | + rows: string[][]; |
| 167 | + labelColumn: number | null; |
| 168 | + valueColumn: number; |
| 169 | + type: QuickChartType; |
| 170 | + aggregation?: ChartAggregation; |
| 171 | + topN?: number | null; |
| 172 | +}): ChartSeries { |
| 173 | + const { |
| 174 | + rows, |
| 175 | + labelColumn, |
| 176 | + valueColumn, |
| 177 | + type, |
| 178 | + aggregation = "none", |
| 179 | + topN, |
| 180 | + } = opts; |
| 181 | + let points = parsePoints(rows, labelColumn, valueColumn); |
| 182 | + if (aggregation !== "none") { |
| 183 | + points = aggregate(points, aggregation); |
| 184 | + } |
| 185 | + |
| 186 | + if (type === "line") { |
| 187 | + if ( |
| 188 | + labelColumn != null && |
| 189 | + isTimeColumn(rows, labelColumn) && |
| 190 | + points.every((p) => p.time) |
| 191 | + ) { |
| 192 | + points = [...points].sort( |
| 193 | + (a, b) => (a.time?.getTime() ?? 0) - (b.time?.getTime() ?? 0), |
| 194 | + ); |
| 195 | + } |
| 196 | + if (points.length <= MAX_LINE_POINTS) { |
| 197 | + return { points, droppedRows: 0 }; |
| 198 | + } |
| 199 | + return { |
| 200 | + points: points.slice(0, MAX_LINE_POINTS), |
| 201 | + droppedRows: points.length - MAX_LINE_POINTS, |
| 202 | + }; |
| 203 | + } |
| 204 | + |
| 205 | + if (type === "pie") { |
| 206 | + points = points.filter((p) => p.value > 0); |
| 207 | + points.sort((a, b) => b.value - a.value); |
| 208 | + } |
| 209 | + |
| 210 | + if (topN == null || points.length <= topN) { |
| 211 | + return { points, droppedRows: 0 }; |
| 212 | + } |
| 213 | + |
| 214 | + const ranked = [...points].sort((a, b) => b.value - a.value); |
| 215 | + const head = ranked.slice(0, topN); |
| 216 | + const rest = ranked.slice(topN).reduce((s, p) => s + p.value, 0); |
| 217 | + return { |
| 218 | + points: [...head, { label: OTHER_LABEL, value: rest }], |
| 219 | + droppedRows: 0, |
| 220 | + }; |
| 221 | +} |
| 222 | + |
| 223 | +export function defaultLabelColumn( |
| 224 | + columns: string[], |
| 225 | + valueCol: number, |
| 226 | + numeric: number[], |
| 227 | +): number | null { |
| 228 | + for (let i = 0; i < columns.length; i++) { |
| 229 | + if (i !== valueCol && !numeric.includes(i)) return i; |
| 230 | + } |
| 231 | + for (let i = 0; i < columns.length; i++) { |
| 232 | + if (i !== valueCol) return i; |
| 233 | + } |
| 234 | + return null; |
| 235 | +} |
| 236 | + |
| 237 | +export function rowsToChartStrings(rows: unknown[][]): string[][] { |
| 238 | + return rows.map((row) => |
| 239 | + row.map((cell) => { |
| 240 | + if (cell === null || cell === undefined) return ""; |
| 241 | + if (typeof cell === "string") return cell === "NULL" ? "" : cell; |
| 242 | + if (typeof cell === "number" || typeof cell === "boolean") { |
| 243 | + return String(cell); |
| 244 | + } |
| 245 | + return JSON.stringify(cell); |
| 246 | + }), |
| 247 | + ); |
| 248 | +} |
0 commit comments