From 0c63895fe9b01bd26eab5f5ed5d6bb8b01ecef85 Mon Sep 17 00:00:00 2001 From: Tristan Murphy Date: Thu, 13 Jun 2024 11:26:53 -0400 Subject: [PATCH] use dynamic height range for plotting example chart --- examples/plot.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/examples/plot.rs b/examples/plot.rs index 34fb391..410ea1b 100644 --- a/examples/plot.rs +++ b/examples/plot.rs @@ -119,21 +119,6 @@ fn main() -> Result<(), Box> { println!("Training complete, collecting data and building chart..."); - let root = SVGBackend::new(OUTPUT_FILE_NAME, (640, 480)).into_drawing_area(); - root.fill(&WHITE)?; - - let mut chart = ChartBuilder::on(&root) - .caption( - "agent fitness values per generation", - ("sans-serif", 50).into_font(), - ) - .margin(5) - .x_label_area_size(30) - .y_label_area_size(30) - .build_cartesian_2d(0usize..GENS, 0f32..1000.0)?; - - chart.configure_mesh().draw()?; - let data: Vec<_> = Arc::into_inner(performance_stats) .unwrap() .into_inner() @@ -142,9 +127,16 @@ fn main() -> Result<(), Box> { .enumerate() .collect(); - let highs = data + let highs: Vec<_> = data + .iter() + .map(|(i, PerformanceStats { high, .. })| (*i, *high)) + .collect(); + + let highest_overall = highs .iter() - .map(|(i, PerformanceStats { high, .. })| (*i, *high)); + .map(|(_, h)| h) + .max_by(|&a, &b| a.partial_cmp(b).unwrap()) + .unwrap(); let medians = data .iter() @@ -154,6 +146,21 @@ fn main() -> Result<(), Box> { .iter() .map(|(i, PerformanceStats { low, .. })| (*i, *low)); + let root = SVGBackend::new(OUTPUT_FILE_NAME, (640, 480)).into_drawing_area(); + root.fill(&WHITE)?; + + let mut chart = ChartBuilder::on(&root) + .caption( + "agent fitness values per generation", + ("sans-serif", 50).into_font(), + ) + .margin(5) + .x_label_area_size(30) + .y_label_area_size(30) + .build_cartesian_2d(0usize..GENS, 0f32..*highest_overall)?; + + chart.configure_mesh().draw()?; + chart .draw_series(LineSeries::new(highs, &GREEN))? .label("high");