CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
plot.cppm
Go to the documentation of this file.
1// Copyright (c) 2024, Victor Castillo, All rights reserved.
2module;
4
5export module cydui.std.charts.plot;
6
7import std;
8
9import cydui;
10
11export import fabric.linalg;
12import fabric.logging;
13
14export import :axis;
15export import :grid;
16export import :series;
17export import :view;
18
19export using la = with_precision<double>;
20
21namespace charts {
22 export COMPONENT(plot, {
23 std::vector<data_series_t> series{};
24 };
25 axis_t<plot> top_axis {*this, {1,0}, {0,-1}, {1,0}, false, 20, 50};
26 axis_t<plot> bottom_axis {*this, {1,0}, {0,1}, {1,0}, true, 20};
27 axis_t<plot> left_axis {*this, {0,-1}, {-1,0}, {1,0}, true, 30, 60};
28 axis_t<plot> right_axis {*this, {0,-1}, {1,0}, {1,0}, false, 25, 55};
29 grid_t<plot> grid {*this};
30 view_map_t<plot> series {*this};
31
32 explicit plot(std::initializer_list<data_series_t>&& series_): cydui::components::component_t<plot>(), props(series_) {
33 for (std::size_t i = 0; i < series_.size(); ++i) {
34 (void)series[i];
35 }
36 }
37
38 plot(const plot& rhl): cydui::components::component_t<plot>() {
39 props = rhl.props;
40 top_axis = rhl.top_axis;
41 bottom_axis = rhl.bottom_axis;
42 left_axis = rhl.left_axis;
43 right_axis = rhl.right_axis;
44 grid = rhl.grid;
45 grid.set_ref(*this);
46 series = rhl.series;
47 series.set_ref(*this);
48 }
49 ) {
50 ON_REDRAW {
52 double title_h = 0;
53
54 double axis_title_size = 25;
55
56 double top_axis_h = component.top_axis.show_? 40: 0;
57 double bottom_axis_h = component.bottom_axis.show_? 40: 0;
58 double left_axis_w = component.left_axis.show_? 50: 0;
59 double right_axis_w = component.right_axis.show_? 50: 0;
60
61 if (top_axis_h > 0 && !component.top_axis.title_.empty()) {
62 top_axis_h += axis_title_size;
63 }
64 if (bottom_axis_h > 0 && !component.bottom_axis.title_.empty()) {
65 bottom_axis_h += axis_title_size;
66 }
67 if (left_axis_w > 0 && !component.left_axis.title_.empty()) {
68 left_axis_w += axis_title_size;
69 }
70 if (right_axis_w > 0 && !component.right_axis.title_.empty()) {
71 right_axis_w += axis_title_size;
72 }
73
74 std::vector<cydui::components::component_holder_t> result{};
75
76 result.emplace_back(component.grid.build_component(
77 screen_measure{left_axis_w},
78 screen_measure{title_h + top_axis_h},
79 $width - left_axis_w - right_axis_w,
80 $height - title_h - top_axis_h - bottom_axis_h
81 ));
82 for (auto& serie: component.series.build_component(
83 screen_measure{left_axis_w},
84 screen_measure{title_h + top_axis_h},
85 $width - left_axis_w - right_axis_w,
86 $height - title_h - top_axis_h - bottom_axis_h
87 )) {
88 result.emplace_back(serie);
89 }
90 if (top_axis_h > 0) {
91 result.emplace_back(component.top_axis.build_component(
92 screen_measure{left_axis_w},
93 screen_measure{title_h},
94 $width - left_axis_w - right_axis_w,
95 screen_measure{top_axis_h}
96 ));
97 }
98 if (bottom_axis_h > 0) {
99 result.emplace_back(component.bottom_axis.build_component(
100 screen_measure{left_axis_w},
101 $height - bottom_axis_h,
102 $width - left_axis_w - right_axis_w,
103 screen_measure{bottom_axis_h}
104 ));
105 }
106 if (left_axis_w > 0) {
107 result.emplace_back(component.left_axis.build_component(
108 0_px,
109 screen_measure{title_h + top_axis_h},
110 screen_measure{left_axis_w},
111 $height - title_h - top_axis_h - bottom_axis_h
112 ));
113 }
114 if (right_axis_w > 0) {
115 result.emplace_back(component.right_axis.build_component(
116 $width - right_axis_w,
117 screen_measure{title_h + top_axis_h},
118 screen_measure{right_axis_w},
119 $height - title_h - top_axis_h - bottom_axis_h
120 ));
121 }
122
123 return result;
124 }
125
126 FRAGMENT {
127 fragment.append(vg::rectangle { }
128 .w($width).h($height)
129 .fill("#000000"_color)
130 );
131 }
132 };
133}
#define FRAGMENT
#define COMPONENT(NAME,...)
#define ON_REDRAW
quantify::quantity_t< screen::pixel, double > screen_measure
Definition _types.cppm:21
quantify::quantity_t< screen::pixel, double > screen_measure
Definition _types.cppm:21
with_precision< double > la
Definition plot.cppm:19
std::vector< data_series_t > series
Definition plot.cppm:22
view_map_t< plot > series
Definition plot.cppm:22
plot(std::initializer_list< data_series_t > &&series_)
Definition plot.cppm:22
props_t props
Definition plot.cppm:22