· · · ·
Public Types | Public Functions | Protected Functions | Protected Static Functions
QCPBars Class Reference

A plottable representing a bar chart in a plot. More...

Inheritance diagram for QCPBars:

## Public Types
enum | WidthType

## Public Functions
| QCPBars (QCPAxis *keyAxis, QCPAxis *valueAxis)
double | width () const
WidthType | widthType () const
QCPBarsGroup * | barsGroup () const
double | baseValue () const
double | stackingGap () const
QCPBars * | barBelow () const
QCPBars * | barAbove () const
QSharedPointer< QCPBarsDataContainer > | data () const
void | setData (QSharedPointer< QCPBarsDataContainer > data)
void | setData (const QVector< double > &keys, const QVector< double > &values, bool alreadySorted=false)
void | setWidth (double width)
void | setWidthType (WidthType widthType)
void | setBarsGroup (QCPBarsGroup *barsGroup)
void | setBaseValue (double baseValue)
void | setStackingGap (double pixels)
void | addData (const QVector< double > &keys, const QVector< double > &values, bool alreadySorted=false)
void | addData (double key, double value)
void | moveBelow (QCPBars *bars)
void | moveAbove (QCPBars *bars)
virtual QCPDataSelection | selectTestRect (const QRectF &rect, bool onlySelectable) const
virtual double | selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=nullptr) const
virtual QCPRange | getKeyRange (bool &foundRange, QCP::SignDomain inSignDomain=QCP::sdBoth) const
virtual QCPRange | getValueRange (bool &foundRange, QCP::SignDomain inSignDomain=QCP::sdBoth, const QCPRange &inKeyRange=QCPRange()) const
virtual QPointF | dataPixelPosition (int index) const
Public Functions inherited from QCPAbstractPlottable1D< QCPBarsData >
| QCPAbstractPlottable1D (QCPAxis *keyAxis, QCPAxis *valueAxis)
virtual int | dataCount () const
virtual double | dataMainKey (int index) const
virtual double | dataSortKey (int index) const
virtual double | dataMainValue (int index) const
virtual QCPRange | dataValueRange (int index) const
virtual bool | sortKeyIsMainKey () const
virtual int | findBegin (double sortKey, bool expandedRange=true) const
virtual int | findEnd (double sortKey, bool expandedRange=true) const
virtual QCPPlottableInterface1D * | interface1D ()
Public Functions inherited from QCPAbstractPlottable
| QCPAbstractPlottable (QCPAxis *keyAxis, QCPAxis *valueAxis)
QString | name () const
bool | antialiasedFill () const
bool | antialiasedScatters () const
QPen | pen () const
QBrush | brush () const
QCPAxis * | keyAxis () const
QCPAxis * | valueAxis () const
QCP::SelectionType | selectable () const
bool | selected () const
QCPDataSelection | selection () const
QCPSelectionDecorator * | selectionDecorator () const
void | setName (const QString &name)
void | setAntialiasedFill (bool enabled)
void | setAntialiasedScatters (bool enabled)
void | setPen (const QPen &pen)
void | setBrush (const QBrush &brush)
void | setKeyAxis (QCPAxis *axis)
void | setValueAxis (QCPAxis *axis)
Q_SLOT void | setSelectable (QCP::SelectionType selectable)
Q_SLOT void | setSelection (QCPDataSelection selection)
void | setSelectionDecorator (QCPSelectionDecorator *decorator)
void | coordsToPixels (double key, double value, double &x, double &y) const
const QPointF | coordsToPixels (double key, double value) const
void | pixelsToCoords (double x, double y, double &key, double &value) const
void | pixelsToCoords (const QPointF &pixelPos, double &key, double &value) const
void | rescaleAxes (bool onlyEnlarge=false) const
void | rescaleKeyAxis (bool onlyEnlarge=false) const
void | rescaleValueAxis (bool onlyEnlarge=false, bool inKeyRange=false) const
bool | addToLegend (QCPLegend *legend)
bool | addToLegend ()
bool | removeFromLegend (QCPLegend *legend) const
bool | removeFromLegend () const
Public Functions inherited from QCPLayerable
| QCPLayerable (QCustomPlot *plot, QString targetLayer=QString(), QCPLayerable *parentLayerable=nullptr)
bool | visible () const
QCustomPlot * | parentPlot () const
QCPLayerable * | parentLayerable () const
QCPLayer * | layer () const
bool | antialiased () const
void | setVisible (bool on)
Q_SLOT bool | setLayer (QCPLayer *layer)
bool | setLayer (const QString &layerName)
void | setAntialiased (bool enabled)
bool | realVisibility () const

