Main Page · Class Overview · Hierarchy · All Classes · Special Pages
Public Types | Public Functions | Protected Functions
QCPBarsGroup Class Reference

Groups multiple QCPBars together so they appear side by side. More...

Inherits QObject.

Public Types

enum  SpacingType

Public Functions

 QCPBarsGroup (QCustomPlot *parentPlot)
SpacingType spacingType () const
double spacing () const
void setSpacingType (SpacingType spacingType)
void setSpacing (double spacing)
QList< QCPBars * > bars () const
QCPBarsbars (int index) const
int size () const
bool isEmpty () const
void clear ()
bool contains (QCPBars *bars) const
void append (QCPBars *bars)
void insert (int i, QCPBars *bars)
void remove (QCPBars *bars)

Protected Functions

void registerBars (QCPBars *bars)
void unregisterBars (QCPBars *bars)
double keyPixelOffset (const QCPBars *bars, double keyCoord)
double getPixelSpacing (const QCPBars *bars, double keyCoord)

Detailed Description

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.)


To add a QCPBars plottable to the group, create a new group and then add the respective bars intances:

QCPBarsGroup *group = new QCPBarsGroup(customPlot);

Alternatively to appending to the group like shown above, you can also set the group on the QCPBars plottable via QCPBars::setBarsGroup.

The spacing between the bars can be configured via setSpacingType and setSpacing. The bars in this group appear in the plot in the order they were appended. To insert a bars plottable at a certain index position, or to reposition a bars plottable which is already in the group, use insert.

To remove specific bars from the group, use either remove or call QCPBars::setBarsGroup(0) on the respective bars plottable.

To clear the entire group, call clear, or simply delete the group.


The image above is generated with the following code:

QVector<double> datax = QVector<double>() << 1 << 2 << 3 << 4;
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);
QCPBars *bars2 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars2->setData(datax, datay2);
bars2->setBrush(QColor(180, 00, 120, 50));
bars2->setPen(QColor(180, 00, 120));
QCPBars *bars1 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars1->setData(datax, datay1);
bars1->setBrush(QColor(0, 0, 255, 50));
bars1->setPen(QColor(0, 255, 0));
QCPBars *bars3 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars3->setData(datax, datay3);
bars3->setBrush(QColor(255, 154, 0, 50));
bars3->setPen(QColor(255, 154, 0));
customPlot->xAxis->setRange(0.1, 4.9);
customPlot->yAxis->setRange(0, 0.7);

Member Enumeration Documentation

§ SpacingType

Defines the ways the spacing between bars in the group can be specified. Thus it defines what the number passed to setSpacing actually means.

See also
setSpacingType, setSpacing

Bar spacing is in absolute pixels.


Bar spacing is given by a fraction of the axis rect size.


Bar spacing is in key coordinates and thus scales with the key axis range.

Constructor & Destructor Documentation

§ QCPBarsGroup()

QCPBarsGroup::QCPBarsGroup ( QCustomPlot parentPlot)

Constructs a new bars group for the specified QCustomPlot instance.

Member Function Documentation

§ setSpacingType()

void QCPBarsGroup::setSpacingType ( SpacingType  spacingType)

Sets how the spacing between adjacent bars is interpreted. See SpacingType.

The actual spacing can then be specified with setSpacing.

See also

§ setSpacing()

void QCPBarsGroup::setSpacing ( double  spacing)

Sets the spacing between adjacent bars. What the number passed as spacing actually means, is defined by the current SpacingType, which can be set with setSpacingType.

See also

§ bars() [1/2]

QList< QCPBars * > QCPBarsGroup::bars ( ) const

Returns all bars currently in this group.

See also
bars(int index)

§ bars() [2/2]

QCPBars * QCPBarsGroup::bars ( int  index) const

Returns the QCPBars instance with the specified index in this group. If no such QCPBars exists, returns nullptr.

See also
bars(), size

§ size()

int QCPBarsGroup::size ( ) const

Returns the number of QCPBars plottables that are part of this group.

§ isEmpty()

bool QCPBarsGroup::isEmpty ( ) const

Returns whether this bars group is empty.

See also

§ clear()

void QCPBarsGroup::clear ( )

Removes all QCPBars plottables from this group.

See also

§ contains()

bool QCPBarsGroup::contains ( QCPBars bars) const

Returns whether the specified bars plottable is part of this group.

§ append()

void QCPBarsGroup::append ( QCPBars bars)

Adds the specified bars plottable to this group. Alternatively, you can also use QCPBars::setBarsGroup on the bars instance.

See also
insert, remove

§ insert()

void QCPBarsGroup::insert ( int  i,
QCPBars bars 

Inserts the specified bars plottable into this group at the specified index position i. This gives you full control over the ordering of the bars.

bars may already be part of this group. In that case, bars is just moved to the new index position.

See also
append, remove

§ remove()

void QCPBarsGroup::remove ( QCPBars bars)

Removes the specified bars plottable from this group.

See also
contains, clear

§ registerBars()

void QCPBarsGroup::registerBars ( QCPBars bars)

Adds the specified bars to the internal mBars list of bars. This method does not change the barsGroup property on bars.

See also

§ unregisterBars()

void QCPBarsGroup::unregisterBars ( QCPBars bars)

Removes the specified bars from the internal mBars list of bars. This method does not change the barsGroup property on bars.

See also

§ keyPixelOffset()

double QCPBarsGroup::keyPixelOffset ( const QCPBars bars,
double  keyCoord 

Returns the pixel offset in the key dimension the specified bars plottable should have at the given key coordinate keyCoord. The offset is relative to the pixel position of the key coordinate keyCoord.

§ getPixelSpacing()

double QCPBarsGroup::getPixelSpacing ( const QCPBars bars,
double  keyCoord 

Returns the spacing in pixels which is between this bars and the following one, both at the key coordinate keyCoord.

Typically the returned value doesn't depend on bars or keyCoord. bars is only needed to get access to the key axis transformation and axis rect for the modes stAxisRectRatio and stPlotCoords. The keyCoord is only relevant for spacings given in stPlotCoords on a logarithmic axis.

The documentation for this class was generated from the following files: