I'm trying to develop a chart that shows the number of sales of each day of the month, it works but I can not change the caption of each position of the x axis. This is the way I'm trying to change:
void Relatorios::vendasMensais(QLineSeries *series, QChart *chart, int mes, bool anoAtual, int ano)
{
QSqlDatabase db = Database::database();
QSqlQuery q(db);
q.prepare("SELECT COUNT(id), feita_em FROM vendas WHERE YEAR(feita_em) = :ano AND MONTH(feita_em) = :mes GROUP BY DAY(feita_em)");
if(anoAtual)
q.bindValue(":ano", QDate::currentDate().year());
else
q.bindValue(":ano", ano);
q.bindValue(":mes", mes);
if(!q.exec()){
qCritical() << "Falha ao realizar a consulta: " << q.lastError().text();
return;
}
QCategoryAxis *xAxis = new QCategoryAxis();
int i = 1;
while(q.next()){
QDateTime data = QDateTime::fromString(q.value(1).toString(), "yyyy-MM-ddThh:mm:ss.z");
xAxis->append(QString::number(data.date().day()), data.date().day());
series->append(data.date().day(), q.value(0).toInt());
}
chart->addSeries(series);
chart->legend()->hide();
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).append(xAxis);
chart->setTitle("Vendas do mês");
}
But does not currently change the caption
Do not use createDefaultAxes()
. Create a value axis for y, then add both the value axis and the category axis using addAxis()
commands.
Your manipulation on axes()
does not change the chart. It only manipulates the returned list which is a copy of the axes used in the chart.
In general with Qt you cannot use getters to directly manipulate properties. To do so, you have to call the setter explicitly.