Add value text to a QChart's bars
Solved
General and Desktop
-
From a dataset I generate a bar chart, what I can't manage to do is to add a text representing the bar's value on each bar.
My code
void ReportUi::ShowGeneralTrendGraph() { Statistics* stats = new Statistics(); QList<QMap<QString, QString>> report = stats->getGeneralJobsTrend(); QBarSet* set = new QBarSet("Jobs"); QBarSeries* series = new QBarSeries(); QCategoryAxis* axisX = new QCategoryAxis(); QStringList categories; for (int i = 0; i < report.size(); i++) { // Years prior to 2013 have missing data if (report[i]["year"].toInt() < 2013) continue; *set << report[i]["amount"].toInt(); categories << report[i]["year"]; } series->append(set); this->chart->addSeries(series); this->chart->setTitle("Jobs Trend"); QBarCategoryAxis* axis = new QBarCategoryAxis(); axis->append(categories); this->chart->createDefaultAxes(); this->chart->setAxisX(axis, series); this->chartView->setRenderHint(QPainter::Antialiasing); this->ui.ReportContainer->addWidget(this->chartView); // Add value labels addValueLabels(this->chart, series); } void ReportUi::addValueLabels(QChart* chart, QBarSeries* series) { for (int i = 0; i < series->count(); ++i) { QBarSet* set = series->barSets().at(i); for (int j = 0; j < set->count(); ++j) { qreal barX = chart->mapToPosition(QPointF(j + 0.5, set->at(j)), series).x(); qreal barY = chart->mapToPosition(QPointF(j + 0.5, set->at(j)), series).y(); QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(QString::number(set->at(j))); label->setPos(barX - label->boundingRect().width() / 2, barY - label->boundingRect().height() - 5); chart->scene()->addItem(label); } } }
What I have found with some debugging is that both barX and barY variables are always evaluated as 0.
Reading around that should be because, when calculating those values, the graph is not yet ready.
Is that actually my issue? What should I do to be sure to calculate them at the correct time? -
-
Hallo
I used the code above to add values to the bars, but the x-axis of the label is always the same. see also my code below.
void ChartWindow::showTeamsBarInOut(){QBarSeries *barseries = new QBarSeries(); for(uint8_t teamnr =0; teamnr< aantalTeams;teamnr++){ QBarSet *setTeamInOut = new QBarSet(namen[teamnr]); *setTeamInOut << inn[teamnr] << out[teamnr]; barseries->append(setTeamInOut); } QStringList categories; categories << "In Time" << "Out Time" ; QBarCategoryAxis *axisX = new QBarCategoryAxis(); axisX->append(categories); axisX->setRange(QString("In Time"), QString("Out Time")); QValueAxis *axisY = new QValueAxis(); axisY->setRange(0, maxInOutTime); chart->addAxis(axisY, Qt::AlignLeft); chart->addAxis(axisX, Qt::AlignBottom); chart->addSeries(barseries); barseries->attachAxis(axisY); barseries->attachAxis(axisX); connect(barseries, &QBarSeries::hovered, this, &ChartWindow::toolTip); chartView->setRenderHint(QPainter::Antialiasing); for (int bsets = 0; bsets < barseries->count(); ++bsets) { QBarSet* Bset = barseries->barSets().at(bsets); for (int valSet = 0; valSet < Bset->count(); ++valSet) { QPointF xAs = chart->mapToPosition(QPointF(valSet + 0.5, Bset->at(valSet)), barseries); qreal barX = xAs.x(); QPointF yAs = chart->mapToPosition(QPointF(valSet + 0.5, Bset->at(valSet)), barseries); qreal barY = yAs.y(); qDebug()<<barX<<" "<<barY<< Bset->at(valSet); QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(QString::number(Bset->at(valSet))); label->setPos(barX - label->boundingRect().width() / 2, barY - label->boundingRect().height() - 5); chart->scene()->addItem(label); } }
}
and get this image, can someone tell me what i am doing wrong?
When i hover over the bars i get the wright values