From 14c97a7e1467dbdd2294cc8c54bb331362ecf439 Mon Sep 17 00:00:00 2001 From: LM Date: Thu, 21 Jul 2011 10:31:00 +0200 Subject: [PATCH] Fixed very bad plot bugs preventing correct plotting, made QGC suspectible to plot crashes --- src/ui/QGCDataPlot2D.cc | 82 ++++++++++++++++++++++------- src/ui/linechart/IncrementalPlot.cc | 4 +- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/ui/QGCDataPlot2D.cc b/src/ui/QGCDataPlot2D.cc index 6a2665b78..659313cae 100644 --- a/src/ui/QGCDataPlot2D.cc +++ b/src/ui/QGCDataPlot2D.cc @@ -375,10 +375,25 @@ void QGCDataPlot2D::loadCsvLog(QString file, QString xAxisName, QString yAxisFil // Clear plot plot->removeData(); - QVector xValues; + QMap* > xValues; QMap* > yValues; curveNames.append(header.split(separator, QString::SkipEmptyParts)); + + // Eliminate any non-string curve names + for (int i = 0; i < curveNames.count(); ++i) + { + if (curveNames.at(i).length() == 0 || + curveNames.at(i) == " " || + curveNames.at(i) == "\n" || + curveNames.at(i) == "\t" || + curveNames.at(i) == "\r") + { + // Remove bogus curve name + curveNames.removeAt(i); + } + } + QString curveName; // Clear UI elements @@ -408,6 +423,7 @@ void QGCDataPlot2D::loadCsvLog(QString file, QString xAxisName, QString yAxisFil if (curveName != xAxisFilter) { if ((yAxisFilter == "") || yAxisFilter.contains(curveName)) { yValues.insert(curveName, new QVector()); + xValues.insert(curveName, new QVector()); // Add separator starting with second item if (curveNameIndex > 0 && curveNameIndex < curveNames.count()) { ui->yAxis->setText(ui->yAxis->text()+"|"); @@ -425,29 +441,58 @@ void QGCDataPlot2D::loadCsvLog(QString file, QString xAxisName, QString yAxisFil double x,y; - while (!in.atEnd()) { + while (!in.atEnd()) + { QString line = in.readLine(); - QStringList values = line.split(separator, QString::SkipEmptyParts); + // Keep empty parts here - we still have to act on them + QStringList values = line.split(separator, QString::KeepEmptyParts); + + bool headerfound = false; - foreach(curveName, curveNames) { - bool okx; - if (curveName == xAxisFilter) { + // First get header - ORDER MATTERS HERE! + foreach(curveName, curveNames) + { + if (curveName == xAxisFilter) + { // X AXIS HANDLING // Take this value as x if it is selected - x = values.at(curveNames.indexOf(curveName)).toDouble(&okx); - xValues.append(x);// - 1270125570000ULL); - } else { - // Y AXIS HANDLING + QString text = values.at(curveNames.indexOf(curveName)); + text = text.trimmed(); + if (text.length() > 0 && text != " " && text != "\n" && text != "\r" && text != "\t") + { + bool okx = true; + x = text.toDouble(&okx); + if (okx && !isnan(x) && !isinf(x)) + { + headerfound = true; + } + } + } + } - if(yAxisFilter == "" || yAxisFilter.contains(curveName)) { - // Only append y values where a valid x value is present - if (yValues.value(curveName)->count() == xValues.count() - 1) { - bool oky; - int curveNameIndex = curveNames.indexOf(curveName); - if (values.count() > curveNameIndex) { - y = values.at(curveNameIndex).toDouble(&oky); + if (headerfound) + { + // Search again from start for values - ORDER MATTERS HERE! + foreach(curveName, curveNames) + { + // Y AXIS HANDLING + if(curveName != xAxisFilter && (yAxisFilter == "" || yAxisFilter.contains(curveName))) + { + bool oky; + int curveNameIndex = curveNames.indexOf(curveName); + if (values.count() > curveNameIndex) + { + QString text(values.at(curveNameIndex)); + text = text.trimmed(); + y = text.toDouble(&oky); + // Only INF is really an issue for the plot + // NaN is fine + if (oky && !isnan(y) && !isinf(y) && text.length() > 0 && text != " " && text != "\n" && text != "\r" && text != "\t") + { + // Only append definitely valid values + xValues.value(curveName)->append(x); yValues.value(curveName)->append(y); } } @@ -459,8 +504,9 @@ void QGCDataPlot2D::loadCsvLog(QString file, QString xAxisName, QString yAxisFil // Add data array of each curve to the plot at once (fast) // Iterates through all x-y curve combinations for (int i = 0; i < yValues.count(); i++) { - plot->appendData(yValues.keys().at(i), xValues.data(), yValues.values().at(i)->data(), xValues.count()); + plot->appendData(yValues.keys().at(i), xValues.values().at(i)->data(), yValues.values().at(i)->data(), xValues.values().at(i)->count()); } + plot->updateScale(); plot->setStyleText(ui->style->currentText()); } diff --git a/src/ui/linechart/IncrementalPlot.cc b/src/ui/linechart/IncrementalPlot.cc index e1f504074..862308f8c 100644 --- a/src/ui/linechart/IncrementalPlot.cc +++ b/src/ui/linechart/IncrementalPlot.cc @@ -259,9 +259,9 @@ void IncrementalPlot::resetScaling() void IncrementalPlot::updateScale() { const double margin = 0.05; - double xMinRange = xmin+(xmin*margin); + double xMinRange = xmin-(xmin*margin); double xMaxRange = xmax+(xmax*margin); - double yMinRange = ymin+(ymin*margin); + double yMinRange = ymin-(ymin*margin); double yMaxRange = ymax+(ymax*margin); if (symmetric) { double xRange = xMaxRange - xMinRange; -- 2.22.0