-
Notifications
You must be signed in to change notification settings - Fork 5
/
linegraphmodel.cpp
386 lines (340 loc) · 11.6 KB
/
linegraphmodel.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "linegraphmodel.h"
#include <cmath>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(lcLineGraphModel, "org.ecloud.charts.model")
const qint64 LineGraphModel::m_timeOffset(LineGraphModel::timeNowMs());
/*!
\qmltype LineGraphModel
\instantiates LineGraphModel
\brief A model to store vertices for LineGraph to render
\inherits QObject
A LineGraphModel stores a time-value series of samples in a form
which is useful both for in-memory storage and later querying,
and for direct rendering: that is, a vector of vertex structs
ready to be sent to the line graph vertex shader.
It's required to append samples in time order, with the oldest first.
*/
LineGraphModel::LineGraphModel(QObject *parent) : QObject(parent)
{
}
double LineGraphModel::triangleArea(
const LineGraphModel::TimeValue &a, const LineGraphModel::TimeValue &b, const LineGraphModel::TimeValue &c)
{
// www.mathopenref.com/coordtrianglearea.html
return qAbs( (a.time * (b.value - c.value) +
b.time * (c.value - a.value) +
c.time * (a.value - b.value)) / 2);
}
LineGraphModel::TimeValue LineGraphModel::largestTriangle(const TimeValue &previous, const QVector<TimeValue> ¤t, const TimeValue &next)
{
if (current.count() == 1)
return current.first();
int chosen = 0;
double chosenArea = triangleArea(previous, current.first(), next);
for (int i = 1; i < current.size(); ++i) {
if (triangleArea(previous, current[i], next) > chosenArea)
chosen = i;
}
return current[chosen];
}
/*!
Returns the average time and value of the given series.
*/
LineGraphModel::TimeValue LineGraphModel::average(const QVector<TimeValue> &tvv)
{
TimeValue ret = {0, 0};
for (const TimeValue &tv : tvv) {
ret.time += tv.time;
ret.value += tv.value;
}
ret.time /= tvv.count();
ret.value /= tvv.count();
return ret;
}
LineGraphModel::TimeValue LineGraphModel::endVertex(int fromLast)
{
int i = (m_vertices.count() / LineNode::verticesPerSample - fromLast) * LineNode::verticesPerSample;
TimeValue ret;
ret.time = m_vertices[i].x /* + m_timeOffset */;
ret.value = m_vertices[i].y;
return ret;
}
/*!
Append the given sample \a value taken at the given \a time,
in seconds since startup.
*/
void LineGraphModel::appendSample(qreal value, qreal time)
{
TimeValue tv { time, value };
// On currentBucket overflow:
// finalize the vertex from m_previousBucket
// current becomes previous
bool modify = !m_currentBucket.isEmpty();
if (m_downsampleMethod == LargestTriangleThreeBuckets) {
if (!m_currentBucket.isEmpty() && time > m_currentBucket.first().time + m_downsampleBucketInterval) {
if (m_vertices.count() > LineNode::verticesPerSample * 2) {
TimeValue previousVtx = largestTriangle(endVertex(2), m_previousBucket, tv);
modifyEndVertices(previousVtx.time, previousVtx.value, 1);
}
m_previousBucket = m_currentBucket;
m_currentBucket.clear();
TimeValue previousVtx = average(m_previousBucket);
modifyEndVertices(previousVtx.time, previousVtx.value);
modify = false;
}
m_currentBucket.append(tv);
// qCDebug(lcLineGraphModel) << "buckets" << m_previousBucket.count() << m_currentBucket.count();
} else {
modify = false;
}
if (modify)
modifyEndVertices(time, value);
else
appendVertices(time, value);
}
/*!
Append the given sample \a value taken at the given \a timestamp,
in milliseconds since the epoch.
*/
void LineGraphModel::appendSampleMs(qreal value, qint64 timestamp)
{
appendSample(value, (timestamp - m_timeOffset) / 1000.0);
}
void LineGraphModel::removeFirstSample()
{
qWarning("poorly tested, maybe wrong"); // removing from the bucket(s) depends on m_downsampleMethod if we even need to do that
m_vertices.remove(0, LineNode::verticesPerSample);
}
/*!
Get the LineVertex instance which is nearest the given \a time,
which is in seconds, beginning with zero from the first sample recorded.
*/
LineNode::LineVertex LineGraphModel::sampleNearest(qreal time)
{
LineNode::LineVertex proto;
proto.x = time;
auto found = std::upper_bound(m_vertices.begin(), m_vertices.end(), proto, [](const auto &lhs, const auto &rhs) {
return lhs.x < rhs.x;
});
if (found == m_vertices.end()) {
proto.x = -1;
proto.y = qQNaN();
return proto;
}
//qDebug() << Q_FUNC_INFO << time << "found" << found->t << found->x << found->y;
return *found;
}
/**
Returns a "nice" number approximately equal to \a range
Rounds the number if \a round = true; takes the ceiling if \a round = false.
From http://stackoverflow.com/questions/8506881/nice-label-algorithm-for-charts-with-minimum-ticks
*/
qreal LineGraphModel::niceNum(qreal range, bool round)
{
qreal exponent; /** exponent of range */
qreal fraction; /** fractional part of range */
qreal niceFraction; /** nice, rounded fraction */
exponent = floor(log10(range));
fraction = range / pow(10.f, exponent);
if (round) {
if (fraction < 1.5)
niceFraction = 1;
else if (fraction < 3)
niceFraction = 2;
else if (fraction < 7)
niceFraction = 5;
else
niceFraction = 10;
} else {
if (fraction <= 1)
niceFraction = 1;
else if (fraction <= 2)
niceFraction = 2;
else if (fraction <= 5)
niceFraction = 5;
else
niceFraction = 10;
}
return niceFraction * pow(10, exponent);
}
void LineGraphModel::autoScale()
{
qreal range = niceNum(m_maxSampleValue - m_minSampleValue, false);
qreal tickSpacing = niceNum(range / (m_maxTicks - 1), true);
setMinValue(floor(m_minSampleValue / tickSpacing) * tickSpacing);
setMaxValue(ceil(m_maxSampleValue / tickSpacing) * tickSpacing);
qCDebug(lcLineGraphModel) << Q_FUNC_INFO << m_minSampleValue << m_maxSampleValue
<< "tickSpacing" << tickSpacing << "autoscaled:" << minValue() << maxValue();
}
void LineGraphModel::setNormalMinValue(qreal normalMinValue)
{
if (m_normalMinValue == normalMinValue)
return;
m_normalMinValue = normalMinValue;
emit normalMinValueChanged();
}
void LineGraphModel::setNormalMaxValue(qreal normalMaxValue)
{
if (m_normalMaxValue == normalMaxValue)
return;
m_normalMaxValue = normalMaxValue;
emit normalMaxValueChanged();
}
void LineGraphModel::setLabel(QString label)
{
if (m_label == label)
return;
m_label = label;
emit labelChanged();
}
void LineGraphModel::setUnit(QString unit)
{
if (m_unit == unit)
return;
m_unit = unit;
emit unitChanged();
}
void LineGraphModel::setTimeSpan(int timeSpan)
{
if (m_timeSpan == timeSpan)
return;
m_timeSpan = timeSpan;
emit timeSpanChanged();
}
void LineGraphModel::setDownsampleInterval(qreal downsampleInterval)
{
if (m_downsampleBucketInterval == downsampleInterval)
return;
m_downsampleBucketInterval = downsampleInterval;
emit downsampleIntervalChanged();
}
void LineGraphModel::setDownsampleMethod(LineGraphModel::DownsampleMethod downsampleMethod)
{
if (m_downsampleMethod == downsampleMethod)
return;
m_downsampleMethod = downsampleMethod;
emit downsampleMethodChanged();
}
qreal LineGraphModel::currentValue() const
{
if (m_vertices.length())
return m_vertices.last().y;
else
return 0;
}
void LineGraphModel::setMinValue(qreal minValue)
{
if (m_minValue == minValue)
return;
m_minValue = minValue;
emit minValueChanged();
}
void LineGraphModel::setMaxValue(qreal maxValue)
{
if (m_maxValue == maxValue)
return;
m_maxValue = maxValue;
emit maxValueChanged();
}
void LineGraphModel::setClipValues(bool clipValues)
{
if (m_clipValues == clipValues)
return;
m_clipValues = clipValues;
emit clipValuesChanged();
}
const QVector<LineNode::LineVertex> *LineGraphModel::vertices()
{
return &m_vertices;
}
/*!
Append vertices representing the given sample \a value
taken at the given \a time, in seconds since startup.
*/
void LineGraphModel::appendVertices(qreal time, qreal value)
{
// qDebug() << m_label << time << value << "already have samples:" << m_vertices.size();
if (m_clipValues) {
if (value < m_minValue)
value = m_minValue;
if (value > m_maxValue)
value = m_maxValue;
}
value *= m_multiplier;
finagle(time, value);
if (value > m_maxSampleValue) {
m_maxSampleValue = value;
emit maxSampleValueChanged();
}
if (value < m_minSampleValue) {
m_minSampleValue = value;
emit minSampleValueChanged();
}
int i = m_vertices.size() - LineNode::verticesPerSample;
if (i >= 0) {
m_vertices[i].nextX = time;
m_vertices[i++].nextY = value;
m_vertices[i].nextX = time;
m_vertices[i++].nextY = value;
m_vertices[i].nextX = time;
m_vertices[i++].nextY = value;
m_vertices[i].nextX = time;
m_vertices[i++].nextY = value;
}
i = m_vertices.size();
m_vertices.resize(i + LineNode::verticesPerSample);
float tp = i > 0 ? m_vertices[i - 1].x : time;
float samplePrev = i > 0 ? m_vertices[i - 1].y : value;
m_vertices[i++].set(0, -1, time, value, tp, samplePrev, time + 0.01, value);
m_vertices[i++].set(1, 1, time, value, tp, samplePrev, time + 0.01, value);
m_vertices[i++].set(2, -1, time, value, tp, samplePrev, time + 0.01, value);
m_vertices[i++].set(3, 1, time, value, tp, samplePrev, time + 0.01, value);
Q_ASSERT(i == m_vertices.size());
qint64 earliestTimeAllowed = m_vertices.last().x - m_timeSpan;
while (m_vertices.size() > 2 && m_vertices[1].x < earliestTimeAllowed)
m_vertices.remove(0, 4);
emit samplesChanged();
}
void LineGraphModel::modifyEndVertices(qreal time, qreal value, int fromLast)
{
// qDebug() << m_label << time << value << "already have samples:" << m_vertices.size();
value *= m_multiplier;
finagle(time, value);
int i = (m_vertices.length() / LineNode::verticesPerSample - fromLast - 1) * LineNode::verticesPerSample;
Q_ASSERT(i >= 0);
qreal tp = time - 0.01;
qreal samplePrev = value;
if (i >= LineNode::verticesPerSample) {
int j = i - LineNode::verticesPerSample;
tp = m_vertices[j].x;
samplePrev = m_vertices[j].y;
m_vertices[j].nextX = time;
m_vertices[j++].nextY = value;
m_vertices[j].nextX = time;
m_vertices[j++].nextY = value;
m_vertices[j].nextX = time;
m_vertices[j++].nextY = value;
m_vertices[j].nextX = time;
m_vertices[j++].nextY = value;
}
qreal tn = time + 0.001;
qreal sampleNext = value;
if (fromLast > 0) {
int j = i + LineNode::verticesPerSample;
tn = m_vertices[j].x;
sampleNext = m_vertices[j].y;
m_vertices[j].prevX = time;
m_vertices[j++].prevY = value;
m_vertices[j].prevX = time;
m_vertices[j++].prevY = value;
m_vertices[j].prevX = time;
m_vertices[j++].prevY = value;
m_vertices[j].prevX = time;
m_vertices[j++].prevY = value;
}
m_vertices[i++].set(0, -1, time, value, tp, samplePrev, tn, sampleNext);
m_vertices[i++].set(1, 1, time, value, tp, samplePrev, tn, sampleNext);
m_vertices[i++].set(2, -1, time, value, tp, samplePrev, tn, sampleNext);
m_vertices[i++].set(3, 1, time, value, tp, samplePrev, tn, sampleNext);
emit samplesChanged();
}