Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add value text to a QChart's bars
Forum Updated to NodeBB v4.3 + New Features

Add value text to a QChart's bars

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 480 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alhazred
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Alhazred
      wrote on last edited by
      #2

      The problem was actually that the graph was not ready.
      I've added a 100ms single shot timer and it now works.

      1 Reply Last reply
      0
      • A Alhazred has marked this topic as solved on
      • R Offline
        R Offline
        Reactionlight
        wrote on last edited by
        #3

        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

        Naamloos.png

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved