-
Notifications
You must be signed in to change notification settings - Fork 35
/
SimCenterGraphPlot.cpp
184 lines (140 loc) · 4.11 KB
/
SimCenterGraphPlot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "SimCenterGraphPlot.h"
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpinBox>
#include <qcustomplot.h>
SimCenterGraphPlot::SimCenterGraphPlot(
QString xLabel,
QString yLabel,
QWidget *parent)
: QWidget(parent), numGraphs(0)
{
yMinValue = 0;
yMaxValue = 0;
xMinValue = 0;
xMaxValue = 0;
// create a main layout
QVBoxLayout *mainLayout = new QVBoxLayout();
//
// graphic window
//
thePlot=new QCustomPlot();
thePlot->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
thePlot->setInteractions(QCP::iSelectPlottables);
QRect rec = QGuiApplication::screens()[0]->geometry();
int height = 0.2*rec.height();
int width = 0.5*rec.width();
thePlot->setMinimumWidth(width);
thePlot->setMinimumHeight(height);
mainLayout->addWidget(thePlot);
thePlot->xAxis->setLabel(xLabel);
thePlot->yAxis->setLabel(yLabel);
// thePlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
this->setLayout(mainLayout);
}
SimCenterGraphPlot::SimCenterGraphPlot(
QString xLabel,
QString yLabel,
int minWidth,
int minHeight,
QWidget *parent)
: QWidget(parent), numGraphs(0)
{
yMinValue = 0;
yMaxValue = 0;
xMinValue = 0;
xMaxValue = 0;
// create a main layout
QVBoxLayout *mainLayout = new QVBoxLayout();
//
// graphic window
//
thePlot=new QCustomPlot();
thePlot->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
thePlot->setInteractions(QCP::iSelectPlottables);
thePlot->setMinimumWidth(minWidth);
thePlot->setMinimumHeight(minHeight);
mainLayout->addWidget(thePlot);
thePlot->xAxis->setLabel(xLabel);
thePlot->yAxis->setLabel(yLabel);
// thePlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
this->setLayout(mainLayout);
}
SimCenterGraphPlot::~SimCenterGraphPlot()
{
}
void
SimCenterGraphPlot::clear() {
thePlot->clearGraphs();
thePlot->clearPlottables();
numGraphs=0;
yMinValue = 1e10;
yMaxValue = -1e10;
xMinValue = 1e10;
xMaxValue = -1e10;
thePlot->replot();
}
void
SimCenterGraphPlot::drawPDF(QVector<double> &x, QVector<double> &y) {
addLine(x,y);
//
// add shade
//
thePlot->graph(numGraphs-1)->setBrush(QBrush(QColor(0, 75, 148,20)));
//
// add some margin
//
double ymargin = (yMaxValue - yMinValue)*0.3;
double xmargin = (xMaxValue - xMinValue)*0.05;
thePlot->yAxis->setRange(yMinValue, yMaxValue + ymargin);
thePlot->xAxis->setRange(xMinValue - xmargin, xMaxValue + xmargin);
thePlot->replot();
//
// righthandside/top axis without ticklabels ("box on")
//
thePlot->xAxis2->setVisible(true);
thePlot->xAxis2->setTickLabels(false);
thePlot->yAxis2->setVisible(true);
thePlot->yAxis2->setTickLabels(false);
}
void
SimCenterGraphPlot::addLine(QVector<double> &x, QVector<double> &y, int width, int r, int g, int b) {
//
// check size of vectors
//
int numSteps = y.size();
if (numSteps != x.size()) {
qDebug() << "SimCenterGraphPlot - vectors of differing sizes";
return;
}
//
// plot data
//
graph = thePlot->addGraph();
QPen pen;
pen.setWidth(width);
pen.setColor(QColor(r,g,b));
graph->setPen(pen);
thePlot->graph(numGraphs)->setData(x, y, true);
numGraphs++;
for (int i=0; i<numSteps; i++) {
double yValue = y.at(i);
double xValue = x.at(i);
if (yValue < yMinValue)
yMinValue = yValue;
if (yValue > yMaxValue)
yMaxValue = yValue;
if (xValue < xMinValue)
xMinValue = xValue;
if (xValue > xMaxValue)
xMaxValue = xValue;
}
//
// resize axis if needed
//
thePlot->yAxis->setRange(yMinValue, yMaxValue);
thePlot->xAxis->setRange(xMinValue, xMaxValue);
thePlot->replot();
}