use AreaSeries when num_interval is over 200

This commit is contained in:
Simon Gardling 2022-02-16 11:26:03 -05:00
parent 5d1b9cd845
commit 098ced8092
2 changed files with 31 additions and 11 deletions

View File

@ -8,7 +8,7 @@ use std::panic;
use wasm_bindgen::prelude::*;
use web_sys::HtmlCanvasElement;
mod misc;
use crate::misc::{ChartOutput, DrawResult, Cache};
use crate::misc::{Cache, ChartOutput, DrawResult};
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
@ -87,14 +87,18 @@ impl ChartManager {
let output: Vec<(f32, f32)> = (1..=self.resolution)
.map(|x| ((x as f32 / self.resolution as f32) * absrange) + self.min_x)
.map(|x| (x, func(x as f64) as f32))
.filter(|(_, y)| &self.min_y <= y && y <= &self.max_y)
.collect();
self.back_cache.set(output.clone());
output
}
};
chart.draw_series(LineSeries::new(data, &RED))?;
let filtered_data: Vec<(f32, f32)> = data
.iter()
.filter(|(_, y)| &self.min_y <= y && y <= &self.max_y)
.map(|(x, y)| (*x, *y))
.collect();
chart.draw_series(LineSeries::new(filtered_data, &RED))?;
let (rect_data, area): (Vec<(f32, f32, f32)>, f32) = match self.front_cache.is_valid() {
true => self.front_cache.get().clone(),
@ -107,12 +111,30 @@ impl ChartManager {
}
};
// Draw rectangles
chart.draw_series(
rect_data
if self.num_interval <= 200 {
// Draw rectangles
chart.draw_series(
rect_data
.iter()
.map(|(x1, x2, y)| Rectangle::new([(*x2, *y), (*x1, 0.0)], &BLUE)),
)?;
} else {
// Save resources by not graphing rectangles and using an AreaSeries when you can no longer see the rectangles
let capped_data: Vec<(f32, f32)> = data
.iter()
.map(|(x1, x2, y)| Rectangle::new([(*x2, *y), (*x1, 0.0)], &BLUE)),
)?;
.map(|(x, y)| {
let new_y: &f32 = if y > &self.max_y {
&self.max_y
} else if &self.min_y > y {
&self.min_y
} else {
&y
};
(*x, *new_y)
})
.collect();
chart.draw_series(AreaSeries::new(capped_data, 0.0, &BLUE))?;
}
root.present()?;
Ok((chart.into_coord_trans(), area))

View File

@ -78,7 +78,5 @@ impl<T> Cache<T> {
}
#[inline]
pub fn is_valid(&self) -> bool {
self.valid
}
pub fn is_valid(&self) -> bool { self.valid }
}