fix histogram bars

This commit is contained in:
Simon Gardling 2022-02-23 15:53:01 -05:00
parent 726782b667
commit 99d7482973
3 changed files with 15 additions and 16 deletions

View File

@ -1,4 +1,3 @@
## TODO: ## TODO:
1. Port to [egui](https://github.com/emilk/egui) 1. Port to [egui](https://github.com/emilk/egui)
- Proper support for dynamic chart display size. - Proper support for dynamic chart display size.
- Fix histogram

View File

@ -8,7 +8,7 @@ pub struct ChartManager {
num_interval: usize, num_interval: usize,
resolution: usize, resolution: usize,
back_cache: Cache<Vec<(f64, f64)>>, back_cache: Cache<Vec<(f64, f64)>>,
front_cache: Cache<(Vec<(f64, f64, f64)>, f64)>, front_cache: Cache<(Vec<(f64, f64)>, f64)>,
} }
impl ChartManager { impl ChartManager {
@ -27,7 +27,7 @@ impl ChartManager {
} }
#[inline] #[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 absrange = (self.max_x - self.min_x).abs();
let data: Vec<(f64, f64)> = match self.back_cache.is_valid() { let data: Vec<(f64, f64)> = match self.back_cache.is_valid() {
true => self.back_cache.get().clone(), 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 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(), true => self.front_cache.get().clone(),
false => { false => {
let step = absrange / (self.num_interval as f64); 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()); self.front_cache.set(output.clone());
output output
} }
@ -60,7 +60,7 @@ impl ChartManager {
pub fn update( pub fn update(
&mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize, &mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize,
resolution: 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 func_str: String = add_asterisks(func_str_new);
let update_func: bool = !self.function.str_compare(func_str.clone()); 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 // Creates and does the math for creating all the rectangles under the graph
#[inline] #[inline]
fn integral_rectangles(&self, step: f64) -> (Vec<(f64, f64, f64)>, f64) { fn integral_rectangles(&self, step: f64) -> (Vec<(f64, f64)>, f64) {
let data2: Vec<(f64, f64, f64)> = (0..self.num_interval) let data2: Vec<(f64, f64)> = (0..self.num_interval)
.map(|e| { .map(|e| {
let x: f64 = ((e as f64) * step) + self.min_x; let x: f64 = ((e as f64) * step) + self.min_x;
@ -103,16 +103,16 @@ impl ChartManager {
let tmp2: f64 = self.function.run(x2); let tmp2: f64 = self.function.run(x2);
// Chooses the y value who's absolute value is the smallest // Chooses the y value who's absolute value is the smallest
let y: f64 = match tmp2.abs() > tmp1.abs() { let output = match tmp2.abs() > tmp1.abs() {
true => tmp1, true => (x2, tmp1),
false => tmp2, false => (x, tmp2),
}; };
(x, x2, y) output
}) })
.filter(|(_, _, y)| !y.is_nan()) .filter(|(_, y)| !y.is_nan())
.collect(); .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) (data2, area)
} }
} }

View File

@ -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(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(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| { egui::CentralPanel::default().show(ctx, |ui| {
@ -97,7 +97,7 @@ impl epi::App for MathApp {
let bars = rect_data let bars = rect_data
.iter() .iter()
.map(|(_, x2, y)| Bar::new(*x2, *y)) .map(|(x, y)| Bar::new(*x, *y))
.collect(); .collect();
let barchart = BarChart::new(bars).color(Color32::BLUE); let barchart = BarChart::new(bars).color(Color32::BLUE);