const LEFT: f64 = 44.0;
const BOTTOM: f64 = 40.0;
const TOP_TITLE: f64 = 24.0;
const CAPTION: f64 = 20.0;
const RIGHT: f64 = 12.0;
const TOP_MIN: f64 = 10.0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct PlotArea {
pub(super) x: f64,
pub(super) y: f64,
pub(super) w: f64,
pub(super) h: f64,
}
pub(super) fn plot_area(
chart_x: f64,
chart_y: f64,
chart_w: f64,
chart_h: f64,
has_title: bool,
has_caption: bool,
) -> PlotArea {
let top = if has_title { TOP_TITLE } else { TOP_MIN };
let bottom = BOTTOM + if has_caption { CAPTION } else { 0.0 };
let x = chart_x + LEFT;
let y = chart_y + top;
let w = (chart_w - LEFT - RIGHT).max(0.0);
let h = (chart_h - top - bottom).max(0.0);
PlotArea { x, y, w, h }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plot_area_no_title_no_caption() {
let pa = plot_area(0.0, 0.0, 400.0, 300.0, false, false);
assert_eq!(pa.x, LEFT);
assert_eq!(pa.y, TOP_MIN);
assert!((pa.w - (400.0 - LEFT - RIGHT)).abs() < 1e-10);
assert!((pa.h - (300.0 - TOP_MIN - BOTTOM)).abs() < 1e-10);
}
#[test]
fn plot_area_with_title() {
let pa = plot_area(0.0, 0.0, 400.0, 300.0, true, false);
assert_eq!(pa.y, TOP_TITLE);
assert!((pa.h - (300.0 - TOP_TITLE - BOTTOM)).abs() < 1e-10);
}
#[test]
fn plot_area_with_caption() {
let pa = plot_area(0.0, 0.0, 400.0, 300.0, false, true);
assert!((pa.h - (300.0 - TOP_MIN - BOTTOM - CAPTION)).abs() < 1e-10);
}
#[test]
fn plot_area_clamps_negative() {
let pa = plot_area(0.0, 0.0, 10.0, 10.0, true, true);
assert_eq!(pa.w, 0.0);
assert_eq!(pa.h, 0.0);
}
#[test]
fn plot_area_translates_origin() {
let pa = plot_area(50.0, 30.0, 400.0, 300.0, false, false);
assert!((pa.x - (50.0 + LEFT)).abs() < 1e-10);
assert!((pa.y - (30.0 + TOP_MIN)).abs() < 1e-10);
}
}