Multiple Line Chart
-
Hi everyone. I have 4 different txt files. I want to read them and add them on a chart with a timer at the same time.
I have written a code for it. But it shows only series on chart. series2 3 and 4 doesnt seems on the screen.
How can i fix it?#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(); 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=queue.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=queue.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("deneme"); chart->setAnimationOptions(QChart::AllAnimations); QValueAxis *axisX=new QValueAxis; axisX->setTickCount(10); 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 I did not call the functions. I fixed it. Now i got an error
ASSERT: "!isEmpty()" in file ../Qt5.12.10/5.12.10/gcc_64/include/QtCore/qlist.h, line 347 11:47:51: The program has unexpectedly finished. 11:47:51: The process was ended forcefully. 11:47:51: /home/ilknur/build-Chart-Desktop_Qt_5_12_10_GCC_64bit-Debug/Chart crashed.
-
@suslucoder Well, use debugger to see where it crashes (in which line of your code)...
-
Hi
Seems you have an empty list so maybe file reading fails.
The debugger should tell you that easily. -
How can i add scrollbar to my chart?
-
There is no scrollbar. If you want one, you have to make one on your own, but it wouldn't make any sense.
You have your data and you have your series. Now you can decide which parts you want to show in your chart (this is the "scrolling")