From 4c18fad8705bc1d9e3a1cde0fe4d2ab76968005b Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 16 Feb 2022 13:14:20 -0500 Subject: [PATCH] better handling of NANs --- src/lib.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 976ca76..a7bde29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -123,6 +123,10 @@ impl ChartManager { let capped_data: Vec<(f32, f32)> = data .iter() .map(|(x, y)| { + if y.is_nan() { + return (*x, 0.0); + } + let new_y: &f32 = if y > &self.max_y { &self.max_y } else if &self.min_y > y { @@ -130,9 +134,11 @@ impl ChartManager { } else { y }; + (*x, *new_y) }) .collect(); + log(&format!("{:?}", capped_data)); chart.draw_series(AreaSeries::new(capped_data, 0.0, &BLUE))?; } @@ -216,13 +222,9 @@ impl ChartManager { false => tmp2, }; - if !y.is_nan() { - (x, x2, y) - } else { - (f32::NAN, f32::NAN, f32::NAN) - } + (x, x2, y) }) - .filter(|ele| ele != &(f32::NAN, f32::NAN, f32::NAN)) + .filter(|(_, _, y)| !y.is_nan()) .collect(); let area: f32 = data2.iter().map(|(_, _, y)| y * step).sum(); // sum of all rectangles' areas (data2, area)