-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmainwindow.cpp
2440 lines (2133 loc) · 97.3 KB
/
mainwindow.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
** **
** QCustomPlot, an easy to use, modern plotting widget for Qt **
** Copyright (C) 2011-2018 Emanuel Eichhammer **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Emanuel Eichhammer **
** Website/Contact: http://www.qcustomplot.com/ **
** Date: 25.06.18 **
** Version: 2.0.1 **
****************************************************************************/
/************************************************************************************************************
** **
** This is the example code for QCustomPlot. **
** **
** It demonstrates basic and some advanced capabilities of the widget. The interesting code is inside **
** the "setup(...)Demo" functions of MainWindow. **
** **
** In order to see a demo in action, call the respective "setup(...)Demo" function inside the **
** MainWindow constructor. Alternatively you may call setupDemo(i) where i is the index of the demo **
** you want (for those, see MainWindow constructor comments). All other functions here are merely a **
** way to easily create screenshots of all demos for the website. I.e. a timer is set to successively **
** setup all the demos and make a screenshot of the window area and save it in the ./screenshots **
** directory. **
** **
*************************************************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QRandomGenerator>
#include <QScreen>
#include <QMessageBox>
#include <QMetaEnum>
#include <bitset>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setGeometry(200, 200, 800, 600);
_loGrid = new QGridLayout;
ui->scrollArea->setWidgetResizable(true);
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->scrollAreaWidgetContents->setLayout(_loGrid);
setupDemo(false);
}
void MainWindow::setupDemo(bool openGl)
{
setupQuadraticDemo(0,0, openGl);
setupSimpleDemo(0,1, openGl);
setupSincScatterDemo(1,0, openGl);
setupScatterStyleDemo(1,1, openGl);
setupScatterPixmapDemo(2,0, openGl);
setupLineStyleDemo(2,1, openGl);
setupDateDemo(3,0, openGl);
setupTextureBrushDemo(3,1, openGl);
setupMultiAxisDemo(4,0, openGl);
setupLogarithmicDemo(4,1, openGl);
setupParametricCurveDemo(5,0, openGl);
setupBarChartDemo(5,1, openGl);
setupStatisticalDemo(6,0, openGl);
setupSimpleItemDemo(6,1, openGl);
setupStyledDemo(7,0, openGl);
setupAdvancedAxesDemo(7,1, openGl);
setupColorMapDemo(8,0, openGl);
setupFinancialDemo(8,1, openGl);
setupRealtimeDataDemo(9,0, openGl);
setupRealtimeBrushDemo(9,1, openGl);
setupRealtimeThresholdDemo(10,0, openGl);
setupRealtimeEcgDemo(10,1, openGl);
setupItemDemo(11,0, openGl);
setupHistoryDemo(11,1, openGl);
setupAxisTagDemo(12,0, openGl);
setupBitFieldDemo(12,1, openGl);
setWindowTitle("QCustomPlot Examples");
}
void MainWindow::setupQuadraticDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Quadratic Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
// generate some data:
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
plot->addGraph();
plot->graph(0)->setData(x, y);
// give the axes some labels:
plot->xAxis->setLabel("x");
plot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
plot->xAxis->setRange(-1, 1);
plot->yAxis->setRange(0, 1);
plot->replot();
}
void MainWindow::setupSimpleDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Simple Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
// add two new graphs and set their look:
plot->addGraph();
plot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
QLinearGradient gradient(1000, 0, 0, 0);
gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));
gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));
QBrush brush(gradient);
plot->graph(0)->setBrush(brush); // first graph will be filled with translucent blue
plot->addGraph();
plot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
// generate some points of data (y0 for first, y1 for second graph):
QVector<double> x(251), y0(251), y1(251);
for (int i=0; i<251; ++i)
{
x[i] = i;
y0[i] = qExp(i/150.0)*qCos(i/10.0); // exponentially decaying cosine
y1[i] = qExp(i/150.0); // exponential envelope
}
// configure right and top axis to show ticks but no labels:
// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
plot->xAxis2->setVisible(true);
plot->xAxis2->setTickLabels(false);
plot->yAxis2->setVisible(true);
plot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
connect(plot->xAxis, SIGNAL(rangeChanged(QCPRange)), plot->xAxis2, SLOT(setRange(QCPRange)));
connect(plot->yAxis, SIGNAL(rangeChanged(QCPRange)), plot->yAxis2, SLOT(setRange(QCPRange)));
// pass data points to graphs:
plot->graph(0)->setData(x, y0);
plot->graph(1)->setData(x, y1);
// let the ranges scale themselves so graph 0 fits perfectly in the visible area:
plot->graph(0)->rescaleAxes();
// same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
plot->graph(1)->rescaleAxes(true);
// Note: we could have also just called customPlot->rescaleAxes(); instead
// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
plot->replot();
}
void MainWindow::setupSincScatterDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Sinc Scatter Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->legend->setVisible(true);
plot->legend->setFont(QFont("Helvetica",9));
// set locale to english, so we get english decimal separator:
plot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// add confidence band graphs:
plot->addGraph();
QPen pen;
pen.setStyle(Qt::DotLine);
pen.setWidth(1);
pen.setColor(QColor(180,180,180));
plot->graph(0)->setName("Confidence Band 68%");
plot->graph(0)->setPen(pen);
plot->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));
plot->addGraph();
plot->legend->removeItem(plot->legend->itemCount()-1); // don't show two confidence band graphs in legend
plot->graph(1)->setPen(pen);
plot->graph(0)->setChannelFillGraph(plot->graph(1));
// add theory curve graph:
plot->addGraph();
pen.setStyle(Qt::DashLine);
pen.setWidth(2);
pen.setColor(Qt::red);
plot->graph(2)->setPen(pen);
plot->graph(2)->setName("Theory Curve");
// add data point graph:
plot->addGraph();
plot->graph(3)->setPen(QPen(Qt::blue));
plot->graph(3)->setLineStyle(QCPGraph::lsNone);
plot->graph(3)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, 4));
// add error bars:
QCPErrorBars *errorBars = new QCPErrorBars(plot->xAxis, plot->yAxis);
errorBars->removeFromLegend();
errorBars->setAntialiased(false);
errorBars->setDataPlottable(plot->graph(3));
errorBars->setPen(QPen(QColor(180,180,180)));
plot->graph(3)->setName("Measurement");
// generate ideal sinc curve data and some randomly perturbed data for scatter plot:
QVector<double> x0(250), y0(250);
QVector<double> yConfUpper(250), yConfLower(250);
for (int i=0; i<250; ++i)
{
x0[i] = (i/249.0-0.5)*30+0.01; // by adding a small offset we make sure not do divide by zero in next code line
y0[i] = qSin(x0[i])/x0[i]; // sinc function
yConfUpper[i] = y0[i]+0.15;
yConfLower[i] = y0[i]-0.15;
x0[i] *= 1000;
}
QVector<double> x1(50), y1(50), y1err(50);
for (int i=0; i<50; ++i)
{
// generate a gaussian distributed random number:
double tmp1 = rand()/(double)RAND_MAX;
double tmp2 = rand()/(double)RAND_MAX;
double r = qSqrt(-2*qLn(tmp1))*qCos(2*M_PI*tmp2); // box-muller transform for gaussian distribution
// set y1 to value of y0 plus a random gaussian pertubation:
x1[i] = (i/50.0-0.5)*30+0.25;
y1[i] = qSin(x1[i])/x1[i]+r*0.15;
x1[i] *= 1000;
y1err[i] = 0.15;
}
// pass data to graphs and let QCustomPlot determine the axes ranges so the whole thing is visible:
plot->graph(0)->setData(x0, yConfUpper);
plot->graph(1)->setData(x0, yConfLower);
plot->graph(2)->setData(x0, y0);
plot->graph(3)->setData(x1, y1);
errorBars->setData(y1err);
plot->graph(2)->rescaleAxes();
plot->graph(3)->rescaleAxes(true);
// setup look of bottom tick labels:
plot->xAxis->setTickLabelRotation(30);
plot->xAxis->ticker()->setTickCount(9);
plot->xAxis->setNumberFormat("ebc");
plot->xAxis->setNumberPrecision(1);
plot->xAxis->moveRange(-10);
// make top right axes clones of bottom left axes. Looks prettier:
plot->axisRect()->setupFullAxesBox();
plot->replot();
}
void MainWindow::setupScatterStyleDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Scatter Style Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->legend->setVisible(true);
plot->legend->setFont(QFont("Helvetica", 9));
plot->legend->setRowSpacing(-3);
QVector<QCPScatterStyle::ScatterShape> shapes;
shapes << QCPScatterStyle::ssCross;
shapes << QCPScatterStyle::ssPlus;
shapes << QCPScatterStyle::ssCircle;
shapes << QCPScatterStyle::ssDisc;
shapes << QCPScatterStyle::ssSquare;
shapes << QCPScatterStyle::ssDiamond;
shapes << QCPScatterStyle::ssStar;
shapes << QCPScatterStyle::ssTriangle;
shapes << QCPScatterStyle::ssTriangleInverted;
shapes << QCPScatterStyle::ssCrossSquare;
shapes << QCPScatterStyle::ssPlusSquare;
shapes << QCPScatterStyle::ssCrossCircle;
shapes << QCPScatterStyle::ssPlusCircle;
shapes << QCPScatterStyle::ssPeace;
shapes << QCPScatterStyle::ssCustom;
QPen pen;
// add graphs with different scatter styles:
for (int i=0; i<shapes.size(); ++i)
{
plot->addGraph();
pen.setColor(QColor(qSin(i*0.3)*100+100, qSin(i*0.6+0.7)*100+100, qSin(i*0.4+0.6)*100+100));
// generate data:
QVector<double> x(10), y(10);
for (int k=0; k<10; ++k)
{
x[k] = k/10.0 * 4*3.14 + 0.01;
y[k] = 7*qSin(x[k])/x[k] + (shapes.size()-i)*5;
}
plot->graph()->setData(x, y);
plot->graph()->rescaleAxes(true);
plot->graph()->setPen(pen);
plot->graph()->setName(QCPScatterStyle::staticMetaObject.enumerator(QCPScatterStyle::staticMetaObject.indexOfEnumerator("ScatterShape")).valueToKey(shapes.at(i)));
plot->graph()->setLineStyle(QCPGraph::lsLine);
// set scatter style:
if (shapes.at(i) != QCPScatterStyle::ssCustom)
{
plot->graph()->setScatterStyle(QCPScatterStyle(shapes.at(i), 10));
}
else
{
QPainterPath customScatterPath;
for (int i=0; i<3; ++i)
customScatterPath.cubicTo(qCos(2*M_PI*i/3.0)*9, qSin(2*M_PI*i/3.0)*9, qCos(2*M_PI*(i+0.9)/3.0)*9, qSin(2*M_PI*(i+0.9)/3.0)*9, 0, 0);
plot->graph()->setScatterStyle(QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10));
}
}
// set blank axis lines:
plot->rescaleAxes();
plot->xAxis->setTicks(false);
plot->yAxis->setTicks(false);
plot->xAxis->setTickLabels(false);
plot->yAxis->setTickLabels(false);
// make top right axes clones of bottom left axes:
plot->axisRect()->setupFullAxesBox();
plot->replot();
}
void MainWindow::setupLineStyleDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Line Style Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->legend->setVisible(true);
plot->legend->setFont(QFont("Helvetica", 9));
QPen pen;
QStringList lineNames;
lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStepCenter" << "lsImpulse";
// add graphs with different line styles:
for (int i=QCPGraph::lsNone; i<=QCPGraph::lsImpulse; ++i)
{
plot->addGraph();
pen.setColor(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));
plot->graph()->setPen(pen);
plot->graph()->setName(lineNames.at(i-QCPGraph::lsNone));
plot->graph()->setLineStyle((QCPGraph::LineStyle)i);
plot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
// generate data:
QVector<double> x(15), y(15);
for (int j=0; j<15; ++j)
{
x[j] = j/15.0 * 5*3.14 + 0.01;
y[j] = 7*qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5 + (QCPGraph::lsImpulse)*5 + 2;
}
plot->graph()->setData(x, y);
plot->graph()->rescaleAxes(true);
}
// zoom out a bit:
plot->yAxis->scaleRange(1.1, plot->yAxis->range().center());
plot->xAxis->scaleRange(1.1, plot->xAxis->range().center());
// set blank axis lines:
plot->xAxis->setTicks(false);
plot->yAxis->setTicks(true);
plot->xAxis->setTickLabels(false);
plot->yAxis->setTickLabels(true);
// make top right axes clones of bottom left axes:
plot->axisRect()->setupFullAxesBox();
plot->replot();
}
void MainWindow::setupScatterPixmapDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Scatter Pixmap Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->axisRect()->setBackground(QPixmap(":/solarpanels.jpg"));
plot->addGraph();
plot->graph()->setLineStyle(QCPGraph::lsLine);
QPen pen;
pen.setColor(QColor(255, 200, 20, 200));
pen.setStyle(Qt::DashLine);
pen.setWidthF(2.5);
plot->graph()->setPen(pen);
plot->graph()->setBrush(QBrush(QColor(255,200,20,70)));
plot->graph()->setScatterStyle(QCPScatterStyle(QPixmap(":/sun.png")));
// set graph name, will show up in legend next to icon:
plot->graph()->setName("Data from Photovoltaic\nenergy barometer 2011");
// set data:
QVector<double> year, value;
year << 2005 << 2006 << 2007 << 2008 << 2009 << 2010 << 2011;
value << 2.17 << 3.42 << 4.94 << 10.38 << 15.86 << 29.33 << 52.1;
plot->graph()->setData(year, value);
// set title of plot:
plot->plotLayout()->insertRow(0);
plot->plotLayout()->addElement(0, 0, new QCPTextElement(plot, "Regenerative Energies", QFont("sans", 12, QFont::Bold)));
// axis configurations:
plot->xAxis->setLabel("Year");
plot->yAxis->setLabel("Installed Gigawatts of\nphotovoltaic in the European Union");
plot->xAxis2->setVisible(true);
plot->yAxis2->setVisible(true);
plot->xAxis2->setTickLabels(false);
plot->yAxis2->setTickLabels(false);
plot->xAxis2->setTicks(false);
plot->yAxis2->setTicks(false);
plot->xAxis2->setSubTicks(false);
plot->yAxis2->setSubTicks(false);
plot->xAxis->setRange(2004.5, 2011.5);
plot->yAxis->setRange(0, 52);
// setup legend:
plot->legend->setFont(QFont(font().family(), 7));
plot->legend->setIconSize(50, 20);
plot->legend->setVisible(true);
plot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft | Qt::AlignTop);
plot->replot();
}
void MainWindow::setupDateDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Date Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
// set locale to english, so we get english month names:
plot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// seconds of current time, we'll use it as starting point in time for data:
double now = QDateTime::currentDateTime().toTime_t();
srand(8); // set the random seed, so we always get the same random data
// create multiple graphs:
for (int gi=0; gi<5; ++gi)
{
plot->addGraph();
QColor color(20+200/4.0*gi,70*(1.6-gi/4.0), 150, 150);
plot->graph()->setLineStyle(QCPGraph::lsLine);
plot->graph()->setPen(QPen(color.lighter(200)));
plot->graph()->setBrush(QBrush(color));
// generate random walk data:
QVector<QCPGraphData> timeData(250);
for (int i=0; i<250; ++i)
{
timeData[i].key = now + 24*3600*i;
if (i == 0)
timeData[i].value = (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);
else
timeData[i].value = qFabs(timeData[i-1].value)*(1+0.02/4.0*(4-gi)) + (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);
}
plot->graph()->data()->set(timeData);
}
// configure bottom axis to show date instead of number:
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("d. MMMM\nyyyy");
plot->xAxis->setTicker(dateTicker);
// configure left axis text labels:
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTick(10, "a bit\nlow");
textTicker->addTick(50, "quite\nhigh");
plot->yAxis->setTicker(textTicker);
// set a more compact font size for bottom and left axis tick labels:
plot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
plot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
// set axis labels:
plot->xAxis->setLabel("Date");
plot->yAxis->setLabel("Random wobbly lines value");
// make top and right axes visible but without ticks and labels:
plot->xAxis2->setVisible(true);
plot->yAxis2->setVisible(true);
plot->xAxis2->setTicks(false);
plot->yAxis2->setTicks(false);
plot->xAxis2->setTickLabels(false);
plot->yAxis2->setTickLabels(false);
// set axis ranges to show all data:
plot->xAxis->setRange(now, now+24*3600*249);
plot->yAxis->setRange(0, 60);
// show legend with slightly transparent background brush:
plot->legend->setVisible(true);
plot->legend->setBrush(QColor(255, 255, 255, 150));
plot->replot();
}
void MainWindow::setupTextureBrushDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Texture Brush Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
// add two graphs with a textured fill:
plot->addGraph();
QPen redDotPen;
redDotPen.setStyle(Qt::DotLine);
redDotPen.setColor(QColor(170, 100, 100, 180));
redDotPen.setWidthF(2);
plot->graph(0)->setPen(redDotPen);
plot->graph(0)->setBrush(QBrush(QPixmap(":/balboa.jpg"))); // fill with texture of specified image
plot->addGraph();
plot->graph(1)->setPen(QPen(Qt::red));
// activate channel fill for graph 0 towards graph 1:
plot->graph(0)->setChannelFillGraph(plot->graph(1));
// generate data:
QVector<double> x(250);
QVector<double> y0(250), y1(250);
for (int i=0; i<250; ++i)
{
// just playing with numbers, not much to learn here
x[i] = 3*i/250.0;
y0[i] = 1+qExp(-x[i]*x[i]*0.8)*(x[i]*x[i]+x[i]);
y1[i] = 1-qExp(-x[i]*x[i]*0.4)*(x[i]*x[i])*0.1;
}
// pass data points to graphs:
plot->graph(0)->setData(x, y0);
plot->graph(1)->setData(x, y1);
// activate top and right axes, which are invisible by default:
plot->xAxis2->setVisible(true);
plot->yAxis2->setVisible(true);
// make tick labels invisible on top and right axis:
plot->xAxis2->setTickLabels(false);
plot->yAxis2->setTickLabels(false);
// set ranges:
plot->xAxis->setRange(0, 2.5);
plot->yAxis->setRange(0.9, 1.6);
// assign top/right axes same properties as bottom/left:
plot->axisRect()->setupFullAxesBox();
}
void MainWindow::setupMultiAxisDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Multi Axis Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
plot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom)); // period as decimal separator and comma as thousand separator
plot->legend->setVisible(true);
QFont legendFont = font(); // start out with MainWindow's font..
legendFont.setPointSize(9); // and make a bit smaller for legend
plot->legend->setFont(legendFont);
plot->legend->setBrush(QBrush(QColor(255,255,255,230)));
// by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:
plot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);
// setup for graph 0: key axis left, value axis bottom
// will contain left maxwell-like function
plot->addGraph(plot->yAxis, plot->xAxis);
plot->graph(0)->setPen(QPen(QColor(255, 100, 0)));
plot->graph(0)->setBrush(QBrush(QPixmap(":/balboa.jpg"))); // fill with texture of specified image
plot->graph(0)->setLineStyle(QCPGraph::lsLine);
plot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
plot->graph(0)->setName("Left maxwell function");
// setup for graph 1: key axis bottom, value axis left (those are the default axes)
// will contain bottom maxwell-like function with error bars
plot->addGraph();
plot->graph(1)->setPen(QPen(Qt::red));
plot->graph(1)->setBrush(QBrush(QPixmap("./balboa.jpg"))); // same fill as we used for graph 0
plot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);
plot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));
plot->graph(1)->setName("Bottom maxwell function");
QCPErrorBars *errorBars = new QCPErrorBars(plot->xAxis, plot->yAxis);
errorBars->removeFromLegend();
errorBars->setDataPlottable(plot->graph(1));
// setup for graph 2: key axis top, value axis right
// will contain high frequency sine with low frequency beating:
plot->addGraph(plot->xAxis2, plot->yAxis2);
plot->graph(2)->setPen(QPen(Qt::blue));
plot->graph(2)->setName("High frequency sine");
// setup for graph 3: same axes as graph 2
// will contain low frequency beating envelope of graph 2
plot->addGraph(plot->xAxis2, plot->yAxis2);
QPen blueDotPen;
blueDotPen.setColor(QColor(30, 40, 255, 150));
blueDotPen.setStyle(Qt::DotLine);
blueDotPen.setWidthF(4);
plot->graph(3)->setPen(blueDotPen);
plot->graph(3)->setName("Sine envelope");
// setup for graph 4: key axis right, value axis top
// will contain parabolically distributed data points with some random perturbance
plot->addGraph(plot->yAxis2, plot->xAxis2);
plot->graph(4)->setPen(QColor(50, 50, 50, 255));
plot->graph(4)->setLineStyle(QCPGraph::lsNone);
plot->graph(4)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));
plot->graph(4)->setName("Some random data around\na quadratic function");
// generate data, just playing with numbers, not much to learn here:
QVector<double> x0(25), y0(25);
QVector<double> x1(15), y1(15), y1err(15);
QVector<double> x2(250), y2(250);
QVector<double> x3(250), y3(250);
QVector<double> x4(250), y4(250);
for (int i=0; i<25; ++i) // data for graph 0
{
x0[i] = 3*i/25.0;
y0[i] = qExp(-x0[i]*x0[i]*0.8)*(x0[i]*x0[i]+x0[i]);
}
for (int i=0; i<15; ++i) // data for graph 1
{
x1[i] = 3*i/15.0;;
y1[i] = qExp(-x1[i]*x1[i])*(x1[i]*x1[i])*2.6;
y1err[i] = y1[i]*0.25;
}
for (int i=0; i<250; ++i) // data for graphs 2, 3 and 4
{
x2[i] = i/250.0*3*M_PI;
x3[i] = x2[i];
x4[i] = i/250.0*100-50;
y2[i] = qSin(x2[i]*12)*qCos(x2[i])*10;
y3[i] = qCos(x3[i])*10;
y4[i] = 0.01*x4[i]*x4[i] + 1.5*(rand()/(double)RAND_MAX-0.5) + 1.5*M_PI;
}
// pass data points to graphs:
plot->graph(0)->setData(x0, y0);
plot->graph(1)->setData(x1, y1);
errorBars->setData(y1err);
plot->graph(2)->setData(x2, y2);
plot->graph(3)->setData(x3, y3);
plot->graph(4)->setData(x4, y4);
// activate top and right axes, which are invisible by default:
plot->xAxis2->setVisible(true);
plot->yAxis2->setVisible(true);
// set ranges appropriate to show data:
plot->xAxis->setRange(0, 2.7);
plot->yAxis->setRange(0, 2.6);
plot->xAxis2->setRange(0, 3.0*M_PI);
plot->yAxis2->setRange(-70, 35);
// set pi ticks on top axis:
plot->xAxis2->setTicker(QSharedPointer<QCPAxisTickerPi>(new QCPAxisTickerPi));
// add title layout element:
// plot->plotLayout()->insertRow(0);
// plot->plotLayout()->addElement(0, 0, new QCPTextElement(plot, "Way too many graphs in one plot", QFont("sans", 12, QFont::Bold)));
// set labels:
plot->xAxis->setLabel("Bottom axis with outward ticks");
plot->yAxis->setLabel("Left axis label");
plot->xAxis2->setLabel("Top axis label");
plot->yAxis2->setLabel("Right axis label");
// make ticks on bottom axis go outward:
plot->xAxis->setTickLength(0, 5);
plot->xAxis->setSubTickLength(0, 3);
// make ticks on right axis go inward and outward:
plot->yAxis2->setTickLength(3, 3);
plot->yAxis2->setSubTickLength(1, 1);
// plot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft | Qt::AlignTop);
plot->replot();
}
void MainWindow::setupLogarithmicDemo(int row, int col, bool openGl)
{
QWidget *widget = new QWidget();
widget->setWindowTitle("Logarithmic Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->setNoAntialiasingOnDrag(true); // more performance/responsiveness during dragging
plot->addGraph();
QPen pen;
pen.setColor(QColor(255,170,100));
pen.setWidth(2);
pen.setStyle(Qt::DotLine);
plot->graph(0)->setPen(pen);
plot->graph(0)->setName("x");
plot->addGraph();
plot->graph(1)->setPen(QPen(Qt::red));
plot->graph(1)->setBrush(QBrush(QColor(255, 0, 0, 20)));
plot->graph(1)->setName("-sin(x)exp(x)");
plot->addGraph();
plot->graph(2)->setPen(QPen(Qt::blue));
plot->graph(2)->setBrush(QBrush(QColor(0, 0, 255, 20)));
plot->graph(2)->setName(" sin(x)exp(x)");
plot->addGraph();
pen.setColor(QColor(0,0,0));
pen.setWidth(1);
pen.setStyle(Qt::DashLine);
plot->graph(3)->setPen(pen);
plot->graph(3)->setBrush(QBrush(QColor(0,0,0,15)));
plot->graph(3)->setLineStyle(QCPGraph::lsStepCenter);
plot->graph(3)->setName("x!");
const int dataCount = 200;
const int dataFactorialCount = 21;
QVector<QCPGraphData> dataLinear(dataCount), dataMinusSinExp(dataCount), dataPlusSinExp(dataCount), dataFactorial(dataFactorialCount);
for (int i=0; i<dataCount; ++i)
{
dataLinear[i].key = i/10.0;
dataLinear[i].value = dataLinear[i].key;
dataMinusSinExp[i].key = i/10.0;
dataMinusSinExp[i].value = -qSin(dataMinusSinExp[i].key)*qExp(dataMinusSinExp[i].key);
dataPlusSinExp[i].key = i/10.0;
dataPlusSinExp[i].value = qSin(dataPlusSinExp[i].key)*qExp(dataPlusSinExp[i].key);
}
for (int i=0; i<dataFactorialCount; ++i)
{
dataFactorial[i].key = i;
dataFactorial[i].value = 1.0;
for (int k=1; k<=i; ++k) dataFactorial[i].value *= k; // factorial
}
plot->graph(0)->data()->set(dataLinear);
plot->graph(1)->data()->set(dataMinusSinExp);
plot->graph(2)->data()->set(dataPlusSinExp);
plot->graph(3)->data()->set(dataFactorial);
plot->yAxis->grid()->setSubGridVisible(true);
plot->xAxis->grid()->setSubGridVisible(true);
plot->yAxis->setScaleType(QCPAxis::stLogarithmic);
plot->yAxis2->setScaleType(QCPAxis::stLogarithmic);
QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
plot->yAxis->setTicker(logTicker);
plot->yAxis2->setTicker(logTicker);
plot->yAxis->setNumberFormat("eb"); // e = exponential, b = beautiful decimal powers
plot->yAxis->setNumberPrecision(0); // makes sure "1*10^4" is displayed only as "10^4"
plot->xAxis->setRange(0, 19.9);
plot->yAxis->setRange(1e-2, 1e10);
// make range draggable and zoomable:
plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
// make top right axes clones of bottom left axes:
plot->axisRect()->setupFullAxesBox();
// connect signals so top and right axes move in sync with bottom and left axes:
connect(plot->xAxis, SIGNAL(rangeChanged(QCPRange)), plot->xAxis2, SLOT(setRange(QCPRange)));
connect(plot->yAxis, SIGNAL(rangeChanged(QCPRange)), plot->yAxis2, SLOT(setRange(QCPRange)));
plot->legend->setVisible(true);
plot->legend->setBrush(QBrush(QColor(255,255,255,150)));
// make legend align in top left corner or axis rect
plot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft|Qt::AlignTop);
plot->replot();
}
void MainWindow::setupRealtimeDataDemo(int row, int col, bool openGl)
{
QTimer *timer = new QTimer();
QWidget *widget = new QWidget();
widget->setWindowTitle("Real Time Data Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->addGraph(); // blue line
plot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
plot->addGraph(); // red line
plot->graph(1)->setPen(QPen(QColor(255, 110, 40)));
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
plot->xAxis->setTicker(timeTicker);
plot->axisRect()->setupFullAxesBox();
plot->yAxis->setRange(-1.2, 1.2);
// make left and bottom axes transfer their ranges to right and top axes:
connect(plot->xAxis, SIGNAL(rangeChanged(QCPRange)), plot->xAxis2, SLOT(setRange(QCPRange)));
connect(plot->yAxis, SIGNAL(rangeChanged(QCPRange)), plot->yAxis2, SLOT(setRange(QCPRange)));
connect(timer, &QTimer::timeout,
[=]() {
static QTime timeData(QTime::currentTime());
// calculate two new data points:
double key = timeData.elapsed()/1000.0; // time elapsed since start of demo, in seconds
static double lastPointKeyData = 0;
if (key-lastPointKeyData > 0.002) // at most add point every 2 ms
{
// add data to lines:
plot->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
plot->graph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
// rescale value (vertical) axis to fit the current data:
//ui->customPlot->graph(0)->rescaleValueAxis();
//ui->customPlot->graph(1)->rescaleValueAxis(true);
lastPointKeyData = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
plot->xAxis->setRange(key, 10, Qt::AlignRight);
plot->replot();
plot->graph(0)->data()->removeBefore(key-11);
plot->graph(1)->data()->removeBefore(key-11);
// calculate frames per second:
static double lastFpsKeyData;
static int frameCountData;
++frameCountData;
if (key-lastFpsKeyData > 2) // average fps over 2 seconds
{
bar->showMessage(
QString("%1 FPS, Total Data points: %2")
.arg(frameCountData/(key-lastFpsKeyData), 0, 'f', 0)
.arg(plot->graph(0)->data()->size()+plot->graph(1)->data()->size())
, 0);
lastFpsKeyData = key;
frameCountData = 0;
}
}
);
timer->start(0); // Interval 0 means to refresh as fast as possible
}
void MainWindow::setupRealtimeThresholdDemo(int row, int col, bool openGl)
{
QTimer *timer = new QTimer();
QWidget *widget = new QWidget();
widget->setWindowTitle("Real Time Threshold Demo");
widget->setMinimumHeight(400);
widget->setMaximumHeight(400);
QCustomPlot *plot = new QCustomPlot();
plot->setOpenGl(openGl);
QStatusBar *bar = new QStatusBar();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(new QLabel(widget->windowTitle()), 0);
layout->addWidget(plot, 1);
layout->addWidget(bar, 0);
widget->setLayout(layout);
_loGrid->addWidget(widget,row,col,1,1);
plot->legend->setVisible(false);
plot->addGraph(); // blue line
plot->graph(0)->setPen(QPen(Qt::blue));
plot->addGraph(); // blue line
plot->graph(1)->setPen(QPen(Qt::green));
plot->addGraph(); // blue line
plot->graph(2)->setPen(QPen(Qt::red));
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
plot->xAxis->setTicker(timeTicker);
plot->axisRect()->setupFullAxesBox();
plot->yAxis->setRange(-1.5, 1.5);
QCPItemLine *lineBottom = new QCPItemLine(plot);
lineBottom->setPen(QPen(Qt::green, 1, Qt::DashLine));
lineBottom->start->setTypeX(QCPItemPosition::ptAxisRectRatio);
lineBottom->start->setTypeY(QCPItemPosition::ptPlotCoords);
lineBottom->start->setAxisRect(plot->axisRect());
lineBottom->start->setAxes(0, plot->yAxis);
lineBottom->start->setCoords(0, -1.0);
lineBottom->end->setTypeX(QCPItemPosition::ptAxisRectRatio);
lineBottom->end->setTypeY(QCPItemPosition::ptPlotCoords);
lineBottom->end->setAxisRect(plot->axisRect());
lineBottom->end->setAxes(0, plot->yAxis);
lineBottom->end->setCoords(1, -1.0);
QCPItemLine *lineTop = new QCPItemLine(plot);
lineTop->setPen(QPen(Qt::red, 1, Qt::DashLine));