-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphworker.cpp
229 lines (193 loc) · 6.38 KB
/
graphworker.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "graphworker.h"
#include <QDebug>
QLabel *GraphWorker::label = nullptr;
template<class Key, class Value>
GraphData<Key, Value>::GraphData()
{
}
template<class Key, class Value>
QVector<Key> &GraphData<Key, Value>::keys()
{
return xScale;
}
template<class Key, class Value>
QVector<Value> &GraphData<Key, Value>::values()
{
return yScale;
}
template<class Key, class Value>
edge<Key> GraphData<Key, Value>::getKeyEdge()
{
return xEdge;
}
template<class Key, class Value>
edge<Value> GraphData<Key, Value>::getValueEdge()
{
return yEdge;
}
template<class Key, class Value>
void GraphData<Key, Value>::scroll(int angleDelta)
{
if (xScale.isEmpty()) return;
if (xScale.last() < defaultWidth) return;
displayWidth += angleDelta / 6; // angleDelta = +-120
int maxWidth = xScale.last() - xScale.first();
displayWidth = displayWidth > maxWidth ? maxWidth : displayWidth;
displayWidth = displayWidth < defaultWidth ? defaultWidth : displayWidth;
}
template<class Key, class Value>
void GraphData<Key, Value>::autoScroll()
{
displayWidth = defaultWidth;
}
template<class Key, class Value>
void GraphData<Key, Value>::appendPoint(const Key &key, const Value &value)
{
if (yScale.isEmpty()) {
yEdge.min = value;
yEdge.max = value;
minPos = 0;
maxPos = 0;
borderPos = 0;
}
xScale.append(key);
yScale.append(value);
// calc range of Y scale
if (value < yEdge.min) {
yEdge.min = value;
minPos = xScale.size() - 1;
}
if (value > yEdge.max) {
yEdge.max = value;
maxPos = xScale.size() - 1;
}
// calc range of X scale
if ((xScale.last() - xScale.at(borderPos)) < displayWidth) { // если надо расширить график
for ( ; borderPos >= 0; borderPos--) {
if ((xScale.last() - xScale.first()) < defaultWidth) break;
if (yScale.at(borderPos) < yEdge.min) {
yEdge.min = yScale.at(borderPos);
minPos = borderPos;
}
if (yScale.at(borderPos) > yEdge.max) {
yEdge.max = yScale.at(borderPos);
maxPos = borderPos;
}
if ((xScale.last() - xScale.at(borderPos)) >= displayWidth) break;
}
}
else { // сжать график
for ( ; borderPos < xScale.size(); borderPos++) {
if ((xScale.last() - xScale.at(borderPos)) <= displayWidth) break;
}
if (borderPos > minPos) { // минимальная точка ушла в небытие
yEdge.min = yScale.at(borderPos);
minPos = borderPos;
for (int i = borderPos; i < yScale.size(); i++) {
if (yScale.at(i) <= yEdge.min) {
yEdge.min = yScale.at(i);
minPos = i;
}
}
}
if (borderPos > maxPos) { // максимальная точка ушла в небытие
yEdge.max = yScale.at(borderPos);
maxPos = borderPos;
for (int i = borderPos; i < yScale.size(); i++) {
if (yScale.at(i) >= yEdge.max) {
yEdge.max = yScale.at(i);
maxPos = i;
}
}
}
}
xEdge.min = xScale.at(borderPos);
xEdge.max = xScale.last();
if ((xScale.last() - xScale.first()) > lastStorageTime) {
xScale.pop_front();
minPos--;
maxPos--;
}
}
template<class Key, class Value>
GraphData<Key, Value>::~GraphData()
{
}
GraphWorker::GraphWorker(QWidget *parent) : QCustomPlot(parent)
{
connect(this, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
connect(this, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(scroll(QWheelEvent*)));
this->addGraph();
this->xAxis->setLabel("Время, [ч:мин:с]");
this->yAxis->setLabel("Значение");
// настройка показа времени на шкале
dateTicker->setDateTimeSpec(Qt::UTC);
dateTicker->setDateTimeFormat("H:mm:ss");
this->xAxis->setTicker(dateTicker);
QAction *autoscale = new QAction("autoscale", this);
connect(autoscale, static_cast<void (QAction::*)(bool)>(&QAction::triggered), this, [this](){
data_.autoScroll();
});
menu.addAction(autoscale);
connect(this, static_cast<void (QCustomPlot::*)(QMouseEvent*)>(&QCustomPlot::mousePress), this, [this](QMouseEvent* event){
if (event->button() == Qt::RightButton){
menu.exec(event->globalPos());
}
});
QPen pen = this->graph(0)->pen();
pen.setWidth(1);
this->graph(0)->setPen(pen);
timer.start();
}
GraphWorker::~GraphWorker()
{
}
void GraphWorker::addLabel(QLabel *lbl)
{
label = lbl;
}
void GraphWorker::setData(float data)
{
float delta_t = (float)timer.restart();
if (delta_t > 1000.0f) {
delta_t = 0.0f;
}
delta_t /= 1000; // ms to sec
if (data_.keys().isEmpty()) {
data_.appendPoint(delta_t, data);
}
else {
data_.appendPoint(data_.keys().last() + delta_t, data);
}
this->graph(0)->setData(data_.keys(), data_.values());
step++;
if (step > replotInterval) {
step = 0;
// set ranges
this->xAxis->setRange(data_.getKeyEdge().min, data_.getKeyEdge().max);
if (data_.getValueEdge().min == 0.0 && data_.getValueEdge().max == 0.0) {
this->yAxis->setRange(-1.0, 1.0);
}
else if (data_.getValueEdge().bias() == 0.0) {
this->yAxis->setRange(data_.getValueEdge().min - 0.05 * fabs(data_.getValueEdge().min),
data_.getValueEdge().max + 0.05 * fabs(data_.getValueEdge().max));
}
else {
this->yAxis->setRange(data_.getValueEdge().min - data_.getValueEdge().bias() * 0.05,
data_.getValueEdge().max + data_.getValueEdge().bias() * 0.05);
}
this->replot();
}
}
void GraphWorker::mouseMove(QMouseEvent *event)
{
if (label != nullptr) {
double x = this->xAxis->pixelToCoord(event->pos().x());
double y = this->yAxis->pixelToCoord(event->pos().y());
label->setText(QString("X: %1 [ч:мин:с],\tY: %2").arg(QDateTime::fromTime_t(x).toUTC().toString("H:mm:ss")).arg(QString::number(y, 'f', 2)));
}
}
void GraphWorker::scroll(QWheelEvent *event)
{
data_.scroll(event->angleDelta().ry());
}