From 1d1574a1ed2bfde83364b55037a4e4401fdcfe20 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 24 Feb 2022 01:13:01 -0500 Subject: [PATCH] fix BarChart --- TODO.md | 1 - src/chart_manager.rs | 20 ++++++++++++++++---- src/egui_app.rs | 4 ++-- src/misc.rs | 1 + 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index f1477cd..b53b106 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,6 @@ 1. Port to [egui](https://github.com/emilk/egui) - Proper support for dynamic chart display size. - Don't re-render plot on mouse movement - - Fix interval display - Fix crashes in parsing strings (`TextDecoder.decode: Decoding failed.`) 2. Multiple functions in one graph. 3. Non `y=` functions. \ No newline at end of file diff --git a/src/chart_manager.rs b/src/chart_manager.rs index a4e80fa..b298553 100644 --- a/src/chart_manager.rs +++ b/src/chart_manager.rs @@ -56,6 +56,10 @@ impl ChartManager { (filtered_data, rect_data, area) } + pub fn get_step(&self) -> f64 { + (self.max_x - self.min_x).abs() / (self.num_interval as f64) + } + pub fn do_update_front(&self, resolution: usize, num_interval: usize) -> bool { (self.resolution != resolution) | (num_interval != self.num_interval) } @@ -100,6 +104,7 @@ 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) { + let half_step = step/2.0; let data2: Vec<(f64, f64)> = (0..self.num_interval) .map(|e| { let x: f64 = ((e as f64) * step) + self.min_x; @@ -114,12 +119,19 @@ impl ChartManager { let tmp2: f64 = self.function.run(x2); // Chooses the y value who's absolute value is the smallest + let mut output = match tmp2.abs() > tmp1.abs() { + true => (x, tmp1), + false => (x2, tmp2), + }; - - match tmp2.abs() > tmp1.abs() { - true => (x2, tmp1), - false => (x, tmp2), + // Applies `half_step` in order to make the bar graph display properly + if output.0 > 0.0 { + output.0 = output.0+half_step; + } else { + output.0 = output.0-half_step; } + + output }) .filter(|(_, y)| !y.is_nan()) .collect(); diff --git a/src/egui_app.rs b/src/egui_app.rs index b75a75a..e74f7e7 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -88,7 +88,7 @@ impl epi::App for MathApp { } } - ui.add(egui::Slider::new(num_interval, 1..=usize::MAX).text("Interval")); + ui.add(egui::Slider::new(num_interval, 10..=usize::MAX).text("Interval")); }); let _update_back = chart_manager.do_update_back(func_str.clone(), *min_x, *max_x); @@ -134,7 +134,7 @@ impl epi::App for MathApp { } } }; - let bar_chart = BarChart::new(bars).color(Color32::BLUE); + let bar_chart = BarChart::new(bars).color(Color32::BLUE).width(chart_manager.get_step()); Plot::new("plot") .view_aspect(1.0) diff --git a/src/misc.rs b/src/misc.rs index aa68e43..bc4e445 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -133,6 +133,7 @@ impl Function { pub fn str_compare(&self, other_string: String) -> bool { self.func_str == other_string } + #[allow(dead_code)] pub fn get_string(&self) -> String { self.func_str.clone() } }