Skip to content

Commit dd92fba

Browse files
committed
Added importSpectrumFromCSV method to FlightLogAnalyser
1 parent 0228278 commit dd92fba

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/graph_spectrum.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,35 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
289289
this.exportSpectrumToCSV = function(onSuccess, options) {
290290
SpectrumExporter(fftData, options).dump(onSuccess);
291291
};
292+
this.importSpectrumFromCSV = function(files) {
293+
const reader = new FileReader();
294+
reader.onload = function (e) {
295+
try {
296+
const stringRows = e.target.result.split("\n");
297+
298+
const header = stringRows[0].split(",");
299+
if (header.length != 2 || header[0] != "freq" || header[1] != "value") {
300+
throw new SyntaxError("Wrong spectrum CSV data format");
301+
}
302+
303+
stringRows.shift();
304+
const spectrumData = stringRows.map( function(row) {
305+
const data = row.split(",");
306+
return {
307+
freq: parseFloat(data[0]),
308+
value: parseFloat(data[1]),
309+
};
310+
});
311+
GraphSpectrumPlot.setImportedSpectrumData(spectrumData);
312+
} catch (e) {
313+
alert('Spectrum data import error: ' + e.message);
314+
return;
315+
}
316+
}
317+
reader.readAsText(files[0]);
318+
}
319+
292320
} catch (e) {
293-
console.log(`Failed to create analyser... error:${e}`);
321+
console.log(`Failed to create analyser... error: ${e}`);
294322
}
295323
}

0 commit comments

Comments
 (0)