25#include "attributes.hpp"
31#ifndef PLOT_WIREFRAME_DEFINED
32#define PLOT_WIREFRAME_DEFINED
36 struct wireframe {
using value_type = tag::wireframe_t; };
41 template <
class... opt_t>
43 using value_type = tag::view_t;
44 std::vector<std::string> labels;
45 std::vector<double> values;
46 std::tuple<opt_t...> opts;
48 std::pair<std::size_t,std::size_t> natural()
const {
49 return std::apply([](
const auto&... o){
50 return impl::natural_size(std::size_t{640}, std::size_t{400}, o...); }, opts);
52 void draw_into(canvas_t& cv,
float x,
float y,
float w,
float h)
const {
53 std::apply([&](
const auto&... o){
54 const theme_t& th = impl::resolve_theme(o...);
55 auto [title, xl, yl] = impl::texts(o...);
56 const std::size_t n = std::min(labels.size(), values.size());
57 constexpr bool wire = arg::tpos<tag::wireframe_t, opt_t...>::present;
59 double vmax = 0.0, vmin = 0.0;
60 for(std::size_t i=0;i<n;++i){
61 if( values[i] > vmax ) vmax = values[i];
62 if( values[i] < vmin ) vmin = values[i];
64 if( vmax <= vmin ) vmax = vmin + 1.0;
68 sx.lo = -0.5; sx.hi = double(n) - 0.5;
69 for(std::size_t i=0;i<n;++i){ sx.ticks.push_back(
double(i)); sx.labels.push_back(labels[i]); }
70 impl::scale_t sy = impl::make_scale(vmin, vmax,
false, 10.0);
72 impl::render_panel_into(cv, x, y,
73 static_cast<std::size_t
>(w),
static_cast<std::size_t
>(h),
74 th, sx, sy, title, xl, yl, {},
75 [&](impl::canvas_t& c,
auto px,
auto py){
76 using attribute_t = plot::attribute::element_t;
77 const float y0 = py(0.0);
78 const float cellw = (px(1.0) - px(0.0));
79 const float bw = std::max(1.0f, cellw * 0.7f);
80 for(std::size_t i=0;i<n;++i){
81 float cx = px(
double(i));
82 float yt = py(values[i]);
83 std::uint32_t col = th.series.empty()? th.fg
84 : th.series[i % th.series.size()];
85 float top = std::min(yt, y0);
86 float hgt = std::abs(y0 - yt);
87 float xl0 = cx - bw*0.5f, xr0 = cx + bw*0.5f;
88 float yb0 = top + hgt;
91 attribute_t a; a.color = plot::attribute::color_t{ col };
92 a.stroke = plot::attribute::stroke_t{1.0f, 1.5f, {}, {}, {}};
93 std::vector<float> X{xl0, xr0, xr0, xl0, xl0};
94 std::vector<float> Y{top, top, yb0, yb0, top};
97 attribute_t a; a.color = plot::attribute::color_t{ col };
98 c.rect(xl0, top, bw, hgt, 0, 0, a);
108 template <
class... opt_t>
109 impl::bar_view<opt_t...> bar(std::vector<std::string> labels,
110 std::vector<double> values, opt_t... opts){
111 return impl::bar_view<opt_t...>{ std::move(labels), std::move(values),
112 std::make_tuple(opts...) };