feat(frontend): add computeNumericSeries helper
This commit is contained in:
@@ -230,3 +230,36 @@ export function extractNumber(str) {
|
||||
const m = String(str).match(/-?\d+(\.\d+)?/);
|
||||
return m ? parseFloat(m[0]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a numeric-vs-x series for a numerical bucket.
|
||||
*
|
||||
* @param {object[]} rows - Parsed CSV rows
|
||||
* @param {string} xColName - Column to plot on the x-axis (frame col, or timestamp as fallback)
|
||||
* @param {string} noteCol - Column containing note values
|
||||
* @param {object} bucket - { name, type:'numerical', notes: string[] }
|
||||
* @returns {{points: {x:number,value:number,raw:string}[], avg:number|null, skipped:number, total:number, xLabel:string}}
|
||||
*/
|
||||
export function computeNumericSeries(rows, xColName, noteCol, bucket) {
|
||||
const noteSet = new Set(bucket.notes);
|
||||
const points = [];
|
||||
let skipped = 0;
|
||||
let total = 0;
|
||||
|
||||
for (const r of rows) {
|
||||
const note = String(r[noteCol] ?? '').trim();
|
||||
if (!noteSet.has(note)) continue;
|
||||
total++;
|
||||
const value = extractNumber(note);
|
||||
const x = parseFloat(r[xColName]);
|
||||
if (value == null || Number.isNaN(x)) { skipped++; continue; }
|
||||
points.push({ x, value, raw: note });
|
||||
}
|
||||
|
||||
points.sort((a, b) => a.x - b.x);
|
||||
const avg = points.length === 0
|
||||
? null
|
||||
: points.reduce((s, p) => s + p.value, 0) / points.length;
|
||||
|
||||
return { points, avg, skipped, total, xLabel: xColName };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user