forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegend.cpp
More file actions
299 lines (240 loc) 路 8.99 KB
/
Copy pathlegend.cpp
File metadata and controls
299 lines (240 loc) 路 8.99 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
//
// Created by Alan Freitas on 10/07/20.
//
#include <matplot/core/axes_type.h>
#include <matplot/core/legend.h>
#include <matplot/util/common.h>
namespace matplot {
legend::legend(class axes_type *parent) : legend(parent, {}) {}
legend::legend(class axes_type *parent,
std::initializer_list<std::string> names)
: legend(parent, std::vector(names)) {}
legend::legend(class axes_type *parent,
const std::vector<std::string> &names)
: strings_(names), parent_(parent) {
if (names.empty() && !parent_->children().empty()) {
for (size_t i = 0; i < parent_->children().size(); ++i) {
strings_.emplace_back("data" + num2str(i + 1));
}
}
}
void legend::touch() { parent_->touch(); }
const std::string &legend::operator[](size_t index) const {
return strings_.operator[](index);
}
std::string &legend::operator[](size_t index) {
return strings_.operator[](index);
}
size_t legend::size() const { return strings_.size(); }
bool legend::empty() const { return strings_.empty(); }
std::vector<std::string>::const_iterator legend::begin() const {
return strings_.begin();
}
std::vector<std::string>::iterator legend::begin() {
return strings_.begin();
}
std::vector<std::string>::const_iterator legend::end() const {
return strings_.end();
}
std::vector<std::string>::iterator legend::end() { return strings_.end(); }
const std::vector<std::string> &legend::strings() const { return strings_; }
std::vector<std::string> &legend::strings() { return strings_; }
void legend::strings(const std::vector<std::string> &strings) {
strings_ = strings;
touch();
}
void legend::inside(bool inside) {
if (inside != inside_) {
inside_ = inside;
touch();
}
}
bool legend::inside() const { return inside_; }
bool legend::label_after_sample() const { return label_after_sample_; }
void legend::label_after_sample(bool label_after_sample) {
if (label_after_sample != label_after_sample_) {
label_after_sample_ = label_after_sample;
touch();
}
}
bool legend::box() const { return box_; }
void legend::box(bool box) {
box_ = box;
touch();
}
const line_spec &legend::box_line() const { return box_line_; }
line_spec &legend::box_line() { return box_line_; }
void legend::box_line(const line_spec &box_line) {
box_line_ = box_line;
touch();
}
bool legend::vertical() const { return vertical_; }
void legend::vertical(bool vertical) {
if (vertical != vertical_) {
vertical_ = vertical;
touch();
}
}
bool legend::horizontal() const { return !vertical(); }
void legend::horizontal(bool horizontal) { vertical(!horizontal); }
legend::general_alignment legend::location() const {
switch (horizontal_location_) {
case horizontal_alignment::left:
switch (vertical_location_) {
case vertical_alignment::top:
return general_alignment::topleft;
case vertical_alignment::center:
return general_alignment::left;
case vertical_alignment::bottom:
return general_alignment::bottomleft;
}
break;
case horizontal_alignment::center:
switch (vertical_location_) {
case vertical_alignment::top:
return general_alignment::top;
case vertical_alignment::center:
return general_alignment::center;
case vertical_alignment::bottom:
return general_alignment::bottom;
}
break;
case horizontal_alignment::right:
switch (vertical_location_) {
case vertical_alignment::top:
return general_alignment::topright;
case vertical_alignment::center:
return general_alignment::right;
case vertical_alignment::bottom:
return general_alignment::bottomright;
}
break;
}
throw std::logic_error("legend::location: could not find the legend location");
}
void legend::location(general_alignment alignment) {
switch (alignment) {
case general_alignment::topleft:
horizontal_location_ = horizontal_alignment::left;
vertical_location_ = vertical_alignment::top;
break;
case general_alignment::top:
horizontal_location_ = horizontal_alignment::center;
vertical_location_ = vertical_alignment::top;
break;
case general_alignment::topright:
horizontal_location_ = horizontal_alignment::right;
vertical_location_ = vertical_alignment::top;
break;
case general_alignment::left:
horizontal_location_ = horizontal_alignment::left;
vertical_location_ = vertical_alignment::center;
break;
case general_alignment::center:
horizontal_location_ = horizontal_alignment::center;
vertical_location_ = vertical_alignment::center;
break;
case general_alignment::right:
horizontal_location_ = horizontal_alignment::right;
vertical_location_ = vertical_alignment::center;
break;
case general_alignment::bottomleft:
horizontal_location_ = horizontal_alignment::left;
vertical_location_ = vertical_alignment::bottom;
break;
case general_alignment::bottom:
horizontal_location_ = horizontal_alignment::center;
vertical_location_ = vertical_alignment::bottom;
break;
case general_alignment::bottomright:
horizontal_location_ = horizontal_alignment::right;
vertical_location_ = vertical_alignment::bottom;
break;
}
touch();
}
legend::horizontal_alignment legend::horizontal_location() const {
return horizontal_location_;
}
void legend::horizontal_location(
legend::horizontal_alignment horizontal_location) {
horizontal_location_ = horizontal_location;
}
legend::vertical_alignment legend::vertical_location() const {
return vertical_location_;
}
void
legend::vertical_location(legend::vertical_alignment vertical_location) {
vertical_location_ = vertical_location;
}
bool legend::manual_position() const { return manual_position_; }
void legend::manual_position(bool manual_position) {
manual_position_ = manual_position;
touch();
}
const std::array<float, 2> &legend::position() const { return position_; }
void legend::position(const std::array<float, 2> &position) {
position_ = position;
manual_position_ = true;
touch();
}
bool legend::invert() const { return invert_; }
void legend::invert(bool invert) {
invert_ = invert;
touch();
}
bool legend::visible() const { return visible_; }
void legend::visible(bool visible) {
if (visible_ != visible) {
visible_ = visible;
if (strings_.empty() && !parent_->children().empty()) {
for (size_t i = 0; i < parent_->children().size(); ++i) {
strings_.emplace_back("data" + num2str(i + 1));
}
}
}
touch();
}
const std::string &legend::title() const { return title_; }
void legend::title(std::string_view title) {
title_ = title;
touch();
}
const std::string &legend::font_name() const { return font_name_; }
void legend::font_name(std::string_view font_name) {
font_name_ = font_name;
touch();
}
float legend::font_size() const { return font_size_; }
void legend::font_size(float font_size) {
font_size_ = font_size;
touch();
}
const std::string &legend::font_angle() const { return font_angle_; }
void legend::font_angle(std::string_view font_angle) {
font_angle_ = font_angle;
touch();
}
const std::string &legend::font_weight() const { return font_weight_; }
void legend::font_weight(std::string_view font_weight) {
font_weight_ = font_weight;
touch();
}
const color_array &legend::text_color() const { return text_color_; }
void legend::text_color(const color_array &text_color) {
text_color_ = text_color;
touch();
}
size_t legend::num_columns() const { return num_columns_; }
void legend::num_columns(size_t num_columns) {
num_columns_ = num_columns;
num_rows_ = 0;
touch();
}
size_t legend::num_rows() const { return num_rows_; }
void legend::num_rows(size_t num_rows) {
num_rows_ = num_rows;
num_columns_ = 0;
touch();
}
} // namespace matplot