13 #include "ui_AnglesCustomPlot.h"
16 const QString& xLabel,
23 setProperty(
"TypeOfCustomPlot",
"AnglesCustomPlot");
27 setWindowTitle(m_label);
33 m_bars->rescaleAxes();
42 double minData = *std::min_element(data.begin(), data.end());
43 double maxData = *std::max_element(data.begin(), data.end());
44 double range = maxData - minData;
46 if (range == 0 || data.size() == 0) {
50 unsigned count = data.size();
51 unsigned numberOfBins;
54 numberOfBins =
static_cast<unsigned>(
56 numberOfBins = std::max<unsigned>(
60 numberOfBins = m_nbBins;
63 m_axis.resize(numberOfBins);
64 for (
unsigned i = 0; i < numberOfBins; i++) {
65 m_axis[i] = minData + i * range / numberOfBins;
70 m_histogram.resize(numberOfBins);
71 }
catch (
const std::bad_alloc&) {
72 CVLog::Warning(
"[computeHistogram] Failed to allocate histogram!");
75 std::fill(m_histogram.begin(), m_histogram.end(), 0);
78 ScalarType step =
static_cast<ScalarType
>(numberOfBins) / range;
79 for (
unsigned i = 0; i <
count; ++i) {
80 const ScalarType& val = data[i];
83 unsigned bin =
static_cast<unsigned>((val - minData) * step);
84 ++m_histogram[
std::min(bin, numberOfBins - 1)];
95 this->removePlottable(m_bars);
99 m_bars =
new QCPBars(xAxis, yAxis);
100 m_bars->setAntialiased(
102 m_bars->setPen(QPen(QColor(0, 168, 140).
lighter(130)));
103 m_bars->setBrush(QColor(0, 168, 140));
104 m_bars->setWidth(m_axis[1] - m_axis[0]);
105 m_bars->setData(m_axis, m_histogram);
106 xAxis->setLabel(xLabel);
107 yAxis->setLabel(
"Counts");
108 setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
114 m_bars->rescaleAxes();
120 if (
event->button() == Qt::LeftButton) {
123 QCustomPlot::mouseDoubleClickEvent(
event);
127 if (
event->button() == Qt::RightButton) {
128 QMenu* menu =
new QMenu(
this);
129 QAction* action =
new QAction(
"Close tab");
130 menu->addAction(action);
131 connect(action, &QAction::triggered,
this,
133 menu->popup(
event->globalPos());
135 QCustomPlot::mousePressEvent(
event);
static const unsigned MAX_HISTOGRAM_SIZE
Default number of classes for associated histogram.
void mousePressEvent(QMouseEvent *event) override
bool computeHistogram(const QVector< double > &data)
AnglesCustomPlot(const QVector< double > &data, const QString &xLabel, int nbBins, QWidget *parent=nullptr)
void mouseDoubleClickEvent(QMouseEvent *event) override
void createQCPBars(const QString &xLabel)
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
static bool ValidValue(ScalarType value)
Returns whether a scalar value is valid or not.
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
constexpr Rgbaf lighter(0.83f, 0.83f, 0.83f, 1.00f)