|
1 | 1 | export function ImportedCurves(curvesChanged) {
|
2 |
| - const maxImportCount = 5; |
3 |
| - this._curvesData = []; |
| 2 | + const MAX_IMPORT_COUNT = 6; // This value is limited by legends size and curves colors visibility. May be increased if needed by users |
| 3 | + const _curvesData = []; |
4 | 4 | const _that = this;
|
5 | 5 | this.minX = Number.MAX_VALUE;
|
6 | 6 | this.maxX = -Number.MAX_VALUE;
|
7 | 7 | this.minY = Number.MAX_VALUE;
|
8 | 8 | this.maxY = -Number.MAX_VALUE;
|
9 |
| - |
| 9 | + |
10 | 10 | this.curvesCount = function() {
|
11 |
| - return this._curvesData.length; |
| 11 | + return _curvesData.length; |
| 12 | + }; |
| 13 | + |
| 14 | + this.getCurve = function(index) { |
| 15 | + if (index < _curvesData.length) { |
| 16 | + return _curvesData[index]; |
| 17 | + } else { |
| 18 | + throw new RangeError(`The imported curves index (${index}) exceeds the maximum allowed value (${_curvesData.length - 1})`); |
| 19 | + } |
12 | 20 | };
|
13 | 21 |
|
14 | 22 | this.importCurvesFromCSV = function(files) {
|
15 |
| - let importsLeft = maxImportCount - this._curvesData.length; |
| 23 | + let importsLeft = MAX_IMPORT_COUNT - _curvesData.length; |
16 | 24 |
|
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; |
| 25 | + for (const file of files) { |
| 26 | + if (importsLeft-- == 0) { |
| 27 | + break; |
| 28 | + } |
| 29 | + const reader = new FileReader(); |
| 30 | + reader.onload = function (e) { |
| 31 | + try { |
| 32 | + const stringRows = e.target.result.split("\n"); |
| 33 | + |
| 34 | + const header = stringRows[0].split(","); |
| 35 | + if (header.length !== 2 || header[0].trim() !== "x" || header[1].trim() !== "y") { |
| 36 | + throw new SyntaxError("Wrong curves CSV data format"); |
60 | 37 | }
|
61 |
| - }; |
62 | 38 |
|
63 |
| - reader.readAsText(file); |
64 |
| - } |
| 39 | + stringRows.shift(); |
| 40 | + //remove bad last row |
| 41 | + if (stringRows.at(-1) == "") { |
| 42 | + stringRows.pop(); |
| 43 | + } |
| 44 | + |
| 45 | + const curvesData = stringRows.map( function(row) { |
| 46 | + const data = row.split(","), |
| 47 | + x = parseFloat(data[0].trim()), |
| 48 | + y = parseFloat(data[1].trim()); |
| 49 | + _that.minX = Math.min(x, _that.minX); |
| 50 | + _that.maxX = Math.max(x, _that.maxX); |
| 51 | + _that.minY = Math.min(y, _that.minY); |
| 52 | + _that.maxY = Math.max(y, _that.maxY); |
| 53 | + return { |
| 54 | + x: x, |
| 55 | + y: y, |
| 56 | + }; |
| 57 | + }); |
| 58 | + |
| 59 | + const curve = { |
| 60 | + name: file.name.split('.')[0], |
| 61 | + points: curvesData, |
| 62 | + }; |
| 63 | + _curvesData.push(curve); |
| 64 | + curvesChanged(); |
| 65 | + } catch (e) { |
| 66 | + alert('Curves data import error: ' + e.message); |
| 67 | + return; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + reader.readAsText(file); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const getCurveRange = function(points) { |
| 76 | + let minX = Number.MAX_VALUE, |
| 77 | + maxX = -Number.MAX_VALUE, |
| 78 | + minY = Number.MAX_VALUE, |
| 79 | + maxY = -Number.MAX_VALUE; |
| 80 | + for (const point of points) { |
| 81 | + minX = Math.min(point.x, minX); |
| 82 | + maxX = Math.max(point.x, maxX); |
| 83 | + minY = Math.min(point.y, minY); |
| 84 | + maxY = Math.max(point.y, maxY); |
| 85 | + } |
| 86 | + return { |
| 87 | + minX: minX, |
| 88 | + maxX: maxX, |
| 89 | + minY: minY, |
| 90 | + maxY: maxY, |
65 | 91 | };
|
| 92 | + }; |
66 | 93 |
|
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; |
| 94 | + const computeGlobalCurvesRange = function () { |
| 95 | + _that.minX = Number.MAX_VALUE; |
| 96 | + _that.maxX = -Number.MAX_VALUE; |
| 97 | + _that.minY = Number.MAX_VALUE; |
| 98 | + _that.maxY = -Number.MAX_VALUE; |
| 99 | + for (const curve of _curvesData) { |
| 100 | + _that.minX = Math.min(curve.range.minX, _that.minX); |
| 101 | + _that.maxX = Math.max(curve.range.maxX, _that.maxX); |
| 102 | + _that.minY = Math.min(curve.range.minY, _that.minY); |
| 103 | + _that.maxY = Math.max(curve.range.maxY, _that.maxY); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + this.addCurve = function(points, name) { |
| 108 | + if (this.curvesCount() < MAX_IMPORT_COUNT) { |
| 109 | + const range = getCurveRange(points); |
| 110 | + _curvesData.push({ |
| 111 | + name: name, |
| 112 | + points: points, |
| 113 | + range: range, |
| 114 | + }); |
| 115 | + |
| 116 | + this.minX = Math.min(range.minX, this.minX); |
| 117 | + this.maxX = Math.max(range.maxX, this.maxX); |
| 118 | + this.minY = Math.min(range.minY, this.minY); |
| 119 | + this.maxY = Math.max(range.maxY, this.maxY); |
| 120 | + |
| 121 | + curvesChanged(); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + this.isNewCurve = function(name) { |
| 126 | + for (const curve of _curvesData) { |
| 127 | + if (curve.name === name) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + return true; |
| 132 | + }; |
| 133 | + |
| 134 | + this.removeAllCurves = function() { |
| 135 | + _curvesData.length = 0; |
| 136 | + computeGlobalCurvesRange(); |
73 | 137 | curvesChanged();
|
74 | 138 | };
|
| 139 | + |
| 140 | + this.removeCurve = function(name) { |
| 141 | + for (let index = 0; index < _curvesData.length; index++) { |
| 142 | + if (_curvesData[index].name === name) { |
| 143 | + _curvesData.splice(index, 1); |
| 144 | + computeGlobalCurvesRange(); |
| 145 | + curvesChanged(); |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + this.isFull = function() { |
| 152 | + return this.curvesCount() === MAX_IMPORT_COUNT; |
| 153 | + }; |
| 154 | + |
| 155 | + this.isEmpty = function() { |
| 156 | + return this.curvesCount() === 0; |
| 157 | + }; |
75 | 158 | }
|
0 commit comments