v2.0.0
Loading...
Searching...
No Matches
bar.cpp
Go to the documentation of this file.
1//=============================================================================================================
20
21//=============================================================================================================
22// INCLUDES
23//=============================================================================================================
24
25#include "bar.h"
26
27//=============================================================================================================
28// QT INCLUDES
29//=============================================================================================================
30
31#include <QPainter>
32#include <QPaintEvent>
33
34//=============================================================================================================
35// EIGEN INCLUDES
36//=============================================================================================================
37
38//=============================================================================================================
39// USED NAMESPACES
40//=============================================================================================================
41
42using namespace DISPLIB;
43
44//=============================================================================================================
45// DEFINE MEMBER METHODS
46//=============================================================================================================
47
48Bar::Bar(const QString& title,
49 QWidget* parent)
50: QWidget(parent)
51, m_sTitle(title)
52, m_iMaxFrequency(0)
53{
54 setMinimumSize(200, 150);
55 setBackgroundRole(QPalette::Base);
56 setAutoFillBackground(true);
57}
58
59//=============================================================================================================
60
61void Bar::paintEvent(QPaintEvent * /*event*/)
62{
63 QPainter painter(this);
64 painter.setRenderHint(QPainter::Antialiasing);
65
66 const int w = width();
67 const int h = height();
68 const int marginLeft = 60;
69 const int marginRight = 20;
70 const int marginTop = 40;
71 const int marginBottom = 100;
72 const int plotW = w - marginLeft - marginRight;
73 const int plotH = h - marginTop - marginBottom;
74
75 if (plotW <= 0 || plotH <= 0)
76 return;
77
78 // Title
79 if (!m_sTitle.isEmpty()) {
80 painter.setPen(Qt::black);
81 QFont titleFont = painter.font();
82 titleFont.setBold(true);
83 titleFont.setPointSize(titleFont.pointSize() + 2);
84 painter.setFont(titleFont);
85 painter.drawText(QRect(0, 0, w, marginTop), Qt::AlignCenter, m_sTitle);
86 painter.setFont(QFont());
87 }
88
89 if (m_frequencies.isEmpty())
90 return;
91
92 const int nBars = m_frequencies.size();
93 const int maxFreq = (m_iMaxFrequency > 0) ? m_iMaxFrequency : 1;
94
95 // Draw axes
96 painter.setPen(QPen(Qt::black, 1));
97 painter.drawLine(marginLeft, marginTop, marginLeft, marginTop + plotH);
98 painter.drawLine(marginLeft, marginTop + plotH, marginLeft + plotW, marginTop + plotH);
99
100 // Y-axis ticks
101 const int nYTicks = 5;
102 for (int i = 0; i <= nYTicks; ++i) {
103 int yVal = maxFreq * i / nYTicks;
104 int y = marginTop + plotH - (plotH * i / nYTicks);
105 painter.drawLine(marginLeft - 5, y, marginLeft, y);
106 painter.drawText(QRect(0, y - 10, marginLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter, QString::number(yVal));
107 }
108
109 // Draw bars
110 double barWidth = static_cast<double>(plotW) / nBars;
111 double barPadding = barWidth * 0.1;
112
113 for (int i = 0; i < nBars; ++i) {
114 double barH = (static_cast<double>(m_frequencies[i]) / maxFreq) * plotH;
115 double x = marginLeft + i * barWidth + barPadding;
116 double bw = barWidth - 2.0 * barPadding;
117 double y = marginTop + plotH - barH;
118
119 painter.setBrush(QColor(70, 130, 180));
120 painter.setPen(QPen(QColor(50, 100, 150), 1));
121 painter.drawRect(QRectF(x, y, bw, barH));
122 }
123
124 // X-axis labels (rotated)
125 painter.setPen(Qt::black);
126 QFont smallFont = painter.font();
127 smallFont.setPointSize(smallFont.pointSize() - 1);
128 painter.setFont(smallFont);
129
130 for (int i = 0; i < nBars && i < m_categories.size(); ++i) {
131 double cx = marginLeft + (i + 0.5) * barWidth;
132 painter.save();
133 painter.translate(cx, marginTop + plotH + 5);
134 painter.rotate(45);
135 painter.drawText(0, 0, m_categories[i]);
136 painter.restore();
137 }
138
139 // Legend
140 if (!m_sLegend.isEmpty()) {
141 painter.setFont(QFont());
142 painter.setPen(Qt::black);
143 painter.drawText(QRect(0, h - 20, w, 20), Qt::AlignCenter, m_sLegend);
144 }
145}
QPainter-based bar / histogram widget for Eigen class-limit + frequency vectors.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
void paintEvent(QPaintEvent *event) override
Definition bar.cpp:61
Bar(const QString &title="", QWidget *parent=nullptr)
Definition bar.cpp:48