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. QChart: Unable to get bar chart value labels to appear
QtWS25 Last Chance

QChart: Unable to get bar chart value labels to appear

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 186 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.
  • M Offline
    M Offline
    MrSurly
    wrote on 18 Mar 2025, 17:38 last edited by
    #1

    I'm trying to simply have the numerical value of a given bar appear within the bar itself. QAbstractBarSeries provides several functions related to labels, but so far I have been unsuccessful in getting the labels to actually appear.

    Here is the code in question:

    QGroupBox *box = new QGroupBox("Cells", this);
    auto *layout = new QBoxLayout(QBoxLayout::LeftToRight, box);
    
    // barSeries holds the actual data
    // one QBarSet (with one value)
    // per cell
    cellBarSeries = new QHorizontalBarSeries(this);
    for (auto i = 0; i < 5; i++){
      auto barSet = new QBarSet("");
      *barSet << 3700;
      cellBarSeries->append(barSet);
    }
    cellBarSeries->setBarWidth(1);
    // cellBarSeries->setLabelsFormat("@value");
    cellBarSeries->setLabelsVisible(true);
    // cellBarSeries->setLabelsPosition(QBarSeries::LabelsCenter);
    
    // Create Chart
    auto *chart = new QChart;
    chart->addSeries(cellBarSeries);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->setAnimationDuration(500);
    chart->legend()->setVisible(false);
    
    // Set Margins
    QMargins margins;
    margins.setLeft(5);
    margins.setRight(5);
    margins.setTop(5);
    margins.setBottom(5);
    chart->setMargins(margins);
    
    // axisX
    auto axisX = new QValueAxis();
    axisX->setRange(2500, 4400);
    axisX->setLabelFormat("%4d");
    
    chart->addAxis(axisX, Qt::AlignTop);
    cellBarSeries->attachAxis(axisX);
    
    // Create widget
    auto *chartView = new QChartView(this);
    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing, true);
    layout->addWidget(chartView);
    
    return box;
    

    Here is the chart that it produces:

    d5f10644-ed3e-401f-a996-dd89b996a6ec-image.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Mar 2025, 19:42 last edited by
      #4

      Might be a silly question but did you set somewhere the label on the QBarSet ? It looks like what you want.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 23 Mar 2025, 20:09
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 18 Mar 2025, 19:54 last edited by
        #2

        Hi and welcome to devnet,

        Do you mean something similar to that example ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrSurly
          wrote on 23 Mar 2025, 18:16 last edited by MrSurly
          #3

          More like this:

          image.png

          Found here: https://github.com/komsit37/qchart?tab=readme-ov-file

          See how each bar has a number above it?

          (sorry for the late reply -- I didn't have email notifications turned on for my profile)
          (also now realizing this link doesn't appear to be affiliated with QT Qchart ...)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 23 Mar 2025, 19:42 last edited by
            #4

            Might be a silly question but did you set somewhere the label on the QBarSet ? It looks like what you want.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply 23 Mar 2025, 20:09
            0
            • S SGaist
              23 Mar 2025, 19:42

              Might be a silly question but did you set somewhere the label on the QBarSet ? It looks like what you want.

              M Offline
              M Offline
              MrSurly
              wrote on 23 Mar 2025, 20:09 last edited by
              #5

              @SGaist The labels come from the set values.

              I've figured out what's causing the issue, but not how to solve: Adding a QValueAxis , and specifically calling series->attachAxis causes the labels to be suppressed:

              Omitting attachAxis:

              1b9c56f7-da30-4556-99b4-a81b90139860-image.png

              With attachAxis:

              3007efb6-7213-4f87-b694-961d55031c6d-image.png

              As you can see, doing the latter causes the bars to scale to the axis (which is my desire), but makes the labels disappear. If I had to guess, I'd say this was an intentional thing.

              M 1 Reply Last reply 23 Mar 2025, 20:20
              0
              • M MrSurly
                23 Mar 2025, 20:09

                @SGaist The labels come from the set values.

                I've figured out what's causing the issue, but not how to solve: Adding a QValueAxis , and specifically calling series->attachAxis causes the labels to be suppressed:

                Omitting attachAxis:

                1b9c56f7-da30-4556-99b4-a81b90139860-image.png

                With attachAxis:

                3007efb6-7213-4f87-b694-961d55031c6d-image.png

                As you can see, doing the latter causes the bars to scale to the axis (which is my desire), but makes the labels disappear. If I had to guess, I'd say this was an intentional thing.

                M Offline
                M Offline
                MrSurly
                wrote on 23 Mar 2025, 20:20 last edited by
                #6

                @SGaist

                Okay, so the labels default to the QAbstractBarSeries::LabelsPosition::LabelsCenter, where center is defined as the middle of the bar numerically, not the middle of the bar as rendered:

                Original range is 2500 to 4400.

                Here's how it looks with range 1100 to 3200:

                d7b9127d-d02c-4c54-8d61-567203bc51cb-image.png

                If I call series->setLabelsPosition(QAbstractBarSeries::LabelsPosition::LabelsInsideEnd), and restore the range, I get:

                143c3499-4c7d-4f97-a844-6ba8dfa9c1da-image.png

                Long story short, the labels were there, but rendered off-screen.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 24 Mar 2025, 19:56 last edited by
                  #7

                  Oh ! That's a tricky one !
                  Glad you found out and thanks for sharing !

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M MrSurly has marked this topic as solved on 24 Mar 2025, 23:20

                  2/7

                  18 Mar 2025, 19:54

                  topic:navigator.unread, 5
                  • Login

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