-
Notifications
You must be signed in to change notification settings - Fork 0
/
charttimer.cpp
108 lines (87 loc) · 3.21 KB
/
charttimer.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
#include "charttimer.h"
#include <QtWidgets>
#include <QtCharts>
#include <QtSql>
#include <QtDebug>
// Function to get MySQL Data
QMap<qint32, QMap<QString, QList<qreal>>> getData() {
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("lpttest");
db.setUserName("user");
db.setPassword("password");
bool ok = db.open();
// Query database
QSqlQuery query;
query.exec("SELECT trace_id, HEX(trace_data) as `data`, trace_time FROM test");
QMap<qint32, QMap<QString, QList<qreal>>> data;
// Loop through query result, decode and convert data, and store in a QMap
while (query.next()) {
QMap<QString, QList<qreal>> blob;
QString bytes_str = query.value(1).toString();
QList<double> blobdata;
for (qint32 i=0; i<bytes_str.length(); i+=8) {
QString sbyte = bytes_str.mid(i, 8);
bool status = false;
qint32 byte = sbyte.toUInt(&status, 16);
qreal datapoint = (qreal)byte/1000;
blobdata.append(datapoint);
}
blob.insert(query.value(2).toString(), blobdata);
data.insert(query.value(0).toInt(), blob);
}
return (data);
}
// Create instance of ChartTimer Class
ChartTimer::ChartTimer(QWidget *parent) : QGraphicsView(new QGraphicsScene, parent) {
num_traces = 50;
trace_id = 1;
// Get Plot Data
plot_data = getData();
// Initialize QChart, Axis, and Timestamp
m_chart = new QChart;
m_chart->setMinimumSize(800, 600);
m_chart->legend()->hide();
axisY = new QValueAxis;
axisY->setRange(-130.0, -30.0);
axisY->setTickCount(10);
axisY->setTickType(QValueAxis::TicksFixed);
axisY->setTickInterval(10.0);
axisY->setTitleText("dBm");
m_chart->addAxis(axisY, Qt::AlignLeft);
scene()->addItem(m_chart);
timestamp = new QGraphicsSimpleTextItem(m_chart);
timestamp->setPos(m_chart->size().width()/2-50, m_chart->size().height()-150);
// Start 1 second update timer
timerId = startTimer(1000);
}
// Function to handle timer update interval
void ChartTimer::timerEvent(QTimerEvent *event)
{
if (event->timerId() == timerId) {
// Clear chart
m_chart->removeAllSeries();
// Get next blob, store in chart series, and add to chart
QLineSeries *series = new QLineSeries();
QMap<QString, QList<qreal>> blob = plot_data[trace_id];
QString key = blob.firstKey();
QList<qreal> datapoints = blob[key];
for (qint32 i=0; i<datapoints.size(); i++) {
series->append(i, datapoints[i]);
}
m_chart->addSeries(series);
// Re-attach axis that was removed with series before
series->attachAxis(axisY);
// Set timestamp to correct format, and set the text
QDateTime tm = QDateTime::fromString(key, "yyyy-MM-ddTHH:mm:ss.zzz");
QString tm_string = tm.toLocalTime().toString("MM/dd/yyyy HH:mm:ss AP");
timestamp->setText(tm_string);
// Update the chart
scene()->update();
// Increment trace_id and reset if necessary for continuous loop
trace_id++;
if (trace_id > num_traces) {
trace_id = 1;
}
}
}