I am creating a simple line chart graph in QT 5.7.
Following is my certificate_page_childs table structure:
My c++ code :
QLineSeries *series = new QLineSeries();
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QString db_path = QDir::currentPath();
db_path = db_path + QString("/some_file");
db.setDatabaseName(db_path);
db.open();
QSqlQuery query;
query.prepare("SELECT hardness_val FROM certificate_page_childs where cp_id=:value");
query.bindValue(0,statisticalanalysis::cert_id_gbl);
if(query.exec()){
int d = 1;
while (query.next()) {
series->append(d, query.value(0).toDouble()); //Method 1
//qDebug() << query.value(0).toString();
//*series << QPointF(d,query.value(0).toDouble()); // Method 2
qDebug() << d;
d++;
}
}
else
{
qDebug() << query.lastError();
}
db.close();
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
chart->setAnimationOptions(QChart::SeriesAnimations);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);
ui->graphicsView->setChart(chart);
Following is my output :
What i need :
X axis : No of readings
Y axis : Some value per reading
Please compare the x-axis with the output image and the desired one.
Can anyone guide me till desired results are achieved ?
In this particular case as the X axis you have values of 1:1:n
you can set a tickCount
and the labelFormat
:
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
chart->setAnimationOptions(QChart::SeriesAnimations);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);
ui->graphicsView->setChart(chart);
QValueAxis *axisX = static_cast<QValueAxis *>(chart->axisX(series)); // <--
axisX->setTickCount(series->count()); // <--
axisX->setLabelFormat("%d"); // <--