v2.0.0
Loading...
Searching...
No Matches
lineplot.cpp
Go to the documentation of this file.
1//=============================================================================================================
35
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
40#include "lineplot.h"
41
42//=============================================================================================================
43// QT INCLUDES
44//=============================================================================================================
45
46#include <QPainter>
47#include <QPainterPath>
48#include <QPaintEvent>
49
50//=============================================================================================================
51// EIGEN INCLUDES
52//=============================================================================================================
53
54//=============================================================================================================
55// USED NAMESPACES
56//=============================================================================================================
57
58using namespace DISPLIB;
59using namespace Eigen;
60
61//=============================================================================================================
62// DEFINE MEMBER METHODS
63//=============================================================================================================
64
65LinePlot::LinePlot(QWidget *parent)
66: QWidget(parent)
67, m_dMinX(0)
68, m_dMaxX(0)
69, m_dMinY(0)
70, m_dMaxY(0)
71{
72 setMinimumSize(200, 150);
73 setBackgroundRole(QPalette::Base);
74 setAutoFillBackground(true);
75}
76
77//=============================================================================================================
78
79LinePlot::LinePlot(const QVector<double> &y,
80 const QString& title,
81 QWidget *parent)
82: QWidget(parent)
83, m_sTitle(title)
84, m_dMinX(0)
85, m_dMaxX(0)
86, m_dMinY(0)
87, m_dMaxY(0)
88{
89 setMinimumSize(200, 150);
90 setBackgroundRole(QPalette::Base);
91 setAutoFillBackground(true);
92 updateData(y);
93}
94
95//=============================================================================================================
96
97LinePlot::LinePlot(const QVector<double> &x,
98 const QVector<double> &y,
99 const QString& title,
100 QWidget *parent)
101: QWidget(parent)
102, m_sTitle(title)
103, m_dMinX(0)
104, m_dMaxX(0)
105, m_dMinY(0)
106, m_dMaxY(0)
107{
108 setMinimumSize(200, 150);
109 setBackgroundRole(QPalette::Base);
110 setAutoFillBackground(true);
111 updateData(x, y);
112}
113
114//=============================================================================================================
115
119
120//=============================================================================================================
121
122void LinePlot::setTitle(const QString &p_sTitle)
123{
124 m_sTitle = p_sTitle;
125 QWidget::update();
126}
127
128//=============================================================================================================
129
130void LinePlot::setXLabel(const QString &p_sXLabel)
131{
132 m_sXLabel = p_sXLabel;
133 QWidget::update();
134}
135
136//=============================================================================================================
137
138void LinePlot::setYLabel(const QString &p_sYLabel)
139{
140 m_sYLabel = p_sYLabel;
141 QWidget::update();
142}
143
144//=============================================================================================================
145
146void LinePlot::updateData(const QVector<double> &y)
147{
148 QVector<double> x(y.size());
149 for(int i = 0; i < x.size(); ++i) {
150 x[i] = static_cast<double>(i);
151 }
152 updateData(x, y);
153}
154
155//=============================================================================================================
156
157void LinePlot::updateData(const QVector<double> &x,
158 const QVector<double> &y)
159{
160 m_vecXData = x;
161 m_vecYData = y;
162
163 if (!x.isEmpty()) {
164 m_dMinX = x[0];
165 m_dMaxX = x[0];
166 for (int i = 1; i < x.size(); ++i) {
167 if (x[i] < m_dMinX) m_dMinX = x[i];
168 if (x[i] > m_dMaxX) m_dMaxX = x[i];
169 }
170 }
171
172 if (!y.isEmpty()) {
173 m_dMinY = y[0];
174 m_dMaxY = y[0];
175 for (int i = 1; i < y.size(); ++i) {
176 if (y[i] < m_dMinY) m_dMinY = y[i];
177 if (y[i] > m_dMaxY) m_dMaxY = y[i];
178 }
179 }
180
181 setWindowTitle(m_sTitle);
182 QWidget::update();
183}
184
185//=============================================================================================================
186
187void LinePlot::paintEvent(QPaintEvent * /*event*/)
188{
189 QPainter painter(this);
190 painter.setRenderHint(QPainter::Antialiasing);
191
192 const int w = width();
193 const int h = height();
194 const int mLeft = 60;
195 const int mRight = 20;
196 const int mTop = 35;
197 const int mBottom = 40;
198 const int plotW = w - mLeft - mRight;
199 const int plotH = h - mTop - mBottom;
200
201 if (plotW <= 0 || plotH <= 0)
202 return;
203
204 // Title
205 if (!m_sTitle.isEmpty()) {
206 QFont titleFont = painter.font();
207 titleFont.setBold(true);
208 titleFont.setPointSize(titleFont.pointSize() + 2);
209 painter.setFont(titleFont);
210 painter.setPen(Qt::black);
211 painter.drawText(QRect(0, 0, w, mTop), Qt::AlignCenter, m_sTitle);
212 painter.setFont(QFont());
213 }
214
215 // Draw axes
216 painter.setPen(QPen(Qt::black, 1));
217 painter.drawLine(mLeft, mTop, mLeft, mTop + plotH);
218 painter.drawLine(mLeft, mTop + plotH, mLeft + plotW, mTop + plotH);
219
220 // Y-axis label
221 if (!m_sYLabel.isEmpty()) {
222 painter.save();
223 painter.translate(12, mTop + plotH / 2);
224 painter.rotate(-90);
225 painter.drawText(QRect(-plotH / 2, 0, plotH, 15), Qt::AlignCenter, m_sYLabel);
226 painter.restore();
227 }
228
229 // X-axis label
230 if (!m_sXLabel.isEmpty()) {
231 painter.drawText(QRect(mLeft, h - 15, plotW, 15), Qt::AlignCenter, m_sXLabel);
232 }
233
234 double rangeX = m_dMaxX - m_dMinX;
235 double rangeY = m_dMaxY - m_dMinY;
236 if (rangeX == 0) rangeX = 1.0;
237 if (rangeY == 0) rangeY = 1.0;
238
239 // Y-axis ticks
240 const int nYTicks = 5;
241 for (int i = 0; i <= nYTicks; ++i) {
242 double yVal = m_dMinY + rangeY * i / nYTicks;
243 int y = mTop + plotH - static_cast<int>(plotH * static_cast<double>(i) / nYTicks);
244 painter.drawLine(mLeft - 5, y, mLeft, y);
245 painter.drawText(QRect(0, y - 10, mLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter,
246 QString::number(yVal, 'g', 4));
247 }
248
249 // X-axis ticks
250 const int nXTicks = 5;
251 for (int i = 0; i <= nXTicks; ++i) {
252 double xVal = m_dMinX + rangeX * i / nXTicks;
253 int x = mLeft + static_cast<int>(plotW * static_cast<double>(i) / nXTicks);
254 painter.drawLine(x, mTop + plotH, x, mTop + plotH + 5);
255 painter.drawText(QRect(x - 30, mTop + plotH + 6, 60, 20), Qt::AlignCenter,
256 QString::number(xVal, 'g', 4));
257 }
258
259 if (m_vecXData.isEmpty() || m_vecYData.isEmpty())
260 return;
261
262 // Draw line series
263 QPainterPath path;
264 auto toPixelX = [&](double dx) -> double {
265 return mLeft + ((dx - m_dMinX) / rangeX) * plotW;
266 };
267 auto toPixelY = [&](double dy) -> double {
268 return mTop + plotH - ((dy - m_dMinY) / rangeY) * plotH;
269 };
270
271 int n = qMin(m_vecXData.size(), m_vecYData.size());
272 path.moveTo(toPixelX(m_vecXData[0]), toPixelY(m_vecYData[0]));
273 for (int i = 1; i < n; ++i) {
274 path.lineTo(toPixelX(m_vecXData[i]), toPixelY(m_vecYData[i]));
275 }
276
277 painter.setPen(QPen(QColor(70, 130, 180), 2));
278 painter.setBrush(Qt::NoBrush);
279 painter.drawPath(path);
280}
LinePlot class declaration.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
void updateData(const QVector< double > &y)
Definition lineplot.cpp:146
void paintEvent(QPaintEvent *event) override
Definition lineplot.cpp:187
LinePlot(QWidget *parent=nullptr)
Definition lineplot.cpp:65
void setTitle(const QString &p_sTitle)
Definition lineplot.cpp:122
void setXLabel(const QString &p_sXLabel)
Definition lineplot.cpp:130
void setYLabel(const QString &p_sYLabel)
Definition lineplot.cpp:138
virtual ~LinePlot()
Definition lineplot.cpp:116