forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxis_type.cpp
More file actions
357 lines (298 loc) 路 10.7 KB
/
Copy pathaxis_type.cpp
File metadata and controls
357 lines (298 loc) 路 10.7 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
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
//
// Created by Alan Freitas on 2020-07-05.
//
#include <cmath>
#include <matplot/core/axes_type.h>
#include <matplot/core/axis_type.h>
#include <matplot/core/figure_type.h>
#include <matplot/util/colors.h>
#include <matplot/util/common.h>
namespace matplot {
axis_type::axis_type() : axis_type(nullptr, inf, inf) {}
axis_type::axis_type(class axes_type *parent) : axis_type(parent, inf, inf) {}
axis_type::axis_type(class axes_type *parent, bool visible)
: axis_type(parent, inf, inf, visible) {}
axis_type::axis_type(class axes_type *parent, double min, double max)
: axis_type(parent, min, max, true) {}
axis_type::axis_type(class axes_type *parent, double min, double max, bool visible)
: parent_(parent), limits_({min, max}), visible_(visible) {}
void axis_type::touch() { parent_->touch(); }
std::array<double, 2> axis_type::limits() const {
if (parent_->parent()->backend()->consumes_gnuplot_commands()) {
return limits_;
} else {
if (!std::isfinite(limits_[0]) || !std::isfinite(limits_[1])) {
return {-10,+10};
} else {
return limits_;
}
}
}
class axis_type &axis_type::limits(const std::array<double, 2> &limits) {
limits_ = limits;
limits_mode_manual(true);
touch();
return *this;
}
bool axis_type::limits_mode_auto() const {
return limits_mode_auto_ ||
(!std::isfinite(limits_[0]) && !std::isfinite(limits_[0]));
}
bool axis_type::limits_mode_manual() const { return !limits_mode_auto_; }
class axis_type &axis_type::limits_mode_auto(bool limits_mode_auto) {
limits_mode_auto_ = limits_mode_auto;
parent_->touch();
return *this;
}
class axis_type &axis_type::limits_mode_manual(bool limits_mode_manual) {
limits_mode_auto_ = !limits_mode_manual;
parent_->touch();
return *this;
}
std::string axis_type::range_string() const {
if (!reverse_) {
std::string r = "[";
if (!limits_mode_auto_ && std::isfinite(limits_[0])) {
r += std::to_string(limits_[0]);
}
r += ":";
if (!limits_mode_auto_ && std::isfinite(limits_[1])) {
r += std::to_string(limits_[1]);
}
r += "] noreverse";
return r;
} else {
std::string r = "[";
if (!limits_mode_auto_ && std::isfinite(limits_[1])) {
r += std::to_string(limits_[1]);
}
r += ":";
if (!limits_mode_auto_ && std::isfinite(limits_[0])) {
r += std::to_string(limits_[0]);
}
r += "] reverse";
return r;
}
}
bool axis_type::reverse() const { return reverse_; }
class axis_type &axis_type::reverse(bool reverse) {
reverse_ = reverse;
parent_->touch();
return *this;
}
const color_array &axis_type::color() const { return color_; }
class axis_type &axis_type::color(const color_array &color) {
color_ = color;
touch();
return *this;
}
class axis_type &axis_type::color(std::string_view c) {
color(string_to_color(c));
return *this;
}
class axis_type &axis_type::color(const enum color &c) {
return color(to_array(c));
}
const std::string &axis_type::label() const { return label_; }
class axis_type &axis_type::label(std::string_view label) {
label_ = label;
touch();
return *this;
}
std::string axis_type::label_string() const {
std::string cmd = " \"";
if (label_weight_ == "bold") {
cmd += "{/:Bold ";
}
if (label_weight_ == "italic") {
cmd += "{/:Italic ";
}
cmd += escape(label());
if (label_weight_ == "bold") {
cmd += "}";
}
if (label_weight_ == "italic") {
cmd += "}";
}
cmd += "\"";
if (parent_->parent()->backend()->supports_fonts()) {
cmd += " font \"" + escape(parent_->font()) + "," +
num2str(unsigned(label_font_size_)) + "\"";
cmd += " textcolor \"" + to_string(label_color_) + "\"";
}
return cmd;
}
const std::string &axis_type::tick_label_format() const {
return tick_label_format_;
}
class axis_type &
axis_type::tick_label_format(std::string_view tick_label_format) {
if (tick_label_format == "usd") {
tick_label_format_ = "$%.2f";
} else if (tick_label_format == "degrees") {
tick_label_format_ = "%g掳";
} else if (tick_label_format == "percentage") {
tick_label_format_ = "%g%%";
} else if (tick_label_format == "auto") {
tick_label_format_ = "%g";
} else {
tick_label_format_ = tick_label_format;
}
touch();
return *this;
}
bool axis_type::tick_values_automatic() const { return tick_values_automatic_; }
class axis_type &
axis_type::tick_values_automatic(bool tick_values_automatic) {
tick_values_automatic_ = tick_values_automatic;
touch();
return *this;
}
bool axis_type::tick_values_manual() const { return !tick_values_automatic_; }
class axis_type &axis_type::tick_values_manual(bool tick_values_manual) {
tick_values_automatic_ = !tick_values_manual;
touch();
return *this;
}
const std::vector<double> &axis_type::tick_values() const {
return tick_values_;
}
class axis_type &
axis_type::tick_values(const std::vector<double> &tick_values) {
tick_values_ = tick_values;
tick_values_automatic_ = false;
touch();
return *this;
}
const std::vector<std::string> &axis_type::ticklabels() const {
return ticklabels_;
}
class axis_type &
axis_type::ticklabels(const std::vector<std::string> &ticklabels) {
ticklabels_ = ticklabels;
if (ticklabels.empty()) {
tick_values({});
}
touch();
return *this;
}
bool axis_type::ticklabels_mode() const { return ticklabels_mode_; }
class axis_type &axis_type::ticklabels_mode(bool ticklabels_mode) {
ticklabels_mode_ = ticklabels_mode;
touch();
return *this;
}
std::string axis_type::tick_values_string(bool minor_ticks) const {
std::string r = " (";
// Future work: use calcticks inside tick_values and
// ticklabels whenever we call this function
// regardless of whether the ticks are automatic.
// This requires keeping preprocessed data in this object.
// std::vector<double> values = tick_values();
// std::vector<std::string> labels = ticklabels();
// And proceed with these labels.
// We can then always use manual labels in the gnuplot
// backend, which would make axes more consistent
// with our format labels and avoid approximation
// errors in polar plots. It would also be useful
// for a future opengl backend, where we always
// need explicit labels.
for (size_t i = 0; i < tick_values_.size(); ++i) {
if (i != 0) {
r += ", ";
}
if (ticklabels_mode() && ticklabels().size() > i) {
r += "\"" + escape(ticklabels_[i]) + "\" ";
} else {
// Unfortunately, gnuplot cancels the formatting
// when we set the tic values explicitly.
// So if there is no explicit label but there is
// a format we want to apply, we also need to explicitly
// create a label according to our format.
r += "\"" +
escape(num2str(tick_values_[i], tick_label_format_)) +
"\" ";
}
r += num2str(tick_values_[i]);
if (minor_ticks) {
r += " 0";
if (i != tick_values_.size() - 1) {
double m_tick =
(tick_values_[i] + tick_values_[i + 1]) / 2.;
r += ", " + num2str(m_tick) + " 1";
}
}
}
r += ")";
return r;
}
std::string axis_type::tick_rotate_string() const {
if (tickangle_ == 0) {
return " norotate";
} else {
return " rotate by " + num2str(tickangle_) + " right";
}
}
axis_type::axis_scale axis_type::scale() const { return scale_; }
class axis_type &axis_type::scale(axis_type::axis_scale scale) {
if (scale != scale_) {
scale_ = scale;
touch();
}
return *this;
}
bool axis_type::visible() const { return visible_; }
class axis_type &axis_type::visible(bool visible) {
visible_ = visible;
touch();
return *this;
}
float axis_type::tick_length() const { return tick_length_; }
class axis_type &axis_type::tick_length(float tick_length) {
tick_length_ = tick_length;
touch();
return *this;
}
bool axis_type::zero_axis() const { return zero_axis_; }
class axis_type &axis_type::zero_axis(bool zero_axis) {
zero_axis_ = zero_axis;
touch();
return *this;
}
bool axis_type::geographic() const { return geographic_; }
class axis_type &axis_type::geographic(bool geographic) {
geographic_ = geographic;
touch();
return *this;
}
bool axis_type::on_axis() const { return on_axis_; }
class axis_type &axis_type::on_axis(bool on_axis) {
on_axis_ = on_axis;
touch();
return *this;
}
const std::string &axis_type::label_weight() const { return label_weight_; }
class axis_type &axis_type::label_weight(std::string_view label_weight) {
label_weight_ = label_weight;
touch();
return *this;
}
float axis_type::label_font_size() const { return label_font_size_; }
class axis_type &axis_type::label_font_size(float label_font_size) {
label_font_size_ = label_font_size;
touch();
return *this;
}
const color_array &axis_type::label_color() const { return label_color_; }
class axis_type &axis_type::label_color(const color_array &label_color) {
label_color_ = label_color;
touch();
return *this;
}
float axis_type::tickangle() const { return tickangle_; }
class axis_type &axis_type::tickangle(float tickangle) {
tickangle_ = tickangle;
touch();
return *this;
}
} // namespace matplot