QCustomPlot Discussion and Comments

read time from fileReturn to overview

Hello, have two new problems,I want to read from a text file,There are examples here on the forum and basicaly it works fine, But the first value in my txt is a timestamp (e.g.13:20:20). This timestamp is stored as 0 in my QVector.Second problem: it reads a lott of o values which aren't there and third problem : it reads only every second line.

[code]void MainWindow::on_pushButton_clicked()
{
    // prepare data:
     int line_count=0;
     QString line;
     QVector<double> x(100);
       QVector<double> y1(100);
       QVector<double> y2(100);
       QVector<double> y3(100);
       QVector<double> y4(100);
       QVector<double> y5(100);
       QVector<double> y6(100);
       QVector<double> y7(100);
       QVector<double> y8(100);
       QVector<double> y9(100);
       QVector<double> y10(100);
        //open file
        QFile file("E:\\QT-examples\\mems-rosco-plot2\\logs\\Martin1.txt");
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
          QMessageBox::warning(this, "Error", "Failed to open log file (" + m_logger->getLogPath() + ")", QMessageBox::Ok);
        }

        //get data from file and stored as text stream
        QTextStream in(&file);

        while (!in.atEnd())
        {

                line=in.readLine();
                line_count++;

             QString line = in.readLine();// read first line and so on
           
           if (line_count>5)
			{ qDebug() << line;}
           
              QStringList fields = line.split(';');// split the string
         
               x.append(fields.at(0).toDouble()); // take first value and stored into x
               y1.append(fields.at(1).toDouble()); // take second value and stored into y1
               y2.append(fields.at(2).toDouble()); // take third value and stored into y2
               y3.append(fields.at(3).toDouble());
               y4.append(fields.at(4).toDouble());
               y5.append(fields.at(5).toDouble());
               y6.append(fields.at(6).toDouble());
               y7.append(fields.at(7).toDouble());
               y8.append(fields.at(8).toDouble());
               y9.append(fields.at(9).toDouble());
               y10.append(fields.at(10).toDouble());
        }
              qDebug() << x;
              qDebug() << y1;
              qDebug() << y2;
              qDebug() << y3;
              qDebug() << line_count;
   

}
[/code]

and what you get"18:38:22,1411,181,136,91,136,29,13.8,0.74,0,16,1,0,16,34,139,48,452,0,22.5,3.058,16,0,0,16,15,255,146,0,185,255,255,0,0,253,100,0,255,34,255,255,48,132,129,181,255,30,192,36,0,43,192,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2094, 2124, 2114, 2111, 2139, 2145, 2522, 3717, 3405, 3478, 3487, 3468, 3470, 3431, 3420, 3403, 4197, 2743, 1577, 1533, 2419, 1801, 1626, 1594, 1585, 1576, 1542, 1563, 1550, 1562, 1547, 1523, 1549, 1539, 1545, 1512, 1509, 1511, 1522, 1350, 1514, 1404, 1446, 1487, 1508, 1499, 1496, 1510, 1493, 1478, 1478, 1479, 1499, 1480, 1494, 1467, 1495)
QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 181, 181, 181, 181, 181, 181, 181, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 176, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 179, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177)
QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136)
91

help is nuch appreciated.

for your second one, you are clearly reading 2 lines. and only keeping the second one. line 30 in your code and line 33.

for the first issue: it looks like the separator is supposed to be a ',' and not a ';' and dates dont convert to doubles in strings. if you have access to write out the information differently, i would write out the msecsSinceEpoch instead of a string, otherwise you can use QDateTime::fromString() and pass in the format of your string to get a date.

Hi Ian,
i changed sth like you said, but i#m still struggling on the time
in line 7

void MainWindow::on_readlog_click_clicked()
{
    // prepare data:
       int line_count=0;
       QString line;

        QVector<float> x= QDateTime::fromString("hh:mm:ss");
         QVector<double> y1(100);
         QVector<double> y2(100);
         QVector<double> y3(100);
         QVector<double> y4(100);
         QVector<double> y5(100);
         QVector<double> y6(100);
         QVector<double> y7(100);
         QVector<double> y8(100);
         QVector<double> y9(100);
         QVector<double> y10(100);
          //open file
          QFile file("C:\\mems-rosco\\logs\\lastlog.txt");
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
          {
            QMessageBox::warning(this, "Error", "Failed to open log file (" + m_logger->getLogPath() + ")", QMessageBox::Ok);
          }

          //get data from file and stored as text stream
          QTextStream in(&file);

          while (!in.atEnd())
          {


               QString line = in.readLine();// read first line and so on
              line_count++;
            if (line_count>1)
            {if (line_count==2)
                qDebug() << line;
                QStringList fields = line.split(',');// split the string

               x.append(fields.at(0).toFloat());

               // take first value and stored into x
                 y1.append(fields.at(1).toDouble()); // take second value and stored into y1
                 y2.append(fields.at(2).toDouble()); // take third value and stored into y2
                 y3.append(fields.at(3).toDouble());
                 y4.append(fields.at(4).toDouble());
                 y5.append(fields.at(5).toDouble());
                 y6.append(fields.at(6).toDouble());
                 y7.append(fields.at(7).toDouble());
                 y8.append(fields.at(8).toDouble());
                 y9.append(fields.at(9).toDouble());
                 y10.append(fields.at(10).toDouble());
          }}
                qDebug() << x;
                qDebug() << y1;
                qDebug() << y2;
                qDebug() << y3;
                qDebug() << line_count;
}

