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. QtCustomPlot Realtime Data Demo
Forum Updated to NodeBB v4.3 + New Features

QtCustomPlot Realtime Data Demo

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 447 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.
  • J Offline
    J Offline
    Jayden123
    wrote on last edited by
    #1

    Hi,
    I'm new to this Qt and I am trying to plot a real time data graph with random data via QtCustomPlot. The graph that I generated was unable to show the real time random data. May I know what is the problem of my code?
    Below is the source code:

    void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
    {
    customPlot->addGraph(); // blue line
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
    customPlot->graph(0)->setAntialiasedFill(false);

    customPlot->addGraph(); // blue dot
    customPlot->graph(1)->setPen(QPen(Qt::blue));
    customPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
    customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);

    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    timeTicker->setTimeFormat("%h:%m:%s");
    customPlot->xAxis->setTicker(timeTicker);
    customPlot->axisRect()->setupFullAxesBox();
    customPlot->yAxis->setRange(-40, 40);

    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
    dataTimer.start(0);
    }

    void MainWindow::realtimeDataSlot()
    {

    std::uniform_real_distribution<double>distribution(1,20.5);
        double value1 = distribution(*QRandomGenerator::global());
        qDebug() << value1;
        while(value1 !=0)
        {
            value1=distribution(*QRandomGenerator::global());
            qDebug() << value1;
    
        }
    
    
    static QTime time(QTime::currentTime());
    double key = time.elapsed()/1000.0; 
    static double lastPointKey = 0;
    if (key-lastPointKey > 0.002) // at most add point every 2 ms
    {
      ui->customPlot->graph(0)->addData(key, value1);
      ui->customPlot->graph(1)->addData(key, value1);
      lastPointKey = key;
    }
    ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
    ui->customPlot->replot();
    

    }

    JonBJ 1 Reply Last reply
    0
    • J Jayden123

      Hi,
      I'm new to this Qt and I am trying to plot a real time data graph with random data via QtCustomPlot. The graph that I generated was unable to show the real time random data. May I know what is the problem of my code?
      Below is the source code:

      void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
      {
      customPlot->addGraph(); // blue line
      customPlot->graph(0)->setPen(QPen(Qt::blue));
      customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
      customPlot->graph(0)->setAntialiasedFill(false);

      customPlot->addGraph(); // blue dot
      customPlot->graph(1)->setPen(QPen(Qt::blue));
      customPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
      customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);

      QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
      timeTicker->setTimeFormat("%h:%m:%s");
      customPlot->xAxis->setTicker(timeTicker);
      customPlot->axisRect()->setupFullAxesBox();
      customPlot->yAxis->setRange(-40, 40);

      connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
      connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

      connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
      dataTimer.start(0);
      }

      void MainWindow::realtimeDataSlot()
      {

      std::uniform_real_distribution<double>distribution(1,20.5);
          double value1 = distribution(*QRandomGenerator::global());
          qDebug() << value1;
          while(value1 !=0)
          {
              value1=distribution(*QRandomGenerator::global());
              qDebug() << value1;
      
          }
      
      
      static QTime time(QTime::currentTime());
      double key = time.elapsed()/1000.0; 
      static double lastPointKey = 0;
      if (key-lastPointKey > 0.002) // at most add point every 2 ms
      {
        ui->customPlot->graph(0)->addData(key, value1);
        ui->customPlot->graph(1)->addData(key, value1);
        lastPointKey = key;
      }
      ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
      ui->customPlot->replot();
      

      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Jayden123 said in QtCustomPlot Realtime Data Demo:

      The graph that I generated was unable to show the real time random data.

      What exactly happens? It plots nothing? It plots the wrong things? It plots the right things, but not all of them? It works for a while and then goes wrong? The re-range-scaling does not work right? Or what?

      Although it may not be your problem, I would recommend you get rid of your static variables and make them members of MainWindow, and initialize them as appropriate.

      J 1 Reply Last reply
      1
      • JonBJ JonB

        @Jayden123 said in QtCustomPlot Realtime Data Demo:

        The graph that I generated was unable to show the real time random data.

        What exactly happens? It plots nothing? It plots the wrong things? It plots the right things, but not all of them? It works for a while and then goes wrong? The re-range-scaling does not work right? Or what?

        Although it may not be your problem, I would recommend you get rid of your static variables and make them members of MainWindow, and initialize them as appropriate.

        J Offline
        J Offline
        Jayden123
        wrote on last edited by
        #3

        @JonB
        Hi, its totally plot nth once I start running it.

        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