-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynchart.cpp
188 lines (142 loc) · 4.55 KB
/
dynchart.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
#include "dynchart.h"
DynChart::DynChart(QChartView *view, QString title, std::string url, int numPoints, int delay, int numBlocks)
{
this->title = title;
this->url = url;
this->maxBlocks = numBlocks;
this->numPoints = numPoints;
this->delay = delay;
this->numBlocks = numBlocks;
this->view = view;
this->chart = new QChart();
this->series = new QLineSeries();
this->series->setUseOpenGL(true);
qDebug() << "using openGL: " << this->series->useOpenGL();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle(title);
chart->setTitleBrush(QBrush(Qt::white));
chart->setPlotAreaBackgroundVisible(false);
chart->setBackgroundVisible(false);
// clear up old chart from heap after new one is added
// program can crash if the current chart points to a deleted pointer
// and setChart() is called
QChart *old = view->chart();
view->setChart(chart);
delete old;
view->setRenderHint(QPainter::Antialiasing);
this->series->setColor(QColor(62, 96, 193, 255));
chart->axes(Qt::Horizontal).back()->setLabelsBrush(QBrush(QColor(255,255,255,255)));
chart->axes(Qt::Vertical).back()->setLabelsBrush(QBrush(QColor(255,255,255,255)));
qRegisterMetaType<std::vector<std::vector<double>>>("std::vector<std::vector<double>>");
this->api = new GrabApi(url, delay, true, numPoints);
this->api->moveToThread(&this->thread);
connect(this, SIGNAL(startAPI()), this->api, SLOT(start()));
connect(this, SIGNAL(stopAPI()), this->api, SLOT(stop()));
connect(this, SIGNAL(deleteAPI()), this->api, SLOT(Delete()));
connect(this->api, SIGNAL(setPoints(std::vector<std::vector<double>>)),
this, SLOT(setPoints(std::vector<std::vector<double>>)));
connect(this->api, SIGNAL(appendPoints(std::vector<std::vector<double>>)),
this, SLOT(appendPoints(std::vector<std::vector<double>>)));
this->thread.start();
emit startAPI();
}
QString DynChart::getTitle() {
return this->title;
}
std::string DynChart::getURL() {
return this->url;
}
DynChart::~DynChart() {
emit stopAPI();
emit deleteAPI();
thread.quit();
delete this->series;
thread.wait();
}
void DynChart::setPoints(std::vector<std::vector<double>> points) {
this->series->clear();
this->appendPoints(points);
}
void DynChart::appendPoints(std::vector<std::vector<double>> points) {
this->removeDuplicatePoints(&points);
QList<QPointF> add;
for (auto point : points) {
qreal x = qreal(point[0]);
qreal y = qreal(point[1]);
QPointF p(x, y);
add.append(p);
}
this->series->append(add);
this->blockSizes.push_back(points.size());
if (this->blockSizes.size() > (unsigned int)this->maxBlocks) {
int num = this->blockSizes[0];
this->blockSizes.erase(this->blockSizes.begin());
this->removeFirst(num);
}
this->updateAxis();
}
void DynChart::updateAxis() {
bool firstIter = true;
qreal maxX = 0;
qreal minX = 0;
qreal maxY = 0;
qreal minY = 0;
for (auto point : this->series->pointsVector()) {
qreal x = point.x();
qreal y = point.y();
if (firstIter) {
maxX = x;
minX = x;
maxY = y;
minY = y;
firstIter = false;
}
else {
if (x > maxX)
maxX = x;
if (x < minX)
minX = x;
if (y > maxY)
maxY = y;
if (y < minY)
minY = y;
}
}
qreal yRange = maxY - minY;
if (yRange == 0) {
yRange = 5;
}
minY -= yRange/10;
maxY += yRange/10;
this->chart->axes(Qt::Horizontal).back()->setRange(minX, maxX);
this->chart->axes(Qt::Vertical).back()->setRange(minY, maxY);
}
void DynChart::removeDuplicatePoints(std::vector<std::vector<double>> *points) {
auto currPoints = this->series->pointsVector();
if (currPoints.size() == 0)
return;
qreal maxX = currPoints[currPoints.size()-1].x();
int index = 0;
for (auto point : *points) {
qreal x = point[0];
if (x <= maxX)
index ++;
else
break;
}
points->erase(points->begin(), points->begin() + index);
}
void DynChart::removeFirst(qreal numPoints) {
this->series->removePoints(0, numPoints);
}
int DynChart::getNumPoints() {
return this->numPoints;
}
int DynChart::getDelay() {
return this->delay;
}
int DynChart::getNumBlocks() {
return this->numBlocks;
}