progress
This commit is contained in:
parent
0d074794de
commit
1e773605f6
@ -18,6 +18,8 @@ egui = "0.17.0"
|
|||||||
eframe = "0.17.0"
|
eframe = "0.17.0"
|
||||||
emath = "0.17.0"
|
emath = "0.17.0"
|
||||||
epaint = "0.17.0"
|
epaint = "0.17.0"
|
||||||
|
instant = { version = "0.1.12", features = ["wasm-bindgen"] }
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
console_error_panic_hook = "0.1.7"
|
console_error_panic_hook = "0.1.7"
|
||||||
tracing-wasm = "0.2.1"
|
tracing-wasm = "0.2.1"
|
||||||
|
|||||||
1
TODO.md
1
TODO.md
@ -1,5 +1,6 @@
|
|||||||
## 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.
|
||||||
|
- Don't re-render plot on mouse movement
|
||||||
2. Multiple functions in one graph.
|
2. Multiple functions in one graph.
|
||||||
3. Non `y=` functions.
|
3. Non `y=` functions.
|
||||||
@ -56,6 +56,19 @@ impl ChartManager {
|
|||||||
(filtered_data, rect_data, area)
|
(filtered_data, rect_data, area)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn do_update_front(&self, resolution: usize, num_interval: usize) -> bool {
|
||||||
|
(self.resolution != resolution) | (num_interval != self.num_interval)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn do_update_back(&self, func_str_new: String, min_x: f64, max_x: f64) -> bool {
|
||||||
|
let func_str: String = add_asterisks(func_str_new);
|
||||||
|
let update_func: bool = !self.function.str_compare(func_str.clone());
|
||||||
|
|
||||||
|
let underlying_update = update_func | (min_x != self.min_x) | (max_x != self.max_x);
|
||||||
|
|
||||||
|
underlying_update
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
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,
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
use crate::chart_manager::ChartManager;
|
use crate::chart_manager::ChartManager;
|
||||||
use crate::misc::{digits_precision, test_func};
|
use crate::misc::{digits_precision, test_func, Cache};
|
||||||
use eframe::{egui, epi};
|
use eframe::{egui, epi};
|
||||||
use egui::widgets::plot::{Bar, BarChart};
|
use egui::widgets::plot::{Bar, BarChart};
|
||||||
use egui::{
|
use egui::{
|
||||||
plot::{Line, Plot, Value, Values},
|
plot::{Line, Plot, Value, Values},
|
||||||
};
|
};
|
||||||
use egui::{Color32};
|
use egui::{Color32, plot};
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
pub struct MathApp {
|
pub struct MathApp {
|
||||||
func_str: String,
|
func_str: String,
|
||||||
@ -15,6 +14,7 @@ pub struct MathApp {
|
|||||||
num_interval: usize,
|
num_interval: usize,
|
||||||
resolution: usize,
|
resolution: usize,
|
||||||
chart_manager: ChartManager,
|
chart_manager: ChartManager,
|
||||||
|
bar_cache: Cache<Vec<Bar>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MathApp {
|
impl Default for MathApp {
|
||||||
@ -26,6 +26,7 @@ impl Default for MathApp {
|
|||||||
num_interval: 100,
|
num_interval: 100,
|
||||||
resolution: 10000,
|
resolution: 10000,
|
||||||
chart_manager: ChartManager::new("x^2".to_string(), -10.0, 10.0, 100, 10000),
|
chart_manager: ChartManager::new("x^2".to_string(), -10.0, 10.0, 100, 10000),
|
||||||
|
bar_cache: Cache::new_empty(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,10 +48,13 @@ impl epi::App for MathApp {
|
|||||||
min_x,
|
min_x,
|
||||||
max_x,
|
max_x,
|
||||||
num_interval,
|
num_interval,
|
||||||
resolution: _,
|
resolution,
|
||||||
chart_manager,
|
chart_manager,
|
||||||
|
bar_cache,
|
||||||
} = self;
|
} = self;
|
||||||
let start = Instant::now();
|
|
||||||
|
// Note: This Instant implementation does not show microseconds when using wasm.
|
||||||
|
let start = instant::Instant::now();
|
||||||
|
|
||||||
let min_x_total: f32 = -1000.0;
|
let min_x_total: f32 = -1000.0;
|
||||||
let max_x_total: f32 = 1000.0;
|
let max_x_total: f32 = 1000.0;
|
||||||
@ -91,6 +95,10 @@ impl epi::App for MathApp {
|
|||||||
ui.add(egui::Slider::new(num_interval, 1..=usize::MAX).text("Interval"));
|
ui.add(egui::Slider::new(num_interval, 1..=usize::MAX).text("Interval"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
let update_back = chart_manager.do_update_back(func_str.clone(), *min_x, *max_x);
|
||||||
|
let update_front = chart_manager.do_update_front(*num_interval, *resolution);
|
||||||
|
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
if !parse_error.is_empty() {
|
if !parse_error.is_empty() {
|
||||||
ui.label(format!("Error: {}", parse_error));
|
ui.label(format!("Error: {}", parse_error));
|
||||||
@ -99,10 +107,10 @@ impl epi::App for MathApp {
|
|||||||
|
|
||||||
let (filtered_data, rect_data, area) = chart_manager.update(
|
let (filtered_data, rect_data, area) = chart_manager.update(
|
||||||
self.func_str.clone(),
|
self.func_str.clone(),
|
||||||
self.min_x,
|
*min_x,
|
||||||
self.max_x,
|
*max_x,
|
||||||
self.num_interval,
|
*num_interval,
|
||||||
self.resolution,
|
*resolution,
|
||||||
);
|
);
|
||||||
|
|
||||||
let filtered_data_values = filtered_data
|
let filtered_data_values = filtered_data
|
||||||
@ -112,17 +120,39 @@ impl epi::App for MathApp {
|
|||||||
|
|
||||||
let curve = Line::new(Values::from_values(filtered_data_values)).color(Color32::RED);
|
let curve = Line::new(Values::from_values(filtered_data_values)).color(Color32::RED);
|
||||||
|
|
||||||
let bars = rect_data
|
let bars: Vec<Bar> = match update_front {
|
||||||
.iter()
|
true => {
|
||||||
.map(|(x, y)| Bar::new(*x, *y))
|
let bars: Vec<Bar> = rect_data
|
||||||
.collect();
|
.iter()
|
||||||
let barchart = BarChart::new(bars).color(Color32::BLUE);
|
.map(|(x, y)| Bar::new(*x, *y))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
bar_cache.set(bars.clone());
|
||||||
|
bars
|
||||||
|
},
|
||||||
|
false => {
|
||||||
|
if bar_cache.is_valid() {
|
||||||
|
bar_cache.get().clone()
|
||||||
|
} else {
|
||||||
|
let bars: Vec<Bar> = rect_data
|
||||||
|
.iter()
|
||||||
|
.map(|(x, y)| Bar::new(*x, *y))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
bar_cache.set(bars.clone());
|
||||||
|
bars
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let bar_chart = BarChart::new(bars).color(Color32::BLUE);
|
||||||
|
|
||||||
|
|
||||||
Plot::new("plot")
|
Plot::new("plot")
|
||||||
.view_aspect(1.0)
|
.view_aspect(1.0)
|
||||||
.include_y(0)
|
.include_y(0)
|
||||||
.show(ui, |plot_ui| {
|
.show(ui, |plot_ui| {
|
||||||
plot_ui.line(curve);
|
plot_ui.line(curve);
|
||||||
plot_ui.bar_chart(barchart);
|
plot_ui.bar_chart(bar_chart);
|
||||||
});
|
});
|
||||||
|
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user