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 plot_width = plot_width.max(50.0);
137 let plot_height = plot_height.max(50.0);
138
139 PlotLayout {
140 total: Rect {
141 x: 0.0,
142 y: 0.0,
143 width,
144 height,
145 },
146 plot_area: Rect {
147 x: plot_x,
148 y: plot_y,
149 width: plot_width,
150 height: plot_height,
151 },
152 title_area: Rect {
153 x: plot_x,
154 y: margin.top,
155 width: plot_width,
156 height: title_height,
157 },
158 subtitle_area: Rect {
159 x: plot_x,
160 y: margin.top + title_height,
161 width: plot_width,
162 height: subtitle_height,
163 },
164 caption_area: Rect {
165 x: plot_x,
166 y: plot_y + plot_height + x_axis_height,
167 width: plot_width,
168 height: caption_height,
169 },
170 x_axis_area: Rect {
171 x: plot_x,
172 y: plot_y + plot_height,
173 width: plot_width,
174 height: x_axis_height,
175 },
176 y_axis_area: Rect {
177 x: margin.left + legend_left,
178 y: plot_y,
179 width: y_axis_width,
180 height: plot_height,
181 },
182 legend_area: Rect {
183 x: plot_x + plot_width,
184 y: plot_y,
185 width: legend_size,
186 height: plot_height,
187 },
188 }
189 }
190}