use eframe::egui;
use rsplot::{CurveData, Plot1D, YAxis};
const N: usize = 400;
struct Y2App {
plot: Plot1D,
}
impl Y2App {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
let rs = cc
.wgpu_render_state
.as_ref()
.expect("eframe must use the wgpu renderer");
let mut plot = Plot1D::new(rs, 0);
plot.set_graph_title("Dual Y-axis");
plot.set_graph_x_label("time (s)");
plot.set_graph_y_label("amplitude", YAxis::Left);
plot.set_graph_y_label("normalized", YAxis::Right);
let xs: Vec<f64> = (0..N).map(|i| 10.0 * i as f64 / (N - 1) as f64).collect();
let left_y: Vec<f64> = xs
.iter()
.map(|&t| 50.0 + 40.0 * (t * 1.5).sin() * (-t / 8.0).exp())
.collect();
let left = CurveData::new(xs.clone(), left_y, egui::Color32::from_rgb(120, 180, 255))
.with_width(2.0);
let h = plot.add_curve_data(&left);
plot.set_item_legend(h, "damped oscillation (left)");
let right_y: Vec<f64> = xs.iter().map(|&t| 1.0 - (-t / 3.0).exp()).collect();
let right = CurveData::new(xs, right_y, egui::Color32::from_rgb(255, 160, 80))
.with_width(2.0)
.with_y_axis(YAxis::Right);
let h2 = plot.add_curve_data(&right);
plot.set_item_legend(h2, "saturation (right)");
plot.set_graph_y_limits(0.0, 100.0, YAxis::Left);
plot.set_graph_y_limits(0.0, 1.0, YAxis::Right);
Self { plot }
}
}
impl eframe::App for Y2App {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
self.plot.show(ui);
}
}
fn main() -> eframe::Result {
eframe::run_native(
"rsplot: dual Y-axis",
eframe::NativeOptions {
renderer: eframe::Renderer::Wgpu,
..Default::default()
},
Box::new(|cc| Ok(Box::new(Y2App::new(cc)) as Box<dyn eframe::App>)),
)
}