From 99d74829739075d453a0f2a9d1cce78edfd9fd53 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 23 Feb 2022 15:53:01 -0500 Subject: [PATCH] fix histogram bars --- TODO.md | 1 - src/chart_manager.rs | 26 +++++++++++++------------- src/egui_app.rs | 4 ++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/TODO.md b/TODO.md index af3432c..79d0fb9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ ## TODO: 1. Port to [egui](https://github.com/emilk/egui) - Proper support for dynamic chart display size. - - Fix histogram \ No newline at end of file diff --git a/src/chart_manager.rs b/src/chart_manager.rs index 096c058..320e09e 100644 --- a/src/chart_manager.rs +++ b/src/chart_manager.rs @@ -8,7 +8,7 @@ pub struct ChartManager { num_interval: usize, resolution: usize, back_cache: Cache>, - front_cache: Cache<(Vec<(f64, f64, f64)>, f64)>, + front_cache: Cache<(Vec<(f64, f64)>, f64)>, } impl ChartManager { @@ -27,7 +27,7 @@ impl ChartManager { } #[inline] - fn draw(&mut self) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) { + fn draw(&mut self) -> (Vec<(f64, f64)>, Vec<(f64, f64)>, f64) { let absrange = (self.max_x - self.min_x).abs(); let data: Vec<(f64, f64)> = match self.back_cache.is_valid() { true => self.back_cache.get().clone(), @@ -43,11 +43,11 @@ impl ChartManager { let filtered_data: Vec<(f64, f64)> = data.iter().map(|(x, y)| (*x, *y)).collect(); - let (rect_data, area): (Vec<(f64, f64, f64)>, f64) = match self.front_cache.is_valid() { + let (rect_data, area): (Vec<(f64, f64)>, f64) = match self.front_cache.is_valid() { true => self.front_cache.get().clone(), false => { let step = absrange / (self.num_interval as f64); - let output: (Vec<(f64, f64, f64)>, f64) = self.integral_rectangles(step); + let output: (Vec<(f64, f64)>, f64) = self.integral_rectangles(step); self.front_cache.set(output.clone()); output } @@ -60,7 +60,7 @@ impl ChartManager { pub fn update( &mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize, resolution: usize, - ) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) { + ) -> (Vec<(f64, f64)>, Vec<(f64, f64)>, f64) { let func_str: String = add_asterisks(func_str_new); let update_func: bool = !self.function.str_compare(func_str.clone()); @@ -88,8 +88,8 @@ impl ChartManager { // Creates and does the math for creating all the rectangles under the graph #[inline] - fn integral_rectangles(&self, step: f64) -> (Vec<(f64, f64, f64)>, f64) { - let data2: Vec<(f64, f64, f64)> = (0..self.num_interval) + fn integral_rectangles(&self, step: f64) -> (Vec<(f64, f64)>, f64) { + let data2: Vec<(f64, f64)> = (0..self.num_interval) .map(|e| { let x: f64 = ((e as f64) * step) + self.min_x; @@ -103,16 +103,16 @@ impl ChartManager { let tmp2: f64 = self.function.run(x2); // Chooses the y value who's absolute value is the smallest - let y: f64 = match tmp2.abs() > tmp1.abs() { - true => tmp1, - false => tmp2, + let output = match tmp2.abs() > tmp1.abs() { + true => (x2, tmp1), + false => (x, tmp2), }; - (x, x2, y) + output }) - .filter(|(_, _, y)| !y.is_nan()) + .filter(|(_, y)| !y.is_nan()) .collect(); - let area: f64 = data2.iter().map(|(_, _, y)| y * step).sum(); // sum of all rectangles' areas + let area: f64 = data2.iter().map(|(_, y)| y * step).sum(); // sum of all rectangles' areas (data2, area) } } diff --git a/src/egui_app.rs b/src/egui_app.rs index 9a2b534..0cfd3d7 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -71,7 +71,7 @@ impl epi::App for MathApp { ui.add(egui::Slider::new(min_x, -1000.0..=1000.0).text("Min X")); ui.add(egui::Slider::new(max_x, *min_x..=1000.0).text("Max X")); - ui.add(egui::Slider::new(num_interval, 0..=usize::MAX).text("Interval")); + ui.add(egui::Slider::new(num_interval, 1..=usize::MAX).text("Interval")); }); egui::CentralPanel::default().show(ctx, |ui| { @@ -97,7 +97,7 @@ impl epi::App for MathApp { let bars = rect_data .iter() - .map(|(_, x2, y)| Bar::new(*x2, *y)) + .map(|(x, y)| Bar::new(*x, *y)) .collect(); let barchart = BarChart::new(bars).color(Color32::BLUE);