32#include "attributes.hpp"
39 template <
class... opt_t>
41 using value_type = tag::view_t;
42 std::vector<std::string> nodes;
43 std::vector<std::pair<std::size_t,std::size_t>> edges;
44 std::tuple<opt_t...> opts;
46 std::pair<std::size_t,std::size_t> natural()
const {
47 return std::apply([](
const auto&... o){
48 return impl::natural_size(std::size_t{500}, std::size_t{500}, o...); }, opts);
50 void draw_into(canvas_t& cv,
float x,
float y,
float w,
float h)
const {
51 std::apply([&](
const auto&... o){
52 const theme_t& th = impl::resolve_theme(o...);
53 auto [title, xl, yl] = impl::texts(o...);
54 using attribute_t = plot::attribute::element_t;
56 cv.group(
static_cast<std::size_t
>(x),
static_cast<std::size_t
>(y), attribute_t{},
59 { attribute_t bg; bg.color = plot::attribute::color_t{ th.bg };
60 cv.rect(0,0, w, h, 0,0, bg); }
61 float top = title.empty() ? 8.0f : 26.0f;
63 attribute_t a; a.color = plot::attribute::color_t{ th.fg };
64 a.font = plot::attribute::font_t{
"Arial, sans-serif",
"bold", 13u};
65 a.align = plot::attribute::align_t::center;
66 cv.text(title, std::size_t(w/2), std::size_t(16), a);
68 const std::size_t n = nodes.size();
70 float cx = w*0.5f, cy = top + (h - top)*0.5f;
71 float R = std::max(10.0f, std::min(w, h - top)*0.5f - 28.0f);
73 auto pos = [&](std::size_t i)->std::pair<float,float>{
74 double ang = -1.5707963267948966 + 2.0*3.141592653589793*double(i)/double(n);
75 return { cx + R*float(std::cos(ang)), cy + R*float(std::sin(ang)) };
79 attribute_t ea; ea.color = plot::attribute::color_t{ th.grid };
80 ea.stroke = plot::attribute::stroke_t{1.0f, 1.2f, {}, {}, {}};
81 for(
auto [a,b] : edges){
82 if( a >= n || b >= n )
continue;
83 auto [ax,ay] = pos(a);
auto [bx,by] = pos(b);
84 cv.line(ax, ay, bx, by, ea);
87 for(std::size_t i=0;i<n;++i){
88 auto [nx,ny] = pos(i);
89 std::uint32_t col = th.series.empty()? th.fg
90 : th.series[i % th.series.size()];
91 attribute_t na; na.color = plot::attribute::color_t{ col };
92 cv.circle(nx, ny, 7.0f, na);
93 attribute_t ta; ta.color = plot::attribute::color_t{ th.fg };
94 ta.font = plot::attribute::font_t{
"Arial, sans-serif",
"normal", 10u};
95 ta.align = plot::attribute::align_t::center;
97 float lx = nx + (nx - cx)*0.16f, ly = ny + (ny - cy)*0.16f;
98 cv.text(nodes[i], std::size_t(lx < 0 ? 0 : lx), std::size_t(ly + 3), ta);
107 template <
class... opt_t>
108 impl::graph_view<opt_t...> graph(std::vector<std::string> nodes,
109 std::vector<std::pair<std::size_t,std::size_t>> edges, opt_t... opts){
110 return impl::graph_view<opt_t...>{ std::move(nodes), std::move(edges),
111 std::make_tuple(opts...) };