forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.cpp
More file actions
270 lines (234 loc) 路 7.91 KB
/
Copy pathlabels.cpp
File metadata and controls
270 lines (234 loc) 路 7.91 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
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
//
// Created by Alan Freitas on 16/07/20.
//
#include <algorithm>
#include <cmath>
#include <matplot/axes_objects/labels.h>
#include <matplot/core/axes.h>
#include <matplot/util/common.h>
#include <sstream>
namespace matplot {
labels::labels(class axes *parent, const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<std::string> &labels,
const std::vector<double> &colors,
const std::vector<double> &sizes)
: axes_object(parent), x_(x), y_(y), labels_(labels), colors_(colors),
sizes_(sizes) {}
std::string labels::set_variables_string() {
std::string res;
if (rectangles_) {
auto word_size = [this](size_t word_index) {
if (sizes_.size() > word_index) {
return round(sizes_[word_index]);
} else {
return static_cast<double>(round(parent_->font_size()));
}
};
for (size_t i = 0; i < labels_.size(); ++i) {
double s = word_size(i);
res += " set object " + num2str(1000 + i) +
" rectangle at " + num2str(x_[i]) + ", " +
num2str(y_[i]) + " size " +
num2str(s * labels_[i].size() * width_factor) + ", " +
num2str(s * height_factor) + " fc lt 2 \n";
}
}
return res;
}
std::string labels::plot_string() {
std::string res = " '-' with labels";
switch (alignment_) {
case alignment::left:
res += " left";
break;
case alignment::right:
res += " right";
break;
case alignment::center:
res += " center";
break;
default:
break;
}
res += " font \"" + escape(font()) + "," +
num2str(unsigned(font_size())) + "\"";
if (!colors_.empty()) {
res += " textcolor palette";
} else {
res += " textcolor rgb '" + to_string(color_) + "'";
}
return res;
}
std::string labels::data_string() {
// in absolute size, the rectangles match the words when
// the ranges are x [-17;+17], y [-17,+17]
// if the ranges are smaller than that, we increase the font size
// if the ranges are larger than that, we reduce the font size
double font_size_factor = 1.0;
if (!absolute_size_) {
auto [xmin, xmax, ymin, ymax] = parent_->child_limits();
if (parent_->x_axis().limits_mode_manual()) {
auto [axmin, axmax] = parent_->x_axis().limits();
if (std::isfinite(axmin)) {
xmin = std::min(xmin, axmin);
}
if (std::isfinite(axmax)) {
xmax = std::max(xmax, axmax);
}
}
if (parent_->y_axis().limits_mode_manual()) {
auto [aymin, aymax] = parent_->y_axis().limits();
if (std::isfinite(aymin)) {
ymin = std::min(ymin, aymin);
}
if (std::isfinite(aymax)) {
ymax = std::max(ymax, aymax);
}
}
double xrange = xmax - xmin;
double yrange = ymax - ymin;
double xrange_increase = xrange / 34;
double yrange_increase = yrange / 34;
font_size_factor = (xrange_increase + yrange_increase) / 2.;
}
std::stringstream ss;
const bool custom_sizes = !sizes_.empty();
const bool custom_colors = !colors_.empty();
for (size_t i = 0; i < labels_.size(); ++i) {
ss << " " << x_[i] << " " << y_[i] << " \"";
if (custom_sizes) {
ss << "{/=" << round(sizes_[i] / font_size_factor) << " ";
}
ss << escape(labels_[i]);
if (custom_sizes) {
ss << "}";
}
ss << "\" ";
if (custom_colors) {
ss << colors_[i];
}
ss << "\n";
}
ss << "e\n";
return ss.str();
}
std::string labels::unset_variables_string() {
std::string res;
if (rectangles_) {
for (size_t i = 0; i < labels_.size(); ++i) {
res += " unset object " + num2str(1000 + i) + "\n";
}
}
return res;
}
double labels::xmax() {
auto it = std::max_element(x_.begin(), x_.end());
if (it != x_.end()) {
return *it + 0.2 + labels_[it - x_.begin()].size() * 0.01;
} else {
return axes_object::xmax();
}
}
double labels::xmin() {
auto it = std::min_element(x_.begin(), x_.end());
if (it != x_.end()) {
return *it - 0.2;
} else {
return axes_object::xmin();
}
}
double labels::ymax() {
auto it = std::max_element(y_.begin(), y_.end());
if (it != y_.end()) {
return *it + 0.2;
} else {
return axes_object::ymax();
}
}
double labels::ymin() {
auto it = std::min_element(y_.begin(), y_.end());
if (it != y_.end()) {
return *it - 0.2;
} else {
return axes_object::ymin();
}
}
enum axes_object::axes_category labels::axes_category() {
return axes_object::axes_category::two_dimensional;
}
bool labels::rectangles() const { return rectangles_; }
class labels &labels::rectangles(bool rectangles) {
rectangles_ = rectangles;
touch();
return *this;
}
bool labels::absolute_size() const { return absolute_size_; }
class labels &labels::absolute_size(bool absolute_size) {
absolute_size_ = absolute_size;
touch();
return *this;
}
enum labels::alignment labels::alignment() const { return alignment_; }
class labels &labels::alignment(enum labels::alignment alignment) {
alignment_ = alignment;
touch();
return *this;
}
const color_array &labels::color() const { return color_; }
class labels &labels::color(const color_array &color) {
color_ = color;
touch();
return *this;
}
const std::string &labels::font() const { return font_; }
class labels &labels::font(const std::string &font) {
font_ = font;
touch();
return *this;
}
float labels::font_size() const { return font_size_; }
class labels &labels::font_size(float font_size) {
font_size_ = font_size;
touch();
return *this;
}
const std::vector<double> &labels::x() const { return x_; }
class labels &labels::x(const std::vector<double> &x) {
x_ = x;
touch();
return *this;
}
const std::vector<double> &labels::y() const { return y_; }
class labels &labels::y(const std::vector<double> &y) {
y_ = y;
touch();
return *this;
}
const std::vector<std::string> &labels::label_values() const {
return labels_;
}
class labels &labels::label_values(const std::vector<std::string> &labels) {
labels_ = labels;
touch();
return *this;
}
const std::vector<double> &labels::colors() const { return colors_; }
class labels &labels::colors(const std::vector<double> &colors) {
colors_ = colors;
touch();
return *this;
}
const std::vector<double> &labels::sizes() const { return sizes_; }
class labels &labels::sizes(const std::vector<double> &sizes) {
sizes_ = sizes;
touch();
return *this;
}
bool labels::visible() const { return visible_; }
class labels &labels::visible(bool visible) {
visible_ = visible;
touch();
return *this;
}
} // namespace matplot