-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinePlot.cpp
More file actions
152 lines (128 loc) · 3.38 KB
/
LinePlot.cpp
File metadata and controls
152 lines (128 loc) · 3.38 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
#include "LinePlot.h"
#include <limits>
#include <QChartView>
#include <QLineSeries>
#include <QValueAxis>
#include <QLegend>
#include "Exceptions.h"
#include "Log.h"
#include "BasicStatistics.h"
#include "PlotUtils.h"
LinePlot::LinePlot()
: yrange_set_(false)
{
}
void LinePlot::addLine(const QVector<double>& values, QString label)
{
if (xvalues_.count()!=0 && values.count()!=xvalues_.count())
{
THROW(ArgumentException, "Plot '" + label + "' has " + QString::number(values.count()) + " values, but " + QString::number(xvalues_.count()) + " are expected because x axis values are set!");
}
lines_.append(PlotLine(values, label));
}
void LinePlot::setXValues(const QVector<double>& xvalues)
{
if (lines_.count()!=0)
{
THROW(ProgrammingException, "You have to set x axis values of LinePlot before adding any lines!");
}
xvalues_ = xvalues;
}
void LinePlot::store(QString filename)
{
if (lines_.isEmpty())
{
Log::warn("LinePlot does not have any lines to plot");
return;
}
if (!yrange_set_)
{
double min_val = std::numeric_limits<double>::max();
double max_val = -std::numeric_limits<double>::max();
for (const PlotLine& line : lines_)
{
for (double value : line.values)
{
min_val = std::min(value, min_val);
max_val = std::max(value, max_val);
}
}
if (max_val > min_val)
{
ymin_ = min_val - 0.01 * (max_val - min_val);
ymax_ = max_val + 0.01 * (max_val - min_val);
}
}
PlotUtils* plot_utils = new PlotUtils();
QChart* chart = plot_utils->getChart();
// create axes
QValueAxis* axis_x = new QValueAxis();
if (!xlabel_.isEmpty()) axis_x->setTitleText(xlabel_);
double xmin = std::numeric_limits<double>::max();
double xmax = -std::numeric_limits<double>::max();
for (double v : xvalues_)
{
if (!std::isfinite(v)) continue;
if (v < xmin) xmin = v;
if (v > xmax) xmax = v;
}
axis_x->setRange(xmin, xmax);
axis_x->applyNiceNumbers();
chart->addAxis(axis_x, Qt::AlignBottom);
QValueAxis* axis_y = new QValueAxis();
if (!ylabel_.isEmpty()) axis_y->setTitleText(ylabel_);
if (BasicStatistics::isValidFloat(ymin_) && BasicStatistics::isValidFloat(ymax_))
{
axis_y->setRange(ymin_, ymax_);
}
axis_y->applyNiceNumbers();
chart->addAxis(axis_y, Qt::AlignLeft);
// series of data points
for (const PlotLine& line : lines_)
{
QLineSeries* series = new QLineSeries();
series->setName(line.label);
int n = line.values.size();
for (int i = 0; i < n; ++i)
{
double x = (i < xvalues_.size()) ? xvalues_[i] : i;
series->append(x, line.values[i]);
}
chart->addSeries(series);
series->attachAxis(axis_x);
series->attachAxis(axis_y);
// improves line smoothness (optional) to look very close to matplotlib
QPen pen = series->pen();
pen.setWidthF(1.5);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
series->setPen(pen);
}
// title and legend
if (lines_.count() == 1)
{
if (!lines_[0].label.isEmpty())
{
chart->setTitle(lines_[0].label);
}
chart->legend()->setVisible(false);
}
else
{
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
QFont legendFont = chart->legend()->font();
legendFont.setPointSize(8);
chart->legend()->setFont(legendFont);
}
plot_utils->applyFontSettings();
plot_utils->saveAsPng(filename, 600, 400);
}
LinePlot::PlotLine::PlotLine()
{
}
LinePlot::PlotLine::PlotLine(const QVector<double>& v, QString l)
: values(v)
, label(l)
{
}