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. QChart update automatically problem
Forum Updated to NodeBB v4.3 + New Features

QChart update automatically problem

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.7k Views
  • 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.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by
    #1

    Hello,
    I've implemented a simple line chart to update automatically with a timer as a data feeder

    #include "ui_mainwindow.h"
    #include <QtCharts/QChartGlobal>
    #include <QValueAxis>
    #include <QDebug>
    
    
    /*******************************************************/
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        m_chart = new QChart();
        m_chart->setTitle("Encoders");
    
        m_series = new QLineSeries();
    
        QValueAxis *axisX = new QValueAxis();
        axisX->setRange(0, 1000);
        QValueAxis *axisY = new QValueAxis();
        axisY->setRange(-100, 100);
    
        /*
         * Without these two lines chart will never update
         *
         */
        m_series->append(0,-100);
        m_series->append(1000,100);
    
    
        m_chart->setAxisX(axisX);
        m_chart->setAxisY(axisY);
    
        m_view = new QChartView(m_chart);
        m_view->setRenderHint(QPainter::Antialiasing);
    
    
        QPen pen(QRgb(0xfd000A));
        pen.setWidth(0.5);
        m_series->setPen(pen);
        m_chart->addSeries(m_series);
    
    
        ui->gridLayout->addWidget(m_view,1,0);
    
    
    
    
        m_timer = new QTimer(this);
        connect(m_timer, SIGNAL(timeout()), this, SLOT(updateChart()));
        m_timer->start(5);
    }
    
    /*******************************************************/
    void MainWindow::updateChart()
    {
        static int seed = 0;
    
        qreal x = seed;
        qreal y = qrand()%100;
    
        y=(qrand()%17==0)?-y:y;
    
        if(seed%1000==0)
        {
            m_series->clear();
            seed=0;
            x=seed;
            qDebug()<<"SET ZERO";
        }
        m_series->append(x,y);
    
        seed+=1;
    }
    
    /*******************************************************/
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    Chart get update after each data which append to m_series but if I remove the below lines in the constructor:

    m_series->append(0,-100);
    m_series->append(1000,100);
    

    chart will never update?!!! Also if I put wrong values for these two lines, chart's behaviuor goes wrong for example if I put the below lines instead of previous one:

    m_series->append(0,-100);
    m_series->append(100,100);
    

    x axis will be limit to 0~100 even though I setAxisX 0~1000.

    I can't understand the behavior of chart could you please give me a hint?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MrShawn
      wrote on last edited by MrShawn
      #2

      Try attaching your axis to the series as well as the chart. Off a quick inspection you are only attaching to the chart.
      By not attaching the axis to the series it is likely building a dflt axis off your initial values. Which is why when you do 100 vs 1000 your range is limited.

      AlienA 2 Replies Last reply
      4
      • M MrShawn

        Try attaching your axis to the series as well as the chart. Off a quick inspection you are only attaching to the chart.
        By not attaching the axis to the series it is likely building a dflt axis off your initial values. Which is why when you do 100 vs 1000 your range is limited.

        AlienA Offline
        AlienA Offline
        Alien
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M MrShawn

          Try attaching your axis to the series as well as the chart. Off a quick inspection you are only attaching to the chart.
          By not attaching the axis to the series it is likely building a dflt axis off your initial values. Which is why when you do 100 vs 1000 your range is limited.

          AlienA Offline
          AlienA Offline
          Alien
          wrote on last edited by
          #4

          @MrShawn ,Thanks a lot now it works fine

          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