1use crate::theme::{LegendPosition, Theme};
2
3use super::Rect;
4
5pub struct PlotLayout {
7 pub total: Rect,
8 pub plot_area: Rect,
9 pub title_area: Rect,
10 pub subtitle_area: Rect,
11 pub caption_area: Rect,
12 pub x_axis_area: Rect,
13 pub y_axis_area: Rect,
14 pub legend_area: Rect,
15}
16
17impl PlotLayout {
18 pub fn compute(
20 width: f64,
21 height: f64,
22 theme: &Theme,
23 has_title: bool,
24 has_legend: bool,
25 ) -> Self {
26 Self::compute_full(
27 width, height, theme, has_title, false, false, has_legend, false,
28 )
29 }
30
31 #[allow(clippy::too_many_arguments)]
33 pub fn compute_full(
34 width: f64,
35 height: f64,
36 theme: &Theme,
37 has_title: bool,
38 has_subtitle: bool,
39 has_caption: bool,
40 has_legend: bool,
41 x_axis_top: bool,
42 ) -> Self {
43 let margin = &theme.plot_margin;
44
45 let title_height = if has_title {
46 theme.title.size * 2.0
47 } else {
48 0.0
49 };
50
51 let subtitle_height = if has_subtitle {
52 theme.subtitle.size * 1.5
53 } else {
54 0.0
55 };
56
57 let caption_height = if has_caption {
58 theme.caption.size * 1.8
59 } else {
60 0.0
61 };
62
63 let x_axis_height = theme.axis_ticks_length
64 + if theme.axis_text_x.visible {
65 if theme.axis_text_x.angle.abs() > 10.0 {
67 theme.axis_text_x.size * 5.0
68 } else {
69 (theme.axis_text_x.size + 4.0) * theme.axis_text_x_dodge.max(1) as f64
71 }
72 } else {
73 0.0
74 }
75 + if theme.axis_title_x.visible {
76 theme.axis_title_x.size + 8.0
77 } else {
78 0.0
79 };
80
81 let y_axis_width = theme.axis_ticks_length
82 + if theme.axis_text_y.visible {
83 theme.axis_text_y.size * 3.5 + 4.0
84 } else {
85 0.0
86 }
87 + if theme.axis_title_y.visible {
88 theme.axis_title_y.size + 8.0
89 } else {
90 0.0
91 };
92
93 let legend_size = if has_legend {
94 theme.legend_margin.left
95 + theme.legend_key_width
96 + theme.legend_spacing
97 + theme.legend_text.size * 6.0
98 + theme.legend_margin.right
99 } else {
100 0.0
101 };
102
103 let (legend_right, legend_left, legend_top, legend_bottom) = if has_legend {
105 match theme.legend_position {
106 LegendPosition::Right => (legend_size, 0.0, 0.0, 0.0),
107 LegendPosition::Left => (0.0, legend_size, 0.0, 0.0),
108 LegendPosition::Top => (0.0, 0.0, legend_size, 0.0),
109 LegendPosition::Bottom => (0.0, 0.0, 0.0, legend_size),
110 LegendPosition::None | LegendPosition::Inside(..) => (0.0, 0.0, 0.0, 0.0),
112 }
113 } else {
114 (0.0, 0.0, 0.0, 0.0)
115 };
116
117 let plot_x = margin.left + y_axis_width + legend_left;
118 let plot_y = margin.top
120 + title_height
121 + subtitle_height
122 + legend_top
123 + if x_axis_top { x_axis_height } else { 0.0 };
124 let plot_width =
125 width - margin.left - margin.right - y_axis_width - legend_right - legend_left;
126 let plot_height = height
127 - margin.top
128 - margin.bottom
129 - title_height
130 - subtitle_height
131 - caption_height
132 - x_axis_height
133 - legend_top
134 - legend_bottom;
135
136 let mut plot_width = plot_width.max(50.0);
137 let mut plot_height = plot_height.max(50.0);
138 let mut plot_x = plot_x;
139 let mut plot_y = plot_y;
140
141 if let Some(r) = theme.aspect_ratio {
144 if r > 0.0 {
145 let (avail_w, avail_h) = (plot_width, plot_height);
146 if avail_w * r <= avail_h {
147 plot_height = avail_w * r;
148 plot_y += (avail_h - plot_height) / 2.0;
149 } else {
150 plot_width = avail_h / r;
151 plot_x += (avail_w - plot_width) / 2.0;
152 }
153 }
154 }
155
156 PlotLayout {
157 total: Rect {
158 x: 0.0,
159 y: 0.0,
160 width,
161 height,
162 },
163 plot_area: Rect {
164 x: plot_x,
165 y: plot_y,
166 width: plot_width,
167 height: plot_height,
168 },
169 title_area: Rect {
170 x: plot_x,
171 y: margin.top,
172 width: plot_width,
173 height: title_height,
174 },
175 subtitle_area: Rect {
176 x: plot_x,
177 y: margin.top + title_height,
178 width: plot_width,
179 height: subtitle_height,
180 },
181 caption_area: Rect {
182 x: plot_x,
183 y: plot_y + plot_height + x_axis_height,
184 width: plot_width,
185 height: caption_height,
186 },
187 x_axis_area: Rect {
188 x: plot_x,
189 y: plot_y + plot_height,
190 width: plot_width,
191 height: x_axis_height,
192 },
193 y_axis_area: Rect {
194 x: margin.left + legend_left,
195 y: plot_y,
196 width: y_axis_width,
197 height: plot_height,
198 },
199 legend_area: Rect {
200 x: plot_x + plot_width,
201 y: plot_y,
202 width: legend_size,
203 height: plot_height,
204 },
205 }
206 }
207}