Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Scroll chart, when new data come



  • Hi. I have a chart. I want to scroll my chart, when new datas come. How can i do it?
    I want to set range according to my datas which are txt file.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QtCharts/QLineSeries>
    #include <QTimer>
    #include <QTextStream>
    #include <QtCharts>
    #include <QChart>
    #include <QString>
    #include <QStringList>
    #include <QtCore/QDateTime>
    #include <QtCharts/QDateTimeAxis>
    #include <QQueue>
    #include <QSplineSeries>
    
    QT_CHARTS_USE_NAMESPACE
    using namespace QtCharts;
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        CreateChart();
        ReadFile();
        ReadFile2();
        ReadFile3();
        ReadFile4();
    
        connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot()));
        timer->setInterval(500);
        timer->start();
    
        connect(timer2, SIGNAL(timeout()), SLOT(Timer_Slot2()));
        timer2->setInterval(500);
        timer2->start();
    
        connect(timer3, SIGNAL(timeout()), SLOT(Timer_Slot3()));
        timer3->setInterval(500);
        timer3->start();
    
        connect(timer4, SIGNAL(timeout()), SLOT(Timer_Slot4()));
        timer4->setInterval(500);
        timer4->start();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::Timer_Slot()
    {
        static float q_x=0;
        if(!queue.isEmpty())
        {
           double num=queue.dequeue();
           q_x += 0.1;
           series->append(q_x, num);
           chart->update();
           
           qDebug() << q_x << num;
         }
    }
    
    void MainWindow::Timer_Slot2()
    {
        static float q_x2=0;
        if(!queue2.isEmpty())
        {
           double num2=queue2.dequeue();
           q_x2 += 0.1;
           series2->append(q_x2, num2);
           chart->update();
           qDebug() << q_x2 << num2;
         }
    }
    void MainWindow::Timer_Slot3()
    {
        static float q_x3=0;
        if(!queue3.isEmpty())
        {
           double num3=queue3.dequeue();
           q_x3 += 0.1;
           series3->append(q_x3, num3);
           chart->update();
           qDebug() << q_x3 << num3;
         }
    }
    
    void MainWindow::Timer_Slot4()
    {
        static float q_x4=0;
        if(!queue4.isEmpty())
        {
           double num4=queue4.dequeue();
           q_x4 += 0.1;
           series4->append(q_x4, num4);
           chart->update();
           qDebug() << q_x4 << num4;
         }
    }
    void MainWindow::ReadFile()
    {
        QFile file("/home/ilknur/Chart/Data1.txt");
        if(file.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file);
            while (!in.atEnd()) {
                QString line = in.readLine();
                QStringList list = line.split(QLatin1Char(' '), QString::SkipEmptyParts);
                for (const QString &entry : list)
                {
                    double num = entry.toDouble();
                    qDebug() << num;
                    queue.enqueue(num);
                }
            }
        }
    }
    
    void MainWindow::ReadFile2()
    {
        QFile file2("/home/ilknur/Chart/Data2.txt");
        if(file2.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file2);
            while (!in.atEnd()) {
                QString line2 = in.readLine();
                QStringList list2 = line2.split(QLatin1Char(' '), QString::SkipEmptyParts);
                for (const QString &entry : list2)
                {
                    double num2 = entry.toDouble();
                    qDebug() << num2;
                    queue2.enqueue(num2);
                }
            }
        }
    }
    void MainWindow::ReadFile3()
    {
        QFile file3("/home/ilknur/Chart/Data3.txt");
        if(file3.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file3);
            while (!in.atEnd()) {
                QString line3 = in.readLine();
                QStringList list3 = line3.split(QLatin1Char(' '), QString::SkipEmptyParts);
                for (const QString &entry : list3)
                {
                    double num3 = entry.toDouble();
                    qDebug() << num3;
                    queue3.enqueue(num3);
                }
            }
        }
    }
    void MainWindow::ReadFile4()
    {
        QFile file4("/home/ilknur/Chart/Data4.txt");
        if(file4.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file4);
            while (!in.atEnd()) {
                QString line4 = in.readLine();
                QStringList list4 = line4.split(QLatin1Char(' '), QString::SkipEmptyParts);
                for (const QString &entry : list4)
                {
                    double num4 = entry.toDouble();
                    qDebug() << num4;
                    queue4.enqueue(num4);
                }
            }
        }
    }
    
    void MainWindow::CreateChart()
    {
            QChart *chart = new QChart();
            chart->addSeries(series);
            chart->addSeries(series2);
            chart->addSeries(series3);
            chart->addSeries(series4);
            qDebug()<< series;
            qDebug()<< series2;
            qDebug()<< series3;
            qDebug() << "bu dördüncü seri"<< series4;
            QPen green(Qt::red);
            green.setWidth(2);
            series->setPen(green);
            series2->setColor("green");
            series3->setColor("blue");
            series4->setColor("black");
            chart->legend()->hide();
            chart->setTitle("MyChart");
            chart->setAnimationOptions(QChart::AllAnimations);
    
            QValueAxis *axisX=new QValueAxis;
            axisX->setTickCount(5);
            axisX->setRange(0,1);
            chart->addAxis(axisX, Qt::AlignBottom);
            series->attachAxis(axisX);
            series2->attachAxis(axisX);
            series3->attachAxis(axisX);
            series4->attachAxis(axisX);
    
            QValueAxis *axisY = new QValueAxis;
            axisY->setTickCount(5);
            axisY->setRange(0,1);
            chart->addAxis(axisY, Qt::AlignLeft);
            series->attachAxis(axisY);
            series2->attachAxis(axisY);
            series3->attachAxis(axisY);
            series4->attachAxis(axisY);
    
            QChartView *chartView = new QChartView(chart);
            chartView->setRenderHint(QPainter::Antialiasing);
            this->setCentralWidget(chartView);
    }
    


  • @suslucoder said in Scroll chart, when new data come:

    void MainWindow::Timer_Slot()
    {
    static float q_x=0;
    if(!queue.isEmpty())
    {
    double num=queue.dequeue();
    q_x += 0.1;
    series->append(q_x, num);
    chart->update();

       qDebug() << q_x << num;
     }
    

    }

    void MainWindow::Timer_Slot2()
    {
    static float q_x2=0;
    if(!queue2.isEmpty())
    {
    double num2=queue2.dequeue();
    q_x2 += 0.1;
    series2->append(q_x2, num2);
    chart->update();
    qDebug() << q_x2 << num2;
    }
    }
    void MainWindow::Timer_Slot3()
    {
    static float q_x3=0;
    if(!queue3.isEmpty())
    {
    double num3=queue3.dequeue();
    q_x3 += 0.1;
    series3->append(q_x3, num3);
    chart->update();
    qDebug() << q_x3 << num3;
    }
    }

    void MainWindow::Timer_Slot4()
    {
    static float q_x4=0;
    if(!queue4.isEmpty())
    {
    double num4=queue4.dequeue();
    q_x4 += 0.1;
    series4->append(q_x4, num4);
    chart->update();
    qDebug() << q_x4 << num4;
    }
    }

    You don't have the same code four times here, right?! :)

    set range

    Here is the solution already (QAbstractAxis::setRange)



  • @Pl45m4 How can i do it, writing the same code 4 times?



  • @suslucoder

    One timer, one function to update all four data series.

    Or four timers, if you want to update them independently / stop one update interval. But then one function is still enough. You can connect all timers to one function and check inside this function which series are enabled and what to update.



  • @Pl45m4 Thank you. I will try



  • @Pl45m4 How about reading? Can i do all of them in one funciton?



  • @suslucoder

    If your files all have the same structure (separator, data etc.), yes, you should actually.