Manu Manohara
Manu Manohara

Reputation: 49

qt c++ plot customplot from sqlite table

enter image description hereI am new to Qt creator C++. I have designed a application where the data is displayed on a tableView fetching it from Sqlite database, till here i am successfull, but now i want to plot the graph on a customplot by giving values for x and y axis from the database which i have fetched it and displayed on tableView widget.Example: the data from my database is like Time and Temperature-Now i want to give values for X axis in Time and Y axis in Temperature, please help me out in the code- I am able to plot the graph normally as shown in the below code,how do i add the database values on x and y axis..

void MainWindow::makePlot()
{
    QVector<double> x(100), y(101); 

//    x[0]=1;Here I want the application to take the values from tableView,like 
              Time on X axis and Temp on Y axis. 
//    x[1]=2;
//    x[2]=3;
//    x[3]=4;

//    y[0]=1;
//    y[1]=2;
//    y[2]=3;
//    y[3]=4;

    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setData(x, y);
    ui->customPlot->xAxis->setLabel("Time");
    ui->customPlot->yAxis->setLabel("Temp");
    ui->customPlot->xAxis->setRange(1, 15);
    ui->customPlot->yAxis->setRange(1, 15);
    ui->customPlot->replot();

}

Here is the code that displays the Database in tableView on push button.
void MainWindow::on_pushButton_clicked()
{
    MainWindow conn;
    QSqlQueryModel * modal=new QSqlQueryModel();
    conn.connOpen();
    QSqlQuery* qry=new QSqlQuery(conn.mydb);
    qry->prepare("select * from empdata");
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);
    conn.connClose();
    qDebug() <<(modal->rowCount());
}[![enter image description here][1]][1]

output of the above code

Please help me out..Thanks in advance..

enter image description here

Upvotes: 1

Views: 2059

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

In this solution I assume that the table is created in the following way (use sqlite as a database):

create table empdata (id INTEGER PRIMARY KEY AUTOINCREMENT, 
                      time DATETIME, 
                      temperature REAL)

I will also use QSqlTableModel as a model, if you want to use QSqlQueryModel the logic is similar.

QSqlTableModel *model=new QSqlTableModel;
model->setTable("empdata");
model->select();
ui->tableView->setModel(model);

ui->customPlot->xAxis->setLabel("Time");
ui->customPlot->yAxis->setLabel("Temp");
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("dd/MM/yyyy hh:mm:ss");
ui->customPlot->xAxis->setTicker(dateTicker);

QVector<QCPGraphData> timeData(model->rowCount());

for(int i=0; i< model->rowCount(); ++i){
    timeData[i].key = model->index(i, model->fieldIndex("time")).data().toDateTime().toTime_t();
    timeData[i].value = model->index(i, model->fieldIndex("temperature")).data().toDouble();
}

double Tmin = (*std::min_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.key < y.key; })).key;
double Tmax = (*std::max_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.key < y.key; })).key;

double Ymin = (*std::min_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.value < y.value; })).value;

double Ymax = (*std::max_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.value < y.value; })).value;

ui->customPlot->xAxis->setRange(Tmin, Tmax);
ui->customPlot->yAxis->setRange(Ymin, Ymax);
ui->customPlot->graph(0)->data()->set(timeData);
ui->customPlot->replot();

enter image description here

In the following link is the complete example.


Update:

The solution is similar but you have to convert that QString to QDateTime

QSqlTableModel *model=new QSqlTableModel;
model->setTable("empdata2");
model->select();
ui->tableView->setModel(model);

ui->customPlot->xAxis->setLabel("Time");
ui->customPlot->yAxis->setLabel("Temp");
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("hh:mm");
ui->customPlot->xAxis->setTicker(dateTicker);

QVector<QCPGraphData> timeData(model->rowCount());

for(int i=0; i< model->rowCount(); ++i){
    timeData[i].key = QDateTime(QDate::currentDate(), model->index(i, model->fieldIndex("time")).data().toTime()).toTime_t();
    timeData[i].value = model->index(i, model->fieldIndex("temp")).data().toDouble();
}

double Tmin = (*std::min_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.key < y.key; })).key;
double Tmax = (*std::max_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.key < y.key; })).key;

double Ymin = (*std::min_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.value < y.value; })).value;

double Ymax = (*std::max_element(timeData.begin(), timeData.end(),
                                [](const QCPGraphData& x, const QCPGraphData& y)
{  return x.value < y.value; })).value;

ui->customPlot->xAxis->setRange(Tmin, Tmax);
ui->customPlot->yAxis->setRange(Ymin, Ymax);
ui->customPlot->graph(0)->data()->set(timeData);
ui->customPlot->replot();

enter image description here

You can find the new solution in the following link

Upvotes: 1

Related Questions