forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork.h
More file actions
278 lines (221 loc) 路 8.92 KB
/
Copy pathnetwork.h
File metadata and controls
278 lines (221 loc) 路 8.92 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
//
// Created by Alan Freitas on 2020-07-04.
//
#ifndef MATPLOTPLUSPLUS_NETWORK_H
#define MATPLOTPLUSPLUS_NETWORK_H
#include <matplot/detail/config.h>
#include <matplot/core/figure_type.h>
#include <matplot/util/concepts.h>
#include <matplot/util/handle_types.h>
#include <matplot/core/axes_object.h>
#include <matplot/core/line_spec.h>
#include <matplot/util/common.h>
#include <array>
namespace matplot {
class axes_type;
class MATPLOT_EXPORTS network : public axes_object {
public:
enum class layout { automatic, force, circle, kawai, random };
public:
explicit network(class axes_type *parent);
network(class axes_type *parent,
const std::vector<std::pair<size_t, size_t>> &edges,
const std::vector<double> &weights, size_t n_vertices,
std::string_view line_spec = "");
/// If we receive an axes_handle, we can convert it to a raw
/// pointer because there is no ownership involved here
template <class... Args>
network(const axes_handle &parent, Args &&...args)
: network(parent.get(), std::forward<Args>(args)...) {}
virtual ~network() = default;
public /* mandatory virtual functions */:
std::string plot_string() override;
std::string legend_string(std::string_view title) override;
std::string data_string() override;
double xmax() override;
double xmin() override;
double ymax() override;
double ymin() override;
enum axes_object::axes_category axes_category() override;
public /* getters and setters */:
class network &line_style(std::string_view line_spec);
const matplot::line_spec &line_spec() const;
matplot::line_spec &line_spec();
class network &line_spec(const class line_spec &line_spec);
const std::vector<double> &y_data() const;
class network &y_data(const std::vector<double> &y_data);
const std::vector<double> &x_data() const;
class network &x_data(const std::vector<double> &x_data);
const std::vector<double> &z_data() const;
class network &z_data(const std::vector<double> &z_data);
const std::vector<size_t> &marker_indices() const;
class network &
marker_indices(const std::vector<size_t> &marker_indices);
bool use_y2() const;
class network &use_y2(bool use_y_2);
bool use_y_2() const;
class network &use_y_2(bool use_y_2);
bool show_labels() const;
class network &show_labels(bool show_labels);
bool directed() const;
class network &directed(bool directed);
const std::vector<std::string> &edge_labels() const;
class network &edge_labels(const std::vector<std::string> &edge_labels);
template <class C>
network &edge_labels(const IterableValues<C> &e_labels) {
std::vector<std::string> str_labels;
std::stringstream ss;
ss.precision(10);
ss << std::fixed;
for (const auto &edge_label : e_labels) {
ss << "{/:Italic " << edge_label << " }";
str_labels.emplace_back(ss.str());
ss.str("");
}
edge_labels(str_labels);
return *this;
}
const std::vector<std::string> &node_labels() const;
class network &node_labels(const std::vector<std::string> &node_labels);
template <class C>
network &node_labels(const IterableValues<C> &e_labels) {
std::vector<std::string> str_labels;
std::stringstream ss;
ss.precision(10);
ss << std::fixed;
for (const auto &edge_label : e_labels) {
ss << edge_label;
str_labels.emplace_back(ss.str());
ss.str("");
}
node_labels(str_labels);
return *this;
}
const std::vector<double> &line_widths() const;
class network &line_widths(const std::vector<double> &line_widths);
const std::vector<std::pair<size_t, size_t>> &edges() const;
class network &
edges(const std::vector<std::pair<size_t, size_t>> &edges);
class network &n_vertices(size_t n_vertices);
const vector_1d &weights() const;
class network &weights(const vector_1d &weights);
layout layout_algorithm() const;
class network &layout_algorithm(layout layout_algorithm);
double layout_k() const;
class network &layout_k(double layout_k);
int layout_iterations() const;
class network &layout_iterations(int layout_iterations);
double energy_threshold() const;
class network &energy_threshold(double energy_threshold);
const std::vector<float> &marker_sizes() const;
class network &marker_sizes(const std::vector<float> &marker_sizes);
const std::vector<double> &marker_colors() const;
bool visible() const;
class network &visible(bool visible);
public /* getters and setters bypassing the line_spec */:
float line_width() const;
class network &line_width(float line_width);
enum line_spec::marker_style marker_style() const;
template <class T> network &marker_style(T marker_style) {
line_spec_.marker_style(marker_style);
return *this;
}
enum line_spec::marker_style marker() const;
template <class T> network &marker(T marker) {
line_spec_.marker(marker);
return *this;
}
float marker_size() const;
class network &marker_size(float size);
class network &marker_size(const std::vector<float> &size_vector);
class network &marker_size(const std::vector<double> &size_vector);
bool marker_face() const;
class network &marker_face(bool size);
const std::array<float, 4> &color() const;
template <class T> network &color(T c) {
line_spec().color(c);
return *this;
}
inline class network &color(std::initializer_list<float> c) {
line_spec().color(c);
return *this;
}
const std::array<float, 4> &marker_color() const;
template <class T> network &marker_color(T c) {
line_spec().marker_color(c);
return *this;
}
template <class T> network &node_color(T c) {
marker_color(c);
return *this;
}
inline class network &marker_color(std::initializer_list<float> c) {
line_spec().marker_color(c);
return *this;
}
inline class network &marker_colors(const std::vector<double> &cs) {
marker_colors_ = cs;
touch();
return *this;
}
const std::array<float, 4> &marker_face_color() const;
template <class T> network &marker_face_color(T c) {
line_spec().marker_face_color(c);
return *this;
}
inline class network &
marker_face_color(std::initializer_list<float> c) {
line_spec().marker_face_color(c);
return *this;
}
inline float marker_face_alpha() {
return line_spec().marker_face_alpha();
}
inline class network &marker_face_alpha(float a) {
line_spec().marker_face_alpha(a);
return *this;
}
size_t n_vertices();
protected:
virtual std::vector<line_spec::style_to_plot> styles_to_plot();
void maybe_update_line_spec();
void maybe_update_graph_layout();
void infer_n_vertices();
void process_random_layout();
void process_force_layout();
void process_circle_layout();
void process_kawai_layout();
protected:
/// Line style
class line_spec line_spec_;
/// Original graph
std::vector<std::pair<size_t, size_t>> edges_{};
/// The number of number is declared by the user or infered from the
/// edges
size_t n_vertices_{0};
/// Weight for each edge
vector_1d weights_{0};
/// True for directed graphs
bool directed_{false};
/// Data in the xlim
layout layout_algorithm_{layout::automatic};
double layout_k_ = -1.;
int layout_iterations_ = -1;
double energy_threshold_ = 1e-2;
std::vector<double> x_data_{};
std::vector<double> y_data_{};
std::vector<double> z_data_{};
/// Style
std::vector<float> marker_sizes_{};
std::vector<double> marker_colors_{};
bool show_labels_{true};
std::vector<std::string> edge_labels_{};
std::vector<std::string> node_labels_{};
std::vector<double> line_widths_{};
/// Use the y2 xlim
bool use_y2_{false};
/// True if visible
bool visible_{true};
};
} // namespace matplot
#endif // MATPLOTPLUSPLUS_NETWORK_H