## Protected Functions
virtual void | draw (QCPPainter *painter)
virtual void | drawLegendIcon (QCPPainter *painter, const QRectF &rect) const
void | getVisibleDataBounds (QCPBarsDataContainer::const_iterator &begin, QCPBarsDataContainer::const_iterator &end) const
QRectF | getBarRect (double key, double value) const
void | getPixelWidth (double key, double &lower, double &upper) const
double | getStackedBaseValue (double key, bool positive) const
Protected Functions inherited from QCPAbstractPlottable1D< QCPBarsData >
void | getDataSegments (QList< QCPDataRange > &selectedSegments, QList< QCPDataRange > &unselectedSegments) const
void | drawPolyline (QCPPainter *painter, const QVector< QPointF > &lineData) const
Protected Functions inherited from QCPAbstractPlottable
virtual QRect | clipRect () const
virtual QCP::Interaction | selectionCategory () const
void | applyDefaultAntialiasingHint (QCPPainter *painter) const
virtual void | selectEvent (QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged)
virtual void | deselectEvent (bool *selectionStateChanged)
void | applyFillAntialiasingHint (QCPPainter *painter) const
void | applyScattersAntialiasingHint (QCPPainter *painter) const
Protected Functions inherited from QCPLayerable
virtual void | parentPlotInitialized (QCustomPlot *parentPlot)
virtual void | mousePressEvent (QMouseEvent *event, const QVariant &details)
virtual void | mouseMoveEvent (QMouseEvent *event, const QPointF &startPos)
virtual void | mouseReleaseEvent (QMouseEvent *event, const QPointF &startPos)
virtual void | mouseDoubleClickEvent (QMouseEvent *event, const QVariant &details)
virtual void | wheelEvent (QWheelEvent *event)
void | initializeParentPlot (QCustomPlot *parentPlot)
void | setParentLayerable (QCPLayerable *parentLayerable)
bool | moveToLayer (QCPLayer *layer, bool prepend)
void | applyAntialiasingHint (QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const

## Protected Static Functions
static void | connectBars (QCPBars *lower, QCPBars *upper)

## Additional Inherited Members
Signals inherited from QCPAbstractPlottable
void | selectionChanged (bool selected)
void | selectionChanged (const QCPDataSelection &selection)
void | selectableChanged (QCP::SelectionType selectable)
Signals inherited from QCPLayerable
void | layerChanged (QCPLayer *newLayer)

## Detailed Description

A plottable representing a bar chart in a plot.

To plot data, assign it with the setData or addData functions.

# Changing the appearance

The appearance of the bars is determined by the pen and the brush (setPen, setBrush). The width of the individual bars can be controlled with setWidthType and setWidth.

Bar charts are stackable. This means, two QCPBars plottables can be placed on top of each other (see QCPBars::moveAbove). So when two bars are at the same key position, they will appear stacked.

If you would like to group multiple QCPBars plottables together so they appear side by side as shown below, use QCPBarsGroup.

# Usage

Like all data representing objects in QCustomPlot, the QCPBars is a plottable (QCPAbstractPlottable). So the plottable-interface of QCustomPlot applies (QCustomPlot::plottable, QCustomPlot::removePlottable, etc.)

Usually, you first create an instance:

QCPBars *newBars = new QCPBars(customPlot->xAxis, customPlot->yAxis);

which registers it with the QCustomPlot instance of the passed axes. Note that this QCustomPlot instance takes ownership of the plottable, so do not delete it manually but use QCustomPlot::removePlottable() instead. The newly created plottable can be modified, e.g.:

newBars->setName("Country population");
newBars->setData(xData, yData);

## Member Enumeration Documentation

## § WidthType

enum QCPBars::WidthType

Defines the ways the width of the bar can be specified. Thus it defines what the number passed to setWidth actually means.

See also
  setWidthType, setWidth

Enumerator
wtAbsolute | Bar width is in absolute pixels.
wtAxisRectRatio | Bar width is given by a fraction of the axis rect size.
wtPlotCoords | Bar width is in key coordinates and thus scales with the key axis range.

## Constructor & Destructor Documentation

## § QCPBars()

QCPBars::QCPBars | ( | QCPAxis * | keyAxis,
|  | QCPAxis * | valueAxis
| ) |  |
| explicit

Constructs a bar chart which uses keyAxis as its key axis ("x") and valueAxis as its value axis ("y"). keyAxis and valueAxis must reside in the same QCustomPlot instance and not have the same orientation. If either of these restrictions is violated, a corresponding message is printed to the debug output (qDebug), the construction is not aborted, though.

The created QCPBars is automatically registered with the QCustomPlot instance inferred from keyAxis. This QCustomPlot instance takes ownership of the QCPBars, so do not delete it manually but use QCustomPlot::removePlottable() instead.

## Member Function Documentation

## § barBelow()

QCPBars * QCPBars::barBelow | ( |  | ) | const
| inline

Returns the bars plottable that is directly below this bars plottable. If there is no such plottable, returns `nullptr`.

See also
  barAbove, moveBelow, moveAbove

## § barAbove()

QCPBars * QCPBars::barAbove | ( |  | ) | const
| inline

Returns the bars plottable that is directly above this bars plottable. If there is no such plottable, returns `nullptr`.

See also
  barBelow, moveBelow, moveAbove

## § data()

QSharedPointer< QCPBarsDataContainer > QCPBars::data | ( |  | ) | const
| inline

Returns a shared pointer to the internal data storage of type QCPBarsDataContainer. You may use it to directly manipulate the data, which may be more convenient and faster than using the regular setData or addData methods.

## § setData() [1/2]

void QCPBars::setData | ( | QSharedPointer< QCPBarsDataContainer > | data | ) |

This is an overloaded function.

Replaces the current data container with the provided data container.

Since a QSharedPointer is used, multiple QCPBars may share the same data container safely. Modifying the data in the container will then affect all bars that share the container. Sharing can be achieved by simply exchanging the data containers wrapped in shared pointers:

bars2->setData(bars1->data()); // bars1 and bars2 now share data container

If you do not wish to share containers, but create a copy from an existing container, rather use the QCPDataContainer<DataType>::set method on the bar's data container directly:

bars2->data()->set(*bars1->data()); // bars2 now has copy of bars1's data in its container

See also
  addData

## § setData() [2/2]

void QCPBars::setData | ( | const QVector< double > & | keys,
|  | const QVector< double > & | values,
|  | bool | alreadySorted = `false`
| ) |  |

This is an overloaded function.

Replaces the current data with the provided points in keys and values. The provided vectors should have equal length. Else, the number of added points will be the size of the smallest vector.

If you can guarantee that the passed data points are sorted by keys in ascending order, you can set alreadySorted to true, to improve performance by saving a sorting run.

See also
  addData

## § setWidth()

void QCPBars::setWidth | ( | double | width | ) |

Sets the width of the bars.

How the number passed as width is interpreted (e.g. screen pixels, plot coordinates,...), depends on the currently set width type, see setWidthType and WidthType.

## § setWidthType()

void QCPBars::setWidthType | ( | QCPBars::WidthType | widthType | ) |

Sets how the width of the bars is defined. See the documentation of WidthType for an explanation of the possible values for widthType.

The default value is wtPlotCoords.

See also
  setWidth

## § setBarsGroup()

void QCPBars::setBarsGroup | ( | QCPBarsGroup * | barsGroup | ) |

Sets to which QCPBarsGroup this QCPBars instance belongs to. Alternatively, you can also use QCPBarsGroup::append.

To remove this QCPBars from any group, set barsGroup to `nullptr`.

## § setBaseValue()

void QCPBars::setBaseValue | ( | double | baseValue | ) |

Sets the base value of this bars plottable.

The base value defines where on the value coordinate the bars start. How far the bars extend from the base value is given by their individual value data. For example, if the base value is set to 1, a bar with data value 2 will have its lowest point at value coordinate 1 and highest point at 3.

For stacked bars, only the base value of the bottom-most QCPBars has meaning.

The default base value is 0.

## § setStackingGap()

void QCPBars::setStackingGap | ( | double | pixels | ) |

If this bars plottable is stacked on top of another bars plottable (moveAbove), this method allows specifying a distance in pixels, by which the drawn bar rectangles will be separated by the bars below it.

## § addData() [1/2]

void QCPBars::addData | ( | const QVector< double > & | keys,
|  | const QVector< double > & | values,
|  | bool | alreadySorted = `false`
| ) |  |

This is an overloaded function.

Adds the provided points in keys and values to the current data. The provided vectors should have equal length. Else, the number of added points will be the size of the smallest vector.

If you can guarantee that the passed data points are sorted by keys in ascending order, you can set alreadySorted to true, to improve performance by saving a sorting run.

Alternatively, you can also access and modify the data directly via the data method, which returns a pointer to the internal data container.

## § addData() [2/2]

void QCPBars::addData | ( | double | key,
|  | double | value
| ) |  |

This is an overloaded function. Adds the provided data point as key and value to the current data.

Alternatively, you can also access and modify the data directly via the data method, which returns a pointer to the internal data container.

## § moveBelow()

void QCPBars::moveBelow | ( | QCPBars * | bars | ) |

Moves this bars plottable below bars. In other words, the bars of this plottable will appear below the bars of bars. The move target bars must use the same key and value axis as this plottable.

Inserting into and removing from existing bar stacking is handled gracefully. If bars already has a bars object below itself, this bars object is inserted between the two. If this bars object is already between two other bars, the two other bars will be stacked on top of each other after the operation.

To remove this bars plottable from any stacking, set bars to `nullptr`.

See also
  moveBelow, barAbove, barBelow

## § moveAbove()

void QCPBars::moveAbove | ( | QCPBars * | bars | ) |

Moves this bars plottable above bars. In other words, the bars of this plottable will appear above the bars of bars. The move target bars must use the same key and value axis as this plottable.

Inserting into and removing from existing bar stacking is handled gracefully. If bars already has a bars object above itself, this bars object is inserted between the two. If this bars object is already between two other bars, the two other bars will be stacked on top of each other after the operation.

To remove this bars plottable from any stacking, set bars to `nullptr`.

See also
  moveBelow, barBelow, barAbove

## § selectTestRect()

QCPDataSelection QCPBars::selectTestRect | ( | const QRectF & | rect,
|  | bool | onlySelectable
| ) |  | const
| virtual

Returns a data selection containing all the data points of this plottable which are contained (or hit by) rect. This is used mainly in the selection rect interaction for data selection (data selection mechanism).

If onlySelectable is true, an empty QCPDataSelection is returned if this plottable is not selectable (i.e. if QCPAbstractPlottable::setSelectable is QCP::stNone).

Note
  rect must be a normalized rect (positive or zero width and height). This is especially important when using the rect of QCPSelectionRect::accepted, which is not necessarily normalized. Use `QRect::normalized()` when passing a rect which might not be normalized.

Reimplemented from QCPAbstractPlottable1D< QCPBarsData >.

## § selectTest()

double QCPBars::selectTest | ( | const QPointF & | pos,
|  | bool | onlySelectable,
|  | QVariant * | details = `nullptr`
| ) |  | const
| virtual

Implements a selectTest specific to this plottable's point geometry.

If details is not 0, it will be set to a QCPDataSelection, describing the closest data point to pos.

For general information about this virtual method, see the base class implementation. QCPAbstractPlottable::selectTest

Reimplemented from QCPAbstractPlottable1D< QCPBarsData >.

## § getKeyRange()

QCPRange QCPBars::getKeyRange | ( | bool & | foundRange,
|  | QCP::SignDomain | inSignDomain = `QCP::sdBoth`
| ) |  | const
| virtual

Returns the coordinate range that all data in this plottable span in the key axis dimension. For logarithmic plots, one can set inSignDomain to either QCP::sdNegative or QCP::sdPositive in order to restrict the returned range to that sign domain. E.g. when only negative range is wanted, set inSignDomain to QCP::sdNegative and all positive points will be ignored for range calculation. For no restriction, just set inSignDomain to QCP::sdBoth (default). foundRange is an output parameter that indicates whether a range could be found or not. If this is false, you shouldn't use the returned range (e.g. no points in data).

Note that foundRange is not the same as QCPRange::validRange, since the range returned by this function may have size zero (e.g. when there is only one data point). In this case foundRange would return true, but the returned range is not a valid range in terms of QCPRange::validRange.

See also
  rescaleAxes, getValueRange

Implements QCPAbstractPlottable.

## § getValueRange()

QCPRange QCPBars::getValueRange | ( | bool & | foundRange,
|  | QCP::SignDomain | inSignDomain = `QCP::sdBoth`,
|  | const QCPRange & | inKeyRange = `QCPRange()`
| ) |  | const
| virtual

Returns the coordinate range that the data points in the specified key range (inKeyRange) span in the value axis dimension. For logarithmic plots, one can set inSignDomain to either QCP::sdNegative or QCP::sdPositive in order to restrict the returned range to that sign domain. E.g. when only negative range is wanted, set inSignDomain to QCP::sdNegative and all positive points will be ignored for range calculation. For no restriction, just set inSignDomain to QCP::sdBoth (default). foundRange is an output parameter that indicates whether a range could be found or not. If this is false, you shouldn't use the returned range (e.g. no points in data).

If inKeyRange has both lower and upper bound set to zero (is equal to `QCPRange()`), all data points are considered, without any restriction on the keys.

Note that foundRange is not the same as QCPRange::validRange, since the range returned by this function may have size zero (e.g. when there is only one data point). In this case foundRange would return true, but the returned range is not a valid range in terms of QCPRange::validRange.

See also
  rescaleAxes, getKeyRange

Implements QCPAbstractPlottable.

## § dataPixelPosition()

QPointF QCPBars::dataPixelPosition | ( | int | index | ) | const
| virtual

Returns the pixel position on the widget surface at which the data point at the given index appears.

Usually this corresponds to the point of dataMainKey/dataMainValue, in pixel coordinates. However, depending on the plottable, this might be a different apparent position than just a coord-to-pixel transform of those values. For example, QCPBars apparent data values can be shifted depending on their stacking, bar grouping or configured base value.

Reimplemented from QCPAbstractPlottable1D< QCPBarsData >.

## § draw()

void QCPBars::draw | ( | QCPPainter * | painter | ) |
| protected virtual

This function draws the layerable with the specified painter. It is only called by QCustomPlot, if the layerable is visible (setVisible).

Before this function is called, the painter's antialiasing state is set via applyDefaultAntialiasingHint, see the documentation there. Further, the clipping rectangle was set to clipRect.

Implements QCPAbstractPlottable.

## § drawLegendIcon()

void QCPBars::drawLegendIcon | ( | QCPPainter * | painter,
|  | const QRectF & | rect
| ) |  | const
| protected virtual

called by QCPLegend::draw (via QCPPlottableLegendItem::draw) to create a graphical representation of this plottable inside rect, next to the plottable name.

The passed painter has its cliprect set to rect, so painting outside of rect won't appear outside the legend icon border.

Implements QCPAbstractPlottable.

## § getVisibleDataBounds()

void QCPBars::getVisibleDataBounds | ( | QCPBarsDataContainer::const_iterator & | begin,
|  | QCPBarsDataContainer::const_iterator & | end
| ) |  | const
| protected

called by draw to determine which data (key) range is visible at the current key axis range setting, so only that needs to be processed. It also takes into account the bar width.

begin returns an iterator to the lowest data point that needs to be taken into account when plotting. Note that in order to get a clean plot all the way to the edge of the axis rect, lower may still be just outside the visible range.

end returns an iterator one higher than the highest visible data point. Same as before, end may also lie just outside of the visible range.

if the plottable contains no data, both begin and end point to constEnd.

## § getBarRect()

QRectF QCPBars::getBarRect | ( | double | key,
|  | double | value
| ) |  | const
| protected

Returns the rect in pixel coordinates of a single bar with the specified key and value. The rect is shifted according to the bar stacking (see moveAbove) and base value (see setBaseValue), and to have non-overlapping border lines with the bars stacked below.

## § getPixelWidth()

void QCPBars::getPixelWidth | ( | double | key,
|  | double & | lower,
|  | double & | upper
| ) |  | const
| protected

This function is used to determine the width of the bar at coordinate key, according to the specified width (setWidth) and width type (setWidthType).

The output parameters lower and upper return the number of pixels the bar extends to lower and higher keys, relative to the key coordinate (so with a non-reversed horizontal axis, lower is negative and upper positive).

## § getStackedBaseValue()

double QCPBars::getStackedBaseValue | ( | double | key,
|  | bool | positive
| ) |  | const
| protected

This function is called to find at which value to start drawing the base of a bar at key, when it is stacked on top of another QCPBars (e.g. with moveAbove).

positive and negative bars are separated per stack (positive are stacked above baseValue upwards, negative are stacked below baseValue downwards). This can be indicated with positive. So if the bar for which we need the base value is negative, set positive to false.

## § connectBars()

void QCPBars::connectBars | ( | QCPBars * | lower,
|  | QCPBars * | upper
| ) |  |
| static protected

Connects below and above to each other via their mBarAbove/mBarBelow properties. The bar(s) currently above lower and below upper will become disconnected to lower/upper.

If lower is zero, upper will be disconnected at the bottom. If upper is zero, lower will be disconnected at the top.

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

- src/plottables/plottable-bars.h
- src/plottables/plottable-bars.cpp