Groups multiple QCPBars together so they appear side by side.
When showing multiple QCPBars in one plot which have bars at identical keys, it may be desirable to have them appearing next to each other at each key. This is what adding the respective QCPBars plottables to a QCPBarsGroup achieves. (An alternative approach is to stack them on top of each other, see QCPBars::moveAbove.)
QVector<double> datax1 = QVector<double>() << 1 << 2 << 3 << 4;
QVector<double> datax2 = QVector<double>() << 5 << 6 << 7 << 8;
QVector<double> datax3 = QVector<double>() << 9 << 10 << 11 << 12;
QVector<double> datay1 = QVector<double>() << 0.6 << 0.5 << 0.3 << 0.15;
QVector<double> datay2 = QVector<double>() << 0.3 << 0.28 << 0.2 << 0.1;
QVector<double> datay3 = QVector<double>() << 0.33 << 0.31 << 0.27 << 0.13;
QCPBarsGroup *group1 = new QCPBarsGroup(customPlot);
group1->setSpacing(1);
QCPBars *bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars->setData(datax1, datay1);
bars->setBrush(QColor(0, 0, 255, 50));
bars->setPen(QColor(0, 0, 255));
bars->setWidth(1);
bars->setBarsGroup(group1);
bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars->setData(datax2, datay2);
bars->setBrush(QColor(180, 00, 120, 50));
bars->setPen(QColor(180, 00, 120));
bars->setWidth(1);
bars->setBarsGroup(group1);
bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars->setData(datax3, datay3);
bars->setBrush(QColor(255, 154, 0, 50));
bars->setPen(QColor(255, 154, 0));
bars->setWidth(1);
bars->setBarsGroup(group1);
//设置一下,否则会出现相邻bar中间有空白
group1->bars(1)->moveAbove(group1->bars(0));
group1->bars(2)->moveAbove(group1->bars(1));
customPlot->xAxis->setRange(0, 15);
customPlot->yAxis->setRange(0, 0.7);
效果图如下: