read csv file values and put these values in a graph
-
I would like to open a csv file, read these values and draw them in a graph I started the code but I got stuck how to identify the first column for which is the x of our graph and the second column for which is y from our graph you will find attached my code of the opening of the file and the code of the graph
could you help meQFile file(":/exampleTable.csv"); if ( !file.open(QFile::ReadOnly | QFile::Text) ) { qDebug() << "File not exists"; } else { // Create a thread to retrieve data from a file QTextStream in(&file); //Reads the data up to the end of file while (!in.atEnd()) { QString line = in.readLine(); //Im stuck there
graphe code :
QLineSeries *series = new QLineSeries(); //![1] //![2] series->append(x, y); //![2] //![3] QChart *chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); chart->createDefaultAxes(); chart->setTitle("Simple line chart example"); //![3] //![4] QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); //![4] //![5] QMainWindow window; window.setCentralWidget(chartView); window.resize(400, 300); window.show();
-
@najlou said in read csv file values and put these values in a graph:
while (!in.atEnd())
{
QString line = in.readLine();
//Im stuck thereThis is not that kind of forum where every answer is like "use the search" but there are actually lots of examples how to read / write csv files using Qt :)
Since you now have your whole line as
QString
.
There are also code samples in the documentation. Start here: -
@najlou If I were you I would use this csv parser to properly read the csv file. There is good enough info on how to use on github and then it should be trivial to fill your chart data. I am using the parser so far without any regret.
-
@DerReisende
I have no experience of it, but that looks to my eye like a lot of code! If the OP wants external code at all, I see there is actively-maintained qtcsv, only fair to mention that. Otherwise lots of other simple possibilities on @Pl45m4's stackoveflow page. -
@JonB Well, it is a full-fledge csv parser, not a half-baked quick solution. Integrating it in a cmake project is dead simple - I am a beginner in c++/qt world and it took not more than 10 minutes figuring out how to do it.
Using the library is simple too:#include <csv.hpp> csv::CSVReader csv_number_reader("file.csv"); for (const csv::CSVRow &row: csv_number_reader) { const auto nodeid = row["NODEID"].get<uint>(); const auto nodename = row["NODENAME"].get<std::string>(); const auto level = row[2].get<uint>(); const auto parent_id = row[3].get<uint>(); //now add to dataset }
This snippet opens file
file.csv
and iterates over each row in the file. From there you can easily access the columns either by name (if provided in the csv) or by index. I left the exception handling code but it is also simple to handle errors
For the OP it should be trivial to extract the contents from the csv and then put it into the chart dataset.I agree there are other possibilites but this is the one I know and use.
-
@DerReisende said in read csv file values and put these values in a graph:
not a half-baked quick solution
If you're saying that is qtcsv, I don't see that. I respect your choice to use such a large code base with all sorts of extras for parsing a CSV file. Personally I would not, and I think it's reasonable for the OP/others who come here to be aware there are smaller alternatives. The chances are that people asking here really need the simplest of CSV parsing features. I was merely drawing attention to possible alternatives.
-
@JonB Sorry for being unclear on that, I was referring to the original OP´s readLine mechanism as half-baked. I should have made that clearer.
I am sure qtcsv and other parsers are equally mature and besides the one I currently use I have tried some others as well but they didn´t fit to me. Which doesn't mean that the others are bad. I just use mine and IMHO it is a quick and easy solution for his problem.
-
Why would one use an extra library, when you can do what you need with 2-3 lines of code using Qt classes which are already there?!
-
@Pl45m4 Depending on size and complexity of a given file there may be a lot of good reasons to use an external library and not to reinvent the wheel. Proper error handling, performance and memory management for example.
I don't have restrictions to use external libs therefore I use the freedom to choose a library I feel comfortable with. And while his use case may be overkill for this library he may have a problem someday which can't be worked out in 3 SLOC but this code will work as well.
Besides this it is just an offer to the OP. I don't force him to use this or any other library but given the fact that he was stuck parsing the data I offer a quick (IMHO well-tested) solution to his problem.
-
@Pl45m4 said in read csv file values and put these values in a graph:
Why would one use an extra library, when you can do what you need with 2-3 lines of code using Qt classes which are already there?!
It depends on what you want to do.
If your file is a "simple" integer value comma/semi-colon separated file, then yes usingQString::split()
is enough.
But if your file contains more complex data, or you want to create a data model from it, then it becomes a more complex task.Re-inventing the wheel is something often done in software world, but must not always be done ;)