-
Notifications
You must be signed in to change notification settings - Fork 15
/
qcustomplot.cpp
15038 lines (13025 loc) · 506 KB
/
qcustomplot.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, a simple to use, modern plotting widget for Qt **
** Copyright (C) 2012 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.WorksLikeClockwork.com/ **
** Date: 09.06.12 **
****************************************************************************/
/*! \mainpage %QCustomPlot Documentation
Below is a brief overview of and guide to the classes and their relations. If you are new to
QCustomPlot and just want to start using it, it's recommended to look at the examples/tutorials
at
http://www.WorksLikeClockWork.com/index.php/components/qt-plotting-widget
This documentation is especially helpful when you're familiar with the basic concept of how to use
%QCustomPlot and you wish to learn more about specific functionality.
\section simpleoverview Simplified Class Overview
\image latex ClassesOverviewSimplified.png "" width=1.2\textwidth
\image html ClassesOverviewSimplified.png
<center>Simplified diagram of most important classes, view the \ref classoverview "Class Overview" to see a full overview.</center>
The central widget which displays the plottables and axes on its surface is QCustomPlot. Usually,
you don't create the axes yourself, but you use the ones already inside every QCustomPlot
instance (xAxis, yAxis, xAxis2, yAxis2).
\section plottables Plottables
\a Plottables are classes that display any kind of data inside the QCustomPlot. They all derive
from QCPAbstractPlottable. For example, the QCPGraph class is a plottable that displays a graph
inside the plot with different line styles, scatter styles, filling etc.
Since plotting graphs is such a dominant use case, QCustomPlot has a special interface for working
with QCPGraph plottables, that makes it very easy to handle them:\n
You create a new graph with QCustomPlot::addGraph and access them with QCustomPlot::graph.
For all other plottables, you need to use the normal plottable interface:\n
First, you create an instance of the plottable you want, e.g.
\code
QCPCurve *newCurve = new QCPCurve(customPlot->xAxis, customPlot->yAxis);\endcode
add it to the customPlot with QCustomPlot::addPlottable:
\code
customPlot->addPlottable(newCurve);\endcode
and then modify the properties of the newly created plottable via <tt>newCurve</tt>.
Plottables (including graphs) can be retrieved via QCustomPlot::plottable. Since the return type
of that function is the abstract base class of all plottables, QCPAbstractPlottable, you will
probably want to qobject_cast (or dynamic_cast) the returned pointer to the respective plottable
subclass. (As usual, if the cast returns zero, the plottable wasn't of that specific subclass.)
All further interfacing with plottables (e.g how to set data) is specific to the plottable type.
See the documentations of the subclasses: QCPGraph, QCPCurve, QCPBars, QCPStatisticalBox.
\section axes Controlling the Axes
As mentioned, QCustomPlot has four axes by default: \a xAxis (bottom), \a yAxis (left), \a xAxis2
(top), \a yAxis2 (right).
Their range is handled by the simple QCPRange class. You can set the range with the
QCPAxis::setRange function. By default, the axes represent a linear scale. To set a logarithmic
scale, set QCPAxis::setScaleType to QCPAxis::stLogarithmic. The logarithm base can be set freely
with QCPAxis::setScaleLogBase.
By default, an axis automatically creates and labels ticks in a sensible manner, i.e. with a tick
interval that's pleasing to the viewer. See the following functions for tick manipulation:\n
QCPAxis::setTicks, QCPAxis::setAutoTicks, QCPAxis::setAutoTickCount, QCPAxis::setAutoTickStep,
QCPAxis::setTickLabels, QCPAxis::setTickLabelType, QCPAxis::setTickLabelRotation,
QCPAxis::setTickStep, QCPAxis::setTickLength,...
Each axis can be given an axis label (e.g. "Voltage [mV]") with QCPAxis::setLabel.
The distance of an axis backbone to the respective QCustomPlot widget border is called its margin.
Normally, the margins are calculated automatically. To change this, set QCustomPlot::setAutoMargin
to false and set the margins manually with QCustomPlot::setMargin.
\section legend Plot Legend
Every QCustomPlot owns a QCPLegend (as \a legend). That's a small window inside the plot which
lists the plottables with an icon of the plottable line/symbol and a description. The Description
is retrieved from the plottable name (QCPAbstractPlottable::setName). Plottables can be added and
removed from the legend via \ref QCPAbstractPlottable::addToLegend and \ref
QCPAbstractPlottable::removeFromLegend. By default, adding a plottable to QCustomPlot
automatically adds it to the legend, too. This behaviour can be modified with the
QCustomPlot::setAutoAddPlottableToLegend property.
The QCPLegend provides an interface to access, add and remove legend items directly, too. See
QCPLegend::item, QCPLegend::itemWithPlottable, QCPLegend::addItem, QCPLegend::removeItem for
example.
\section userinteraction User Interactions
QCustomPlot currently supports dragging axis ranges with the mouse (\ref
QCustomPlot::setRangeDrag), zooming axis ranges with the mouse wheel (\ref
QCustomPlot::setRangeZoom) and a complete selection mechanism of most objects.
The availability of these interactions is controlled with \ref QCustomPlot::setInteractions. For
details about the interaction system, see the documentation there.
Further, QCustomPlot always emits corresponding signals, when objects are clicked or
doubleClicked. See \ref QCustomPlot::plottableClick, \ref QCustomPlot::plottableDoubleClick
and \ref QCustomPlot::axisClick for example.
\section items Items
Apart from plottables there is another category of plot objects that are important: Items. The
base class of all items is QCPAbstractItem. An item sets itself apart from plottables in that
it's not necessarily bound to any axes. This means it may also be positioned in absolute pixel
coordinates or placed at a relative position on the axis rect. Further it usually doesn't
represent data directly but acts as decoration, emphasis, description etc.
Multiple items can be arranged in a parent-child-hierarchy allowing for dynamical behaviour. For
example, you could place the head of an arrow at a certain plot coordinate, so it always points
to some important part of your data. The tail of the arrow can be fixed at a text label item
which always resides in the top center of the axis rect (independent of where the user drags the
axis ranges).
For a more detailed introduction, see the QCPAbstractItem documentation, and from there the
documentations of the individual built-in items, to find out how to use them.
\section performancetweaks Performance Tweaks
Although QCustomPlot is quite fast, some features like semi-transparent fills and antialiasing
can cause a significant slow down. Here are some thoughts on how to increase performance. By far
the most time is spent in the drawing functions, specifically the drawing of graphs. For maximum
performance, consider the following (most recommended/effective measures first):
\li use Qt 4.8.0 and up. Performance has doubled or tripled with respect to Qt 4.7.4. However they broke QPainter,
drawing pixel precise things, e.g. scatters, isn't possible with Qt 4.8.0/1. So it's a performance vs. plot
quality tradeoff when switching to Qt 4.8.
\li To increase responsiveness during dragging, consider setting \ref QCustomPlot::setNoAntialiasingOnDrag to true.
\li On X11 (linux), avoid the (slow) native drawing system, use raster by supplying
"-graphicssystem raster" as command line argument or calling QApplication::setGraphicsSystem("raster")
before creating the QApplication object.
\li On all operating systems, use OpenGL hardware acceleration by supplying "-graphicssystem
opengl" as command line argument or calling QApplication::setGraphicsSystem("opengl"). If OpenGL
is available, this will slightly decrease the quality of antialiasing, but extremely increase
performance especially with alpha (semi-transparent) fills, much antialiasing and a large
QCustomPlot drawing surface. Note however, that the maximum frame rate might be constrained by
the vertical sync frequency of your monitor (VSync can be disabled in the graphics card driver
configuration). So for simple plots (where the potential framerate is far above 60 frames per
second), OpenGL acceleration might achieve numerically lower frame rates than the other
graphics systems, because they are not capped at the VSync frequency.
\li Avoid any kind of alpha (transparency), especially in fills
\li Avoid any kind of antialiasing, especially in graph lines (see QCustomPlot::setNotAntialiasedElements)
\li Avoid repeatedly setting the complete data set with QCPGraph::setData. Use QCPGraph::addData instead, if most
data points stay unchanged, e.g. in a running measurement.
\li Set the \a copy parameter of the setData functions to false, so only pointers get
transferred. (Relevant only if preparing data maps with a large number of points, i.e. over 10000)
*/
/*! \page classoverview Class Overview
\image latex ClassesOverview.png "Overview of all classes and their relations" width=1.2\textwidth
\image html ClassesOverview.png "Overview of all classes and their relations"
*/
#include "qcustomplot.h"
// ================================================================================
// =================== QCPData
// ================================================================================
/*! \class QCPData
\brief Holds the data of one single data point for QCPGraph.
The stored data is:
\li \a key: coordinate on the key axis of this data point
\li \a value: coordinate on the value axis of this data point
\li \a keyErrorMinus: negative error in the key dimension (for error bars)
\li \a keyErrorPlus: positive error in the key dimension (for error bars)
\li \a valueErrorMinus: negative error in the value dimension (for error bars)
\li \a valueErrorPlus: positive error in the value dimension (for error bars)
\see QCPDataMap
*/
/*!
Constructs a data point with key, value and all errors set to zero.
*/
QCPData::QCPData() :
key(0),
value(0),
keyErrorPlus(0),
keyErrorMinus(0),
valueErrorPlus(0),
valueErrorMinus(0)
{
}
/*!
Constructs a data point with the specified \a key and \a value. All errors are set to zero.
*/
QCPData::QCPData(double key, double value) :
key(key),
value(value),
keyErrorPlus(0),
keyErrorMinus(0),
valueErrorPlus(0),
valueErrorMinus(0)
{
}
// ================================================================================
// =================== QCPCurveData
// ================================================================================
/*! \class QCPCurveData
\brief Holds the data of one single data point for QCPCurve.
The stored data is:
\li \a t: the free parameter of the curve at this curve point (cp. the mathematical vector <em>(x(t), y(t))</em>)
\li \a key: coordinate on the key axis of this curve point
\li \a value: coordinate on the value axis of this curve point
\see QCPCurveDataMap
*/
/*!
Constructs a curve data point with t, key and value set to zero.
*/
QCPCurveData::QCPCurveData() :
t(0),
key(0),
value(0)
{
}
/*!
Constructs a curve data point with the specified \a t, \a key and \a value.
*/
QCPCurveData::QCPCurveData(double t, double key, double value) :
t(t),
key(key),
value(value)
{
}
// ================================================================================
// =================== QCPBarData
// ================================================================================
/*! \class QCPBarData
\brief Holds the data of one single data point (one bar) for QCPBars.
The stored data is:
\li \a key: coordinate on the key axis of this bar
\li \a value: height coordinate on the value axis of this bar
\see QCPBarDataaMap
*/
/*!
Constructs a bar data point with key and value set to zero.
*/
QCPBarData::QCPBarData() :
key(0),
value(0)
{
}
/*!
Constructs a bar data point with the specified \a key and \a value.
*/
QCPBarData::QCPBarData(double key, double value) :
key(key),
value(value)
{
}
// ================================================================================
// =================== QCPGraph
// ================================================================================
/*! \class QCPGraph
\brief A plottable representing a graph in a plot.
Usually QCustomPlot creates it internally via QCustomPlot::addGraph and the resulting instance is
accessed via QCustomPlot::graph.
To plot data, assign it with the \ref setData or \ref addData functions.
\section appearance Changing the appearance
The appearance of the graph is mainly determined by the line style, scatter style, brush and pen
of the graph (\ref setLineStyle, \ref setScatterStyle, \ref setBrush, \ref setPen).
\subsection filling Filling under or between graphs
QCPGraph knows two types of fills: Normal graph fills towards the zero-value-line parallel to
the key axis of the graph, and fills between two graphs, called channel fills. To enable a fill,
just set a brush with \ref setBrush which is neither Qt::NoBrush nor fully transparent.
By default, a normal fill towards the zero-value-line will be drawn. To set up a channel fill
between this graph and another one, call \ref setChannelFillGraph with the other graph as
parameter.
\see QCustomPlot::addGraph, QCustomPlot::graph, QCPLegend::addGraph
*/
/*!
Constructs a graph which uses \a keyAxis as its key axis ("x") and \a valueAxis as its value
axis ("y"). \a keyAxis and \a valueAxis must reside in the same QCustomPlot instance and not have
the same orientation. If either of these restrictions is violated, a corresponding message is
printed to the debug output (qDebug), the construction is not aborted, though.
The constructed QCPGraph can be added to the plot with QCustomPlot::addPlottable, QCustomPlot
then takes ownership of the graph.
To directly create a graph inside a plot, you can also use the simpler QCustomPlot::addGraph function.
*/
QCPGraph::QCPGraph(QCPAxis *keyAxis, QCPAxis *valueAxis) :
QCPAbstractPlottable(keyAxis, valueAxis)
{
mData = new QCPDataMap;
setPen(QPen(Qt::blue));
setErrorPen(QPen(Qt::black));
setBrush(Qt::NoBrush);
setSelectedPen(QPen(QColor(80, 80, 255), 2.5));
setSelectedBrush(Qt::NoBrush);
setLineStyle(lsLine);
setScatterStyle(QCP::ssNone);
setScatterSize(6);
setErrorType(etNone);
setErrorBarSize(6);
setErrorBarSkipSymbol(true);
setChannelFillGraph(0);
}
QCPGraph::~QCPGraph()
{
if (mParentPlot)
{
// if another graph has a channel fill towards this graph, set it to zero
for (int i=0; i<mParentPlot->graphCount(); ++i)
{
if (mParentPlot->graph(i)->channelFillGraph() == this)
mParentPlot->graph(i)->setChannelFillGraph(0);
}
}
delete mData;
}
/*!
Replaces the current data with the provided \a data.
If \a copy is set to true, data points in \a data will only be copied. if false, the graph
takes ownership of the passed data and replaces the internal data pointer with it. This is
significantly faster than copying for large datasets.
*/
void QCPGraph::setData(QCPDataMap *data, bool copy)
{
if (copy)
{
*mData = *data;
} else
{
delete mData;
mData = data;
}
}
/*! \overload
Replaces the current data with the provided points in \a key and \a value pairs. The provided
vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
*/
void QCPGraph::setData(const QVector<double> &key, const QVector<double> &value)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
mData->insertMulti(newData.key, newData);
}
}
/*!
Replaces the current data with the provided points in \a key and \a value pairs. Additionally the
symmetrical value error of the data points are set to the values in \a valueError.
For error bars to show appropriately, see \ref setErrorType.
The provided vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
For asymmetrical errors (plus different from minus), see the overloaded version of this function.
*/
void QCPGraph::setDataValueError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &valueError)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
n = qMin(n, valueError.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
newData.valueErrorMinus = valueError[i];
newData.valueErrorPlus = valueError[i];
mData->insertMulti(key[i], newData);
}
}
/*!
\overload
Replaces the current data with the provided points in \a key and \a value pairs. Additionally the
negative value error of the data points are set to the values in \a valueErrorMinus, the positive
value error to \a valueErrorPlus.
For error bars to show appropriately, see \ref setErrorType.
The provided vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
*/
void QCPGraph::setDataValueError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &valueErrorMinus, const QVector<double> &valueErrorPlus)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
n = qMin(n, valueErrorMinus.size());
n = qMin(n, valueErrorPlus.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
newData.valueErrorMinus = valueErrorMinus[i];
newData.valueErrorPlus = valueErrorPlus[i];
mData->insertMulti(key[i], newData);
}
}
/*!
Replaces the current data with the provided points in \a key and \a value pairs. Additionally the
symmetrical key error of the data points are set to the values in \a keyError.
For error bars to show appropriately, see \ref setErrorType.
The provided vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
For asymmetrical errors (plus different from minus), see the overloaded version of this function.
*/
void QCPGraph::setDataKeyError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyError)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
n = qMin(n, keyError.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
newData.keyErrorMinus = keyError[i];
newData.keyErrorPlus = keyError[i];
mData->insertMulti(key[i], newData);
}
}
/*!
\overload
Replaces the current data with the provided points in \a key and \a value pairs. Additionally the
negative key error of the data points are set to the values in \a keyErrorMinus, the positive
key error to \a keyErrorPlus.
For error bars to show appropriately, see \ref setErrorType.
The provided vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
*/
void QCPGraph::setDataKeyError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyErrorMinus, const QVector<double> &keyErrorPlus)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
n = qMin(n, keyErrorMinus.size());
n = qMin(n, keyErrorPlus.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
newData.keyErrorMinus = keyErrorMinus[i];
newData.keyErrorPlus = keyErrorPlus[i];
mData->insertMulti(key[i], newData);
}
}
/*!
Replaces the current data with the provided points in \a key and \a value pairs. Additionally the
symmetrical key and value errors of the data points are set to the values in \a keyError and \a valueError.
For error bars to show appropriately, see \ref setErrorType.
The provided vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
For asymmetrical errors (plus different from minus), see the overloaded version of this function.
*/
void QCPGraph::setDataBothError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyError, const QVector<double> &valueError)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
n = qMin(n, valueError.size());
n = qMin(n, keyError.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
newData.keyErrorMinus = keyError[i];
newData.keyErrorPlus = keyError[i];
newData.valueErrorMinus = valueError[i];
newData.valueErrorPlus = valueError[i];
mData->insertMulti(key[i], newData);
}
}
/*!
\overload
Replaces the current data with the provided points in \a key and \a value pairs. Additionally the
negative key and value errors of the data points are set to the values in \a keyErrorMinus and \a valueErrorMinus. The positive
key and value errors are set to the values in \a keyErrorPlus \a valueErrorPlus.
For error bars to show appropriately, see \ref setErrorType.
The provided vectors should have equal length. Else, the number of added points will be the size of the
smallest vector.
*/
void QCPGraph::setDataBothError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyErrorMinus, const QVector<double> &keyErrorPlus, const QVector<double> &valueErrorMinus, const QVector<double> &valueErrorPlus)
{
mData->clear();
int n = key.size();
n = qMin(n, value.size());
n = qMin(n, valueErrorMinus.size());
n = qMin(n, valueErrorPlus.size());
n = qMin(n, keyErrorMinus.size());
n = qMin(n, keyErrorPlus.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = key[i];
newData.value = value[i];
newData.keyErrorMinus = keyErrorMinus[i];
newData.keyErrorPlus = keyErrorPlus[i];
newData.valueErrorMinus = valueErrorMinus[i];
newData.valueErrorPlus = valueErrorPlus[i];
mData->insertMulti(key[i], newData);
}
}
/*!
Sets how the single data points are connected in the plot or how they are represented visually
apart from the scatter symbol. For scatter-only plots, set \a ls to \ref lsNone and \ref
setScatterStyle to the desired scatter style.
\see setScatterStyle
*/
void QCPGraph::setLineStyle(LineStyle ls)
{
mLineStyle = ls;
}
/*!
Sets the visual appearance of single data points in the plot. If set to \ref QCP::ssNone, no scatter points
are drawn (e.g. for line-only-plots with appropriate line style).
\see ScatterStyle, setLineStyle
*/
void QCPGraph::setScatterStyle(QCP::ScatterStyle ss)
{
mScatterStyle = ss;
}
/*!
This defines how big (in pixels) single scatters are drawn, if scatter style (\ref
setScatterStyle) isn't \ref QCP::ssNone, \ref QCP::ssDot or \ref QCP::ssPixmap. Floating point values are
allowed for fine grained control over optical appearance with antialiased painting.
\see ScatterStyle
*/
void QCPGraph::setScatterSize(double size)
{
mScatterSize = size;
}
/*!
If the scatter style (\ref setScatterStyle) is set to ssPixmap, this function defines the QPixmap
that will be drawn centered on the data point coordinate.
\see ScatterStyle
*/
void QCPGraph::setScatterPixmap(const QPixmap &pixmap)
{
mScatterPixmap = pixmap;
}
/*!
Sets which kind of error bars (Key Error, Value Error or both) should be drawn on each data
point. If you set \a errorType to something other than \ref etNone, make sure to actually pass
error data via the specific setData functions along with the data points (e.g. \ref
setDataValueError, \ref setDataKeyError, \ref setDataBothError).
\see ErrorType
*/
void QCPGraph::setErrorType(ErrorType errorType)
{
mErrorType = errorType;
}
/*!
Sets the pen with which the error bars will be drawn.
\see setErrorBarSize, setErrorType
*/
void QCPGraph::setErrorPen(const QPen &pen)
{
mErrorPen = pen;
}
/*!
Sets the width of the handles at both ends of an error bar in pixels.
*/
void QCPGraph::setErrorBarSize(double size)
{
mErrorBarSize = size;
}
/*!
If \a enabled is set to true, the error bar will not be drawn as a solid line under the scatter symbol but
leave some free space around the symbol.
This feature uses the current scatter size (\ref setScatterSize) to determine the size of the
area to leave blank. So when drawing Pixmaps as scatter points (\ref QCP::ssPixmap), the scatter size
must be set manually to a value corresponding to the size of the Pixmap, if the error bars should
leave gaps to its boundaries.
*/
void QCPGraph::setErrorBarSkipSymbol(bool enabled)
{
mErrorBarSkipSymbol = enabled;
}
/*!
Sets the target graph for filling the area between this graph and \a targetGraph with the current
brush (\ref setBrush).
When \a targetGraph is set to 0, a normal graph fill will be produced. This means, when the brush
is not Qt::NoBrush or fully transparent, a fill all the way to the zero-value-line parallel to
the key axis of this graph will be drawn. To disable any filling, set the brush to Qt::NoBrush.
\see setBrush
*/
void QCPGraph::setChannelFillGraph(QCPGraph *targetGraph)
{
// prevent setting channel target to this graph itself:
if (targetGraph == this)
{
qDebug() << Q_FUNC_INFO << "targetGraph is this graph itself";
mChannelFillGraph = 0;
return;
}
// prevent setting channel target to a graph not in the plot:
if (targetGraph && targetGraph->mParentPlot != mParentPlot)
{
qDebug() << Q_FUNC_INFO << "targetGraph not in same plot";
mChannelFillGraph = 0;
return;
}
mChannelFillGraph = targetGraph;
}
/*!
Adds the provided data points in \a dataMap to the current data.
\see removeData
*/
void QCPGraph::addData(const QCPDataMap &dataMap)
{
mData->unite(dataMap);
}
/*! \overload
Adds the provided single data point in \a data to the current data.
\see removeData
*/
void QCPGraph::addData(const QCPData &data)
{
mData->insertMulti(data.key, data);
}
/*! \overload
Adds the provided single data point as \a key and \a value pair to the current data.
\see removeData
*/
void QCPGraph::addData(double key, double value)
{
QCPData newData;
newData.key = key;
newData.value = value;
mData->insertMulti(newData.key, newData);
}
/*! \overload
Adds the provided data points as \a key and \a value pairs to the current data.
\see removeData
*/
void QCPGraph::addData(const QVector<double> &keys, const QVector<double> &values)
{
int n = qMin(keys.size(), values.size());
QCPData newData;
for (int i=0; i<n; ++i)
{
newData.key = keys[i];
newData.value = values[i];
mData->insertMulti(newData.key, newData);
}
}
/*!
Removes all data points with keys smaller than \a key.
\see addData, clearData
*/
void QCPGraph::removeDataBefore(double key)
{
QCPDataMap::iterator it = mData->begin();
while (it != mData->end() && it.key() < key)
it = mData->erase(it);
}
/*!
Removes all data points with keys greater than \a key.
\see addData, clearData
*/
void QCPGraph::removeDataAfter(double key)
{
if (mData->isEmpty()) return;
QCPDataMap::iterator it = mData->upperBound(key);
while (it != mData->end())
it = mData->erase(it);
}
/*!
Removes all data points with keys between \a fromKey and \a toKey.
if \a fromKey is greater or equal to \a toKey, the function does nothing. To remove
a single data point with known key, use \ref removeData(double key).
\see addData, clearData
*/
void QCPGraph::removeData(double fromKey, double toKey)
{
if (fromKey >= toKey || mData->isEmpty()) return;
QCPDataMap::iterator it = mData->upperBound(fromKey);
QCPDataMap::iterator itEnd = mData->upperBound(toKey);
while (it != itEnd)
it = mData->erase(it);
}
/*! \overload
Removes a single data point at \a key. If the position is not known with absolute precision,
consider using \ref removeData(double fromKey, double toKey) with a small fuzziness interval around
the suspected position, depeding on the precision with which the key is known.
\see addData, clearData
*/
void QCPGraph::removeData(double key)
{
mData->remove(key);
}
/*!
Removes all data points.
\see removeData, removeDataAfter, removeDataBefore
*/
void QCPGraph::clearData()
{
mData->clear();
}
/* inherits documentation from base class */
double QCPGraph::selectTest(const QPointF &pos) const
{
if (mData->isEmpty() || !mVisible)
return -1;
return pointDistance(pos);
}
/*! \overload
Allows to define whether error bars are taken into consideration when determining the new axis
range.
*/
void QCPGraph::rescaleAxes(bool onlyEnlarge, bool includeErrorBars) const
{
rescaleKeyAxis(onlyEnlarge, includeErrorBars);
rescaleValueAxis(onlyEnlarge, includeErrorBars);
}
/*! \overload
Allows to define whether error bars (of kind \ref QCPGraph::etKey) are taken into consideration
when determining the new axis range.
*/
void QCPGraph::rescaleKeyAxis(bool onlyEnlarge, bool includeErrorBars) const
{
// this code is a copy of QCPAbstractPlottable::rescaleKeyAxis with the only change
// that getKeyRange is passed the includeErrorBars value.
if (mData->isEmpty()) return;
SignDomain signDomain = sdBoth;
if (mKeyAxis->scaleType() == QCPAxis::stLogarithmic)
signDomain = (mKeyAxis->range().upper < 0 ? sdNegative : sdPositive);
bool validRange;
QCPRange newRange = getKeyRange(validRange, signDomain, includeErrorBars);
if (validRange)
{
if (onlyEnlarge)
{
if (mKeyAxis->range().lower < newRange.lower)
newRange.lower = mKeyAxis->range().lower;
if (mKeyAxis->range().upper > newRange.upper)
newRange.upper = mKeyAxis->range().upper;
}
mKeyAxis->setRange(newRange);
}
}
/*! \overload
Allows to define whether error bars (of kind \ref QCPGraph::etValue) are taken into consideration
when determining the new axis range.
*/
void QCPGraph::rescaleValueAxis(bool onlyEnlarge, bool includeErrorBars) const
{
// this code is a copy of QCPAbstractPlottable::rescaleValueAxis with the only change
// is that getValueRange is passed the includeErrorBars value.
if (mData->isEmpty()) return;
SignDomain signDomain = sdBoth;
if (mValueAxis->scaleType() == QCPAxis::stLogarithmic)
signDomain = (mValueAxis->range().upper < 0 ? sdNegative : sdPositive);
bool validRange;
QCPRange newRange = getValueRange(validRange, signDomain, includeErrorBars);
if (validRange)
{
if (onlyEnlarge)
{
if (mValueAxis->range().lower < newRange.lower)
newRange.lower = mValueAxis->range().lower;
if (mValueAxis->range().upper > newRange.upper)
newRange.upper = mValueAxis->range().upper;
}
mValueAxis->setRange(newRange);
}
}
/* inherits documentation from base class */
void QCPGraph::draw(QCPPainter *painter)
{
if (mKeyAxis->range().size() <= 0 || mData->isEmpty()) return;
if (mLineStyle == lsNone && mScatterStyle == QCP::ssNone) return;
// allocate line and (if necessary) point vectors:
QVector<QPointF> *lineData = new QVector<QPointF>;
QVector<QCPData> *pointData = 0;
if (mScatterStyle != QCP::ssNone)
pointData = new QVector<QCPData>;
// fill vectors with data appropriate to plot style:
getPlotData(lineData, pointData);
// draw fill of graph:
drawFill(painter, lineData);
// draw line:
if (mLineStyle == lsImpulse)
drawImpulsePlot(painter, lineData);
else if (mLineStyle != lsNone)
drawLinePlot(painter, lineData); // also step plots can be drawn as a line plot
// draw scatters:
if (pointData)
drawScatterPlot(painter, pointData);
// free allocated line and point vectors:
delete lineData;
if (pointData)
delete pointData;
}
/* inherits documentation from base class */
void QCPGraph::drawLegendIcon(QCPPainter *painter, const QRect &rect) const
{
// draw fill:
if (mBrush.style() != Qt::NoBrush)
{
applyFillAntialiasingHint(painter);
painter->fillRect(QRectF(rect.left(), rect.top()+rect.height()/2.0, rect.width(), rect.height()/3.0), mBrush);
}
// draw line vertically centered:
if (mLineStyle != lsNone)
{
applyDefaultAntialiasingHint(painter);
painter->setPen(mPen);
painter->drawLine(QLineF(rect.left(), rect.top()+rect.height()/2.0, rect.right()+5, rect.top()+rect.height()/2.0)); // +5 on x2 else last segment is missing from dashed/dotted pens
}
// draw scatter symbol:
if (mScatterStyle != QCP::ssNone)
{
if (mScatterStyle == QCP::ssPixmap && (mScatterPixmap.size().width() > rect.width() || mScatterPixmap.size().height() > rect.height()))
{
// handle pixmap scatters that are larger than legend icon rect separately.
// We resize them and draw them manually, instead of calling drawScatter:
QSize newSize = mScatterPixmap.size();
newSize.scale(rect.size(), Qt::KeepAspectRatio);
QRect targetRect;
targetRect.setSize(newSize);
targetRect.moveCenter(rect.center());
bool smoothBackup = painter->testRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
painter->drawPixmap(targetRect, mScatterPixmap);
painter->setRenderHint(QPainter::SmoothPixmapTransform, smoothBackup);
} else
{
applyScattersAntialiasingHint(painter);
painter->setPen(mPen);
painter->drawScatter(QRectF(rect).center().x(), QRectF(rect).center().y(), mScatterSize, mScatterStyle);
}
}
}
/*!
\internal
This function branches out to the line style specific "get(...)PlotData" functions, according to the
line style of the graph.
\param lineData will be filled with raw points that will be drawn with the according draw functions, e.g. \ref drawLinePlot and \ref drawImpulsePlot.
These aren't necessarily the original data points, since for step plots for example, additional points are needed for drawing lines that make up steps.
If the line style of the graph is \ref lsNone, the \a lineData vector will be left untouched.
\param pointData will be filled with the original data points so \ref drawScatterPlot can draw the scatter symbols accordingly. If no scatters need to be
drawn, i.e. scatter style is \ref QCP::ssNone, pass 0 as \a pointData, and this step will be skipped.
\see getScatterPlotData, getLinePlotData, getStepLeftPlotData, getStepRightPlotData, getStepCenterPlotData, getImpulsePlotData
*/
void QCPGraph::getPlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const
{
switch(mLineStyle)
{
case lsNone: getScatterPlotData(pointData); break;
case lsLine: getLinePlotData(lineData, pointData); break;
case lsStepLeft: getStepLeftPlotData(lineData, pointData); break;
case lsStepRight: getStepRightPlotData(lineData, pointData); break;
case lsStepCenter: getStepCenterPlotData(lineData, pointData); break;
case lsImpulse: getImpulsePlotData(lineData, pointData); break;
}
}
/*!
\internal
If line style is \ref lsNone and scatter style is not \ref QCP::ssNone, this function serves at providing the
visible data points in \a pointData, so the \ref drawScatterPlot function can draw the scatter points
accordingly.
If line style is not \ref lsNone, this function is not called and the data for the scatter points
are (if needed) calculated inside the corresponding other "get(...)PlotData" functions.
\see drawScatterPlot
*/
void QCPGraph::getScatterPlotData(QVector<QCPData> *pointData) const
{
if (!pointData) return;
// get visible data range:
QCPDataMap::const_iterator lower, upper;
int dataCount;
getVisibleDataBounds(lower, upper, dataCount);
// prepare vectors:
if (pointData)
pointData->resize(dataCount);