This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithlineflowplotwidget.cpp
279 lines (220 loc) · 9.67 KB
/
withlineflowplotwidget.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
#include "withlineflowplotwidget.h"
#include "ui_withlineflowplotwidget.h"
#include <QThread>
#include <QSqlQuery>
#include <QTime>
#include <QSqlError>
#include <QSqlQuery>
#include <QtCore>
#include <QtConcurrent>
#include <QDateTimeAxis>
#include <QValueAxis>
#include "utilities/BDateTime.h"
#include "utilities/bdatabasemanager.h"
WithLineFlowPlotWidget::WithLineFlowPlotWidget(QWidget *parent) :
BasePlotWidget(parent),
ui(new Ui::WithLineFlowPlotWidget) {
ui->setupUi(this);
ui->startingTimeEdit->setTime(QTime::fromString("05:30", BugenZhao::TIME_FORMAT_NO_SEC));
ui->endingTimeEdit->setTime(QTime::fromString("23:30", BugenZhao::TIME_FORMAT_NO_SEC));
ui->timeStepBox->setValue(20);
auto layout = new QVBoxLayout(ui->plotBox);
layout->setContentsMargins(0, 0, 0, 0);
chartView = new QChartView(ui->plotBox);
// chartViewLeft->setContentsMargins(0, 0, 0, 0);
layout->addWidget(chartView);
ui->plotBox->setLayout(layout);
chartView->setRenderHint(QPainter::Antialiasing);
connect(ui->analyzeButton, &QPushButton::clicked, this, &WithLineFlowPlotWidget::dynamicAnalyzeBetter);
// connect(this, &WithLineFlowPlotWidget::preparedSplineChart, this, &WithLineFlowPlotWidget::setDateTimeSplineChart);
connect(this, &WithLineFlowPlotWidget::preparedChart, this, &WithLineFlowPlotWidget::dynamicInitChart);
connect(this, &WithLineFlowPlotWidget::newData, this, &WithLineFlowPlotWidget::dynamicAppendData);
}
WithLineFlowPlotWidget::~WithLineFlowPlotWidget() {
delete ui;
}
void WithLineFlowPlotWidget::setBzEnabled(bool enabled) {
ui->analyzeButton->setEnabled(enabled);
}
void WithLineFlowPlotWidget::dynamicAnalyzeBetter() {
auto thread = QThread::create([this]() {
auto _filterDataList = filterDataList;
QMap<QString, bool> filterNeeded;
QStringList linesToAnalyze;
QStringList filterJudgmentPieces;
for (const auto &filterData:_filterDataList) {
if (filterData.first == "LINES") {
for (const auto &item:filterData.second.keys()) {
if (filterData.second[item])
linesToAnalyze.append(item);
}
continue;
}
bool allChecked = true;
bool anyChecked = false;
qInfo() << filterData.second;
for (bool checked:filterData.second) {
allChecked &= checked;
anyChecked |= checked;
}
if (!anyChecked) {
emit statusBarMessage("Some filters are set incorrectly", 5000);
return;
}
if (allChecked) continue;
QStringList pieces;
if (filterData.first == "PAY_TYPES") {
for (const auto &item:filterData.second.keys()) {
if (filterData.second[item])
pieces.push_back(QString("payType = %1").arg(item));
}
}
filterJudgmentPieces.push_back(QString("( %1 )").arg(pieces.join(" OR ")));
}
auto filterJudgment = filterJudgmentPieces.join(" AND ");
qInfo() << filterJudgment;
auto startTime = QTime::currentTime();
auto timeStepMinutes = ui->timeStepBox->value();
auto dateStr = ui->dateEdit->text();
auto startingTimeStr = ui->startingTimeEdit->text();
auto endingTimeStr = ui->endingTimeEdit->text();
qInfo() << QString("%1 %2").arg(dateStr).arg(startingTimeStr);
int startingTimestamp = BDateTime::bToLocalTimestamp(QString("%1 %2").arg(dateStr).arg(startingTimeStr));
int endingTimestamp = BDateTime::bToLocalTimestamp(QString("%1 %2").arg(dateStr).arg(endingTimeStr));
auto date = ui->dateEdit->date();
auto _startingTime = ui->startingTimeEdit->time();
auto _endingTime = ui->endingTimeEdit->time();
QDateTime startingDateTime(date, QTime(_startingTime.hour(), _startingTime.minute() / 10 * 10));
QDateTime endingDateTime(date, QTime(_endingTime.hour(), _endingTime.minute() / 10 * 10));
QString TITLE = QString("Traffic Inflow Trend with Lines from %1 %2 to %3")
.arg(dateStr)
.arg(startingTimeStr)
.arg(endingTimeStr);
if (!filterJudgment.isEmpty()) TITLE += QString("\n Where %1").arg(filterJudgment);
QVector<int> timestampsToDo;
for (auto timestamp = startingTimestamp;
timestamp <= endingTimestamp; timestamp = timestamp + 60 * timeStepMinutes) {
timestampsToDo.push_back(timestamp);
}
auto queryStr = QString("SELECT\n"
"\tcount( * ) \n"
"FROM\n"
"\tbz \n"
"WHERE\n"
"\ttimestamp > %1 \n"
"\tAND timestamp < %2 \n"
"\tAND status = 1 \n"
"\tAND lineID = '%3' \n");
if (!filterJudgment.isEmpty()) {
queryStr += QString(" AND %1").arg(filterJudgment);
}
auto worker = [this, TITLE, timeStepMinutes, queryStr](
int curTimestamp, const QString &line) -> QPair<int, int> {
auto threadDb = BDatabaseManager::connection(
QString("plot_thread_") + QString::number(quintptr(QThread::currentThreadId())));
QSqlQuery query(threadDb);
auto curQueryStr = queryStr.arg(curTimestamp).arg(curTimestamp + 60 * timeStepMinutes).arg(line);
if (!query.exec(curQueryStr))
qInfo() << query.lastError().text();
query.next();
auto flow = query.value(0).toInt();
threadDb.close();
return {curTimestamp, flow};
};
qInfo() << linesToAnalyze;
BDataTable emptyTable;
for (int i = 0; i < linesToAnalyze.size(); ++i) {
emptyTable.append(BDataList{});
}
emit preparedChart({TITLE,
linesToAnalyze,
emptyTable},
startingDateTime,
endingDateTime);
BData tmpFlowData;
QList<QPair<int, QPair<int, int>>> results;
int cur = 0;
for (auto timestamp:timestampsToDo) {
auto _newData = BDataList();
for (const auto &line:linesToAnalyze) {
auto pair = worker(timestamp, line);
qInfo() << timestamp * 1000L << pair.second;
tmpFlowData = {QPointF{static_cast<qreal>(timestamp * 1000L),
static_cast<qreal>(pair.second)}, ""};
_newData.append(tmpFlowData);
}
QThread::msleep(expectedTimeMs() / timestampsToDo.size());
emit newData(_newData);
++cur;
}
emit statusBarMessage(QString("Done in %1s").arg(startTime.msecsTo(QTime::currentTime()) / 1000.0),
3000);
});
thread->start();
connect(thread, &QThread::started, this, &WithLineFlowPlotWidget::onAnalysisStarted);
connect(thread, &QThread::finished, this, &WithLineFlowPlotWidget::onAnalysisFinished);
}
void WithLineFlowPlotWidget::dynamicInitChart(const WithLineFlowPlotWidget::BzChartData &chartBaseData,
const QDateTime &dt0, const QDateTime &dt1) {
auto oldChart = chartView->chart();
auto chart = new QChart();
if (speedLevel == FASTEST)
chart->setAnimationOptions(QChart::NoAnimation);
else
chart->setAnimationOptions(QChart::SeriesAnimations);
_axisX = new QDateTimeAxis(chart);
auto axisX = dynamic_cast<QDateTimeAxis *>(_axisX);
axisX->setFormat(TIME_FORMAT_NO_SEC);
axisX->setTickCount(19);
chart->addAxis(axisX, Qt::AlignBottom);
_axisY = new QValueAxis(chart);
auto axisY = dynamic_cast<QValueAxis *>(_axisY);
axisY->setLabelFormat("%d");
axisY->setTickCount(11);
chart->addAxis(axisY, Qt::AlignLeft);
chart->setTitle(chartBaseData.title);
int cur = 0;
maxY = 0;
seriesList.clear();
for (const auto &dataList:chartBaseData.dataTable) {
auto series = new QSplineSeries(chart);
series->setName(chartBaseData.seriesNames[cur++]);
chart->addSeries(series);
series->attachAxis(axisX);
series->attachAxis(axisY);
series->setUseOpenGL(true);
seriesList.push_back(series);
}
axisX->setMin(dt0);
axisX->setMax(dt1);
axisY->setMin(0.0);
axisY->setMax(static_cast<int>( maxY * 11 / 10 + 1));
chartView->setChart(chart);
delete oldChart;
}
void WithLineFlowPlotWidget::dynamicAppendData(const BDataList &dataList) {
auto axisX = dynamic_cast<QDateTimeAxis *>(_axisX);
auto axisY = dynamic_cast<QValueAxis *>(_axisY);
for (int i = 0; i < dataList.size(); ++i) {
auto series = seriesList[i];
auto point = dataList[i].first;
series->append(point);
maxY = (abs(point.y()) > maxY) ? abs(point.y()) : maxY;
}
axisY->setMax(static_cast<int>( maxY * 11 / 10 + 1));
axisY->setMin(0);
}
void WithLineFlowPlotWidget::onAnalysisStarted() {
emit statusBarMessage("Analyzing...", 0);
ui->analyzeButton->setEnabled(false);
}
void WithLineFlowPlotWidget::onAnalysisFinished() {
ui->analyzeButton->setEnabled(true);
}
void WithLineFlowPlotWidget::setDate(const QString &pureDateStr) {
ui->dateEdit->setDate(QDate::fromString(pureDateStr, DATE_FORMAT));
}
void WithLineFlowPlotWidget::setFilterDataList(FilterDataList _filterDataList) {
qInfo() << "filter data updated";
this->filterDataList = _filterDataList;
}