Creating a chart with multiple datas
-
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.
Is there any easiest way to do it?
I create 4 different read file function, and im thiking creating 4 different display chart function. Is it silly? -
@suslucoder said in Creating a chart with multiple datas:
Is it silly
If your files are similar, yes.
(This got answered in your other threads. So I guess you dont need this one here?!) -
@Pl45m4 is it correct?
void MainWindow::Timer_Slot() { static float q_x=0; if(!queue.isEmpty()) { double num=queue.dequeue(); if (series->count()> 10) series->removePoints(0, 1); q_x += 0.1; series->append(q_x, num); chart->scroll(10, 0); chart->update(); qDebug() << q_x << num; } static float q_x2=0; if(!queue2.isEmpty()) { double num2=queue2.dequeue(); if(series2->count()>10) series2->removePoints(0,1); q_x2 += 0.1; series2->append(q_x2, num2); chart->scroll(10, 0); chart->update(); qDebug() << q_x2 << num2; } static float q_x3=0; if(!queue3.isEmpty()) { double num3=queue3.dequeue(); if(series3->count()>10) series3->removePoints(0,1); q_x3 += 0.1; series3->append(q_x3, num3); chart->scroll(10, 0); chart->update(); qDebug() << q_x3 << num3; } static float q_x4=0; if(!queue4.isEmpty()) { double num4=queue4.dequeue(); if(series4->count()>10) series4->removePoints(0,1); q_x4 += 0.1; series4->append(q_x4, num4); chart->scroll(10, 0); //chart->update(); qDebug() << q_x4 << num4; } }
-
@suslucoder said in Creating a chart with multiple datas:
is it correct?
It's almost the same - you're still putting same code several times...
Implement a method which gets the queue as parameter and putstatic float q_x=0; if(!queue.isEmpty()) { double num=queue.dequeue(); if (series->count()> 10) series->removePoints(0, 1); q_x += 0.1; series->append(q_x, num); chart->scroll(10, 0); chart->update(); qDebug() << q_x << num; }
in that method. Then simply call this method for each queue. Simple, isn't it?
-
You just merged your four functions into one. This doesn't change the fact that you have redundant code.
@jsulm was faster :)
-
@suslucoder said in Creating a chart with multiple datas:
But i have 4 different queue, all them should be parameter?
Just loop your queues.
Bad:
if(!queue1.isEmpty()) { double num=queue1.dequeue(); } if(!queue2.isEmpty()) { double num=queue2.dequeue(); } if(!queue3.isEmpty()) { double num=queue3.dequeue(); }
Better:
// Container with all your data or just pass one QQueue after another to your function QVector<QQueue<double> > data; for(int i = 0; i < data.size(); i++) { if(!data.at(i).isEmpty()) { double num=data.at(i).dequeue(); // ... } }
-
@suslucoder said in Creating a chart with multiple datas:
But i have 4 different queue, all them should be parameter?
Yes, that's the idea:
void MainWindow::someMethod(const QQueue<double> &queue) { ... } ... someMethod(queue); someMethod(queue1); someMethod(queue2); someMethod(queue3);
Or do it like @Pl45m4 suggested.
-
-
@jsulm is it true?
void MainWindow::ReadFile() { QFile file("/home/ilknur/Chart/Data1.txt"); QFile file2("/home/ilknur/Chart/Data2.txt"); QFile file3("/home/ilknur/Chart/Data3.txt"); QFile file4("/home/ilknur/Chart/Data4.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); } } } 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); } } } 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); } } } 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); } } } }
-
@suslucoder said in Creating a chart with multiple datas:
is it true?
Of course not!
You again copy same code several times.
You already was shown how to do this correctly, I'm not going to explain the same second time... -
@suslucoder No, I did it above already...
-
QString file("/home/ilknur/Chart/Data1.txt"); // ... // ... // all your files void MainWindow::ReadFile(QString file) { QFile(file); 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; // pick correct queue here queue.enqueue(num); } } } }
Then just call this read function ones for every file you want to read (with the file path as argument)
ReadFile(file);
Or even make a
QStringList
with all your filenames and call this function for every element of your list.To write "good" and clean code (OOP programming) , you'll need to stop that linear thinking (like script language). And even most of the script languages nowadays have functions, which you can define, to avoid writing the same code over and over and over again.
Doing something X times
!=
writing it X times.
It's all about effiency (trying to avoid unnecessary things that will waste time, space and hardware ressources at runtime) -
@Pl45m4 where should i store
QString file("/home/ilknur/Chart/Data1.txt"); // ... // ... // all your files
And i have 4 different files. In this case,
QString file("/home/ilknur/FourLineChart/Data1.txt"); QString file2("/home/ilknur/FourLineChart/Data2.txt"); QString file3("/home/ilknur/FourLineChart/Data3.txt"); QString file4("/home/ilknur/FourLineChart/Data4.txt");
-
@suslucoder said in Creating a chart with multiple datas:
where should i store
Do you even read what we wrote?
@Pl45m4 said in Creating a chart with multiple datas:
Then just call this read function ones for every file you want to read (with the file path as argument)
ReadFile(file);Or even make a QStringList with all your filenames and call this function for every element of your list.
^
-
@Pl45m4 said in
Do you even read what we wrote?
Of course. I just confused.
QString file("/home/ilknur/FourLineChart/Data1.txt"); QString file2("/home/ilknur/FourLineChart/Data2.txt"); QString file3("/home/ilknur/FourLineChart/Data3.txt"); QString file4("/home/ilknur/FourLineChart/Data4.txt"); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); CreateChart(); ReadFile(file); ReadFile(file2); ReadFile(file3); ReadFile(file4); void MainWindow::ReadFile(QString file) { QFile(file); 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; // pick correct queue here queue.enqueue(num); } } } }
I miss something. But i dont know.
-
@suslucoder I really don't understand why it is so hard! Please try to figure out such trivial things by yourself.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); CreateChart(); QString file("/home/ilknur/FourLineChart/Data1.txt"); QString file2("/home/ilknur/FourLineChart/Data2.txt"); QString file3("/home/ilknur/FourLineChart/Data3.txt"); QString file4("/home/ilknur/FourLineChart/Data4.txt"); ReadFile(file); ReadFile(file2); ReadFile(file3); ReadFile(file4);
Or shorter:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); CreateChart(); ReadFile("/home/ilknur/FourLineChart/Data1.txt"); ReadFile("/home/ilknur/FourLineChart/Data2.txt"); ReadFile("/home/ilknur/FourLineChart/Data3.txt"); ReadFile("/home/ilknur/FourLineChart/Data4.txt");