-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqcpl_utils.cpp
74 lines (63 loc) · 2.35 KB
/
qcpl_utils.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
#include "qcpl_types.h"
#include "qcpl_axis_factor.h"
#include "qcustomplot/qcustomplot.h"
#include <QRandomGenerator>
namespace QCPL {
GraphData makeRandomSample(int count, double height)
{
QVector<double> xs(count);
QVector<double> ys(count);
auto rnd = QRandomGenerator::global();
double y = (rnd->generate()%100)*height*0.01;
for (int i = 0; i < count; i++)
{
y = qAbs(y + (rnd->generate()%100)*height*0.01 - height*0.5);
xs[i] = i;
ys[i] = y;
}
return {xs, ys};
}
Qt::Alignment legendLocation(QCPLegend* legend)
{
// https://www.qcustomplot.com/index.php/tutorials/basicplotting
// 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:
return legend->parentPlot()->axisRect()->insetLayout()->insetAlignment(0);
}
void setLegendLocation(QCPLegend* legend, Qt::Alignment align)
{
legend->parentPlot()->axisRect()->insetLayout()->setInsetAlignment(0, align);
}
QMargins legendMargins(QCPLegend* legend)
{
return legend->parentPlot()->axisRect()->insetLayout()->margins();
}
void setLegendMargins(QCPLegend* legend, const QMargins& margins)
{
legend->parentPlot()->axisRect()->insetLayout()->setMargins(margins);
}
void updateAxisTicker(QCPAxis* axis)
{
QSharedPointer<QCPAxisTicker> curTicker = axis->ticker();
QSharedPointer<QCPAxisTicker> newTicker;
auto curTickerRaw = curTicker.get();
if (auto factorTicker = dynamic_cast<FactorAxisTicker*>(curTickerRaw); factorTicker)
curTickerRaw = factorTicker->prevTicker.get();
if (axis->scaleType() == QCPAxis::stLogarithmic && !dynamic_cast<QCPAxisTickerLog*>(curTickerRaw))
newTicker.reset(new QCPAxisTickerLog);
else if (axis->scaleType() == QCPAxis::stLinear && dynamic_cast<QCPAxisTickerLog*>(curTickerRaw))
newTicker.reset(new QCPAxisTicker);
if (newTicker && curTicker)
{
newTicker->setTickStepStrategy(curTicker->tickStepStrategy());
newTicker->setTickCount(curTicker->tickCount());
newTicker->setTickOrigin(curTicker->tickOrigin());
if (auto factorTicker = dynamic_cast<FactorAxisTicker*>(curTicker.get()); factorTicker)
{
factorTicker->prevTicker = newTicker;
newTicker = curTicker;
}
axis->setTicker(newTicker);
}
}
} // namespace QCPL