Skip to content

Commit ac8355d

Browse files
authored
Auto fill of curves table by selecting group curves names on Chart setup dialog box (betaflight#747)
* added group fields name extender function in graph_config.js * The group curves selection is self extended in charts setup dialog box * Code style improvement * added curves auto color after extend of curves group * added group fields auto fill after charts samples selection * code style improvement * the code is simplified * Update graph_config.js Resolve code style issue * Update graph_config.js Resolved semicolon issue * Update graph_config.js Removed unusing variable * Update graph_config.js Resolved code style issue * Update graph_config_dialog.js Resolved semicoma missing * Update main.js Code style improvement * Code style improvement: the for loops are changed to for of * Code style improvement * Code style improvement * resolved issue of wrong curves color draw on Chart setup dialog box * Improve curves collor assign * Resolved code issue
1 parent 60711ef commit ac8355d

File tree

3 files changed

+162
-189
lines changed

3 files changed

+162
-189
lines changed

src/graph_config.js

Lines changed: 81 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { DSHOT_MIN_VALUE, DSHOT_RANGE, RATES_TYPE, DEBUG_MODE } from "./flightlo
33
import { escapeRegExp } from "./tools";
44

55
export function GraphConfig(graphConfig) {
6-
var
7-
graphs = graphConfig ? graphConfig : [],
8-
listeners = [],
9-
that = this;
6+
const listeners = [];
7+
const that = this;
8+
let graphs = graphConfig ?? [];
109

1110
function notifyListeners() {
12-
for (var i = 0; i < listeners.length; i++) {
13-
listeners[i](that);
11+
for (const listener of listeners) {
12+
listener(that);
1413
}
1514
}
1615

@@ -44,20 +43,58 @@ export function GraphConfig(graphConfig) {
4443
}
4544
};
4645

46+
this.extendFields = function(flightLog, field) {
47+
const matches = field.name.match(/^(.+)\[all\]$/);
48+
const logFieldNames = flightLog.getMainFieldNames();
49+
const fields = [];
50+
if (matches) {
51+
const
52+
nameRoot = matches[1],
53+
nameRegex = new RegExp("^" + escapeRegExp(nameRoot) + "\[[0-9]+\]$");
54+
55+
for (const fieldName of logFieldNames) {
56+
if (fieldName.match(nameRegex)) {
57+
// forceNewCurve must be true for min max computing extended curves.
58+
const forceNewCurve = true;
59+
field.color = undefined;
60+
fields.push(adaptField(flightLog, $.extend({}, field, {curve: $.extend({}, field.curve), name: fieldName, friendlyName: FlightLogFieldPresenter.fieldNameToFriendly(fieldName, flightLog.getSysConfig().debug_mode)}), forceNewCurve));
61+
}
62+
}
63+
} else {
64+
// Don't add fields if they don't exist in this log
65+
if (flightLog.getMainFieldIndexByName(field.name) !== undefined) {
66+
fields.push(adaptField(flightLog, $.extend({}, field, {curve: $.extend({}, field.curve), friendlyName: FlightLogFieldPresenter.fieldNameToFriendly(field.name, flightLog.getSysConfig().debug_mode)})));
67+
}
68+
}
69+
return fields;
70+
};
71+
72+
let adaptField = function(flightLog, field, forceNewCurve) {
73+
const defaultCurve = GraphConfig.getDefaultCurveForField(flightLog, field.name);
74+
if (field.curve === undefined || forceNewCurve) {
75+
field.curve = defaultCurve;
76+
} else {
77+
if (field.curve.MinMax == undefined)
78+
field.curve.MinMax = defaultCurve.MinMax;
79+
}
80+
81+
if (field.smoothing === undefined) {
82+
field.smoothing = GraphConfig.getDefaultSmoothingForField(flightLog, field.name);
83+
}
84+
85+
return field;
86+
};
87+
4788
/**
4889
* Convert the given graph configs to make them appropriate for the given flight log.
4990
*/
50-
this.adaptGraphs = function(flightLog, graphs, isNewLog) {
51-
var
52-
logFieldNames = flightLog.getMainFieldNames(),
53-
91+
this.adaptGraphs = function(flightLog, graphs) {
92+
const
5493
// Make copies of graphs into here so we can modify them without wrecking caller's copy
5594
newGraphs = [];
5695

57-
for (var i = 0; i < graphs.length; i++) {
58-
var
59-
graph = graphs[i],
60-
newGraph = $.extend(
96+
for (const graph of graphs) {
97+
const newGraph = $.extend(
6198
// Default values for missing properties:
6299
{
63100
height: 1,
@@ -68,66 +105,11 @@ export function GraphConfig(graphConfig) {
68105
{
69106
fields:[],
70107
},
71-
),
72-
colorIndex = 0;
73-
74-
for (var j = 0; j < graph.fields.length; j++) {
75-
var
76-
field = graph.fields[j],
77-
matches;
78-
79-
var adaptField = function(field, colorIndexOffset, forceNewCurve) {
80-
const defaultCurve = GraphConfig.getDefaultCurveForField(flightLog, field.name);
81-
82-
if (field.curve === undefined || forceNewCurve) {
83-
field.curve = defaultCurve;
84-
}
85-
else {
86-
if (field.curve.MinMax == undefined)
87-
field.curve.MinMax = defaultCurve.MinMax;
88-
}
89-
90-
if(colorIndexOffset!=null && field.color != undefined) { // auto offset the actual color (to expand [all] selections)
91-
var index;
92-
for(index=0; index < GraphConfig.PALETTE.length; index++)
93-
{
94-
if(GraphConfig.PALETTE[index].color == field.color) break;
95-
}
96-
field.color = GraphConfig.PALETTE[(index + colorIndexOffset) % GraphConfig.PALETTE.length].color
97-
}
98-
99-
if (field.color === undefined) {
100-
field.color = GraphConfig.PALETTE[colorIndex % GraphConfig.PALETTE.length].color;
101-
colorIndex++;
102-
}
103-
104-
if (field.smoothing === undefined) {
105-
field.smoothing = GraphConfig.getDefaultSmoothingForField(flightLog, field.name);
106-
}
107-
108-
return field;
109-
};
108+
);
110109

111-
if ((matches = field.name.match(/^(.+)\[all\]$/))) {
112-
var
113-
nameRoot = matches[1],
114-
nameRegex = new RegExp("^" + escapeRegExp(nameRoot) + "\[[0-9]+\]$"),
115-
colorIndexOffset = 0;
116-
117-
for (var k = 0; k < logFieldNames.length; k++) {
118-
if (logFieldNames[k].match(nameRegex)) {
119-
// forceNewCurve must be true for min max computing extended curves.
120-
let forceNewCurve = true;
121-
newGraph.fields.push(adaptField($.extend({}, field, {curve: $.extend({}, field.curve), name: logFieldNames[k], friendlyName: FlightLogFieldPresenter.fieldNameToFriendly(logFieldNames[k], flightLog.getSysConfig().debug_mode)}), colorIndexOffset, forceNewCurve));
122-
colorIndexOffset++;
123-
}
124-
}
125-
} else {
126-
// Don't add fields if they don't exist in this log
127-
if (flightLog.getMainFieldIndexByName(field.name) !== undefined) {
128-
newGraph.fields.push(adaptField($.extend({}, field, {curve: $.extend({}, field.curve), friendlyName: FlightLogFieldPresenter.fieldNameToFriendly(field.name, flightLog.getSysConfig().debug_mode)})));
129-
}
130-
}
110+
for (const field of graph.fields) {
111+
const fields = this.extendFields(flightLog, field);
112+
newGraph.fields = newGraph.fields.concat(fields);
131113
}
132114

133115
newGraphs.push(newGraph);
@@ -173,15 +155,10 @@ GraphConfig.PALETTE = [
173155
GraphConfig.load = function(config) {
174156
// Upgrade legacy configs to suit the newer standard by translating field names
175157
if (config) {
176-
for (var i = 0; i < config.length; i++) {
177-
var graph = config[i];
178-
179-
for (var j = 0; j < graph.fields.length; j++) {
180-
var
181-
field = graph.fields[j],
182-
matches;
183-
184-
if ((matches = field.name.match(/^gyroData(.+)$/))) {
158+
for (const graph of config) {
159+
for (const field of graph.fields) {
160+
const matches = field.name.match(/^gyroData(.+)$/);
161+
if (matches) {
185162
field.name = "gyroADC" + matches[1];
186163
}
187164
}
@@ -214,10 +191,10 @@ GraphConfig.load = function(config) {
214191
};
215192

216193
GraphConfig.getDefaultCurveForField = function(flightLog, fieldName) {
217-
var
194+
const
218195
sysConfig = flightLog.getSysConfig();
219196

220-
var maxDegreesSecond = function(scale) {
197+
let maxDegreesSecond = function(scale) {
221198
switch(sysConfig["rates_type"]){
222199
case RATES_TYPE.indexOf('ACTUAL'):
223200
case RATES_TYPE.indexOf('QUICK'):
@@ -231,14 +208,14 @@ GraphConfig.load = function(config) {
231208
}
232209
}
233210

234-
var getMinMaxForFields = function(/* fieldName1, fieldName2, ... */) {
211+
let getMinMaxForFields = function(/* fieldName1, fieldName2, ... */) {
235212
// helper to make a curve scale based on the combined min/max of one or more fields
236213
let
237214
min = Number.MAX_VALUE,
238215
max = -Number.MAX_VALUE;
239216

240-
for(let i in arguments) {
241-
const mm = flightLog.getMinMaxForFieldDuringAllTime(arguments[i]);
217+
for(const argument of arguments) {
218+
const mm = flightLog.getMinMaxForFieldDuringAllTime(argument);
242219
min = Math.min(mm.min, min);
243220
max = Math.max(mm.max, max);
244221
}
@@ -250,7 +227,7 @@ GraphConfig.load = function(config) {
250227
return {min:-500, max:500};
251228
}
252229

253-
var getCurveForMinMaxFields = function(/* fieldName1, fieldName2, ... */) {
230+
let getCurveForMinMaxFields = function(/* fieldName1, fieldName2, ... */) {
254231
const mm = getMinMaxForFields.apply(null, arguments);
255232
// added convertation min max values from log file units to friendly chart
256233
const mmChartUnits =
@@ -264,10 +241,10 @@ GraphConfig.load = function(config) {
264241
};
265242
}
266243

267-
var getCurveForMinMaxFieldsZeroOffset = function(/* fieldName1, fieldName2, ... */) {
244+
let getCurveForMinMaxFieldsZeroOffset = function(/* fieldName1, fieldName2, ... */) {
268245
const mm = getMinMaxForFields.apply(null, arguments);
269246
// added convertation min max values from log file units to friendly chart
270-
let mmChartUnits =
247+
const mmChartUnits =
271248
{
272249
min: FlightLogFieldPresenter.ConvertFieldValue(flightLog, fieldName, true, mm.min),
273250
max: FlightLogFieldPresenter.ConvertFieldValue(flightLog, fieldName, true, mm.max)
@@ -417,7 +394,7 @@ GraphConfig.load = function(config) {
417394
};
418395
} else if (fieldName.match(/^debug.*/) && sysConfig.debug_mode!=null) {
419396

420-
var debugModeName = DEBUG_MODE[sysConfig.debug_mode];
397+
const debugModeName = DEBUG_MODE[sysConfig.debug_mode];
421398
switch (debugModeName) {
422399
case 'CYCLETIME':
423400
switch (fieldName) {
@@ -558,7 +535,7 @@ GraphConfig.load = function(config) {
558535
max: 50
559536
}
560537
};
561-
case 'debug[3]': // Vario
538+
case 'debug[3]': // vario
562539
return {
563540
power: 1.0,
564541
MinMax: {
@@ -1282,7 +1259,7 @@ GraphConfig.load = function(config) {
12821259
const minTime = WindowCenterTime - WindowWidthTime/2;
12831260
const maxTime = WindowCenterTime + WindowWidthTime/2;
12841261

1285-
let mm = flightLog.getMinMaxForFieldDuringTimeInterval(fieldName, minTime, maxTime);
1262+
const mm = flightLog.getMinMaxForFieldDuringTimeInterval(fieldName, minTime, maxTime);
12861263
if (mm == undefined)
12871264
return {
12881265
min: -500,
@@ -1309,7 +1286,7 @@ GraphConfig.load = function(config) {
13091286
if (maxTime == false)
13101287
maxTime = flightLog.getMaxTime();
13111288

1312-
let mm = flightLog.getMinMaxForFieldDuringTimeInterval(fieldName, minTime, maxTime);
1289+
const mm = flightLog.getMinMaxForFieldDuringTimeInterval(fieldName, minTime, maxTime);
13131290
if (mm == undefined)
13141291
return {
13151292
min: -500,
@@ -1326,7 +1303,7 @@ GraphConfig.load = function(config) {
13261303
* @param fieldName Name of the field
13271304
*/
13281305
GraphConfig.getMinMaxForFieldDuringAllTime = function(flightLog, fieldName) {
1329-
let mm = flightLog.getMinMaxForFieldDuringAllTime(fieldName);
1306+
const mm = flightLog.getMinMaxForFieldDuringAllTime(fieldName);
13301307
if (mm.min == Number.MAX_VALUE || mm.max == -Number.MAX_VALUE) {
13311308
return {
13321309
min: -500,
@@ -1345,10 +1322,7 @@ GraphConfig.load = function(config) {
13451322
* Supply an array of strings `graphNames` to only fetch the graph with the given names.
13461323
*/
13471324
GraphConfig.getExampleGraphConfigs = function(flightLog, graphNames) {
1348-
var
1349-
result = [],
1350-
i, j;
1351-
1325+
const result = [];
13521326
const EXAMPLE_GRAPHS = [];
13531327

13541328
if (!flightLog.isFieldDisabled().MOTORS) {
@@ -1396,20 +1370,19 @@ GraphConfig.load = function(config) {
13961370
EXAMPLE_GRAPHS.push({label: "GPS",fields: ["GPS_numSat", "GPS_altitude", "GPS_speed", "GPS_ground_course", "GPS_coord[all]"]});
13971371
}
13981372

1399-
for (i = 0; i < EXAMPLE_GRAPHS.length; i++) {
1400-
var
1401-
srcGraph = EXAMPLE_GRAPHS[i],
1373+
for (const srcGraph of EXAMPLE_GRAPHS) {
1374+
const
14021375
destGraph = {
14031376
label: srcGraph.label,
14041377
fields: [],
14051378
height: srcGraph.height || 1
1406-
},
1407-
found;
1379+
};
1380+
let found;
14081381

14091382
if (graphNames !== undefined) {
14101383
found = false;
1411-
for (j = 0; j < graphNames.length; j++) {
1412-
if (srcGraph.label == graphNames[j]) {
1384+
for (const name of graphNames) {
1385+
if (srcGraph.label == name) {
14131386
found = true;
14141387
break;
14151388
}
@@ -1420,10 +1393,8 @@ GraphConfig.load = function(config) {
14201393
}
14211394
}
14221395

1423-
for (j = 0; j < srcGraph.fields.length; j++) {
1424-
var
1425-
srcFieldName = srcGraph.fields[j],
1426-
destField = {
1396+
for (const srcFieldName of srcGraph.fields) {
1397+
const destField = {
14271398
name: srcFieldName
14281399
};
14291400

0 commit comments

Comments
 (0)