this code has error: conversion from 'QDateTime'to non-scalar type 'Qvector<float>'requested.
I#ve tried with char , const char and string already.
Here is part of the file I want to read.
#time,engine-rpm,coolant_temp,ambient_temp,intake_air_temp,fuel_temp,map_kpa,battery_voltage,throttle_pot_voltage,idle_switch,uk1,park_neutral_switch,fault_codes,idle_set_point,idle_hot,uk2,iac_position,idle_error,ignition_advance_offset,ignition_advance,coil_time,uk3,uk4,uk5,ignition_switch,throttle_angle,uk6,air_fuel_ratio,fault_code0,lambda_voltage_mv,lambda_sensor_frequency,lambda_sensor_dutycycle,lambda_sensor_status,closed_loop,long_term_fuel_trim,short_term_fuel_trim,carbon_canister_dutycycle,fault_code1,idle_base_pos,uk7,uk8,ignition_advance2,uk9,idle_error2,uk10,fault_code4,uk11,uk12,uk13,uk14,uk15,uk16,uk17,uk18,uk19
18:38:22,1411,181,136,91,136,29,13.8,0.74,0,16,1,0,16,34,139,48,452,0,22.5,3.058,16,0,0,16,15,255,146,0,185,255,255,0,0,253,100,0,255,34,255,255,48,132,129,181,255,30,192,36,0,43,192,58,64,10
18:38:22,1387,181,136,91,136,28,13.8,0.74,0,16,1,0,16,34,139,48,449,0,22,3.056,16,0,0,16,15,255,146,0,186,255,255,0,0,253,100,0,255,34,255,255,48,132,129,200,255,30,192,36,0,43,192,58,64,10

That's not what i meant for the QDateTime

remove line 7 and just make a QVector<double> x;

on line 39, you would replace that with

x.append(QTime::fromString(fields.at(0),"hh:mm:ss").msecsSinceStartOfDay()/1000.0);

i didnt realize it was just a time and not a datetime, this should work. This line will convert your field to a QTime object using fromString(), and then take the time and return the milliseconds since the start of the day (00:00:00) and then divide by 1000.0 to get a double in seconds needed by qcustomplot.

Thank you Ian,
I thought I could get the timestamps easier but that works.

Hi,my problems continue.
I can fill the vectors and see them with the debugger but whatever I do they will not be shown in the plot window.

void MainWindow::setuplog_plot(QCustomPlot *log_plot)


{


 // include this section to fully disable antialiasing for higher performance:

m_ui->log_plot->setNotAntialiasedElements(QCP::aeAll);
 QFont font;
 font.setStyleStrategy(QFont::NoAntialias);
m_ui->log_plot->xAxis->setTickLabelFont(font);
m_ui->log_plot->yAxis->setTickLabelFont(font);
m_ui->log_plot->legend->setVisible(true);
m_ui->log_plot->legend->setFont(font);


m_ui->log_plot->addGraph(); // blue line
m_ui->log_plot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
m_ui->log_plot->graph(0)->setName("curves");
m_ui->log_plot->addGraph(); // red line
m_ui->log_plot->graph(1)->setPen(QPen(QColor(255, 110, 40)));
m_ui->log_plot->graph(1)->setName("nothing");
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
 timeTicker->setTimeFormat("%h:%m:%s");
//m_ui->log_plot->xAxis->setTicker(timeTicker);
m_ui->log_plot->axisRect()->setupFullAxesBox();
m_ui->log_plot->yAxis->setRange(0,6000);
m_ui->log_plot->yAxis->setLabel("curves");
 // make left and bottom axes transfer their ranges to right and top axes:
 connect(m_ui->log_plot->xAxis, SIGNAL(rangeChanged(QCPRange)),m_ui->log_plot->xAxis2, SLOT(setRange(QCPRange)));
 connect(m_ui->log_plot->yAxis, SIGNAL(rangeChanged(QCPRange)),m_ui->log_plot->yAxis2, SLOT(setRange(QCPRange)));

// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
//connect(&dataTimer, SIGNAL(timeout()), this, SLOT( on_file_read_button_clicked()));
//dataTimer.start(0); // Interval 0 means to refresh as fast as possible
}


