-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotwidget.cpp
More file actions
159 lines (124 loc) · 3.69 KB
/
plotwidget.cpp
File metadata and controls
159 lines (124 loc) · 3.69 KB
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
#include "plotwidget.h"
#include "theming.h"
PlotWidget::PlotWidget(QWidget *parent)
: QWidget(parent), _x(-100), yBot(0), yTop(100), runStart(0) {
// Initialize the plot
plot = new QCustomPlot(this);
graph = plot->addGraph();
graph->setLineStyle(QCPGraph::lsStepCenter);
QPen pen;
pen.setWidth(2);
pen.setColor(UiRed);
graph->setPen(pen);
plot->xAxis->setLabel("Time (min)");
plot->yAxis->setLabel("Conc (mM)");
plot->xAxis->setTickLabelFont(QFont("Arial", 12));
plot->yAxis->setTickLabelFont(QFont("Arial", 12));
// Set layout
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(plot);
setLayout(layout);
setYAxis(0,1);
onChange();
}
void PlotWidget::setX(double currX) {
_x = currX;
onChange();
}
double PlotWidget::x() const {
return _x;
}
void PlotWidget::setYAxis(int pac, int pbc) {
//qDebug() << "updating Yaxes " << pac << " "<< pbc;
yBot = std::min(pac, pbc);
yTop = std::max(pac, pbc);
onChange();
}
void PlotWidget::setYlabel(QString label){
plot->yAxis->setLabel(label);
}
void PlotWidget::clearAxes() {
plot->clearGraphs();
graph = plot->addGraph();
graph->setPen(QPen(QColor(222,101, 94)));
plot->xAxis->setLabel("");
plot->yAxis->setLabel("");
plot->replot();
}
void PlotWidget::setStart(double time) {
runStart = time;
}
double PlotWidget::getStart() const {
return runStart;
}
void PlotWidget::setStop() {
runStart = 0;
}
void PlotWidget::setData(QVector<double> xVals, QVector<double> yVals) {
// I was getting weird jagged lines when two consecutive X vals were the same
// This inserts some fuzzy values into X so they aren't the same
QVector<double> adjustedX;
adjustedX.reserve(xVals.size());
double lastX = -std::numeric_limits<double>::infinity();
for (double x : xVals) {
if (x <= lastX) {
// Add a small random epsilon between 0 and 0.001
double epsilon = QRandomGenerator::global()->bounded(0.001);
x = lastX + epsilon;
}
adjustedX.append(x);
lastX = x;
}
xData = adjustedX;
yData = yVals;
onChange();
}
QVector<QVector<double>> PlotWidget::getData()
{
QVector<QVector<double>> result;
result.append(xData);
result.append(yData);
return result;
}
void PlotWidget::appendData(double x, double y) {
xData.append(x);
yData.append(y);
onChange();
}
void PlotWidget::onChange() {
plot->clearItems();
graph->setData(xData, yData);
//qDebug() << "Updating data for plot; min/maxY is " << yBot << yTop;
// Dynamic x/y range setup
double xMin = 0.0, xMax = 10.0;
double yMin = yBot, yMax = yTop;
//if (yTop == yBot == 0)
//{
// if (!yData.isEmpty()) {
// auto yMinMax = std::minmax_element(yData.constBegin(), yData.constEnd());
// yMin = *yMinMax.first;
// yMax = *yMinMax.second;
// }
//}
if (!xData.isEmpty()) {
auto xMinMax = std::minmax_element(xData.constBegin(), xData.constEnd());
xMin = *xMinMax.first;
xMax = *xMinMax.second;
}
// Add padding
double xPadding = (xMax - xMin) * 0.05;
double yPadding = (yMax - yMin) * 0.05;
plot->xAxis->setRange(xMin - xPadding, xMax + xPadding);
plot->yAxis->setRange(yMin - yPadding, yMax + yPadding);
// Draw vertical line if needed
if (_x >= 0) {
QCPItemLine *vLine = new QCPItemLine(plot);
QPen m_pen;
m_pen.setWidth(2);
m_pen.setColor(UiGreen);
vLine->setPen(m_pen);
vLine->start->setCoords(_x, yBot - yPadding);
vLine->end->setCoords(_x, yTop + yPadding);
}
plot->replot();
}