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