QgsHistogram class

Calculator for a numeric histogram from a list of values.

Constructors, destructors, conversion operators

QgsHistogram() defaulted
Constructor for QgsHistogram.

Public functions

auto binEdges(int bins) const -> QList<double>
Returns a list of edges for the histogram for a specified number of bins.
auto counts(int bins) const -> QList<int>
Returns the calculated list of the counts for the histogram bins.
auto optimalBinWidth() const -> double
Calculates the optimal bin width using the Freedman-Diaconis rule.
auto optimalNumberBins() const -> int
Returns the optimal number of bins for the source values, calculated using the Freedman-Diaconis rule.
void setValues(const QList<double>& values)
Assigns numeric source values for the histogram.
auto setValues(const QgsVectorLayer* layer, const QString& fieldOrExpression, QgsFeedback* feedback = nullptr) -> bool
Assigns numeric source values for the histogram from a vector layer's field or as the result of an expression.

Function documentation

QList<double> QgsHistogram::binEdges(int bins) const

Returns a list of edges for the histogram for a specified number of bins.

Parameters
bins number of bins
Returns list of bin edges

This list will be length bins + 1, as both the first and last value are also included.

QList<int> QgsHistogram::counts(int bins) const

Returns the calculated list of the counts for the histogram bins.

Parameters
bins number of histogram bins
Returns list of histogram counts

double QgsHistogram::optimalBinWidth() const

Calculates the optimal bin width using the Freedman-Diaconis rule.

Returns optimal width for bins

Bins widths are determined by the inter-quartile range of values and the number of values.

int QgsHistogram::optimalNumberBins() const

Returns the optimal number of bins for the source values, calculated using the Freedman-Diaconis rule.

Returns optimal number of bins

The number of bins are determined by the inter-quartile range of values and the number of values.

void QgsHistogram::setValues(const QList<double>& values)

Assigns numeric source values for the histogram.

Parameters
values list of doubles

bool QgsHistogram::setValues(const QgsVectorLayer* layer, const QString& fieldOrExpression, QgsFeedback* feedback = nullptr)

Assigns numeric source values for the histogram from a vector layer's field or as the result of an expression.

Parameters
layer vector layer
fieldOrExpression field name or expression to be evaluated
feedback optional feedback object to allow cancelation of calculation
Returns true if values were successfully set