1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::theme::{LegendPosition, Theme};
use super::Rect;
/// Computed layout areas for the plot.
pub struct PlotLayout {
pub total: Rect,
pub plot_area: Rect,
pub title_area: Rect,
pub subtitle_area: Rect,
pub caption_area: Rect,
pub x_axis_area: Rect,
pub y_axis_area: Rect,
pub legend_area: Rect,
}
impl PlotLayout {
/// Compute layout from total dimensions and theme settings.
pub fn compute(
width: f64,
height: f64,
theme: &Theme,
has_title: bool,
has_legend: bool,
) -> Self {
Self::compute_full(
width, height, theme, has_title, false, false, has_legend, false, None, None,
)
}
/// Compute layout with full subtitle/caption support.
///
/// `y_label_width` optionally overrides the reserved width (px) of the
/// y-axis tick-label text — measured from the actual break labels by the
/// caller so long categorical labels (e.g. a flipped bar chart's rows)
/// aren't clipped. `None` keeps the fixed `axis_text_y.size * 3.5` estimate.
#[allow(clippy::too_many_arguments)]
pub fn compute_full(
width: f64,
height: f64,
theme: &Theme,
has_title: bool,
has_subtitle: bool,
has_caption: bool,
has_legend: bool,
x_axis_top: bool,
y_label_width: Option<f64>,
x_label_height: Option<f64>,
) -> Self {
let margin = &theme.plot_margin;
let title_height = if has_title {
theme.title.size * 2.0
} else {
0.0
};
let subtitle_height = if has_subtitle {
theme.subtitle.size * 1.5
} else {
0.0
};
let caption_height = if has_caption {
theme.caption.size * 1.8
} else {
0.0
};
let x_axis_height = theme.axis_ticks_length
+ if theme.axis_text_x.visible {
// Rotated labels extend vertically, so reserve more bottom space —
// measured from the actual labels by the caller when available.
if theme.axis_text_x.angle.abs() > 10.0 {
x_label_height.unwrap_or(theme.axis_text_x.size * 5.0)
} else {
// Dodged labels stack across N rows.
(theme.axis_text_x.size + 4.0) * theme.axis_text_x_dodge.max(1) as f64
}
} else {
0.0
}
+ if theme.axis_title_x.visible {
theme.axis_title_x.size + 8.0
} else {
0.0
};
let y_axis_width = theme.axis_ticks_length
+ if theme.axis_text_y.visible {
y_label_width.unwrap_or(theme.axis_text_y.size * 3.5 + 4.0)
} else {
0.0
}
+ if theme.axis_title_y.visible {
theme.axis_title_y.size + 8.0
} else {
0.0
};
let legend_size = if has_legend {
theme.legend_margin.left
+ theme.legend_key_width
+ theme.legend_spacing
+ theme.legend_text.size * 6.0
+ theme.legend_margin.right
} else {
0.0
};
// A horizontal (top/bottom) legend only needs ONE row of height, not the
// width-based `legend_size` — otherwise a big gap opens above the plot.
let legend_band = if has_legend {
theme.legend_margin.top
+ theme.legend_key_height
+ theme.legend_spacing
+ theme.legend_margin.bottom
} else {
0.0
};
// Determine legend space allocation per position
let (legend_right, legend_left, legend_top, legend_bottom) = if has_legend {
match theme.legend_position {
LegendPosition::Right => (legend_size, 0.0, 0.0, 0.0),
LegendPosition::Left => (0.0, legend_size, 0.0, 0.0),
LegendPosition::Top => (0.0, 0.0, legend_band, 0.0),
LegendPosition::Bottom => (0.0, 0.0, 0.0, legend_band),
// Inside/None overlay the panel, reserving no external space.
LegendPosition::None | LegendPosition::Inside(..) => (0.0, 0.0, 0.0, 0.0),
}
} else {
(0.0, 0.0, 0.0, 0.0)
};
let plot_x = margin.left + y_axis_width + legend_left;
// A top x-axis reserves its space above the panel instead of below.
let plot_y = margin.top
+ title_height
+ subtitle_height
+ legend_top
+ if x_axis_top { x_axis_height } else { 0.0 };
let plot_width =
width - margin.left - margin.right - y_axis_width - legend_right - legend_left;
let plot_height = height
- margin.top
- margin.bottom
- title_height
- subtitle_height
- caption_height
- x_axis_height
- legend_top
- legend_bottom;
let mut plot_width = plot_width.max(50.0);
let mut plot_height = plot_height.max(50.0);
let mut plot_x = plot_x;
let mut plot_y = plot_y;
// Fix the panel aspect ratio (R's `aspect.ratio` = height/width),
// shrinking the larger dimension and centering within the available box.
if let Some(r) = theme.aspect_ratio {
if r > 0.0 {
let (avail_w, avail_h) = (plot_width, plot_height);
if avail_w * r <= avail_h {
plot_height = avail_w * r;
plot_y += (avail_h - plot_height) / 2.0;
} else {
plot_width = avail_h / r;
plot_x += (avail_w - plot_width) / 2.0;
}
}
}
PlotLayout {
total: Rect {
x: 0.0,
y: 0.0,
width,
height,
},
plot_area: Rect {
x: plot_x,
y: plot_y,
width: plot_width,
height: plot_height,
},
title_area: Rect {
x: plot_x,
y: margin.top,
width: plot_width,
height: title_height,
},
subtitle_area: Rect {
x: plot_x,
y: margin.top + title_height,
width: plot_width,
height: subtitle_height,
},
caption_area: Rect {
x: plot_x,
y: plot_y + plot_height + x_axis_height,
width: plot_width,
height: caption_height,
},
x_axis_area: Rect {
x: plot_x,
y: plot_y + plot_height,
width: plot_width,
height: x_axis_height,
},
y_axis_area: Rect {
x: margin.left + legend_left,
y: plot_y,
width: y_axis_width,
height: plot_height,
},
legend_area: Rect {
x: plot_x + plot_width,
y: plot_y,
width: legend_size,
height: plot_height,
},
}
}
}