我正在QT 5.7中创建一个简单的折线图。
以下是我的certificate_page_childs表结构:
我的c ++代码:
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);
以下是我的输出:
我需要什么:
X轴:无读数
Y轴:每个读数一些值
请比较x轴与输出图像和所需的图像。
任何人都可以指导我直到达到预期的结果吗?
答案 0 :(得分:0)
在这种特殊情况下,作为X轴,您有1:1:n
的值,您可以设置tickCount
和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"); // <--