use crate::primitives::chart::{Chart, ChartLayout};
use crate::primitives::layout_metrics::pixel_chart_layout;
pub fn mac_chart_layout(
chart: &Chart,
x: f64,
y: f64,
w: f64,
h: f64,
line_height: f64,
char_width: f64,
) -> ChartLayout {
pixel_chart_layout(
chart,
x as f32,
y as f32,
w as f32,
h as f32,
line_height as f32,
char_width as f32,
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::primitives::chart::{ChartHit, ChartKind, Series};
use crate::types::WidgetId;
fn sample_line() -> Chart {
Chart {
id: WidgetId::new("ch"),
kind: ChartKind::Line,
series: vec![Series {
label: "x".into(),
data: vec![0.0, 1.0, 0.5, 2.0, 1.0],
color: Some(crate::types::Color::rgb(80, 160, 255)),
fill: false,
}],
x_label: None,
y_label: None,
y_range: Some((0.0, 2.0)),
x_range: None,
show_legend: false,
y_ticks: Some(0),
x_ticks: Some(0),
show_grid: false,
}
}
#[test]
fn hit_test_inside_plot_area_returns_body() {
let chart = sample_line();
let layout = mac_chart_layout(&chart, 0.0, 0.0, 200.0, 120.0, 16.0, 8.0);
let cx = layout.plot_area.x + layout.plot_area.width * 0.5;
let cy = layout.plot_area.y + layout.plot_area.height * 0.5;
assert_eq!(layout.hit_test(cx, cy), ChartHit::Body(WidgetId::new("ch")),);
}
#[test]
fn hit_test_inside_plot_area_at_nonzero_origin() {
let chart = sample_line();
let origin_x = 7.0_f64;
let origin_y = 13.0_f64;
let layout = mac_chart_layout(&chart, origin_x, origin_y, 200.0, 120.0, 16.0, 8.0);
let cx = layout.plot_area.x + layout.plot_area.width * 0.5;
let cy = layout.plot_area.y + layout.plot_area.height * 0.5;
assert_eq!(layout.hit_test(cx, cy), ChartHit::Body(WidgetId::new("ch")),);
}
}