Skip to content

Commit 6db3827

Browse files
committed
Added graph_imported_curves module for import curves
1 parent 95cbf3a commit 6db3827

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/graph_imported_curves.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export function ImportedCurves(curvesChanged) {
2+
const maxImportCount = 5;
3+
this._curvesData = [];
4+
let _that = this;
5+
this.minX = Number.MAX_VALUE;
6+
this.maxX = -Number.MAX_VALUE;
7+
this.minY = Number.MAX_VALUE;
8+
this.maxY = -Number.MAX_VALUE;
9+
10+
this.curvesCount = function() {
11+
return this._curvesData.length;
12+
}
13+
14+
this.importCurvesFromCSV = function(files) {
15+
let importsLeft = maxImportCount - this._curvesData.length;
16+
17+
for (const file of files) {
18+
if (importsLeft-- == 0) {
19+
break;
20+
}
21+
const reader = new FileReader();
22+
reader.onload = function (e) {
23+
try {
24+
const stringRows = e.target.result.split("\n");
25+
26+
const header = stringRows[0].split(",");
27+
if (header.length != 2 || header[0] != "x" || header[1] != "y") {
28+
throw new SyntaxError("Wrong curves CSV data format");
29+
}
30+
31+
stringRows.shift();
32+
//remove bad last row
33+
if (stringRows.at(-1) == "") {
34+
stringRows.pop();
35+
}
36+
37+
const curvesData = stringRows.map( function(row) {
38+
const data = row.split(","),
39+
x = parseFloat(data[0]),
40+
y = parseFloat(data[1]);
41+
_that.minX = Math.min(x, _that.minX);
42+
_that.maxX = Math.max(x, _that.maxX);
43+
_that.minY = Math.min(y, _that.minY);
44+
_that.maxY = Math.max(y, _that.maxY);
45+
return {
46+
x: x,
47+
y: y,
48+
};
49+
});
50+
51+
const curve = {
52+
name: file.name.split('.')[0],
53+
points: curvesData,
54+
};
55+
_that._curvesData.push(curve);
56+
curvesChanged();
57+
} catch (e) {
58+
alert('Curves data import error: ' + e.message);
59+
return;
60+
}
61+
};
62+
63+
reader.readAsText(file);
64+
}
65+
};
66+
67+
this.removeCurves = function() {
68+
this._curvesData.length = 0;
69+
this.minX = Number.MAX_VALUE;
70+
this.maxX = -Number.MAX_VALUE;
71+
this.minY = Number.MAX_VALUE;
72+
this.maxY = -Number.MAX_VALUE;
73+
curvesChanged();
74+
}
75+
}

0 commit comments

Comments
 (0)