Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Plot live data on QtChart
Qt 6.11 is out! See what's new in the release blog

Plot live data on QtChart

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 3 Posters 20.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rockon209
    wrote on last edited by rockon209
    #1

    Hello Everyone,

    I want to plot live data which i am reading from a sensor and want to plot it on QtChart. I can read the data but dont know how to plot or pass the values to QtChart. Please help, thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you have something like the QtChart QMLOscilloscope in mind ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rockon209
        wrote on last edited by rockon209
        #3

        @SGaist

        Yes i want to use only Qtchart, I havent used QML till now so dont know how it works. How can i plot it in Qtchart?
        I want somthing like this just instead of microphone as input i want my sensor as the input see the link below
        http://doc.qt.io/qt-5/qtcharts-audio-example.html

        jsulmJ 1 Reply Last reply
        0
        • R rockon209

          @SGaist

          Yes i want to use only Qtchart, I havent used QML till now so dont know how it works. How can i plot it in Qtchart?
          I want somthing like this just instead of microphone as input i want my sensor as the input see the link below
          http://doc.qt.io/qt-5/qtcharts-audio-example.html

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rockon209 Then use your sensor as input.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rockon209
            wrote on last edited by rockon209
            #5

            @jsulm
            Ya that obvious but how i can do that i was not able to pass the values form sensor to QtChart i can read the data from sensor and i am getting it in a varaible now how to pass it to Qtchart?????
            For e.g.
            read the data and store it in a double variable
            double tempSensor= readSignal Sensor(tempChannel);

            jsulmJ 1 Reply Last reply
            0
            • R rockon209

              @jsulm
              Ya that obvious but how i can do that i was not able to pass the values form sensor to QtChart i can read the data from sensor and i am getting it in a varaible now how to pass it to Qtchart?????
              For e.g.
              read the data and store it in a double variable
              double tempSensor= readSignal Sensor(tempChannel);

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @rockon209 like in the example:

              qint64 XYSeriesIODevice::writeData(const char * data, qint64 maxSize)
              {
                  qint64 range = 2000;
                  QVector<QPointF> oldPoints = m_series->pointsVector();
                  QVector<QPointF> points;
                  int resolution = 4;
              
                  if (oldPoints.count() < range) {
                      points = m_series->pointsVector();
                  } else {
                      for (int i = maxSize/resolution; i < oldPoints.count(); i++)
                          points.append(QPointF(i - maxSize/resolution, oldPoints.at(i).y()));
                  }
              
                  qint64 size = points.count();
                  for (int k = 0; k < maxSize/resolution; k++)
                      points.append(QPointF(k + size, ((quint8)data[resolution * k] - 128)/128.0));
              
                  m_series->replace(points);
                  return maxSize;
              }
              

              in your case instead of char* data you have a double.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rockon209
                wrote on last edited by rockon209
                #7

                @jsulm
                ya but i was not able to understand the code, what i am doing in my code is i am reading every on second from the sensor and i store it to the variable and then immediately i want to pass it to Qtchart and again read the sensor after one second how i can so it with the above code i new to this data acquisition

                jsulmJ 1 Reply Last reply
                0
                • R rockon209

                  @jsulm
                  ya but i was not able to understand the code, what i am doing in my code is i am reading every on second from the sensor and i store it to the variable and then immediately i want to pass it to Qtchart and again read the sensor after one second how i can so it with the above code i new to this data acquisition

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @rockon209 Change the code from the example, so it matches your requirements:

                  qint64 XYSeriesIODevice::writeData(const double data, qint64 maxSize)
                  {
                  // Adjust here as well as needed
                  }
                  

                  Then call writeData every time you get new value from sensor.
                  To read sensor every second use QTimer.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rockon209
                    wrote on last edited by rockon209
                    #9

                    @jsulm
                    thanks for the reply but i have a question in the above code how the microphone data is pass to this writeData function if const char* data varaible has the info of the microphone data how is it pass to const char* data varaible??? I am not able to understand

                    jsulmJ 1 Reply Last reply
                    0
                    • R rockon209

                      @jsulm
                      thanks for the reply but i have a question in the above code how the microphone data is pass to this writeData function if const char* data varaible has the info of the microphone data how is it pass to const char* data varaible??? I am not able to understand

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @rockon209 In the example they implement a device:

                      m_device = new XYSeriesIODevice(m_series, this);
                      m_device->open(QIODevice::WriteOnly);
                      
                      m_audioInput->start(m_device);
                      

                      XYSeriesIODevice is a QIODevice which is writing incoming char* data to the chart. The data is coming from m_audioInput.
                      In your case you do not need to implement a device: just use a QTimer to read your sensor each second and write the data to the chart in a similar way as in the example.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • R Offline
                        R Offline
                        rockon209
                        wrote on last edited by rockon209
                        #11

                        @jsulm
                        ok thanks for the explanation
                        but it is confusing because in XYSeriesIODevice class have m_series as XYseries and widget class has m_series as Qlineseries
                        and tried your above suggestion i am getting the following error
                        error: invalid types 'double[int]' for array subscript
                        points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0));

                        jsulmJ 1 Reply Last reply
                        0
                        • R rockon209

                          @jsulm
                          ok thanks for the explanation
                          but it is confusing because in XYSeriesIODevice class have m_series as XYseries and widget class has m_series as Qlineseries
                          and tried your above suggestion i am getting the following error
                          error: invalid types 'double[int]' for array subscript
                          points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0));

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @rockon209 said in Plot live data on QtChart:

                          QPointF

                          Can you show the whole method?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            rockon209
                            wrote on last edited by rockon209
                            #13

                            @jsulm
                            with timer i call the following function every second
                            void test:: readTemp()
                            {
                            double tempSensor= readSignal Sensor(tempChannel);
                            writeData();
                            }
                            void test::writeData()
                            {
                            qint64 range = 2000;
                            QVector<QPointF> oldPoints = m_series->pointsVector();
                            QVector<QPointF> points;
                            int resolution = 4;
                            qint64 maxSize;

                            if (oldPoints.count() < range) {
                                points = m_series->pointsVector();
                            } else {
                                for (int i = maxSize/resolution; i < oldPoints.count(); i++)
                                    points.append(QPointF(i - maxSize/resolution, oldPoints.at(i).y()));
                            }
                            
                            qint64 size = points.count();
                            
                            for (int k = 0; k < maxSize/resolution; k++)
                                points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0));
                            
                            m_series->replace(points);
                            

                            //return maxSize;
                            }

                            jsulmJ 1 Reply Last reply
                            0
                            • R rockon209

                              @jsulm
                              with timer i call the following function every second
                              void test:: readTemp()
                              {
                              double tempSensor= readSignal Sensor(tempChannel);
                              writeData();
                              }
                              void test::writeData()
                              {
                              qint64 range = 2000;
                              QVector<QPointF> oldPoints = m_series->pointsVector();
                              QVector<QPointF> points;
                              int resolution = 4;
                              qint64 maxSize;

                              if (oldPoints.count() < range) {
                                  points = m_series->pointsVector();
                              } else {
                                  for (int i = maxSize/resolution; i < oldPoints.count(); i++)
                                      points.append(QPointF(i - maxSize/resolution, oldPoints.at(i).y()));
                              }
                              
                              qint64 size = points.count();
                              
                              for (int k = 0; k < maxSize/resolution; k++)
                                  points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0));
                              
                              m_series->replace(points);
                              

                              //return maxSize;
                              }

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @rockon209 said in Plot live data on QtChart:

                              double tempSensor= readSignal Sensor(tempChannel);

                              this is not going to work - you store the temperature in a local variable!

                              Also, this is wrong if tempSensor isn't an array:

                              points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0));
                              

                              As I said you need to adjust the code for your needs...

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                rockon209
                                wrote on last edited by
                                #15

                                @jsulm
                                then how to make array as i am reading from the sensor every second and getting only one value.

                                jsulmJ 1 Reply Last reply
                                0
                                • R rockon209

                                  @jsulm
                                  then how to make array as i am reading from the sensor every second and getting only one value.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by jsulm
                                  #16

                                  @rockon209 You don't need any array:

                                  void test:: readTemp()
                                  {
                                  writeData( readSignal Sensor(tempChannel));
                                  }
                                  
                                  void test::writeData(const double tempSensor)
                                  {
                                  qint64 range = 2000;
                                  QVector<QPointF> oldPoints = m_series->pointsVector();
                                  QVector<QPointF> points;
                                  int resolution = 4;
                                  qint64 maxSize;
                                  
                                  if (oldPoints.count() < range) {
                                      points = m_series->pointsVector();
                                  } else {
                                      for (int i = maxSize/resolution; i < oldPoints.count(); i++)
                                          points.append(QPointF(i - maxSize/resolution, oldPoints.at(i).y()));
                                  }
                                  
                                  qint64 size = points.count();
                                  points.append(QPointF(k + size, ((quint8)tempSensor - 128)/128.0));
                                  m_series->replace(points);
                                  }

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    rockon209
                                    wrote on last edited by
                                    #17

                                    @jsulm
                                    i am getting following error and the application is shutting down
                                    terminate called after throwing an instance of 'std::bad_alloc'
                                    what(): std::bad_alloc

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • R rockon209

                                      @jsulm
                                      i am getting following error and the application is shutting down
                                      terminate called after throwing an instance of 'std::bad_alloc'
                                      what(): std::bad_alloc

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @rockon209 Please debug you app to see where it crashes...

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        rockon209
                                        wrote on last edited by
                                        #19

                                        @jsulm
                                        when i read the sensor data it crashes

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • R rockon209

                                          @jsulm
                                          when i read the sensor data it crashes

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @rockon209 Then find out why and fix it...

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved