Building histograms (QT)

Building histograms in QT is done using Qwt. The logic of building them is similar to building graphs. (First, I suggest reading this.) Let's consider a simple example of building a histogram using QwtPlotHistogram.

#include "qwt_plot_histogram.h"

#include "qmath.h"
.....
QwtPlotHistogram *hystogram = new QwtPlotHistogram;
QVector<QwtIntervalSample> *intervals = new QVector<QwtIntervalSample>;
float di = 0.1;
for (float i = 0; i <= 10; i += di)
    intervals->append(QwtIntervalSample(qAbs(qSin(i)), i, i + di));
hystogram->setSamples(*intervals);
hystogram->attach(ui->qwtPlot);
ui->qwtPlot->replot();
The arguments QwtIntervalSample(double value, double min, double max) - represent the height of the column, the start, and the end (on the X-axis).

Just like graphs, histograms can be colored:

hystogram->setPen(QPen(QColor(255, 0, 0, 100)));
hystogram->setBrush(QBrush(QColor(255, 0, 0, 255)));
This is what we got: