-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacet.cpp
More file actions
169 lines (155 loc) · 4.93 KB
/
facet.cpp
File metadata and controls
169 lines (155 loc) · 4.93 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
#include "gnuplotpp/facet.hpp"
#include <algorithm>
#include <cmath>
#include <limits>
#include <stdexcept>
namespace gnuplotpp {
std::pair<int, int> facet_grid(const int n) {
if (n <= 0) {
return {1, 1};
}
const int cols = static_cast<int>(std::ceil(std::sqrt(static_cast<double>(n))));
const int rows = static_cast<int>(std::ceil(static_cast<double>(n) / static_cast<double>(cols)));
return {rows, cols};
}
void apply_facet_axes(Figure& fig, const AxesSpec& base, const std::vector<std::string>& titles) {
const auto& spec = fig.spec();
const int total = spec.rows * spec.cols;
for (int i = 0; i < total; ++i) {
AxesSpec ax = base;
if (i < static_cast<int>(titles.size())) {
ax.title = titles[static_cast<std::size_t>(i)];
}
fig.axes(i).set(ax);
}
}
void apply_panel_titles(Figure& fig, const std::vector<std::string>& titles) {
const auto& spec = fig.spec();
const int total = spec.rows * spec.cols;
for (int i = 0; i < total && i < static_cast<int>(titles.size()); ++i) {
auto ax = fig.axes(i).spec();
ax.title = titles[static_cast<std::size_t>(i)];
fig.axes(i).set(ax);
}
}
void apply_shared_legend(Figure& fig, const LegendSpec& legend, const int anchor_axes_index) {
const auto& spec = fig.spec();
const int total = spec.rows * spec.cols;
if (anchor_axes_index < 0 || anchor_axes_index >= total) {
throw std::out_of_range("anchor_axes_index out of range");
}
for (int i = 0; i < total; ++i) {
auto ax = fig.axes(i).spec();
if (i == anchor_axes_index) {
ax.legend = legend.enabled;
ax.legend_spec = legend;
} else {
ax.legend = false;
}
fig.axes(i).set(ax);
}
}
void apply_shared_colorbar_label(Figure& fig, const std::string& label, int owner_axes_index) {
const auto& spec = fig.spec();
const int total = spec.rows * spec.cols;
if (owner_axes_index < 0) {
owner_axes_index = total - 1;
}
if (owner_axes_index < 0 || owner_axes_index >= total) {
throw std::out_of_range("owner_axes_index out of range");
}
for (int i = 0; i < total; ++i) {
auto ax = fig.axes(i).spec();
ax.colorbar_label = (i == owner_axes_index) ? label : "";
fig.axes(i).set(ax);
}
}
LegendPosition auto_legend_position(std::span<const double> x, std::span<const double> y) {
if (x.empty() || y.empty() || x.size() != y.size()) {
return LegendPosition::TopRight;
}
double xmin = x.front();
double xmax = x.front();
double ymin = y.front();
double ymax = y.front();
for (std::size_t i = 1; i < x.size(); ++i) {
xmin = std::min(xmin, x[i]);
xmax = std::max(xmax, x[i]);
ymin = std::min(ymin, y[i]);
ymax = std::max(ymax, y[i]);
}
const double xspan = std::max(1e-12, xmax - xmin);
const double yspan = std::max(1e-12, ymax - ymin);
struct Candidate {
LegendPosition pos;
double cx;
double cy;
};
const Candidate corners[] = {
{LegendPosition::TopLeft, 0.1, 0.9},
{LegendPosition::TopRight, 0.9, 0.9},
{LegendPosition::BottomLeft, 0.1, 0.1},
{LegendPosition::BottomRight, 0.9, 0.1},
};
double best_score = std::numeric_limits<double>::infinity();
LegendPosition best = LegendPosition::TopRight;
for (const auto& c : corners) {
double score = 0.0;
for (std::size_t i = 0; i < x.size(); ++i) {
const double xn = (x[i] - xmin) / xspan;
const double yn = (y[i] - ymin) / yspan;
const double dx = xn - c.cx;
const double dy = yn - c.cy;
const double d2 = dx * dx + dy * dy;
score += 1.0 / (1e-3 + d2);
}
if (score < best_score) {
best_score = score;
best = c.pos;
}
}
return best;
}
void auto_place_legend(AxesSpec& ax, std::span<const double> x, std::span<const double> y) {
ax.legend = true;
ax.legend_spec.position = auto_legend_position(x, y);
}
void apply_small_multiples_defaults(Figure& fig,
const bool sync_x_limits,
const bool sync_y_limits) {
const auto& spec = fig.spec();
double gx_min = std::numeric_limits<double>::infinity();
double gx_max = -std::numeric_limits<double>::infinity();
double gy_min = std::numeric_limits<double>::infinity();
double gy_max = -std::numeric_limits<double>::infinity();
bool any = false;
for (const auto& ax : fig.all_axes()) {
for (const auto& s : ax.series()) {
for (std::size_t i = 0; i < s.x.size(); ++i) {
gx_min = std::min(gx_min, s.x[i]);
gx_max = std::max(gx_max, s.x[i]);
gy_min = std::min(gy_min, s.y[i]);
gy_max = std::max(gy_max, s.y[i]);
any = true;
}
}
}
if (!any) {
return;
}
for (int i = 0; i < spec.rows * spec.cols; ++i) {
auto ax = fig.axes(i).spec();
if (sync_x_limits) {
ax.has_xlim = true;
ax.xmin = gx_min;
ax.xmax = gx_max;
}
if (sync_y_limits) {
ax.has_ylim = true;
ax.ymin = gy_min;
ax.ymax = gy_max;
}
fig.axes(i).set(ax);
}
}
} // namespace gnuplotpp