-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
692 lines (592 loc) · 20.1 KB
/
graph.cpp
File metadata and controls
692 lines (592 loc) · 20.1 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#include "graph.h"
/* **Top Level Function **
* func : remove datas before given time
* use : to maintain memory */
void RemoveData(QVector<QPointF>& data, QVector<QPointF>& data2, qreal start, qreal end){
for(int i = 0; i<data.size(); i++){
if(data[i].x() >= start){
if(data[i].x() > end) return;
else {
data.remove(i);
data2.remove(i);
i--;
}
}
}
}
void RemoveData(QVector<QPointF>& data, qreal start, qreal end){
for(int i = 0; i<data.size(); i++){
if(data[i].x() >= start){
if(data[i].x() > end) return;
else {
data.remove(i);
i--;
}
}
}
}
void RemoveData(QVector<QPointF>& data, QVector<QPointF>& data2, QVector<QPointF>& data3, qreal start, qreal end){
for(int i = 0; i<data.size(); i++){
if(data[i].x() >= start){
if(data[i].x() > end) return;
else {
data.remove(i);
data2.remove(i);
data3.remove(i);
i--;
}
}
}
}
void AddPointToVectorByQuery(QVector<QPointF>& data, QVector<QPointF>& data2, qreal start, qreal end, GRAPH_TYPE type, INSERT_TYPE iType){
QSqlQuery query;
if(type == VT) query.prepare("SELECT time , fine_V , coarse_V FROM " + firstTabTable + " WHERE time BETWEEN ? AND ?");
else query.prepare("SELECT time , fine_W , coarse_W FROM " + firstTabTable + " WHERE time BETWEEN ? AND ?");
query.bindValue(0, start);
query.bindValue(1, end);
query.exec();
if(iType == FRONT){
int i = 0;
while(query.next()){
data.insert(i, {query.value(0).toDouble(), query.value(1).toDouble()});
data2.insert(i, {query.value(0).toDouble(), query.value(2).toDouble()});
i++;
}
}
else{
while(query.next()){
data.append({query.value(0).toDouble(), query.value(1).toDouble()});
data2.append({query.value(0).toDouble(), query.value(2).toDouble()});
}
}
}
void AddPointToVectorByQuery(QVector<QPointF>& data, qreal start, qreal end, TABLE_TYPE tType, INSERT_TYPE iType){
QSqlQuery query;
query.prepare("SELECT time , data FROM " + GetTablePath(tType) + " WHERE time BETWEEN ? AND ?");
query.bindValue(0, start);
query.bindValue(1, end);
query.exec();
if(iType == FRONT){
int i = 0;
while(query.next()){
data.insert(i, {query.value(0).toDouble(), query.value(1).toDouble()});
i++;
}
}
else{
while(query.next()){
data.append({query.value(0).toDouble(), query.value(1).toDouble()});
}
}
}
void AddPointToVectorByQuery(QVector<QPointF>& data, QVector<QPointF>& data2, QVector<QPointF>& data3, qreal start, qreal end, INSERT_TYPE iType){
QSqlQuery query;
query.prepare("SELECT time , Dr, Dg, Db FROM " + GetTablePath(CONT) + " WHERE time BETWEEN ? AND ?");
query.bindValue(0, start);
query.bindValue(1, end);
query.exec();
if(iType == FRONT){
int i = 0;
while(query.next()){
data.insert(i, {query.value(0).toDouble(), query.value(1).toDouble()});
data2.insert(i, {query.value(0).toDouble(), query.value(2).toDouble()});
data3.insert(i, {query.value(0).toDouble(), query.value(3).toDouble()});
i++;
}
}
else{
while(query.next()){
data.append({query.value(0).toDouble(), query.value(1).toDouble()});
data2.append({query.value(0).toDouble(), query.value(2).toDouble()});
data3.append({query.value(0).toDouble(), query.value(3).toDouble()});
}
}
}
QString GetTablePath(TABLE_TYPE type){
if(type == DEGREE) return degreeGraphTable;
else if (type == RS) return rsGraphTable;
else if (type == OX) return oxGraphTable;
else if (type == CONT) return contGraphTable;
else return NULL;
}
Graph::Graph(QWidget* parent) : QwtPlot(parent) {
end = start+interval;
this->setAxisAutoScale(QwtPlot::xBottom,false);
this->setAxisAutoScale(QwtPlot::yLeft,true);
firstGraph = new QwtPlotCurve();
secondGraph = new QwtPlotCurve();
firstGraph->setSamples(firstData);
secondGraph->setSamples(secondData);
QPen pen;
pen.setColor("black");
pen.setWidth(2);
pen.setStyle(Qt::SolidLine);
firstGraph->setPen(pen);
pen.setColor("blue");
secondGraph->setPen(pen);
QFont font("Arial");
font.setPointSize(11);
font.setBold(false);
this->setAxisFont(QwtPlot::xBottom, font);
this->setAxisFont(QwtPlot::yLeft, font);
this->axisWidget(QwtPlot::yLeft)->setSpacing(10);
this->axisWidget(QwtPlot::xBottom)->setSpacing(10);
firstGraph->setStyle(QwtPlotCurve::CurveStyle::Lines);
secondGraph->setStyle(QwtPlotCurve::CurveStyle::Lines);
firstGraph->setTitle("Fine Particles");
secondGraph->setTitle("Coarse Particles");
firstGraph->attach(this);
secondGraph->attach(this);
this->setMinimumSize(400, 400);
this->setMaximumSize(400, 400);
}
Graph::~Graph() {}
qreal Graph::GetStartTime(){
return start;
}
qreal Graph::GetInterval(){
return interval;
}
void Graph::ClearAll(){
firstData.clear();
secondData.clear();
start = 0;
end = start + interval;
this->setAxisScale(QwtPlot::xBottom, 0, end);
}
void Graph::ReplotPastGraph(int lower, int upper){
ResetDataFromQuery(static_cast<qreal>(lower)/SLIDER_TIME_RATE, static_cast<qreal>(upper - lower)/SLIDER_TIME_RATE);
}
void Graph::HideShowLegend(bool checked){
if(checked){
this->insertLegend(new QwtLegend(), QwtPlot::BottomLegend, 0.5);
QwtLegend* legend = dynamic_cast<QwtLegend*>(this->legend());
legend->setMaxColumns(2);
legend->contentsWidget()->layout()->setSpacing(10);
}
else{
this->insertLegend(NULL);
}
this->updateLegend();
}
void Graph::HideShowFine(bool checked){
if(checked){
firstGraph->attach(this);
}
else{
firstGraph->detach();
}
this->updateLegend();
this->replot();
}
void Graph::HideShowCoarse(bool checked){
if(checked){
secondGraph->attach(this);
}
else{
secondGraph->detach();
}
this->updateLegend();
this->replot();
}
VTGraph::VTGraph(QWidget* parent, qreal _dangerValue, qreal _cautionValue, qreal _dangerSlope, qreal _cautionSlope) :
Graph(parent), dangerValue(_dangerValue), cautionValue(_cautionValue), dangerSlope(_dangerSlope), cautionSlope(_cautionSlope) {
accTime = 0;
prevEndTime = -1;
fineSlopeFile = new QFile(logDir +"Slope(Fine).txt");
fineSlopeFile->open(QFile::WriteOnly|QFile::Text);
fineFileStream = new QTextStream(fineSlopeFile);
*fineFileStream << "Time(s)" << " " << "Slope" << endl << endl;
coarseSlopeFile = new QFile(logDir + "Slope(Coarse).txt");
coarseSlopeFile->open(QFile::WriteOnly|QFile::Text);
coarseFileStream = new QTextStream(coarseSlopeFile);
*coarseFileStream << "Time(s)" << " " << "Slope" << endl << endl;
*fineFileStream << fixed;
fineFileStream->setRealNumberPrecision(2);
*coarseFileStream << fixed;
coarseFileStream->setRealNumberPrecision(2);
QFont font("Arial");
font.setPointSize(15);
font.setBold(true);
QwtText title("Voltage-Time Graph");
title.setFont(font);
this->setTitle(title);
dangerLine = new QwtPlotCurve("Threshold of Danger");
cautionLine = new QwtPlotCurve("Threshold of Caution");
//pen setting
QPen pen;
pen.setColor(Qt::red);
pen.setStyle(Qt::DashDotLine);
pen.setWidth(3);
dangerLine->setPen(pen);
pen.setColor("#FF7F00");
pen.setStyle(Qt::DotLine);
cautionLine->setPen(pen);
//axis setting
//font setting
font.setPointSize(11);
font.setBold(true);
title.setText("Time (s)");
title.setFont(font);
this->setAxisTitle(QwtPlot::xBottom, title);
this->setAxisScale(QwtPlot::xBottom, 0, end);
title.setText("Voltage (V)");
this->setAxisTitle(QwtPlot::yLeft, title);
this->setAxisScale(QwtPlot::yLeft, 0, 5);
//legend
this->HideShowLegend(true);
//intial value setting
dangerLineVector.push_back({0, dangerValue});
dangerLineVector.push_back({end, dangerValue});
cautionLineVector.push_back({0, cautionValue});
cautionLineVector.push_back({end, cautionValue});
dangerLine->setSamples(dangerLineVector);
cautionLine->setSamples(cautionLineVector);
dangerLine->attach(this);
cautionLine->attach(this);
this->replot();
}
VTGraph::~VTGraph(){
fineSlopeFile->close();
coarseSlopeFile->close();
}
qreal VTGraph::getDanger(){
return dangerValue;
}
qreal VTGraph::getCaution(){
return cautionValue;
}
qreal VTGraph::getDangerSlope(){
return dangerSlope;
}
qreal VTGraph::getCautionSlope(){
return cautionSlope;
}
void VTGraph::ClearAll(){
Graph::ClearAll();
accTime = 0;
prevEndTime = -1;
accFineVector.clear();
accCoarseVector.clear();
*fineFileStream << "Graph Cleared" << endl << endl;
*coarseFileStream << "Graph Cleared" << endl << endl;
}
void VTGraph::ResetDataFromQuery(qreal _start, qreal _interval){
//four cases
qreal _end = _start + _interval;
if(start < _start && end < _end){
RemoveData(firstData, secondData, start, _start);
AddPointToVectorByQuery(firstData, secondData, end, _end, VT, BACK);
}
else if (start < _start && _end < end){
RemoveData(firstData, secondData, start, _start);
RemoveData(firstData, secondData, _end, end);
}
else if (_start < start && _end < end){
RemoveData(firstData, secondData, _end, end);
AddPointToVectorByQuery(firstData, secondData, _start, start, VT, FRONT);
}
else{
firstData.clear();
secondData.clear();
AddPointToVectorByQuery(firstData, secondData, _start, _end, VT, BACK);
}
start = _start;
interval = _interval;
end = _end;
dangerLineVector.clear();
cautionLineVector.clear();
dangerLineVector.push_back({start, dangerValue});
dangerLineVector.push_back({end, dangerValue});
cautionLineVector.push_back({start, cautionValue});
cautionLineVector.push_back({end, cautionValue});
firstGraph->setSamples(firstData);
secondGraph->setSamples(secondData);
dangerLine->setSamples(dangerLineVector);
cautionLine->setSamples(cautionLineVector);
this->setAxisScale(QwtPlot::xBottom, start, end);
this->replot();
}
void VTGraph::HideShowDanger(bool checked){
if(checked){
dangerLine->attach(this);
}
else{
dangerLine->detach();
}
this->updateLegend();
this->replot();
}
void VTGraph::HideShowCaution(bool checked){
if(checked){
cautionLine->attach(this);
}
else{
cautionLine->detach();
}
this->updateLegend();
this->replot();
}
void VTGraph::CheckSlope(qreal _firstData, qreal _secondData, qreal time){
if(prevEndTime < 0) {
prevEndTime = time;
}
qreal dx = time - prevEndTime;
accTime += dx;
accFineVector.push_back({time, _firstData});
accCoarseVector.push_back({time, _secondData});
prevEndTime = time;
if(accTime >= 1){
accTime = 0;
CheckSlopeUt(CalSlope(accFineVector), time, DATA_TYPE::FINE);
CheckSlopeUt(CalSlope(accCoarseVector), time, DATA_TYPE::COARSE);
}
}
qreal VTGraph::CalSlope(QVector<QPointF>& dataVector){
QVector<qreal> meanVector;
qreal sum = 0;
int i = 0;
for(i = 0; i < dataVector.size() - 1; i++){
sum += (dataVector[i+1].y() - dataVector[i].y()) / (dataVector[i+1].x() - dataVector[i].x());
}
meanVector.push_back(sum / i);
sum = 0;
i = 0;
qreal meanValue_1;
qreal meanTime_1;
qreal meanValue_2;
qreal meanTime_2;
while(i + 1 < dataVector.size()){
if(i % 2 == 0){
meanValue_1 = (dataVector[i].y() + dataVector[i+1].y()) / 2;
meanTime_1 = (dataVector[i].x() + dataVector[i+1].x()) / 2;
}
else{
meanValue_2 = (dataVector[i].y() + dataVector[i+1].y()) / 2;
meanTime_2 = (dataVector[i].x() + dataVector[i+1].x()) / 2;
sum += (meanValue_2 - meanValue_1) / (meanTime_2 - meanTime_1);
}
i++;
}
meanVector.push_back(sum / i);
sum = 0;
i = 0;
while(i + 2 < dataVector.size()){
if(i % 2 == 0){
meanValue_1 = (dataVector[i].y() + dataVector[i+1].y() + dataVector[i+2].y()) / 3;
meanTime_1 = (dataVector[i].x() + dataVector[i+1].x() + dataVector[i+2].x()) / 3;
}
else{
meanValue_2 = (dataVector[i].y() + dataVector[i+1].y() + dataVector[i+2].y()) / 3;
meanTime_2 = (dataVector[i].x() + dataVector[i+1].x() + dataVector[i+2].x()) / 3;
sum += (meanValue_2 - meanValue_1) / (meanTime_2 - meanTime_1);
}
i++;
}
meanVector.push_back(sum / i);
dataVector.clear();
qreal meanSum = 0;
for(int i = 0; i<meanVector.size(); i++){
meanSum += meanVector[i];
}
return meanSum / meanVector.size();
}
void VTGraph::CheckSlopeUt(qreal slope, qreal time, DATA_TYPE type){
if(slope >= dangerSlope){
if(type == FINE) *fineFileStream << time << " " << slope << endl;
else *coarseFileStream << time << " " << slope << endl;
QMessageBox errorBox;
errorBox.critical(0, "Warning", "This slope has exceeded the danger level.");
errorBox.setFixedSize(200, 200);
errorBox.show();
}
else if(slope >= cautionSlope){
QMessageBox errorBox;
errorBox.critical(0, "Warning", "This slope has exceeded the caution level.");
errorBox.setFixedSize(200, 200);
errorBox.show();
}
}
void VTGraph::ChangerProperties(qreal _dangerV, qreal _cautionV, qreal _dangerS, qreal _cautionS){
dangerValue = _dangerV;
cautionValue = _cautionV;
dangerSlope = _dangerS;
cautionSlope = _cautionS;
dangerLineVector.clear();
cautionLineVector.clear();
dangerLineVector.push_back({start, dangerValue});
dangerLineVector.push_back({end, dangerValue});
cautionLineVector.push_back({start, cautionValue});
cautionLineVector.push_back({end, cautionValue});
dangerLine->setSamples(dangerLineVector);
cautionLine->setSamples(cautionLineVector);
replot();
}
WTGraph::WTGraph(QWidget* parent) : Graph(parent){
QFont font("Arial");
font.setPointSize(15);
font.setBold(true);
QwtText title("Weight-Time Graph");
title.setFont(font);
this->setTitle(title);
font.setPointSize(11);
font.setBold(true);
title.setText("Time (s)");
title.setFont(font);
this->setAxisTitle(QwtPlot::xBottom, title);
this->setAxisScale(QwtPlot::xBottom, 0, end);
title.setText("Weight of Wear Particles (mg)");
this->setAxisTitle(QwtPlot::yLeft, title);
this->setAxisScale(QwtPlot::yLeft, 0, 500);
this->HideShowLegend(true);
this->updateLegend();
}
WTGraph::~WTGraph(){}
void WTGraph::ResetDataFromQuery(qreal _start, qreal _interval){
qreal _end = _start + _interval;
if(start < _start && end < _end){
RemoveData(firstData, secondData, start, _start);
AddPointToVectorByQuery(firstData, secondData, end, _end, WT, BACK);
}
else if (start < _start && _end < end){
RemoveData(firstData, secondData, start, _start);
RemoveData(firstData, secondData, _end, end);
}
else if (_start < start && _end < end){
RemoveData(firstData, secondData, _end, end);
AddPointToVectorByQuery(firstData, secondData, _start, start, WT, FRONT);
}
else{
firstData.clear();
secondData.clear();
AddPointToVectorByQuery(firstData, secondData, _start, _end, WT, BACK);
}
start = _start;
interval = _interval;
end = _end;
firstGraph->setSamples(firstData);
secondGraph->setSamples(secondData);
this->setAxisScale(QwtPlot::xBottom, start, end);
this->replot();
}
void WTGraph::HideShowLegend(bool checked){
if(checked){
this->insertLegend(new QwtLegend(), QwtPlot::BottomLegend, 0.5);
QwtLegend* legend = dynamic_cast<QwtLegend*>(this->legend());
legend->setMaxColumns(2);
legend->contentsWidget()->layout()->setSpacing(10);
legend->contentsWidget()->layout()->addWidget(new QLabel);
legend->contentsWidget()->layout()->addWidget(new QLabel);
}
else{
this->insertLegend(NULL);
}
this->updateLegend();
}
/*********************************
* for second tab
* Only Use Fine graph between Two
* *******************************
*/
OnlyFirstGraph::OnlyFirstGraph(TABLE_TYPE tType, QString _title, QString graphName, QString xTitle, QString yTitle, qreal yEnd) : Graph(NULL){
graphType = tType;
secondGraph->detach();
QFont font("Arial");
font.setPointSize(15);
font.setBold(true);
QwtText title = _title;
title.setFont(font);
this->setTitle(title);
firstGraph->setTitle(graphName);
font.setPointSize(11);
font.setBold(true);
title.setText(xTitle);
title.setFont(font);
this->setAxisTitle(QwtPlot::xBottom, title);
this->setAxisScale(QwtPlot::xBottom, 0, end);
title.setText(yTitle);
this->setAxisTitle(QwtPlot::yLeft, title);
this->setAxisScale(QwtPlot::yLeft, 0, yEnd);
this->setFixedSize(400,250);
}
void OnlyFirstGraph::ResetDataFromQuery(qreal _start, qreal _interval){
qreal _end = _start + _interval;
if(start < _start && end < _end){
RemoveData(firstData, start, _start);
AddPointToVectorByQuery(firstData, end, _end, graphType, BACK);
}
else if (start < _start && _end < end){
RemoveData(firstData, start, _start);
RemoveData(firstData, _end, end);
}
else if (_start < start && _end < end){
RemoveData(firstData, _end, end);
AddPointToVectorByQuery(firstData, _start, start, graphType, FRONT);
}
else{
firstData.clear();
AddPointToVectorByQuery(firstData, _start, _end, graphType, BACK);
}
start = _start;
interval = _interval;
end = _end;
firstGraph->setSamples(firstData);
this->setAxisScale(QwtPlot::xBottom, start, end);
this->replot();
}
QString OnlyFirstGraph::GetGraphType(){
return GetTablePath(graphType);
}
/*********************************
* for second tab
* Three graphs
* *******************************
*/
ContGraph::ContGraph(TABLE_TYPE tType, QString _title,
QString graphName_1, QString graphName_2, QString graphName_3,
QString xTitle, QString yTitle, qreal yEnd)
: OnlyFirstGraph(tType, _title, graphName_1, xTitle, yTitle, yEnd){
//adding Two graphs
thirdGraph = new QwtPlotCurve();
QPen pen;
pen.setColor("red");
pen.setWidth(2);
pen.setStyle(Qt::SolidLine);
thirdGraph->setPen(pen);
//setting graphs
secondGraph->setTitle(graphName_2);
thirdGraph->setTitle(graphName_3);
thirdGraph->setSamples(thirdData);
firstGraph->attach(this);
secondGraph->attach(this);
thirdGraph->attach(this);
}
void ContGraph::ResetDataFromQuery(qreal _start, qreal _interval){
qreal _end = _start + _interval;
if(start < _start && end < _end){
RemoveData(firstData, secondData, thirdData, start, _start);
AddPointToVectorByQuery(firstData, secondData, thirdData, end, _end, BACK);
}
else if (start < _start && _end < end){
RemoveData(firstData, secondData, thirdData, start, _start);
RemoveData(firstData, secondData, thirdData, _end, end);
}
else if (_start < start && _end < end){
RemoveData(firstData, secondData, thirdData, _end, end);
AddPointToVectorByQuery(firstData, secondData, thirdData, _start, start, FRONT);
}
else{
firstData.clear();
secondData.clear();
thirdData.clear();
AddPointToVectorByQuery(firstData, secondData, thirdData, _start, _end, BACK);
}
start = _start;
interval = _interval;
end = _end;
firstGraph->setSamples(firstData);
secondGraph->setSamples(secondData);
thirdGraph->setSamples(thirdData);
this->setAxisScale(QwtPlot::xBottom, start, end);
this->replot();
}