QChart update automatically problem



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



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



  • This post is deleted!


  • @MrShawn ,Thanks a lot now it works fine


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.