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 init();
84
85 QVector<QPointF> t_qVecPointFPaths;
86 //No X data given
87 m_dMinX = 0 < m_dMinX ? 0 : m_dMinX;
88 m_dMaxX = p_dVec.size() - 1 > m_dMaxX ? p_dVec.size() - 1 : m_dMaxX;
89
90 m_dMinY = p_dVec.minCoeff() < m_dMinY ? p_dVec.minCoeff() : m_dMinY;
91 m_dMaxY = p_dVec.maxCoeff() > m_dMaxY ? p_dVec.maxCoeff() : m_dMaxY;
92
93 double t_dX = 0;
94 for (qint32 i = 0; i < p_dVec.size(); ++i) {
95 t_qVecPointFPaths.append(QPointF(t_dX, p_dVec[i]));
96 t_dX += 1;
97 }
98
99 m_qListVecPointFPaths.append(t_qVecPointFPaths);
100
101 update();
102 }
103}
104
105//=============================================================================================================
106
107void Plot::paintEvent(QPaintEvent* event)
108{
109 Q_UNUSED(event);
110
111 //
112 // The painter used for the drawing below is constructed inside the block.
113 // A second one was also being constructed here and never used, which means
114 // two QPainters were active on this widget at once - Qt only allows one.
115 //
116 if (m_qListVecPointFPaths.size() > 0) {
117 QPoint t_qPointTopLeft(m_iBorderLeftRight, m_iBorderTopBottom);
118
119 QSize t_qSizePlot = m_qSizeWidget;
120
121 t_qSizePlot.setWidth(t_qSizePlot.width() - 2 * m_iBorderLeftRight);
122 t_qSizePlot.setHeight(t_qSizePlot.height() - 2 * m_iBorderTopBottom);
123
124 //Draw background
125 QPainter painter(this);
126 painter.fillRect(t_qPointTopLeft.x(), t_qPointTopLeft.y(), t_qSizePlot.width(), t_qSizePlot.height(), Qt::white);
127
128 //Draw border
129 painter.drawRect(t_qPointTopLeft.x() - 1, t_qPointTopLeft.y() - 1, t_qSizePlot.width() + 1, t_qSizePlot.height() + 1);
130
131 // -- Data --
132 painter.save();
133 QPen pen;
134 pen.setWidth(1);
135 pen.setBrush(Qt::blue);
136 painter.setPen(pen);
137 painter.translate(m_iBorderLeftRight - m_dMinX, m_iBorderTopBottom + t_qSizePlot.height() / 2);
138 for (qint32 i = 0; i < m_qListVecPointFPaths.size(); ++i) {
139 double scale_x = t_qSizePlot.width() / (m_dMaxX - m_dMinX);
140 double scale_y = (t_qSizePlot.height() - (t_qSizePlot.height() * 0.1)) / (m_dMaxY - m_dMinY);
141
142 //scale
143 QVector<QPointF> t_qVecPointFPath;
144 QVector<QPointF>::ConstIterator it;
145 for (it = m_qListVecPointFPaths[i].begin(); it != m_qListVecPointFPaths[i].end(); ++it)
146 t_qVecPointFPath.append(QPointF(it->x() * scale_x, it->y() * scale_y));
147
148 //draw
149 for (it = t_qVecPointFPath.begin() + 1; it != t_qVecPointFPath.end(); ++it)
150 painter.drawLine(*(it - 1), *it);
151 }
152 painter.restore();
153
154 //Draw title & axes
155 Graph::drawLabels(t_qSizePlot.width(), t_qSizePlot.height());
156 }
157}
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:107
double m_dMinY
Definition plot.h:128
bool m_bHoldOn
Definition plot.h:125