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 {
83 if(!m_bHoldOn)
84 init();
85
86 QVector<QPointF> t_qVecPointFPaths;
87 //No X data given
88 m_dMinX = 0 < m_dMinX ? 0 : m_dMinX;
89 m_dMaxX = p_dVec.size()-1 > m_dMaxX ? p_dVec.size()-1 : m_dMaxX;
90
91 m_dMinY = p_dVec.minCoeff() < m_dMinY ? p_dVec.minCoeff() : m_dMinY;
92 m_dMaxY = p_dVec.maxCoeff() > m_dMaxY ? p_dVec.maxCoeff() : m_dMaxY;
93
94 double t_dX = 0;
95 for(qint32 i = 0; i < p_dVec.size(); ++i)
96 {
97 t_qVecPointFPaths.append(QPointF(t_dX, p_dVec[i]));
98 t_dX += 1;
99 }
100
101 m_qListVecPointFPaths.append(t_qVecPointFPaths);
102
103 update();
104 }
105}
106
107//=============================================================================================================
108
109void Plot::paintEvent(QPaintEvent *event)
110{
111 Q_UNUSED(event);
112
113 QPainter painter(this);
114 if (m_qListVecPointFPaths.size() > 0)
115 {
116 QPoint t_qPointTopLeft(m_iBorderLeftRight,m_iBorderTopBottom);
117
118 QSize t_qSizePlot = m_qSizeWidget;
119
120 t_qSizePlot.setWidth(t_qSizePlot.width() - 2*m_iBorderLeftRight);
121 t_qSizePlot.setHeight(t_qSizePlot.height() - 2*m_iBorderTopBottom);
122
123 //Draw background
124 QPainter painter(this);
125 painter.fillRect(t_qPointTopLeft.x(), t_qPointTopLeft.y(), t_qSizePlot.width(), t_qSizePlot.height(), Qt::white);
126
127 //Draw border
128 painter.drawRect(t_qPointTopLeft.x()-1, t_qPointTopLeft.y()-1, t_qSizePlot.width()+1, t_qSizePlot.height()+1);
129
130 // -- Data --
131 painter.save();
132 QPen pen;
133 pen.setWidth(1);
134 pen.setBrush(Qt::blue);
135 painter.setPen(pen);
136 painter.translate(m_iBorderLeftRight-m_dMinX,m_iBorderTopBottom+t_qSizePlot.height()/2);
137 for(qint32 i = 0; i < m_qListVecPointFPaths.size(); ++i)
138 {
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}
158
MATLAB-style line plot for an Eigen vector, built on 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:71
Graph(QWidget *parent=0)
Definition graph.cpp:43
void init()
Definition graph.cpp:51
QSize m_qSizeWidget
Definition graph.h:136
qint32 m_iBorderLeftRight
Definition graph.h:143
qint32 m_iBorderTopBottom
Definition graph.h:142
void drawLabels(qint32 p_iContentWidth, qint32 p_iContentHeight)
Definition graph.cpp:99
double m_dMaxY
Definition plot.h:127
double m_dMaxX
Definition plot.h:125
double m_dMinX
Definition plot.h:124
void init()
Definition plot.cpp:64
QList< QVector< QPointF > > m_qListVecPointFPaths
Definition plot.h:121
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:109
double m_dMinY
Definition plot.h:126
bool m_bHoldOn
Definition plot.h:123