QChart: Unable to get bar chart value labels to appear
-
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:
-
Might be a silly question but did you set somewhere the label on the QBarSet ? It looks like what you want.
-
More like this:
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 ...) -
Might be a silly question but did you set somewhere the label on the QBarSet ? It looks like what you want.
-
@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 callingseries->attachAxis
causes the labels to be suppressed:Omitting
attachAxis
:With
attachAxis
: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.
-
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:
If I call
series->setLabelsPosition(QAbstractBarSeries::LabelsPosition::LabelsInsideEnd)
, and restore the range, I get:Long story short, the labels were there, but rendered off-screen.
-
Oh ! That's a tricky one !
Glad you found out and thanks for sharing ! -