Changing the Color and State of Bars▲
This is part of the Charts with Widgets Gallery example.
The example shows how to change the color and state of bars using setBarSelected().
We create the sets and fill them with the data. Then we create a series and append data to it.
QBarSet *
setChicken =
createChickenSet();
QBarSet *
setPork =
createPorkSet();
QBarSet *
setTurkey =
createTurkeySet();
QBarSet *
setHam =
createHamSet();
qreal totalSum =
setChicken-&
gt;sum() +
setPork-&
gt;sum() +
setTurkey-&
gt;sum() +
setHam-&
gt;sum();
QList&
lt;QBarSet *&
gt; setList =
QList&
lt;QBarSet *&
gt;{
setChicken, setPork, setTurkey, setHam}
;
auto
series =
new
QBarSeries;
series-&
gt;append(setList);
We create the chart and add series to it. Also, we add a title to the chart, set animation for the chart, and align the legend.
auto
chart =
new
QChart;
chart-&
gt;addSeries(series);
chart-&
gt;setTitle(tr("Meat Consumption (Click on bars to select them)"
));
chart-&
gt;setAnimationOptions(QChart::
SeriesAnimations);
chart-&
gt;legend()-&
gt;setVisible(true
);
chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignBottom);
chart-&
gt;layout()-&
gt;setContentsMargins(0
, 0
, 0
, 0
);
Here we set the color for the selected bars.
const
auto
barSets =
series-&
gt;barSets();
for
(QBarSet *
barSet : barSets)
barSet-&
gt;setSelectedColor(barSet-&
gt;brush().color().darker());
Next step is adding axes: QBarCategoryAxis for years of measurements and QValueAxis for values range.
QStringList categories =
createYearCategories();
auto
axisX =
new
QBarCategoryAxis;
axisX-&
gt;setCategories(categories);
chart-&
gt;addAxis(axisX, Qt::
AlignBottom);
series-&
gt;attachAxis(axisX);
auto
axisY =
new
QValueAxis;
axisY-&
gt;setRange(0
, 20
);
axisY-&
gt;setTitleText(tr("Tons"
));
axisY-&
gt;setLabelsAngle(-
90
);
axisY-&
gt;setTitleVisible(true
);
chart-&
gt;addAxis(axisY, Qt::
AlignLeft);
series-&
gt;attachAxis(axisY);
Then we add the chart view to put the chart in.
auto
chartView =
new
QChartView(chart, this
);
chartView-&
gt;setRenderHint(QPainter::
Antialiasing);
Here we create a widget for labels of values of selected and unselected bars.
auto
labelWidget =
new
QWidget(this
);
auto
labelLayout =
new
QHBoxLayout(labelWidget);
labelLayout-&
gt;setAlignment(Qt::
AlignCenter);
auto
totalSumLabel =
new
QLabel(tr("Total sum: %1 T"
).arg(totalSum), this
);
labelLayout-&
gt;addWidget(totalSumLabel);
totalSumLabel-&
gt;setContentsMargins(0
, 0
, 54
, 0
);
auto
selectedSumLabel =
new
QLabel(tr("Selected sum: 0 T"
), this
);
labelLayout-&
gt;addWidget(selectedSumLabel);
auto
unselectedSumLabel =
new
QLabel(tr("Unselected sum: %1 T"
).arg(totalSum), this
);
labelLayout-&
gt;addWidget(unselectedSumLabel);
unselectedSumLabel-&
gt;setContentsMargins(54
, 0
, 0
, 0
);
We connect selecting of a specific bar with labels of values using a lambda. set->toggleSelection({index}) sets the bar selected.
QObject::
connect(series, &
amp;QAbstractBarSeries::
clicked, series, [=
](int
index, QBarSet *
set) {
set-&
gt;toggleSelection({
index}
);
qreal selectedSum =
0.
;
for
(int
i =
0
; i &
lt; setList.size(); ++
i) {
auto
selectedIndices =
setList.at(i)-&
gt;selectedBars();
for
(int
k =
0
; k &
lt; selectedIndices.size(); ++
k)
selectedSum +=
setList.at(i)-&
gt;at(selectedIndices.at(k));
}
selectedSumLabel-&
gt;setText(tr("Selected sum: %1 T"
).arg(selectedSum));
// Because of rounding errors, selectedSum can result in being bigger than total sum
qreal unselectedSum =
totalSum -
selectedSum &
lt; 0
? 0.
: totalSum -
selectedSum;
unselectedSumLabel-&
gt;setText(
tr("Unselected sum: %1 T"
)
.arg(unselectedSum)
);
}
);
Finally, we create the main layout.
auto
mainLayout =
new
QVBoxLayout(this
);
mainLayout-&
gt;addWidget(chartView);
mainLayout-&
gt;addWidget(labelWidget);