v2.0.0
Loading...
Searching...
No Matches
plot.cpp
Go to the documentation of this file.
1//=============================================================================================================
14
15//=============================================================================================================
16// INCLUDES
17//=============================================================================================================
18
19#include "plot.h"
20
21//=============================================================================================================
22// QT INCLUDES
23//=============================================================================================================
24
25#include <QPoint>
26#include <QPainter>
27
28//=============================================================================================================
29// USED NAMESPACES
30//=============================================================================================================
31
32using namespace DISPLIB;
33using namespace Eigen;
34
35//=============================================================================================================
36// DEFINE MEMBER METHODS
37//=============================================================================================================
38
39Plot::Plot(QWidget* parent)
40: Graph(parent)
41, m_bHoldOn(false)
42{
43 init();
44}
45
46//=============================================================================================================
47
48Plot::Plot(VectorXd& p_dVec, QWidget* parent)
49: Graph(parent)
50, m_bHoldOn(false)
51{
52 init();
53 updateData(p_dVec);
54}
55
56//=============================================================================================================
57
59{
60}
61
62//=============================================================================================================
63
65{
66 //Parent init
68
70
71 m_dMinX = 0;
72 m_dMaxX = 0;
73 m_dMinY = 0;
74 m_dMaxY = 0;
75}
76
77//=============================================================================================================
78
79void Plot::updateData(VectorXd& p_dVec)
80{
81 if (p_dVec.size() > 0) {
82 if (!m_bHoldOn) {
83 // Replace the curves; title and axis labels stay
85 m_dMinX = 0;
86 m_dMaxX = 0;
87 m_dMinY = 0;
88 m_dMaxY = 0;
89 }
90
91 QVector<QPointF> t_qVecPointFPaths;
92 //No X data given
93 m_dMinX = 0 < m_dMinX ? 0 : m_dMinX;
94 m_dMaxX = p_dVec.size() - 1 > m_dMaxX ? p_dVec.size() - 1 : m_dMaxX;
95
96 m_dMinY = p_dVec.minCoeff() < m_dMinY ? p_dVec.minCoeff() : m_dMinY;
97 m_dMaxY = p_dVec.maxCoeff() > m_dMaxY ? p_dVec.maxCoeff() : m_dMaxY;
98
99 double t_dX = 0;
100 for (qint32 i = 0; i < p_dVec.size(); ++i) {
101 t_qVecPointFPaths.append(QPointF(t_dX, p_dVec[i]));
102 t_dX += 1;
103 }
104
105 m_qListVecPointFPaths.append(t_qVecPointFPaths);
106
107 update();
108 }
109}
110
111//=============================================================================================================
112
113void Plot::paintEvent(QPaintEvent* event)
114{
115 Q_UNUSED(event);
116
117 //
118 // The painter used for the drawing below is constructed inside the block.
119 // A second one was also being constructed here and never used, which means
120 // two QPainters were active on this widget at once - Qt only allows one.
121 //
122 if (m_qListVecPointFPaths.size() > 0) {
123 QPoint t_qPointTopLeft(m_iBorderLeftRight, m_iBorderTopBottom);
124
125 QSize t_qSizePlot = m_qSizeWidget;
126
127 t_qSizePlot.setWidth(t_qSizePlot.width() - 2 * m_iBorderLeftRight);
128 t_qSizePlot.setHeight(t_qSizePlot.height() - 2 * m_iBorderTopBottom);
129
130 //Draw background
131 QPainter painter(this);
132 painter.fillRect(t_qPointTopLeft.x(), t_qPointTopLeft.y(), t_qSizePlot.width(), t_qSizePlot.height(), Qt::white);
133
134 //Draw border
135 painter.drawRect(t_qPointTopLeft.x() - 1, t_qPointTopLeft.y() - 1, t_qSizePlot.width() + 1, t_qSizePlot.height() + 1);
136
137 // -- Data --: x from min to max over the width, y upwards over 90 % of the height
138 QPen pen;
139 pen.setWidth(1);
140 pen.setBrush(Qt::blue);
141 painter.setPen(pen);
142 const double rangeX = m_dMaxX > m_dMinX ? m_dMaxX - m_dMinX : 1.0;
143 const double rangeY = m_dMaxY > m_dMinY ? m_dMaxY - m_dMinY : 1.0;
144 const double scale_x = t_qSizePlot.width() / rangeX;
145 const double scale_y = 0.9 * t_qSizePlot.height() / rangeY;
146 const double bottom = t_qPointTopLeft.y() + 0.95 * t_qSizePlot.height();
147 for (const QVector<QPointF>& path : std::as_const(m_qListVecPointFPaths)) {
148 for (qsizetype k = 1; k < path.size(); ++k) {
149 painter.drawLine(QPointF(t_qPointTopLeft.x() + (path[k - 1].x() - m_dMinX) * scale_x, bottom - (path[k - 1].y() - m_dMinY) * scale_y),
150 QPointF(t_qPointTopLeft.x() + (path[k].x() - m_dMinX) * scale_x, bottom - (path[k].y() - m_dMinY) * scale_y));
151 }
152 }
153 painter.end();
154
155 //Draw title & axes
156 Graph::drawLabels(t_qSizePlot.width(), t_qSizePlot.height());
157 }
158}
MATLAB-style line plot for an Eigen vector, built on DISPLIB::Graph.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
Common base widget providing title, axis labels and resize handling for DISPLIB's QPainter plots.
Definition graph.h:73
Graph(QWidget *parent=0)
Definition graph.cpp:43
void init()
Definition graph.cpp:51
QSize m_qSizeWidget
Definition graph.h:138
qint32 m_iBorderLeftRight
Definition graph.h:145
qint32 m_iBorderTopBottom
Definition graph.h:144
void drawLabels(qint32 p_iContentWidth, qint32 p_iContentHeight)
Definition graph.cpp:99
double m_dMaxY
Definition plot.h:129
double m_dMaxX
Definition plot.h:127
double m_dMinX
Definition plot.h:126
void init()
Definition plot.cpp:64
QList< QVector< QPointF > > m_qListVecPointFPaths
Definition plot.h:123
Plot(QWidget *parent=nullptr)
Definition plot.cpp:39
void updateData(Eigen::VectorXd &p_dVec)
Definition plot.cpp:79
void paintEvent(QPaintEvent *event)
Definition plot.cpp:113
double m_dMinY
Definition plot.h:128
bool m_bHoldOn
Definition plot.h:125