Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • 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 21.2k 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 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
                            • R Offline
                              R Offline
                              rockon209
                              wrote on last edited by
                              #21

                              @jsulm
                              do you know how maxSize value is calculated because i my code maxsize value is very very very big and in think that is the problem

                              jsulmJ 1 Reply Last reply
                              0
                              • R rockon209

                                @jsulm
                                do you know how maxSize value is calculated because i my code maxsize value is very very very big and in think that is the problem

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

                                @rockon209 In the code in the example it is passed as parameter:

                                qint64 XYSeriesIODevice::writeData(const char * data, qint64 maxSize)
                                

                                In your code you don't assign anything to it so it has arbitrary value.

                                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
                                • Unsolved