//_____________________________________________________________
void MainWindow::log_plotSlot()

    {
        // prepare data:
           int line_count=0;
           QString line;

            QVector<double> x;
             QVector<double> y1;
             QVector<double> y2;
             QVector<double> y3;
             QVector<double> y4;
             QVector<double> y5;
             QVector<double> y6;
             QVector<double> y7;
             QVector<double> y8;
             QVector<double> y9;
             QVector<double> y10;
              //open file
              QFile file("C:\\mems-rosco\\logs\\lastlog.txt");
              if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
              {
                QMessageBox::warning(this, "Error", "Failed to open log file (" + m_logger->getLogPath() + ")", QMessageBox::Ok);
              }

              //get data from file and stored as text stream
              QTextStream in(&file);

              while (!in.atEnd())
              {


                   QString line = in.readLine();// read first line and so on
                  line_count++;
                if (line_count>1)
                {if (line_count==2)
                    qDebug() << line;
                    QStringList fields = line.split(',');// split the string

                   x.append(QTime::fromString(fields.at(0),"hh:mm:ss").msecsSinceStartOfDay()/1000.0);

                   // take first value and store into x
                     y1.append(fields.at(1).toDouble()); // take second value and stored into y1
                     y2.append(fields.at(2).toDouble()); // take third value and stored into y2
                     y3.append(fields.at(3).toDouble());
                     y4.append(fields.at(4).toDouble());
                     y5.append(fields.at(5).toDouble());
                     y6.append(fields.at(6).toDouble());
                     y7.append(fields.at(7).toDouble());
                     y8.append(fields.at(8).toDouble());
                     y9.append(fields.at(9).toDouble());
                     y10.append(fields.at(10).toDouble());

              }

                     m_ui->log_plot->graph(0)->setData(x,y1);
                    m_ui->log_plot->show();
    }

}

additionall i have this warning "In file included from E':\QT-examples\mems-rosco-src2\mainwindow.cpp:13:0: E:/QT-examples/build-mems-rosco-src2-Desktop_Qt_5_6_2_MinGW_32bit-Releasemit Debeuginformationen ui.mainwindow.hIn member function 'void Ui_MainWindow::setupUi(QMainWindow*)'.:
E:/QT-examples/build-mems-rosco-src2-Desktop_Qt_5_6_2MinGW_32bit-Release mit Debuginformationen/ui_.mainwindow.h :362:10: void setupUI(QMainWindow *MainWindow)"
when compiling.
What does this warning express, do I have to change sth?

I have a second plotwindow running, could that be the problem. That m_data in qcustomplot.cpp is already occupied by other data? Is it enough to clear graph there first?

most likely the issue is that you arent calling 'replot' in qcustomplot.

No i tried the replot as well.
"m_ui->log_plot->replot();"
Thats why I'm confused!
And what about the warning?

no idea about the warning. i've never seen it before. it also looks like it is missing chunks.

the setData function should destroy any data that was in graph 0 previously. Can you try not using the time ticker and see if it shows up properly? also can you call rescale() before the replot?

are your axis ranges right for the data? Try rescaleAxes(), as Ian suggested.

You have a big chaos of indents and different coding styles in your snippets. You should fix that if you want people to actually go through your code and find your bug.

@Ian
the rescale was the tip ineeded. But now I have other problems, I want to solve them myself first. When i stuck again I will come up here again.Thank you.
@isso
the axis range is right, when you have a look to my vectors, one has values up to 6200.Now i will try to have two different axis with range up to 200 on one side and up to 6500 on the other side.The example Nr 8 shows how to do that.
But thank you too.

Hi,
next problem I have.
The plot shows all graphs now.But I need different tick ranges.In the examples e.g. example 8 you build a graph with value axis on right side and then on left side.
Whatever I do , all values are on left side. How to change this?

        m_ui->logplot->addGraph(m_ui->logplot->xAxis,m_ui->logplot->yAxis2);//0
        m_ui->logplot->graph(0)->setPen(QPen(Qt::black));
        m_ui->logplot->graph(0)->setVisible(true);
        m_ui->logplot->yAxis2->setVisible(true);
        m_ui->logplot->yAxis2->setRange(0,6500);
        m_ui->logplot->yAxis2->atRight;
        m_ui->logplot->yAxis2->setTickLabelFont(font);
        m_ui->logplot->yAxis2->setTickLabels(true);
        m_ui->checkBox_rpm->setChecked(true);
        m_ui->logplot->addGraph(m_ui->logplot->xAxis,m_ui->logplot->yAxis);//1

        m_ui->logplot->yAxis->setRange(0,400);
        m_ui->logplot->graph(1)->setVisible(false);

btw: how can I post the graph ( a picture) here?

seems like it should work to me. though your line 6 doesnt do anything.

as for images, you would hit url and then paste in an image from some image source like imgur.

Hi Ian,
you are right, it works.In line 9 you see that I have checkboxes.I had to repeat all the setting for each graph and checkbox, then it worked.
Thanks.